Student Lab 1: Input, Processing, and Output
Lab 8: Arrays
This lab accompanies Chapter 8 of Starting Out with Programming Logic & Design.
Name: ___________________________
Lab 8.1 – Arrays and Pseudocode
|Critical Review |
| |
|An array allows you to store a group of items of the same data type together in memory. |
| |
|A variable stores just a single value, and oftentimes can be cumbersome to work with when your program has similar values. |
| |
|Values stored in an array are called elements. Each element has a subscript that makes it unique. |
| |
|An array is defined as follows: |
| |
|Declare Integer numbers[10] |
| |
|Integer defines the type of numbers that can be stored, numbers is the name of the array, and [10] is how many numbers can be |
|stored. |
| |
|In most languages, the first element is assigned the subscript of 0, so the above array actually runs from 0 to 9. |
| |
|Constant variables can also be used to declare the size of an array. |
| |
|Constant Integer SIZE = 5 |
|Declare Integer numbers[SIZE] = 847, 1238, 48, 123, 840 |
| |
|Elements in the array |
|[pic] |
| |
|847 |
|1238 |
|48 |
|123 |
|840 |
| |
|0 |
|1 |
|2 |
|3 |
|4 |
| |
|[pic] |
|Subscript or Index starting a 0 |
| |
|Loops are generally used to step through an array. This can be done using any type of loop and for any process such as filling, |
|calculating, searching, sorting, or outputting elements of the array. |
This lab examines the various ways of working with arrays by writing pseudocode. Read the following programming problem prior to completing the lab.
The American Red Cross wants you to write a program that will calculate the average pints of blood donated during a blood drive. The program should take in the number of pints donated during the drive, based on a seven hour drive period. The average pints donated during that period should be calculated and displayed. Additionally, the highest and the lowest number of pints donated should be determined and displayed. Write a loop around the program to run multiple times.
Step 1: Declare the following variables:
• An array named pints of the data type Real of size 7
• A variable named totalPints of the data type Real
• A variable named averagePints of the data type Real initialized to 0
• A variable named highPints of the data type Real initialized to 0
• A variable named lowPints of the data type Real initialized to 0
Module main()
//Declare local variables
Declare String again = “yes”
____________________________________
____________________________________
____________________________________
____________________________________
____________________________________
While again == “yes”
//module calls below
Display “Do you want to run again: yes or no”
Input again
End While
End Module
Step 2: Write a function call to a function named getPints that passes the pints array. Additionally, write a function header named getPints that accepts the pints array and returns a Real array. (Reference: Passing an Array as an Argument to a Function, page 322).
//Function call
pints = ________________(______________)
// Function header
Function ___________(Real ______________[ ])
Step 3: Write a for loop that runs 7 times using the counter variable. Inside the for loop, allow the user to enter values into the array. (Reference: Using a Loop to Step Through an Array, page 301).
Declare Integer counter = 0
For __________________ = 0 to _______________
Display “Enter pints collected:”
Input ___________[_________]
End For
Step 4: Write a function call to a function named getTotal that passes the pints array and the totalPints variable. Additionally, write a function header named getTotal that accepts the pints array and the totalPints variable and returns a Real value.
//Function call
totalPints = ___________(__________, ________)
//Function header
Function _________(Real ______________[ ], Real __________)
Step 5: Write a for loop that runs 7 times using the counter variable. Inside the for loop, total up the values of the array and store in the variable totalPints. Also, return the correct variable from the function. (Reference: Totaling the Values in an Array, page 317).
Declare Integer counter = 0
Declare Real totalPints = 0
For __________________ = 0 to _______________
_________ = ________ + ______________[________]
End For
Return _________________
Step 6: Write a function call to a function named getAverage that passes the totalPints variable and the averagePints variable. Additionally, write a function header named getAverage that accepts the totalPints variable and the averagePints variable and returns a Real value.
//Function call
averagePints = __________(__________, _______)
//Function header
Function _________(Real ______________, Real ___________)
Step 7: Write a statement that will calculate the average pints donated over the drive period. Also, return the correct variable from the function. (Reference: Averaging the Values in an Array, page 318).
averagePints = _______________ / _________________
Return ______________________
Step 8: Write a function call to a module named getHigh that passes the highPints variable and the pints array. Additionally, write a function header named getHigh that accepts the highPints variable and the pints array and returns a Real value.
//Function call
highPints = __________(___________, _________)
//Function header
Function _________(Real _____________, Real ___________[ ])
Step 9: Write the code that will determine the highest value in an array. Also, return the correct variable from the function. (Reference: Finding the Highest Value in an Array, page 311).
highPints = pints[________]
Declare Integer index
For index = 1 to 6
If _______________[_______] > highPints Then
____________ = __________[_______]
End If
End For
Return ______________________
Step 10: Write a function call to a module named getLow that passes the lowPints variable and the pints array. Additionally, write a function header named getLow that accepts the lowPints variable and the pints array and returns a Real value.
//Function call
Declare Real lowPints = __________(____________, _________)
//Function header
Function _________(Real _____________, Real ___________[ ])
Step 11: Write the code that will determine the lowest value in an array. Also, return the correct variable from the function. (Reference: Finding the Lowest Value in an Array, page 320).
lowPints = pints[________]
Declare Integer index
For index = 1 to 6
If _______________[_______] < lowPints Then
____________ = __________[_______]
End If
End For
Return ______________________
Step 12: Write a module call to a module named displayInfo. Pass the necessary variable to the functions that are needed to display the averagePints, the highPints, and the lowPints. Also, write the module header that accepts the same variables.
//Module call
Call ____________(______________, ___________, ___________)
//Module header
Module ________(Real ________, Real ________, Real _______)
Lab 8.2 – Checking the Work
Using the program from Lab 8.1, complete the following checks for a better understanding of your work.
Step 1: Imagine the following number of pints were entered into the array.
|Element |34 |39 |25 |18 |43 |31 |12 |
|Index |0 |1 |2 |3 |4 |5 |6 |
Step 2: Recall Step 5 of Lab 8.1 that accumulates the pints collected.
Declare Integer counter = 0
totalPints = 0
For counter = 0 to 6
totalPints = totalPints + pints[counter]
End For
Step 3: Complete the following chart by writing what the counter and the totalPints value stores on each iteration of the loop.
|Counter |totalPints |
|0 |34 |
|1 |73 |
|2 | |
| | |
| | |
| | |
| | |
Step 4: Recall Step 9 from Lab 8.1 that determines the high value.
highPints = pints[0]
index = 1
For index = 1 to 6
If pints[index] > highPints Then
highPints = pints[index]
End If
End For
Step 5: Complete the following chart by writing what the highPints and the pints array value stores on each iteration of the loop. Also conclude whether it will be True or False.
|Pints |highPints |True or False |
|39 |34 |TRUE |
|25 |39 |FALSE |
|18 | | |
| | | |
| | | |
| | | |
Step 6: Recall Step 11 from Lab 8.1 that determines the low value.
lowPints = pints[0]
index = 1
For index = 1 to 6
If pints[index] < lowPints Then
lowPints = pints[index]
End If
End For
Step 7: Complete the following chart by writing what the lowPints and the pints array value stores on each iteration of the loop. Also conclude whether it will be True or False.
|Pints |lowPints |True or False |
|39 |34 |FALSE |
|25 |34 |TRUE |
|18 |25 | |
| | | |
| | | |
| | | |
Lab 8.3 – Arrays and a Flowchart
|Critical Review |
| |
|Arrays in Flowgorithm are declared like any other variable. With an array in addition to a variable name, type and the array size |
|must be specified. The following is an example: |
| |
|[pic] |
| |
|The size can be specified as either an numeric variable that has been declared and assigned a value, or a specific number such as |
|10. |
| |
|Array indices in Flowgorithm start at 0. This means that the largest index value is one less than the array size. So, if the |
|array is 10 in size, the largest array index value is 9. Or said another way, the last array location is identified with the index|
|value of 9. Values are placed in an array with an assign statement just was with other variables. With an array the specific |
|location within the array must be also specified. The array index value is enclosed in brackets ([]) and follows the array name as|
|follows: |
| |
|[pic] |
This lab requires you to create a flowchart for the blood drive program in Lab 8.1. Since Flowgorithm cannot return an array, the getPints module code will be included in the main method.
Step 1: Start Flowgorithm and save your document as Lab8_3. The .fprg file extension will be added automatically.
Step 2: Start by adding a Comment box that specifies your name, the date and a brief description of the program function. Then declare the variables and assign the values as specified in lab 8.1 step 1.
Step 3: Add a While loop with an Output prompt to run again and an Input statement to read in the user response such that the loop runs until the user specifies “no”.
Step 4: Inside the loop add a For loop that loops 7 times using a control variable (named ctr) with the values of 0 to 6.
Step 5: In the For loop add the following:
• An Output statement that prompts the user to enter the number of pints with the following text “Enter pints collected: ”
• An Input statement that stores the answer in the array. The input statement should be entered as below.
[pic]
Step 6: Add a getTotal function that accepts the array of pints, the totalPints variable and returns the totalPints variable. Add the following inside the function:
• A Declare statement that defines ctr.
• A For loop that loops 7 times using ctr as the control variable with the values of 0 to 6
• In the For loop, add an Assign statement that accumulates the total value of the array. The input should be as below:
[pic]
Step 7: In main, after the For loop, add:
• An Assign statement that sets totalPints to 0.
• A call to getTotal that passes the correct variables in the correct order and assigns the returned value to totalPints.
Step 8: Add the getAverage function such that its header matches Lab 8_1 step 6 and that is returns averagePints.
Step 9: Go to the getAverage() module and add an Assign statement that calculates and sets the value of averagePints.
Step 10: In main, after the Call to getTotal, add a Call to getAverage that passes the correct variables in the correct order and assigns the returned value to averagePints.
Step 11: Add a getHigh function such that its header matches Lab 8_1 step 8 and that it returns highPints. Go to the getHigh function and add the following inside the module:
• A Declare statement that defines ctr.
• Add an assignment statement that sets highPints to the first value in the pints array
• A For loop that loops 6 times using ctr as the control variable with the values of 1 to 6
• In the For loop, add an If statement that checks if the next array value is greater than highPints.
• If it is true, set highPints to the value in the array.
Step 12: In main, after the Call to getAverage, add a Call to getHigh that passes the correct variables in the correct order and assigns the returned value to highPints.
Step 13: Add a getLow( ) function to find the lowest pint value and return it to main.
Step 14: In main, after the Call to getHigh, add a Call to getLow that passes the correct variables in the correct order and assigns the returned value to lowPints
Step15: Add a displayInfo( ) module such that it displays the information as shown in step 17.
Step 16: In main, add a Call to displayInfo.
Step 17: Using the following input values, check your results. If there are errors, verify steps 1 through 16.
|Pint amounts |34 |39 |25 |18 |43 |31 |12 |
Output should be as follows:
[pic]
Send the .fprg file to rjanson@fscj.edu
Lab 8.4 – Arrays and Java Code
The goal of this lab is to convert the blood drive program from Lab 8.1 to Java code.
Step 1: Start Notepad. Prior to entering code, save your file by clicking on File and then Save. Select your location and save this file as Lab8_4.java. Be sure to include the .java extension. Then copy and paste the following code into Lab8_4.java.
Step 2: Document the first few lines of your program to include your name, the date, and a brief description of what the program does.
Step 3: Start your program with the following code:
//Lab8_4 Blood Drive
import java.util.Scanner;
public class Lab8_4 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) {
// declare variables
String again = "yes";
// method and function calls
System.out.println("Do you want to run again: yes or no");
keyboard.nextLine();
again = keyboard.nextLine();
}
//the getPints function
//the getTotal function
//the getAverage function
//the getHigh function
//the getLow function
//the displayInfo method
}
Step 4: Under the documentation for declaring variables, declare your variables and initialize them to 0. The array/list should be declared as follows:
double[] pints = new double[7];
Step 5: : In main write a function call to the getPints function and pass it pints. The function will return pints, so set pints equal to the calls returned value. This should be done as follows:
pints = getPints(pints);
Step 6: Under the documentation for the getPints function, write a for loop that will allow the user to enter pints into the array. This method might be written as follows.
// the getPints function
public static double[] getPints(double[] pints) {
int ctr;
for(ctr = 0; ctr < 7; ctr++) {
System.out.print("Enter pints collected: " );
pints[ctr] = keyboard.nextDouble();
}
return pints;
}
Step 7: In main initialize totalPints to 0 and write a call to the getTotal function and pass it pints and totalPints. This Call should set the totalPints variable since it will be returned from the function. The call might look as follows:
totalPints = getTotal(pints, totalPints);
Step 8: Under the documentation for the getTotal function, add the following statements:
• Create counter variable called ctr.
• Add a for loop that runs 7 iterations and accumulates totalPints by setting totalPints = totalPints + pints[ctr]
• Returns totalPints
Step 9: In main write a call to the getAverage function and pass it totalPints and averagePints. This function should set the averagePints variable since it will be returned from the method.
Step 10: Under the documentation for the getAverage function, add the following statements:
• A statement that will calculate averagePints as totalPints / 7
• Return averagePints
Step 11: In main write a call to the getHigh function and pass it pints and highPints. This call should set the highPints variable since it will be returned from the function. The call might look as follows:
highPints = getHigh(pints, highPints);
Step 12: Under the documentation for the getHigh method, add the following statements:
• Initialize highPints to pints[0]
• Create ctr
• Write a for loop that runs 6 iterations and includes:
o An if statement that checks to see if pints[ctr] > highPints
▪ If it is true, set highPints to pints[ctr]
• Return highPints
• Be careful to watch your indentation on this method.
Step 13: In main write a call to the getLow function and pass it pints and lowPints. This call should set the lowPints variable since it will be returned from the function.
Step 14: Under the documentation for the getLow function, add statements to find the lowest value, assign it to lowPints and return lowPints.
• Be careful to watch your indentation on this method.
Step 15: In main write a method call to the displayInfo method and pass it averagePints, highPints, and lowPints.
Step 16: Under the documentation for the displayInfo method, write the statements that will do the following:
• Display the average pints donated
• Display the highest number of pints donated
• Display the lowest number of pints donated
• Display a blank line
Step 17: In main, add a while loop such that if the user responds “yes” to the prompt, the user will enter a new set of data and the new results will be displayed.
Step 18: Run your program and check against the following input and output. If there are errors, go back through the steps to troubleshoot.
Enter pints collected: 43
Enter pints collected: 25
Enter pints collected: 64
Enter pints collected: 35
Enter pints collected: 19
Enter pints collected: 37
Enter pints collected: 46
The average number of pints donated is 38.42857142857143
The highest pints donated is 64.0
The lowest pints donated is 19.0
Do you want to run again: yes or no no
Step 19: Execute your program so that it works correctly.
Send the .java file to rjanson@fscj.edu
Lab 8.5 – Graded Assg -- Going Green
Finish the pseudocode and write the Flowchart (GoneGreen.fprg), and Java code (GoneGreen.java) for the following programming problem statement.
Last year, a local college implemented rooftop gardens as a way to promote energy efficiency and save money. Write a program that will allow the user to enter the energy bills from January to June prior to going green. Next, allow the user to enter the energy bills from January to June after going green. The program should calculate the energy difference and display the 6 months’ worth of data, along with the savings.
Hints: Create 4 global arrays of size 6 each. The first array (notGreenCost) will store the first 6 months of energy costs, the second array (goneGreenCost) will store the 6 months after going green, and the third array (savings) will store the difference. Also, create an array (months) that stores the month names.
The input and output should look as follows:
Enter NOT GREEN energy costs for January
789
Enter NOT GREEN energy costs for February
790
Enter NOT GREEN energy costs for March
890
Enter NOT GREEN energy costs for April
773
Enter NOT GREEN energy costs for May
723
Enter NOT GREEN energy costs for June
759
Enter GONE GREEN energy costs for January
546
Enter GONE GREEN energy costs for February
536
Enter GONE GREEN energy costs for March
519
Enter GONE GREEN energy costs for April
493
Enter GONE GREEN energy costs for May
472
Enter GONE GREEN energy costs for June
432
SAVINGS NOT GREEN GONE GREEN MONTH
____________________________________________________
$243 $789 $546 January
$254 $790 $536 February
$371 $890 $519 March
$280 $773 $493 April
$251 $723 $472 May
$327 $759 $432 June
Do you want to end the program(enter no or yes): yes
The Pseudocode
//Add statements to declare the global array variables
?????????
Module main()
//Declare local variables
Declare String endProgram = “no”
Call initMonths()
While endProgram == “no”
//Module calls
Call getNotGreen()
Call getGoneGreen()
Call energySaved()
Call displayInfo()
Display “Do you want to end the program (enter yes or no):”
Input endProgram
While endProgram”no” AND endProgram”yes”
Display “Please enter a value of yes or no: ”
Input endProgram
End While
End While
End Module
Module initMonths()
months = “January”, “February”, “March”, “April”, “May”, “June”
End Module
Module getNotGreen()
//Add statements to retrieve 6 months of info and save to the array
?????????
End Module
Module getGoneGreen()
//Add statements to retrieve 6 months of info and save to the array
?????????
End Module
Module energySaved()
//Add statements to calculate 6 months of savings and save to the array
?????????
End Module
Module displayInfo()
//Add statements to display results as shown above
?????????
End Module
The Pseudocode
Send the GoneGreen pseudocode file to rjanson@fscj.edu
The Flowcharts
Because Flowgorithm does not support global variables or allow a function to return an array, we will have to deviate from the pseudocode design. In the flowchart, main will create the 4 arrays and the initMonths function will be done in the main method. In addition, create a daisy-chain of calls between the various methods and pass the four arrays to each method. So main will still call getNotGreen but getNotGreen will call getGoneGreen, getGoneGreen will call energySaved, and energySaved will call displayInfo, each call passing the 4 arrays.
The results should look like the following:
[pic]
Send the GoneGreen.fprg file to rjanson@fscj.edu
The Java Code
The java code must follow the pseudocode design not the flowchart.
Send GoneGreen.java file to rjanson@fscj.edu
-----------------------
Critical Review
In Java, an array’s index starts at 0 (unlike Raptor).
The following is a method used when you know the elements of the array.
int[] even_numbers = {2, 4, 6, 8, 10};
The following is a method used when you do not know what the elements should be, but you know the size.
int[] numbers = new int[5];
You can use the System.out.println method to display an array element:
System.out.println(numbers[0]);
A loop is needed print all the elements of the array. An example is as follows:
//A for in loop
for (int ctr = 0; ctr < numbers.length; ctr++) {
System.out.println(numbers[ctr]);
}
//A while loop
int ctr = 0;
while (ctr < numbers.length) {
System.out.println(numbers[ctr]);
ctr = ctr + 1;
}
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related searches
- input and output calculator
- input and output table calculator
- java input and output stream
- java input and output file
- input and output equations
- input and output function math
- input and output math solver
- input and output equation
- input and output in math
- input and output equation calculator
- input and output tables
- input and output in java