130 likes | 275 Views
C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Topic 5: Array versus Struct Topic 6 : Array in Struct. Objectives: In this chapter, you will: Know the different between struct and array Discover how arrays are used in a struct. Arrays versus struct s.
E N D
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Topic 5: Array versus Struct Topic 6 : Array in Struct Objectives:In this chapter, you will: Know the different between struct and arrayDiscover how arrays are used in a struct
Arrays versus structs C++ Programming: From Problem Analysis to Program Design, Fourth Edition
Arrays versus structs (Continue) Given declaration int arr1[10],arr2[10]; studentRec stud1,stud2; C++ Programming: From Problem Analysis to Program Design, Fourth Edition
Arrays in structs Struct definition: structsalesPerRec {int ID;int age; double saleByQuarter[4]; }; struct declaration: salesPerRec SP1; salesPerRec SP2; C++ Programming: From Problem Analysis to Program Design, Fourth Edition
Arrays in structs (continued) SP1 SP2 C++ Programming: From Problem Analysis to Program Design, Fourth Edition struct SP1 and SP2
Programming Example C++ Programming: From Problem Analysis to Program Design, Fourth Edition
More Programming Example C++ Programming: From Problem Analysis to Program Design, Fourth Edition
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. • 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
Exercise2 1. Change the saleByQuarter declaration into two dimensional arrays that have 4 rows and 2 columns. • 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
Exercise 3 Given a struct definition: structstudentRec { 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
Exercise 3(continue) Write a function definition that will: • inputData() //this function will input name, matric number, //quizzes mark, test mark and final mark • calcTotQuiz() //this function will calculate and return the total //mark for quizzes • calcTotTest() //this function will calculate and return the total //mark for test • 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)