Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 1620 Structures. Suppose I wish to store information about a student: name:string student ID:integer gpa:double.

Similar presentations


Presentation on theme: "Computer Science 1620 Structures. Suppose I wish to store information about a student: name:string student ID:integer gpa:double."— Presentation transcript:

1 Computer Science 1620 Structures

2 Suppose I wish to store information about a student: name:string student ID:integer gpa:double

3 #include using namespace std; int main() { string name = "Kevin"; int id = 123456; double gpa = 3.5; cout << "Student:" << endl; cout << "Name: " << name << endl; cout << "ID: " << id << endl; cout << "GPA: " << gpa << endl; return 0; } Could I store these three values in an array? No, values are different types.

4 Problems: 1) Passing student information to a function requires 3 parameters Example: write a function that takes the information about a student, and writes it to output void print_student(string name, int id, double gpa) { cout << "Student:" << endl; cout << "Name: " << name << endl; cout << "ID: " << id << endl; cout << "GPA: " << gpa << endl; }

5 Problems: 2) Copying information from one student to another takes three assignments

6 // included iostream and string library void print_student(string, int, double); int main() { string name = "Kevin"; int id = 123456; double gpa = 3.5; // suppose I want a copy of this student information string name_copy = name; int id_copy = id; double gpa_copy = gpa; print_student(name, id, gpa); print_student(name_copy, id_copy, gpa_copy); return 0; } // print_student definition goes down here

7 Problems: 3) Suppose I wish to have more than one student Solution 1: create 3 new variables for each student

8 // included iostream and string library void print_student(string, int, double); int main() { string name1 = "Kevin"; int id1 = 123456; double gpa1 = 3.5; string name2 = "Rex"; int id2 = 987654; double gpa2 = 4.0; string name3 = "David"; int id3 = 101010; double gpa3 = 3.75; print_student(name1, id1, gpa1); print_student(name2, id2, gpa2); print_student(name3, id3, gpa3); return 0; } Size of program grows quickly with each student.

9 Problems: 3) Suppose I wish to have more than one student Solution 2: Parallel Arrays create an array for each piece of information one for names one for IDs one for gpas Student 1's name goes in name[0], id goes in id[0], and gpa goes in gpa[0] Student 2's name goes in name[1], id goes in id[1], and gpa goes in gpa[1] and so on …

10 // included iostream and string library void print_student(string, int, double); int main() { const int MAXSIZE = 50; string name[MAXSIZE] = {"Kevin", "Rex", "David"}; int id[MAXSIZE] = {123456, 987654, 101010}; double gpa[MAXSIZE] = {3.5, 4.0, 3.75}; int size = 3; for (int i = 0; i < size; i++) print_student(name[i], id[i], gpa[i]); return 0; }

11 Problems with Parallel Arrays 1) Requires an array declaration for each attribute of record in our last example, 3 arrays imagine a real user database first name last name address phone number date of birth etc..

12 Problems with Parallel Arrays 2) Loose association of information programmer must be very careful to ensure that student's data stays in same place across all arrays example: suppose I wanted to swap the first student and the last student in my array

13 // included iostream and string library void print_student(string, int, double); int main() { const int MAXSIZE = 50; string name[MAXSIZE] = {"Kevin", "Rex", "David"}; int id[MAXSIZE] = {123456, 987654, 101010}; double gpa[MAXSIZE] = {3.5, 4.0, 3.75}; int size = 3; string tempname = name[0]; int tempid = id[0]; double tempgpa = gpa[0]; name[0] = name[size – 1]; id[0] = id[size – 1]; gpa[0] = gpa[size – 1]; name[size-1] = tempname; id[size-1] = tempid; gpa[size-1] = tempgpa; for (int i = 0; i < size; i++) print_student(name[i], id[i], gpa[i]); return 0; } If programmer forgets any of these statements, data will be out of sync. program will still compile and run

14 Structs a collection of a fixed number of components in which the components are accessed by name components may be of a different type Syntax: struct struct_name { dataType1 name1; dataType2 name2; dataType3 name3; … dataTypen namen; }; these are variable declarations. struct name can be any valid identifier

15 Example: Date write a struct called date that contains three variables year, month, day (all integers) struct Date { int day; int month; int year; };

16 How to use a struct once a struct is declared, it can be used like a data type you can declare variables of that type int main() { struct Date { int day; int month; int year; }; Date birthday; Date anniversary; //rest of code Variable is of type Date

17 A struct Variable when we declare a variable with a struct type, that variable is essentially a group of other variables those declared in the struct int main() { struct Date { int day; int month; int year; }; Date birthday; this variable "holds" three other variables day month year

18 How to access the variables in a struct? use the dot (.) operator int main() { struct Date { int day; int month; int year; }; Date birthday; birthday.day = 28; birthday.month = 12; birthday.year = 86; cout << "My birth date is " << birthday.month << '/' << birthday.day << '/' << birthday.year << endl;...

19 How to access the variables in a struct? use the dot (.) operator int main() { struct Date { int day; int month; int year; }; Date birthday; birthday.day = 28; birthday.month = 12; birthday.year = 86; cout << "My birth date is " << birthday.month << '/' << birthday.day << '/' << birthday.year << endl;...

20

21 What is the importance of this? couldn't I just write the previous code as: int main() { int day; int month; int year; day = 28; month = 12; year= 86; cout << "My birth date is " << month << '/' << day << '/' << year << endl;...

22 What if I want two dates? int main() { int day_birth, int day_grad; int month_birth, int month_grad; int year_birth, int year_grad;... int main() { struct Date { int day; int month; int year; }; Date birth, grad;... Without Structs With Structs

23 What if I want 5 dates? int main() { int day1, int day2, int day3, int day4, int day5; int month1, int month2, int month3, int month4, int month5; int year1, int year2, int year3, int year4, int year5;... int main() { struct Date { int day; int month; int year; }; Date day1, day2, day3, day4, day5; Without Structs With Structs Declaring a struct variable is the same as declaring three variables above.

24 Heterogenous Structs the variables inside a struct do not have to be the same type int main() { struct Student { string name; int number; double gpa; }; Student student1; student1.name = "Kevin"; student1.number = 123456; student1.average = 3.5;

25 Structs & Visibility a struct type can be used wherever it is visible (within its scope) void print_Xmas() { Date date1; date1.day = 25; date1.month = 12; date1.year = 2007; cout << "Xmas: " << date1.month << "/" << date1.day << endl; } int main() { struct Date { int day; int month; int year; }; print_Xmas(); return 0; } Cannot use Date in this function – out of scope.

26 Structs & Visibility most structs are declared in global space, at the start of the file this allows all functions to use the struct type struct Date { int day; int month; int year; }; void print_Xmas() { Date date1; date1.day = 25; date1.month = 12; date1.year = 2007; cout << "Xmas: " << date1.month << "/" << date1.day << endl; } int main() { print_Xmas(); return 0; } Now this is ok, Date's scope exists across all functions.

27 Structs – Assignment copying the values from one struct to another can be done with one assignment struct Date { int day, month, year; }; int main() { Date birthday; birthday.day = 28; birthday.month = 12; birthday.year = 86; Date birthday2; birthday2.day = birthday.day; birthday2.month = birthday.month; birthday2.year = birthday.year; return 0; } This can actually be done in one step.

28 Structs – Assignment copying the values from one struct to another can be done with one assignment struct Date { int day, month, year; }; int main() { Date birthday; birthday.day = 28; birthday.month = 12; birthday.year = 86; Date birthday2; birthday2 = birthday; return 0; } This can actually be done in one step.

29 Structs – Assignment copying the values from one struct to another can be done with one assignment struct Date { int day, month, year; }; int main() { Date birthday; birthday.day = 28; birthday.month = 12; birthday.year = 86; Date birthday2 = birthday; return 0; } This can actually be done in one step.

30 // included iostream and string library void print_student(string, int, double); int main() { string name = "Kevin"; int id = 123456; double gpa = 3.5; // suppose I want a copy of this student information string name_copy = name; int id_copy = id; double gpa_copy = gpa; print_student(name, id, gpa); print_student(name_copy, id_copy, gpa_copy); return 0; } // print_student definition goes down here Rewrite this example, using a struct to store the student information

31 // included iostream and string library struct Student { string name; int id; double gpa; }; void print_student(string, int, double); int main() { string name = "Kevin"; int id = 123456; double gpa = 3.5; // suppose I want a copy of this student information string name_copy = name; int id_copy = id; double gpa_copy = gpa; print_student(name, id, gpa); print_student(name_copy, id_copy, gpa_copy); return 0; } // print_student definition goes down here

32 // included iostream and string library struct Student { string name; int id; double gpa; }; void print_student(string, int, double); int main() { Student s; s.name = "Kevin"; s.id = 123456; s.gpa = 3.5; // suppose I want a copy of this student information string name_copy = s.name; int id_copy = s.id; double gpa_copy = s.gpa; print_student(s.name, s.id, s.gpa); print_student(name_copy, id_copy, gpa_copy); return 0; } // print_student definition goes down here

33 // included iostream and string library struct Student { string name; int id; double gpa; }; void print_student(string, int, double); int main() { Student s; s.name = "Kevin"; s.id = 123456; s.gpa = 3.5; // suppose I want a copy of this student information Student s_copy = s; print_student(s.name, s.id, s.gpa); print_student(s_copy.name, s_copy.id, s_copy.gpa); return 0; } // print_student definition goes down here This copies all of the variables in s

34 Passing Structs as Parameters like arrays, entire structs can be passed as a parameter passed like any other variable when the struct is passed, all of the information in the struct is passed along with it

35 Passing Structs as Parameters e.g. Write a function that takes in a Student struct, and prints that student's information to the screen our previous approach void print_student(string name, int id, double gpa) { cout << "Student:" << endl; cout << "Name: " << name << endl; cout << "ID: " << id << endl; cout << "GPA: " << gpa << endl; }

36 Passing Structs as Parameters e.g. Write a function that takes in a Student struct, and prints that student's information to the screen we can pass all of the information with a single struct void print_student(Student s) { cout << "Student:" << endl; cout << "Name: " << s.name << endl; cout << "ID: " << s.id << endl; cout << "GPA: " << s.gpa << endl; }

37 // included iostream and string library struct Student { string name; int id; double gpa; }; void print_student(string, int, double); int main() { Student s; s.name = "Kevin"; s.id = 123456; s.gpa = 3.5; // suppose I want a copy of this student information Student s_copy = s; print_student(s.name, s.id, s.gpa); print_student(s_copy.name, s_copy.id, s_copy.gpa); return 0; } void print_student(string name, int id, double gpa) { cout << "Student:" << endl; cout << "Name: " << name << endl; cout << "ID: " << id << endl; cout << "GPA: " << gpa << endl; }

38 // included iostream and string library struct Student { string name; int id; double gpa; }; void print_student(Student); int main() { Student s; s.name = "Kevin"; s.id = 123456; s.gpa = 3.5; // suppose I want a copy of this student information Student s_copy = s; print_student(s); print_student(s_copy); return 0; } void print_student(Student s) { cout << "Student:" << endl; cout << "Name: " << s.name << endl; cout << "ID: " << s.id << endl; cout << "GPA: " << s.gpa << endl; } All information passed through one variable, instead of three variables.

39

40 Question: are structs passed by value or by reference? e.g. write a function called curve that takes a Student struct as a parameter, and increases their GPA by 0.25

41 // included iostream and string library struct Student { string name; int id; double gpa; }; void curve_student(Student x) { x.gpa += 0.25; } void print_student(Student); int main() { Student s; s.name = "Kevin"; s.id = 123456; s.gpa = 3.5; print_student(s); curve_student(s); print_student(s); return 0; } What will this output? 3.5? 3.75?

42

43 Structs are passed by value therefore, a copy of the struct's data is passed any changes to the parameter variable does not affect the variable in the calling function

44 // included iostream and string library struct Student { string name; int id; double gpa; }; void curve_student(Student x) { s.gpa += 0.25; } void print_student(Student); int main() { Student s; s.name = "Kevin"; s.id = 123456; s.gpa = 3.5; print_student(s); curve_student(s); print_student(s); return 0; } x and s do not share memory.  changes to x does not affect s

45 Passing structs by reference structs can be passed by reference use the reference parameter syntax void curve_student(Student &x) { s.gpa += 0.25; }

46 // included iostream and string library struct Student { string name; int id; double gpa; }; void curve_student(Student &x) { s.gpa += 0.25; } void print_student(Student); int main() { Student s; s.name = "Kevin"; s.id = 123456; s.gpa = 3.5; print_student(s); curve_student(s); print_student(s); return 0; } x and s now share the same memory.  changes to x affect s

47

48 Structs as Parameters structs are often passed by reference saves memory saves copying time when a function does not change the parameter struct, it is declared const

49 // included iostream and string library struct Student { string name; int id; double gpa; }; void print_student(Student); int main() { Student s; s.name = "Kevin"; s.id = 123456; s.gpa = 3.5; print_student(s); return 0; } void print_student(Student x) { cout << "Student:" << endl; cout << "Name: " << x.name << endl; cout << "ID: " << x.id << endl; cout << "GPA: " << x.gpa << endl; } Two copies of student information one for x one for s

50 // included iostream and string library struct Student { string name; int id; double gpa; }; void print_student(const Student &); int main() { Student s; s.name = "Kevin"; s.id = 123456; s.gpa = 3.5; print_student(s); return 0; } void print_student(const Student &x) { cout << "Student:" << endl; cout << "Name: " << x.name << endl; cout << "ID: " << x.id << endl; cout << "GPA: " << x.gpa << endl; } Now, only one copy of student information (x and s share) const ensures no changes will happen to that copy

51 Structs as Return Types structs can also be returned as a return type e.g. write a function that returns a Date struct that represents the last day of classes struct Date { int day, month, year; }; Date last_day() { Date result; result.day = 31; result.month = 12; result.year = 2008; } int main() { Date last = last_day(); cout << "Last day of classes: " << last.month << "/" << last.day << "/" << last.year << endl; return 0; }

52 Struct Data variables inside structs can be any valid C++ type, including arrays struct Student { string name; int id; double assign_marks[5]; }; int main() { Student s; s.name = "Kevin"; s.id = 123456; s.assign_marks[0] = 75.0; s.assign_marks[1] = 64.1;...

53 Struct Data variables inside structs can be any valid C++ type, including other structs example: make a structure called Course Course will have three values the name of the course the date that the course starts (e.g., 5 th of September) the date that the course ends (e.g., 7 th of December)

54 struct Course { string name; int start_day, start_month, start_year; int finish_day, finish_month, finish_year; }; int main() { Course cs1620; cs1620.name = "Computer Science 1620"; cs1620.start_day = 5; cs1620.start_month = 9; cs1620.start_year = 2007; cs1620.finish_day = 7; cs1620.finish_month = 12; cs1620.finish_year = 2007; return 0; }

55 struct Date { int day, month, year; }; struct Course { string name; int start_day, start_month, start_year; int finish_day, finish_month, finish_year; }; int main() { Course cs1620; cs1620.name = "Computer Science 1620"; cs1620.start_day = 5; cs1620.start_month = 9; cs1620.start_year = 2007; cs1620.finish_day = 7; cs1620.finish_month = 12; cs1620.finish_year = 2007; return 0; }

56 struct Date { int day, month, year; }; struct Course { string name; Date start, finish; }; int main() { Course cs1620; cs1620.name = "Computer Science 1620"; // how do we access the starting day, starting month, etc … return 0; }

57 struct Date { int day, month, year; }; struct Course { string name; Date start, finish; }; int main() { Course cs1620; cs1620.name = "Computer Science 1620"; cs1620.start.day = 5; cs1620.start.month = 9; cs1620.start.year = 2007; cs1620.finish.day = 7; cs1620.finish.month = 12; cs1620.finish.year = 2007; return 0; } cs1620.start is a Date struct therefore, we can use the dot operator to access its variables.

58 Initializing a structure can use an initialization list, just like with an array must send in arguments in proper order struct Student { string name; int id; double gpa; }; int main() { Student s = {"Kevin", 123456, 3.75}; return 0; }

59 Initializing a structure can use an initialization list, just like with an array must send in arguments in proper order struct Student { string name; int id; double gpa; }; int main() { Student s = {"Kevin", 123456, 3.75}; return 0; }

60 Initializing a structure can use an initialization list, just like with an array must send in arguments in proper order struct Student { string name; int id; double gpa; }; int main() { Student s = {"Kevin", 123456, 3.75}; return 0; }

61 Initializing a structure can use an initialization list, just like with an array must send in arguments in proper order struct Student { string name; int id; double gpa; }; int main() { Student s = {"Kevin", 123456, 3.75}; return 0; }

62 Arrays of Structs just as a struct can hold an array, an array can hold a collection of structs e.g. write a program that reads in student information from the keyboard the program should be able to store up to 50 students name, id, student number, gpa sentinel value: "Done" (no student can have this name) when the information has been put in, the program will output the student information, in tabular form, as well as output the average mark

63 Steps: 1) Read in student information 2) Output student data to screen (table) 3) Calculate average mark of class 4) Output average mark of class

64 // included iostream, iomanip and string library struct Student { string name; int id; double gpa; }; int main() { 1) Read in student information 2) Output student data to screen (table) 3) Calculate average mark of class 4) Output average mark of class

65 // included iostream, iomanip and string library struct Student { string name; int id; double gpa; }; int main() { 1) Read in student information 2) Output student data to screen (table) 3) Calculate average mark of class 4) Output average mark of class

66 // included iostream, iomanip and string library struct Student { string name; int id; double gpa; }; int main() { 1.1) Declare storage for all students 1.2) Read in each student's information into storage 2) Output student data to screen (table) 3) Calculate average mark of class 4) Output average mark of class

67 // included iostream, iomanip and string library struct Student { string name; int id; double gpa; }; int main() { 1.1) Declare storage for all students 1.2) Read in each student's information into storage 2) Output student data to screen (table) 3) Calculate average mark of class 4) Output average mark of class

68 // included iostream, iomanip and string library struct Student { string name; int id; double gpa; }; int main() { Student students[50]; 1.2) Read in each student's information into storage 2) Output student data to screen (table) 3) Calculate average mark of class 4) Output average mark of class Now we have room for 50 students. What if we store less than that?

69 // included iostream, iomanip and string library struct Student { string name; int id; double gpa; }; int main() { Student students[50]; int size = 0; 1.2) Read in each student's information into storage 2) Output student data to screen (table) 3) Calculate average mark of class 4) Output average mark of class The size variable tells us how many students are actually in the array

70 // included iostream, iomanip and string library struct Student { string name; int id; double gpa; }; int main() { Student students[50]; int size = 0; 1.2) Read in each student's information into storage 2) Output student data to screen (table) 3) Calculate average mark of class 4) Output average mark of class

71 // included iostream, iomanip and string library struct Student { string name; int id; double gpa; }; int main() { Student students[50]; int size = 0; 1.2.) Read in a student name 1.3) While that student name is not "Done" 1.3.1) Read in the rest of the student information 1.3.2) Place this information in the array 1.3.2) Increment the number of students 1.3.3) Read in the next student name 2) Output student data to screen (table) 3) Calculate average mark of class 4) Output average mark of class

72 // included iostream, iomanip and string library struct Student { string name; int id; double gpa; }; int main() { Student students[50]; int size = 0; 1.2.) Read in a student name 1.3) While that student name is not "Done" 1.3.1) Read in the rest of the student information 1.3.2) Place this information in the array 1.3.2) Increment the number of students 1.3.3) Read in the next student name 2) Output student data to screen (table) 3) Calculate average mark of class 4) Output average mark of class

73 // included iostream, iomanip and string library struct Student { string name; int id; double gpa; }; int main() { Student students[50]; int size = 0; string name; cout << "Student name (type 'Done' to finish): "; cin >> name; 1.3) While that student name is not "Done" 1.3.1) Read in the rest of the student information 1.3.2) Place this information in the array 1.3.3) Increment the number of students 1.3.4) Read in the next student name 2) Output student data to screen (table) 3) Calculate average mark of class 4) Output average mark of class

74 // included iostream, iomanip and string library struct Student { string name; int id; double gpa; }; int main() { Student students[50]; int size = 0; string name; cout << "Student name (type 'Done' to finish): "; cin >> name; 1.3) While that student name is not "Done" 1.3.1) Read in the rest of the student information 1.3.2) Place this information in the array 1.3.3) Increment the number of students 1.3.4) Read in the next student name 2) Output student data to screen (table) 3) Calculate average mark of class 4) Output average mark of class

75 // included iostream, iomanip and string library struct Student { string name; int id; double gpa; }; int main() { Student students[50]; int size = 0; string name; cout << "Student name (type 'Done' to finish): "; cin >> name; while (name != "Done") { 1.3.1) Read in the rest of the student information 1.3.2) Place this information in the array 1.3.3) Increment the number of students 1.3.4) Read in the next student name } 2) Output student data to screen (table) 3) Calculate average mark of class 4) Output average mark of class

76 // included iostream, iomanip and string library struct Student { string name; int id; double gpa; }; int main() { Student students[50]; int size = 0; string name; cout << "Student name (type 'Done' to finish): "; cin >> name; while (name != "Done") { 1.3.1) Read in the rest of the student information 1.3.2) Place this information in the array 1.3.3) Increment the number of students 1.3.4) Read in the next student name } 2) Output student data to screen (table) 3) Calculate average mark of class 4) Output average mark of class

77 // included iostream, iomanip and string library struct Student { string name; int id; double gpa; }; int main() { Student students[50]; int size = 0; string name; cout << "Student name (type 'Done' to finish): "; cin >> name; while (name != "Done") { int id; cout << "Student ID: "; cin >> id; double gpa; cout << "GPA: "; cin >> gpa; 1.3.2) Place this information in the array 1.3.3) Increment the number of students 1.3.4) Read in the next student name } 2) Output student data to screen (table) 3) Calculate average mark of class 4) Output average mark of class

78 // included iostream, iomanip and string library struct Student { string name; int id; double gpa; }; int main() { Student students[50]; int size = 0; string name; cout << "Student name (type 'Done' to finish): "; cin >> name; while (name != "Done") { int id; cout << "Student ID: "; cin >> id; double gpa; cout << "GPA: "; cin >> gpa; 1.3.2) Place this information in the array 1.3.3) Increment the number of students 1.3.4) Read in the next student name } 2) Output student data to screen (table) 3) Calculate average mark of class 4) Output average mark of class

79 // included iostream, iomanip and string library struct Student { string name; int id; double gpa; }; int main() { Student students[50]; int size = 0; string name; cout << "Student name (type 'Done' to finish): "; cin >> name; while (name != "Done") { int id; cout << "Student ID: "; cin >> id; double gpa; cout << "GPA: "; cin >> gpa; students[size].name = name; students[size].id = id; students[size].gpa = gpa; 1.3.3) Increment the number of students 1.3.4) Read in the next student name } 2) Output student data to screen (table) 3) Calculate average mark of class 4) Output average mark of class Optimization: Instead of reading id and gpa in separate variables and transferring these values to struct, read id and gpa directly to struct

80 // included iostream, iomanip and string library struct Student { string name; int id; double gpa; }; int main() { Student students[50]; int size = 0; string name; cout << "Student name (type 'Done' to finish): "; cin >> name; while (name != "Done") { cout << "Student ID: "; cin >> students[size].id; cout << "GPA: "; cin >> students[size].gpa; students[size].name = name; 1.3.3) Increment the number of students 1.3.4) Read in the next student name } 2) Output student data to screen (table) 3) Calculate average mark of class 4) Output average mark of class

81 // included iostream, iomanip and string library struct Student { string name; int id; double gpa; }; int main() { Student students[50]; int size = 0; string name; cout << "Student name (type 'Done' to finish): "; cin >> name; while (name != "Done") { cout << "Student ID: "; cin >> students[size].id; cout << "GPA: "; cin >> students[size].gpa; students[size].name = name; 1.3.3) Increment the number of students 1.3.4) Read in the next student name } 2) Output student data to screen (table) 3) Calculate average mark of class 4) Output average mark of class

82 // included iostream, iomanip and string library struct Student { string name; int id; double gpa; }; int main() { Student students[50]; int size = 0; string name; cout << "Student name (type 'Done' to finish): "; cin >> name; while (name != "Done") { cout << "Student ID: "; cin >> students[size].id; cout << "GPA: "; cin >> students[size].gpa; students[size].name = name; size++; 1.3.4) Read in the next student name } 2) Output student data to screen (table) 3) Calculate average mark of class 4) Output average mark of class

83 // included iostream, iomanip and string library struct Student { string name; int id; double gpa; }; int main() { Student students[50]; int size = 0; string name; cout << "Student name (type 'Done' to finish): "; cin >> name; while (name != "Done") { cout << "Student ID: "; cin >> students[size].id; cout << "GPA: "; cin >> students[size].gpa; students[size++].name = name; 1.3.4) Read in the next student name } 2) Output student data to screen (table) 3) Calculate average mark of class 4) Output average mark of class

84 // included iostream, iomanip and string library struct Student { string name; int id; double gpa; }; int main() { Student students[50]; int size = 0; string name; cout << "Student name (type 'Done' to finish): "; cin >> name; while (name != "Done") { cout << "Student ID: "; cin >> students[size].id; cout << "GPA: "; cin >> students[size].gpa; students[size++].name = name; 1.3.4) Read in the next student name } 2) Output student data to screen (table) 3) Calculate average mark of class 4) Output average mark of class

85 // included iostream, iomanip and string library struct Student { string name; int id; double gpa; }; int main() { Student students[50]; int size = 0; string name; cout << "Student name (type 'Done' to finish): "; cin >> name; while (name != "Done") { cout << "Student ID: "; cin >> students[size].id; cout << "GPA: "; cin >> students[size].gpa; students[size++].name = name; cout << "Student name (type 'Done' to finish): "; cin >> name; } 2) Output student data to screen (table) 3) Calculate average mark of class 4) Output average mark of class

86 // continued from last slide 2) Output student data to screen (table) 3) Calculate average mark of class 4) Output average mark of class Name ID GPA ----------------------------------- Kevin 123456 3.50 Rex 987654 4.00 David 101010 3.75 15 10

87 // continued from last slide 2.1) Output table header 2.2) Output each student's information 3) Calculate average mark of class 4) Output average mark of class Name ID GPA ----------------------------------- Kevin 123456 3.50 Rex 987654 4.00 David 101010 3.75 15 10

88 // continued from last slide 2.1) Output table header 2.2) Output each student's information 3) Calculate average mark of class 4) Output average mark of class Name ID GPA ----------------------------------- Kevin 123456 3.50 Rex 987654 4.00 David 101010 3.75 15 10

89 // continued from last slide cout << endl << left << setw(15) << "Name" << right << setw(10) << "ID" << setw(10) << "GPA" << endl; cout << "-----------------------------------" << endl; 2.2) Output each student's information 3) Calculate average mark of class 4) Output average mark of class Name ID GPA ----------------------------------- Kevin 123456 3.50 Rex 987654 4.00 David 101010 3.75 15 10

90 // continued from last slide cout << endl << left << setw(15) << "Name" << right << setw(10) << "ID" << setw(10) << "GPA" << endl; cout << "-----------------------------------" << endl; 2.2) Output each student's information 3) Calculate average mark of class 4) Output average mark of class Name ID GPA ----------------------------------- Kevin 123456 3.50 Rex 987654 4.00 David 101010 3.75 15 10

91 // continued from last slide cout << endl << left << setw(15) << "Name" << right << setw(10) << "ID" << setw(10) << "GPA" << endl; cout << "-----------------------------------" << endl; 2.2) for each student in array 2.2.1) output their information in table 3) Calculate average mark of class 4) Output average mark of class Name ID GPA ----------------------------------- Kevin 123456 3.50 Rex 987654 4.00 David 101010 3.75 15 10

92 // continued from last slide cout << endl << left << setw(15) << "Name" << right << setw(10) << "ID" << setw(10) << "GPA" << endl; cout << "-----------------------------------" << endl; 2.2) for each student in array 2.2.1) output their information in table 3) Calculate average mark of class 4) Output average mark of class Name ID GPA ----------------------------------- Kevin 123456 3.50 Rex 987654 4.00 David 101010 3.75 15 10

93 // continued from last slide cout << endl << left << setw(15) << "Name" << right << setw(10) << "ID" << setw(10) << "GPA" << endl; cout << "-----------------------------------" << endl; for (int i = 0; i < size; i++) { 2.2.1) output their information in table } 3) Calculate average mark of class 4) Output average mark of class Name ID GPA ----------------------------------- Kevin 123456 3.50 Rex 987654 4.00 David 101010 3.75 15 10

94 // continued from last slide cout << endl << left << setw(15) << "Name" << right << setw(10) << "ID" << setw(10) << "GPA" << endl; cout << "-----------------------------------" << endl; for (int i = 0; i < size; i++) { 2.2.1) output their information in table } 3) Calculate average mark of class 4) Output average mark of class Name ID GPA ----------------------------------- Kevin 123456 3.50 Rex 987654 4.00 David 101010 3.75 15 10

95 // continued from last slide cout << endl << left << setw(15) << "Name" << right << setw(10) << "ID" << setw(10) << "GPA" << endl; cout << "-----------------------------------" << endl; for (int i = 0; i < size; i++) { 2.2.1) output their information in table } 3) Calculate average mark of class 4) Output average mark of class Name ID GPA ----------------------------------- Kevin 123456 3.50 Rex 987654 4.00 David 101010 3.75 15 10

96 // continued from last slide cout << endl << left << setw(15) << "Name" << right << setw(10) << "ID" << setw(10) << "GPA" << endl; cout << "-----------------------------------" << endl; for (int i = 0; i < size; i++) { cout << left << setw(15) << students[i].name.c_str() << right << setw(10) << students[i].id << setw(10) << students[i].gpa << endl; } 3) Calculate average mark of class 4) Output average mark of class Name ID GPA ----------------------------------- Kevin 123456 3.50 Rex 987654 4.00 David 101010 3.75 15 10 When outputting a string using setw, safer to use null-terminated version (implementation dependent)

97 // continued from last slide cout << endl << left << setw(15) << "Name" << right << setw(10) << "ID" << setw(10) << "GPA" << endl; cout << "-----------------------------------" << endl; cout << fixed << showpoint << setprecision(2); for (int i = 0; i < size; i++) { cout << left << setw(15) << students[i].name.c_str() << right << setw(10) << students[i].id << setw(10) << students[i].gpa << endl; } 3) Calculate average mark of class 4) Output average mark of class Name ID GPA ----------------------------------- Kevin 123456 3.50 Rex 987654 4.00 David 101010 3.75 15 10

98 // continued from last slide cout << endl << left << setw(15) << "Name" << right << setw(10) << "ID" << setw(10) << "GPA" << endl; cout << "-----------------------------------" << endl; cout << fixed << showpoint << setprecision(2); for (int i = 0; i < size; i++) { cout << left << setw(15) << students[i].name.c_str() << right << setw(10) << students[i].id << setw(10) << students[i].gpa << endl; } 3) Calculate average mark of class 4) Output average mark of class

99 // continued from last slide cout << endl << left << setw(15) << "Name" << right << setw(10) << "ID" << setw(10) << "GPA" << endl; cout << "-----------------------------------" << endl; cout << fixed << showpoint << setprecision(2); for (int i = 0; i < size; i++) { cout << left << setw(15) << students[i].name.c_str() << right << setw(10) << students[i].id << setw(10) << students[i].gpa << endl; } double sum = 0.0; for (int i = 0; i < size; i++) { sum += students[i].gpa; } double average = sum / size; 4) Output average mark of class

100 // continued from last slide cout << endl << left << setw(15) << "Name" << right << setw(10) << "ID" << setw(10) << "GPA" << endl; cout << "-----------------------------------" << endl; cout << fixed << showpoint << setprecision(2); for (int i = 0; i < size; i++) { cout << left << setw(15) << students[i].name.c_str() << right << setw(10) << students[i].id << setw(10) << students[i].gpa << endl; } double sum = 0.0; for (int i = 0; i < size; i++) { sum += students[i].gpa; } double average = sum / size; 4) Output average mark of class

101 // continued from last slide cout << endl << left << setw(15) << "Name" << right << setw(10) << "ID" << setw(10) << "GPA" << endl; cout << "-----------------------------------" << endl; cout << fixed << showpoint << setprecision(2); for (int i = 0; i < size; i++) { cout << left << setw(15) << students[i].name.c_str() << right << setw(10) << students[i].id << setw(10) << students[i].gpa << endl; } double sum = 0.0; for (int i = 0; i < size; i++) { sum += students[i].gpa; } double average = sum / size; cout << "\nAverage GPA: " << average << "/4" << endl; return 0; }

102 // continued from last slide cout << endl << left << setw(15) << "Name" << right << setw(10) << "ID" << setw(10) << "GPA" << endl; cout << "-----------------------------------" << endl; cout << fixed << showpoint << setprecision(2); for (int i = 0; i < size; i++) { cout << left << setw(15) << students[i].name.c_str() << right << setw(10) << students[i].id << setw(10) << students[i].gpa << endl; } double sum = 0.0; for (int i = 0; i < size; i++) { sum += students[i].gpa; } cout << "\nAverage GPA: " << (sum / size) << "/4" << endl; return 0; }

103


Download ppt "Computer Science 1620 Structures. Suppose I wish to store information about a student: name:string student ID:integer gpa:double."

Similar presentations


Ads by Google