>ws; cin.getline(student[i].name,40); cout<<"\nEnter matric number:"; cin>>student[i].matricNum; cout<<"\nEnter cgpa:"; cin>>student[i].cgpa; } double sum=0; for(int i=0;i<3;i++) sum=sum+student[i].cgpa; float average=sum/3; cout<<"\nAverage cgpa:"< Download presentation Presentation is loading. Please wait. Published byOphelia Booker
Modified over 9 years ago
1
Topic 7: Records in Arrays C++ Programming: From Problem Analysis to Program Design, Fourth Edition Objective In this chapter, you will: Learn how to create an array of struct items
2
struct s in Arrays C++ Programming: From Problem Analysis to Program Design, Fourth Edition 2 struct employeeType {char firstName[20]; char lastName[20]; int personID; char deptID[10]; double yearlySalary; double monthlySalary; double yearToDatePaid; double monthlyBonus; };
4
Programming Example C++ Programming: From Problem Analysis to Program Design, Fourth Edition 4 struct studentRec { char name[40]; int matricNum; double cgpa; }; void main() { studentRec student[3]; for(int i=0;i<3;i++) { cout<<"Enter name:"; cin>>ws; cin.getline(student[i].name,40); cout<<"\nEnter matric number:"; cin>>student[i].matricNum; cout<<"\nEnter cgpa:"; cin>>student[i].cgpa; } double sum=0; for(int i=0;i<3;i++) sum=sum+student[i].cgpa; float average=sum/3; cout<<"\nAverage cgpa:"<<average; getch(); }
5
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 5 More Programming Example struct studentRec { char name[30]; int ID; double totMark; }; void inputData(studentRec stud[]); void outputData(studentRec stud[]); double calcAvg(studentRec stud[]); void findHighest(studentRec stud[],int &highIndex); void main() { studentRec student[5]; double highest; double average; int highIndex; cout<<"\nEnter a details for 5 students:"; inputData(student); cout<<"\nOutput:"; outputData(student); average=calcAvg(student); cout<<"\nAverage mark:"<<average; findHighest(student,highIndex); cout<<"\nHighest Mark:"<<student[highIndex].totMark; cout<<"\nThe name for highest mark:"<<student[highIndex].name;}
6
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 6 More Programming Example(continue) void inputData(studentRec stud[]) { for(int i=0;i<5;i++) { cout<<"\nName:"; cin>>ws; cin.getline(stud[i].name,30); cout<<"\nID:"; cin>>stud[i].ID; cout<<"\nTotal Mark:"; cin>>stud[i].totMark; } void outputData(studentRec stud[]) { for(int i=0;i<5;i++) { cout<<"\nName:"<<stud[i].name; cout<<"\nID:"<<stud[i].ID; cout<<"\nTotal Mark:"<<stud[i].totMark; }
7
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 7 More Programming Example(continue) double calcAvg(studentRec stud[]) { double sum=0,avg; for(int i=0;i<5;i++) {sum=sum+stud[i].totMark;} avg=sum/5; return avg; } void findHighest(studentRec stud[],int &highIndex) { double high=0; for(int i=0;i<5;i++) { if(stud[i].totMark>high) {high=stud[i].totMark; highIndex=i; }
8
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 8 Exercise 1 Given the struct definition and the function prototype: struct itemRec { char itemName[30]; int itemID; double price; }; void inputData(itemRec barang[ ], int size); // input all the details of item double findHighest(itemRec barang[ ], int size); //find and return the highest item price void findLowest(itemRec barang[ ], int size, int &lowIndex); //return lowest index of the price
9
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 9 Exercise 1 (continue) Your task: 1.Write all the functions according to the function prototype 2.Write a main program that will: a) Declare an array of struct variable for 3 items. b)Input data through inputData function c)Display the highest price d)Display the lowest price together with the itemName and itemID. Hint : For task number 2(b,c and d) you should call an appropriate function
10
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 10 Exercise 2 Given the struct definition and the function prototype: struct employeeRec { char name[30]; int ID; double salary; char dept[20];//IT,Finance,Human Resource,Account}; void inputData(employeeRec emp[], int size); // input all the details of employee int countITDept(employeeRec emp[], int size); //count the number of employee under IT department void highSalary(employeeRec emp[],int size); //display the highest salary together with the name and ID of the person that get //the highest salary void lowSalary(employeeRec emp[],int &lowIndex, int size); // find the lowest salary and return to main program the index of lowest value void outputData(employeeRec emp[], int size); //display the output of employee details
11
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 11 Exercise 2(continue) Your task: 1.Write all the functions according to the function prototype 2.Write a main program that will: a) Declare an array of struct for 4 employees. b)Input data through inputData function c)Display the number of employee under IT department d)Display the highest salary together with the name and ID of the person that get the highest salary e)Display the lowest salary together with the name of the person and department f)Display all the details of the employee Hint : For task number 2(b,c,d,e and f) you should call an appropriate function
12
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 12 Exercise 3(from final exam question) Given the definition of Book record and the declaration of books array: struct Book { char title[40]; double price; int language; //1 for Bahasa Melayu and // 0 for other language }; void main() { Book books[3][30]; //There are 3 categories (0 for reference, 1 for text //and 2 for fiction) //Each category has 30 books
13
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 13 Exercise 3(continue) Write a C++ complete program by using modular programming techniques that performs each of the following task(one function for each task): a)Read data into the books array. b)Display the title and price of the most expensive book under the reference category. c)Display all the titles of the books written in Bahasa Melayu in any category and display the number of those books. d)Display the price of the book titled “Back to C++” if it is found, otherwise display an appropriate message.
14
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 14 Exercise 4 Given the definition of hotelGuest record and the declaration of guest array: struct hotelGuest { char name[40]; char paymentType[20];//Master Card or Visa or Cash}; double roomCharge;//per day int numOfStay; double totPayment; }; void main() { hotelGuest guest[5]; : }
15
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 15 Exercise 4(continue) Write a C++ complete program by using modular programming techniques that performs each of the following task(one function for each task): a)Read data into the guest array. b)Display the total payment together with their name for each guest. c)Calculate the total income gain by the hotel. d)Display the number of person that do a payment by using Visa.
Similar presentations © 2025 SlidePlayer.com. Inc. Log in
Topic 7: Records in Arrays C++ Programming: From Problem Analysis to Program Design, Fourth Edition Objective In this chapter, you will: Learn how to create.
Similar presentations
Presentation on theme: "Topic 7: Records in Arrays C++ Programming: From Problem Analysis to Program Design, Fourth Edition Objective In this chapter, you will: Learn how to create."— Presentation transcript:
Similar presentations
All rights reserved.