Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 14 – Student Grades Application: Introducing.

Similar presentations


Presentation on theme: "© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 14 – Student Grades Application: Introducing."— Presentation transcript:

1 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 14 – Student Grades Application: Introducing Two- Dimensional Arrays and References Outline 14.1Test Driving the Student Grades Application 14.2Two-Dimensional Arrays Student Grade Application

2 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Objectives In this tutorial, you will learn to: –Differentiate between one-dimensional and two- dimensional arrays. –Declare and manipulate two-dimensional arrays. –Understand applications of two-dimensional arrays. –Use the right stream manipulator to right justify output in a field.

3 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 14.1Test Driving the Student Grades Application

4 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 14.1Test Driving the Student Grades Application (Cont.) Figure 14.2 Inputting grades into the Student Grades application. Figure 14.4 Displaying test grades and averages.

5 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 14.1Test Driving the Student Grades Application (Cont.) Figure 14.3 Error message displayed after inputting invalid grade.

6 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 14.2Two-Dimensional Arrays Two dimensional arrays –Require two indices –Good for representing tables One index represents the row, the other represents the column –First row number and first column number are always zero Figure 14.5 Two-dimensional array with three rows and four columns.

7 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 14.2Two-Dimensional Arrays (Cont.) Initializing two-dimensional arrays Figure 14.6 Two-dimensional numbers array after initialization.

8 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. For each student in the class Prompt the user for and input three test grades While the user enters an invalid grade Prompt the user for and input the grade again Display a table header For each student Display each student’s test grades and average grade For each test Display the average grade on the test Display the class average Figure 14.7 Pseudocode for the Student Grades application. Student Grades Application getStudentGrades Function displayGrades Function

9 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Initialization Section When declaring a function prototype that specifies a two- dimensional array parameter –The number of rows is not required –The number of columns is required

10 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Initialization Section Figure 14.8 Defining constant variables. Define constants representing the number of students and tests Figure 14.9 Declaring function prototypes. Declaring function prototypes that specify two-dimensional array parameters Specify the number of row and columns for a two-dimensional array and define the function prototype

11 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Main – Line 1 If there are more array entries than initializers, the remaining entries are initialized to 0 –This statement: would initialize the entire array to 0 ’s Initialize the studentGrade two-dimensional array to Zero. Figure 14.10 Defining and initializing a two-dimensional array. Declaring a two- dimensional array

12 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Main – Line 2 Figure 14.11 Calling the getStudentGrades function to input test grades into the studentGrades array. Prompting the user for and inputting test grades Call the getStudentGrades function and pass studentGrade a two-dimensional array as its argument

13 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Main – Line 3+ Figure 14.12 Formatting floating-point values and displaying a grade summary. Formatting floating- point values Displaying test grades and average grades Call the displayGrades function and pass studentGrade a two-dimensional array as its argument

14 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. getStudentGrades Function Figure 14.13 Declare the getStudentGrades function. Defining the function getStudentGrades Figure 14.2 Inputting grades into the Student Grades application.

15 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. getStudentGrades Function (Cont.) Nested for loops are for loops enclosed inside another control statement (usually another for loop). Particularly useful when processing two-dimensional arrays Figure 14.14 Declaring a for statement. Outside Loop Iterating once for each student Figure 14.15 Using a nested loop to input each test grade. Inside Loop Prompting the user for and inputting each of the student’s three grades

16 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. getStudentGrades Function (Cont.) Figure 14.16 while loop repeats until the user enters a valid grade. Ensuring that the user enters a valid test grade Figure 14.3 Error message displayed after inputting invalid grade. Add a nested while loop inside the inner for loop to check user- input.

17 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. displayGrades Function Figure 14.17 Defining local variables in the displayGrades function. Defining local variables

18 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. displayGrades Function 1. Table Header 2. Display student number. Outside Loop 4. Calculate and display the student’s average grade Outside Loop 6. Calculate and display the class average. 5. Calculate and display the average grade for each test. 3. Output the student’s grades. Inside Loop

19 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. displayGrades Function (1) Table Header Figure 14.18 Displaying descriptive text and a table header. Displaying a table header The right stream manipulator right justifies output in a field

20 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. displayGrades Function (2) Display student number. Beginning of the Outside Loop Figure 14.19 Displaying a number corresponding to each student. Displaying a number corresponding to each student

21 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. displayGrades Function (3) Output the student’s grades. Inside Loop Figure 14.20 Outputting the student’s grades and adding each grade to the total grades. Displaying a test grade, right justified in a field of width 8 Add the test grade to the student’s total and the class’s total

22 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. displayGrades Function (4) Calculate and display the student’s average grade. End of the Outside Loop Figure 14.21 Calculating and displaying the student’s average grade. Calculating and displaying the student’s average Resetting the student’s average

23 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. displayGrades Function (5) Calculate & Display the Average Grade for Each Test Figure 14.22 Calculating and displaying the average grade for each test. Displaying descriptive text for the row Summing each column of the table to find the total grade for each test Calculating and displaying the average grade for each test

24 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. displayGrades Function (6) Calculate and Display the Class Average. Figure 14.23 Calculating and displaying the class average. Calculating and displaying the class average

25 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. StudentGrades.cpp (1 of 7) Define constants to represent the number of students and tests Declare function prototypes that specify two-dimensional array parameters

26 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. StudentGrades.cpp (2 of 7) Declare a two- dimensional array Prompt the user for and input test grades Format floating-point values Display test grades and average grades

27 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. StudentGrades.cpp (3 of 7) Iterate once for each student Prompt user for and input each of the student’s grades

28 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. StudentGrades.cpp (4 of 7) Ensuring that user enters a valid test grade

29 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. StudentGrades.cpp (5 of 7) Define local variables

30 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. StudentGrades.cpp (6 of 7) Displaying a test grade, right justified in a field of width 8 Add the test grade to the student’s total and the class’s total Calculate and display the student’s average Reset the student average

31 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. StudentGrades.cpp (7 of 7) Display descriptive text for the row Sum each column of the table to find the total grade for each test Calculate and display the class average for each test Calculate and display the class average

32 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Lab and Homework Assignment Tutorial 13 − Salary Survey Application. Turn in annotated source file with your own comments. Answer and Turn-in Tutorial 12 Questions 12.4, 12.5, 12.9; Tutorial 13 Questions 13.3, 13.7, 13.9, 13.10; and Tutorial 14 Questions 14.1, 14.4, 14.7 with the following corrections… If an array is declared using int myArray[2][3]={{2,4,6},{1,3,5}}; the expression myArray[1][2] will return what value? For question Always write the question followed by the answer. Remember to highlight the answer. Exercises 12.11, 13.11, and 14.11. For all exercises start with the provided templates. Due next Wednesday


Download ppt "© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 14 – Student Grades Application: Introducing."

Similar presentations


Ads by Google