Question
I need help making a program, I am currently studying CSE 205 and my teacher is not great at teaching and honestly I have a
I need help making a program, I am currently studying CSE 205 and my teacher is not great at teaching and honestly I have a hardtime understanding the material.
here is what the program is about:
Let list be a nonempty sequence of nonnegative random integers, each in the range [0, 32767] and let n be the length of list, e.g., list = { 2, 8, 3, 2, 9, 8, 6, 3, 4, 6, 1, 9 } where n = 12. List elements are numbered starting at 0. We define a run up to be a (k+1)-length subsequence listi, listi+1, listi+2, ..., listi+k, that is monotonically increasing (i.e., listi+j listi+j-1 for each j = 1, 2, 3, ..., k). Similarly, a run down is a (k+1)-length subsequence listi, listi+1, listi+2, ..., listi+k, that is monotonically decreasing (i.e., listi+j-1 listi+j for each j = 1, 2, 3, ..., k). For the above example list we have these runs up and runs down: Runs Up list0 through list1 = { 2, 8 }; k = 1 list2 = { 3 }; k = 0 list3 through list4 = { 2, 9 }; k = 1 list5 = { 8 }; k = 0 list6 = { 6 }; k = 0 list7 through list9 = { 3, 4, 6 }; k = 2 list10 through list11 = { 1, 9 }; k = 1 Runs Down list0 = { 2 }; k = 0 list1 through list3 = { 8, 3, 2 }; k = 2 list4 through list7 = { 9, 8, 6, 3 }; k = 3 list8 = { 4 }; k = 0 list9 through list10 = { 6, 1 }; k = 1 list11 = { 9 }; k = 0 We are interested in the value of k for each run up and run down and in particular we are interested in the total umber of runs for each nonzero k, which we shall denote by runsk, 0 < k < n - 1. For the example list we have: k runsk runs 1 4 { 2, 8 }, { 2, 9 }, { 1, 9 }, and { 6, 1 } 2 2 { 3, 4, 6, } and { 8, 3, 2 } 3 1 { 9, 8, 6, 3 } 4-11 0 Let runstotal be the the sum from k = 1 to n - 1 of runsk. For the example list, runstotal = 4 + 2 + 1 = 7.
. Open a file named p01-in.txt containing n integers, 1 n 1000, with each integer in [0, 32767]. There will be one or more integers per line. A sample input file: Sample p01-in.txt 2 8 3 2 9 863 4 6 1 9 2. The program shall compute runsk for k = 1, 2, 3, ..., n - 1. 3. The program shall compute runstotal. 4. The program shall produce an output file named p01-runs.txt containing runstotal and runsk for k = 1, 2, 3, ..., n - 1. The file shall be formatted as shown in the example file below. Sample p01-runs.txt runs_total, 7 runs_1, 4 runs_2, 2 runs_3, 1 runs_4, 0 runs_5, 0 runs_6, 0 5. If the input file p01-in.txt cannot be opened for reading (because it does not exist) then display an error message on the output window and immediately terminate the program, e.g., run program... Sorry, could not open 'p01-in.txt' for reading. Stopping.
Pseudocode Method Run() Returns Nothing Declare and create an ArrayList of Integers named list list ReadFile("p01-in.txt") Declare and create an ArrayList of Integers named listRunsUpCount Declare and create an ArrayList of Integers named listRunsDnCount listRunsUpCount FindRuns(list, RUNS_UP) listRunsUpCount FindRuns(list, RUNS_DN) Declare and create an ArrayList of Integers named listRunsCount listRunsCount Merge(listRunsUpCount, listRunsDnCount) Output("p01-runs.txt", listRunsCount) End Method Run Method FindRuns(In: pList is ArrayList of Integers; int pDir is RUNS_UP or RUNS_DN) Returns ArrayList of Integers listRunsCount arrayListCreate(pList.size(), 0) Declare int varaibles i 0, k 0 While i < pList.size() - 1 Do If pDir is RUNS_UP and pList element at i is pList element at i + 1 Then Increment k ElseIf pDir is RUNS_DN and pList element at i is pList element at i + 1 Then Increment k Else If k 0 Then Increment the element at index k of listRunsCount k 0 End if End If Increment i End While If k 0 Then Increment the element at index k of listRunsCount End If Return listRunsCount End Method FindRuns Method Merge(In: pListRunsUpCount, In: pListRunsDnCount) Returns ArrayList of Integers listRunsCount arrayListCreate(pListRunsUpCount.size(), 0) For i 0 to pListRunsUpCount.size() - 1 Do Set element i of listRunsCount to the sum of the elements at i in pListRunsUpCount and pListRunsDnCount End For Return listRunsCount End Method Merge Method arrayListCreate(In: int pSize; In: int pInitValue) Returns ArrayList of Integers Declare and create an ArrayList of Integers named list Write a for loop that iterates pSize times and each time call add(pInitValue) on list Return list End Method arrayListCreate Method Output(In: pFilename; pListRuns ArrayList of Integers) Returns Nothing out open pFilename for writing out.print("runs_total, ", the sum of pListRuns) For k 1 to pListRuns.size() - 1 Do out.print("runs_k, ", the element at index k of pListRuns) End For Close out End Method Output
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started