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.

Slides:



Advertisements
Similar presentations
Write a program step by step. Step 1: Problem definition. Given the coordinate of two points in 2-D space, compute and print their straight distance.
Advertisements

1 Programming Structures COMP102 Prog. Fundamentals, Structures / Slide 2 2 Structures l A Structure is a collection of related data items, possibly.
Structure.
Structures in C.
Student Data Score First Name Last Name ID GPA DOB Phone... How to store student data in our programs? 1.
Sorting Leena A PGT Comp SC KV No:2 Jalahalli. Introduction Common problem: sort a list of values, starting from lowest to highest. –List of exam scores.
CS Class 16 Today Announcements Review for Exam
1 Pre-Exam Class CSIT121 Fall 2000 Exam-II (Final Examination) TUESDAY DECEMBER 19th 8:30AM.
Chapter 7 Arrays C++ Programming, Namiq Sultan1 Namiq Sultan University of Duhok Department of Electrical and Computer Engineering Reference: Starting.
Wednesday, 11/6/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 11/6/02  QUESTIONS?? – HW # 4 due Monday  Today:  Return HW #3  Arrays (Chap. 10)  Reading:
Arrays.
Chapter 11: Structured Data. Slide Introduction An array makes it possible to access a list or table of data of the same data type by using a single.
By: Mr. Baha Hanene Chapter 3. Learning Outcomes We will cover the learning outcome 02 in this chapter i.e. Use basic data-types and input / output in.
Structs. Structures We already know that arrays are many variables of the same type grouped together under the same name. Structures are like arrays except.
1 Structures. Structure (struct) Definition A Structure is a container, it can hold a bunch of things. –These things can be of any type. Structures are.
LAB SESSION 5 STRUCT WITH FUNCTION 1. QUESTION 1 Given a struct definition: struct athleteRec { char name[30]; int age; char IC_number[20]; int score_event1;
Homework #3: Classes and Constructors
Edited from Powerpoint Slides provided by Thomson Learning
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Objectives: In this chapter, you will: Know the different between struct and array.
Course Title Object Oriented Programming with C++ Course instructor ADEEL ANJUM Chapter No: 05 ARRAY 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER) 1.
Module 4: Structures ITEI222 Advanced Programming.
Structure A structure is a collection of variables of different data type under one name. It is a derived data type. e.g. struct employee {int empno; char.
Structures in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR.
A First Book of ANSI C, Fourth Edition1 Functions for Modularity 04/24/15.
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures Lecture
Structured Data Types struct class Structured Data Types array – homogeneous container collections of only one type struct – heterogeneous data type.
Passing Structure to function.  structure to function structure to function  Passing structure to function in C Passing structure to function in C 
1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration.
Struct 1. Definition: Using struct to define a storage containing different types. For example it can contain int, char, float and array at the same time.
Introducing Arrays in C. PURPOSE: Storing multiple data items under the same name Example:  Salaries of 10 employees  Percentage of marks of my dear.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
Quiz // // The function exchanges the two parameters. // Param: ( ) // Param:
Chapter 7: Arrays. Outline Array Definition Access Array Array Initialization Array Processing 2D Array.
11/5/2016CS150 Introduction to Computer Science 1 Announcements  Assignment 6 due on Wednesday, December 3, 2003  Final Exam on Tuesday, December 9,
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 07 classes 1 BY ADEEL ANJUM ( MSc - cs, CCNA, WEB DEVELOPER )
User Defined Data Types - Structures in C CHAPTER 4.
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 06 FUNCTIONS 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER) 1.
Programming II Array of objects. this Using the this Pointer this Objects use the this pointer implicitly or explicitly. – this is – this is used implicitly.
Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures as Functions.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Struct Data Type in C++ What Are Structures?
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
Arrays.
Multiple Items in one Data Object Arrays are a way to store more than one piece of data in a data object, provided that all the data is of the same type.
Arrays. Topics to be Covered... Arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops Multi-dimensional arrays ◦ 2D arrays ◦ Declaration.
1. 2 Introduction Structure Definitions and Declarations Initializing Structures Operations on Structures Members Structures as Functions Parameters Array.
Class Method Read class Student { private: string id; string firstName, lastName; float gpa; public: // Will the method change any data members? // Yes!
Pointer Lecture 2 Course Name: High Level Programming Language Year : 2010.
Grouping Data Together Often we want to group together a number of values or objects to be treated in the same way e.g. names of students in a tutorial.
Exam 2 EXAM 2 Thursday!!! 25% of Final Grade Know: loops, switch/case Files Input failure (e.g. scan.hasNextInt())
13/10/2016CS150 Introduction to Computer Science 1 Multidimensional Arrays  Arrays can have more than one column  Two dimensional arrays have two columns.
Arrays. Arrays are objects that help us organize large amounts of information.
Chapter 11 Structures, Unions and Typedef 11.1 Structures Structures allow us to group related data items of different types under a common name. The individual.
Lecture 10: Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
1 Programming in C++ Dale/Weems/Headington Chapter 11 One-Dimensional Arrays.
UFCFY5-30-1Multimedia Studio Coding for Interactive Media Fundamental Concepts.
Topic 4 Data Structures Program Development and Design Using C++, Third Edition.
REVISION CSC 138 – Structured Programming One Dimensional Array
Programming Structures.
Visit for more Learning Resources
Structures.
C++ Data Types Simple Structured Address Integral Floating
Buy book Online -
S. Kiran, PGT (CS) KV, Malleswaram
Review for Final Exam.
Struct Data Type in C++ What Are Structures?
CPS120: Introduction to Computer Science
Review for Final Exam.
Exam #1 February 23rd (Next Friday)
Presentation transcript:

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

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; };

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(); }

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;}

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; }

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; }

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

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

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

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

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

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.

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]; : }

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.