Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ Programming: From Problem Analysis to Program Design, Fourth Edition Objectives: In this chapter, you will: Know the different between struct and array.

Similar presentations


Presentation on theme: "C++ Programming: From Problem Analysis to Program Design, Fourth Edition Objectives: In this chapter, you will: Know the different between struct and array."— Presentation transcript:

1 C++ Programming: From Problem Analysis to Program Design, Fourth Edition Objectives: In this chapter, you will: Know the different between struct and array Discover how arrays are used in a struct Topic 5: Array versus Struct Topic 6 : Array in Struct

2 Arrays versus struct s C++ Programming: From Problem Analysis to Program Design, Fourth Edition 2

3 Arrays versus struct s (Continue) C++ Programming: From Problem Analysis to Program Design, Fourth Edition 3 Aggregate OperationArrayStruct ArithmeticNo Example: sum=arr1+arr2; ---invalid No Example: sum=stud1+stud2; ---invalid AssignmentNo Example: arr1=arr2; ----- invalid Yes Example: stud1=stud2;-----valid Input/ Output No Example: cin>>arr1; ------- invalid cout<<arr1; ------- invalid No Example: cin>>stud1; ------- invalid cout<<stud1; ------- invalid Comparison No Example: if(arr1==arr2)---------invalid No Example: if(stud1==stud2)---------invalid Given declaration int arr1[10],arr2[10]; studentRec stud1,stud2;

4 Arrays in struct s Struct definition: struct salesPerRec { int ID; int age; double saleByQuarter[4]; }; struct declaration: salesPerRec SP1; salesPerRec SP2; C++ Programming: From Problem Analysis to Program Design, Fourth Edition 4

5 Arrays in struct s (continued) C++ Programming: From Problem Analysis to Program Design, Fourth Edition 5 ID age saleByQuarter SP2 struct SP1 and SP2 SP1 ID age saleByQuarter

6 Programming Example C++ Programming: From Problem Analysis to Program Design, Fourth Edition 6 struct studentRec { char name[40]; int age; double quiz[4]; }; void main() { studentRec student; cout<<"Enter name:"; cin.getline(student.name,40 ); cout<<"\nEnter age:"; cin>>student.age; cout<<"\nEnter quiz score:"; for(int i=0;i<4;i++) {cin>>student.quiz[i];} double sum=0; for(int i=0;i<4;i++) sum=sum+student.quiz[i]; float average=sum/4; cout<<"\nAverage quiz score:"<<average; getch(); }

7 More Programming Example C++ Programming: From Problem Analysis to Program Design, Fourth Edition 7 struct salesPerRec { int ID; int age; double saleByQuarter[4]; }; void inputData(salesPerRec &SP); void outputData(salesPerRec SP); double calcSum(salesPerRec SP); void main() { salesPerRec salesPerson1; double sumSales1; cout<<"\nInput for salesPerson1:"; inputData(salesPerson1); cout<<"\nOutput for salesPerson1:"; outputData(salesPerson1); sumSales1=calcSum(salesPerson1); cout<<"Sum of sales(SP1):"<<sumSales1; getch(); } void inputData(salesPerRec &SP) { cout >SP.ID; cout >SP.age; cout >SP.saleByQuarter[count]; } void outputData(salesPerRec SP) { cout<<"\nID:"<<SP.ID; cout<<"\nAge:"<<SP.age; cout<<"\n Sales By Quarter:"; for(int count=0;count<4;count++) cout<<SP.saleByQuarter[count]<<endl; } double calcSum(salesPerRec SP) { double total=0; for(int i=0;i<4;i++) total+=SP.saleByQuarter[i]; return total; }

8 Exercise1 1.Add more function: a)Named findAvg that will return the average value for sale by quarter. b)Named findLowest that will display the lowest sales. c) Named findIndex that will return the index of highest sales. 2. Write the function calling for all the above functions. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 8

9 Exercise2 1.Change the saleByQuarter declaration into two dimensional arrays that have 4 rows and 2 columns. 2. Write the functions that will: a)input the details of the sales person b)display the sum for each row c) return the highest value for column 0 3.Write the main function that will call all the above functions. C++ Programming: From Problem Analysis to Program Design, Fourth Edition 9

10 Exercise 3 Given a struct definition: struct studentRec { char name[40]; char matricNum; double quiz[3]; double test[2]; double final; double totMark; }; C++ Programming: From Problem Analysis to Program Design, Fourth Edition 10

11 Exercise 3(continue) Write a function definition that will: a) inputData() //this function will input name, matric number, //quizzes mark, test mark and final mark b) calcTotQuiz() //this function will calculate and return the total //mark for quizzes c) calcTotTest() //this function will calculate and return the total //mark for test d) calcTotalMark() //this function will calculate and return the total //mark based on the formula given: Total Mark= (50% of final)+(20% of total test)+(30% of total quiz) 11


Download ppt "C++ Programming: From Problem Analysis to Program Design, Fourth Edition Objectives: In this chapter, you will: Know the different between struct and array."

Similar presentations


Ads by Google