1 / 15

C++ Programming: From Problem Analysis to Program Design, Fourth Edition

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Topic 7: Records in Arrays. Objective In this chapter, you will: Learn how to create an array of struct items. struct s in Arrays . struct employeeType { char firstName[20]; char lastName[20]; int personID;

tejana
Download Presentation

C++ Programming: From Problem Analysis to Program Design, Fourth Edition

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C++ Programming: From Problem Analysis to Program Design, Fourth Edition Topic 7: Records in Arrays Objective In this chapter, you will: Learn how to create an array of struct items

  2. structs in Arrays struct employeeType { char firstName[20]; char lastName[20]; int personID; char deptID[10]; double yearlySalary; double monthlySalary; double yearToDatePaid; double monthlyBonus; }; C++ Programming: From Problem Analysis to Program Design, Fourth Edition

  3. Programming Example C++ Programming: From Problem Analysis to Program Design, Fourth Edition

  4. More Programming Example structstudentRec { 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; inthighIndex; 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;} C++ Programming: From Problem Analysis to Program Design, Fourth Edition

  5. More Programming Example(continue) void inputData(studentRec stud[]) { for(inti=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(inti=0;i<5;i++) { cout<<"\nName:"<<stud[i].name; cout<<"\nID:"<<stud[i].ID; cout<<"\nTotal Mark:"<<stud[i].totMark; } } C++ Programming: From Problem Analysis to Program Design, Fourth Edition

  6. More Programming Example(continue) double calcAvg(studentRec stud[]) { double sum=0,avg; for(inti=0;i<5;i++) {sum=sum+stud[i].totMark; } avg=sum/5; return avg; } void findHighest(studentRec stud[],int&highIndex) { double high=0; for(inti=0;i<5;i++) { if(stud[i].totMark>high) { high=stud[i].totMark; highIndex=i; } } } C++ Programming: From Problem Analysis to Program Design, Fourth Edition

  7. Exercise 1 Given the struct definition and the function prototype: structitemRec { char itemName[30]; intitemID; double price; }; void inputData(itemRecbarang[ ], int size); // input all the details of item double findHighest(itemRecbarang[ ], int size); //find and return the highest item price void findLowest(itemRecbarang[ ], int size, int &lowIndex); //return lowest index of the price C++ Programming: From Problem Analysis to Program Design, Fourth Edition

  8. Exercise 1 (continue) Your task: 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 C++ Programming: From Problem Analysis to Program Design, Fourth Edition

  9. Exercise 2 Given the struct definition and the function prototype: structemployeeRec { char name[30]; int ID; double salary; char dept[20];//IT,Finance,HumanResource,Account}; void inputData(employeeRecemp[], int size); // input all the details of employee intcountITDept(employeeRecemp[], int size); //count the number of employee under IT department void highSalary(employeeRecemp[],int size); //display the highest salary together with the name and ID of the person that get //the highest salary void lowSalary(employeeRecemp[],int&lowIndex,int size); // find the lowest salary and return to main program the index of lowest value void outputData(employeeRecemp[], int size); //display the output of employee details C++ Programming: From Problem Analysis to Program Design, Fourth Edition

  10. Exercise 2(continue) Your task: 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 C++ Programming: From Problem Analysis to Program Design, Fourth Edition

  11. 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 BahasaMelayu 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 C++ Programming: From Problem Analysis to Program Design, Fourth Edition

  12. 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): • Read data into the books array. • Display the title and price of the most expensive book under the reference category. • Display all the titles of the books written in BahasaMelayu in any category and display the number of those books. • Display the price of the book titled “Back to C++” if it is found, otherwise display an appropriate message. C++ Programming: From Problem Analysis to Program Design, Fourth Edition

  13. Exercise 4 Given the definition of hotelGuest record and the declaration of guest array: structhotelGuest { char name[40]; char paymentType[20];//Master Card or Visa or Cash}; double roomCharge;//per day intnumOfStay; double totPayment; }; void main() { hotelGuest guest[5]; : } C++ Programming: From Problem Analysis to Program Design, Fourth Edition

  14. 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): • Read data into the guest array. • Display the total payment together with their name for each guest. • Calculate the total income gain by the hotel. • Display the number of person that do a payment by using Visa. C++ Programming: From Problem Analysis to Program Design, Fourth Edition

More Related