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

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Student Data Score First Name Last Name ID GPA DOB Phone... How to store student data in our programs? 1.
Topic 2 Passing Two Dimensional Arrays and One Dimensional Arrays As Parameter to Functions By: Nor Zalina Ismail Faculty of Computer and Mathematical.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 10: Records ( structs )
Introduction to Application Programming IST 256 Application Programming for Information Systems Xiaozhong Liu
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
An Introduction to Programming with C++ Fifth Edition
CS102--Object Oriented Programming Discussion 1: – Project 4 on Page 328 – The use of arrays Copyright © 2008 Xiaoyan Li.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 11: Records (structs)
1 11/8/06CS150 Introduction to Computer Science 1 Arrays Chapter 8 page 477 November 13, 2006.
Multiple-Subscripted Array
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 1 Overview – Chapter Section 4 Arrays and Classes (10.4)
Chapter 7 Arrays C++ Programming, Namiq Sultan1 Namiq Sultan University of Duhok Department of Electrical and Computer Engineering Reference: Starting.
Chapter 7 Arrays. Problem: Assume that an instructor has given an exam in class and wants to find the average mark and the highest mark. Write a complete.
Chapter 7: Arrays. Outline Array Definition Access Array Array Initialization Array Processing 2D Array.
03/16/ What is an Array?... An array is an object that stores list of items. Each slot of an array holds an individual element. Characteristics.
Chapter 10: Records (structs)
 Review structures  Program to demonstrate a structure containing a pointer.
 For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning.
Crypto Project Sample Encrypted Data: –Turing: CS Public\CryptoProjectData Crypto_Short_Keys_P_U.out Crypto_Long_Keys_O_R.out Bonus +10 points on.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 11: Records ( struct s)
Chapter 10: Records (structs)
Chapter 10: Records (structs)
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.
Edited from Powerpoint Slides provided by Thomson Learning
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.
DATA STRUCTURES LAB 1 TA: Nouf Al-harbi
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data.
Computer Programming TCP1224 Chapter 11 Arrays. Objectives Using Arrays Declare and initialize a one-dimensional array Manipulate a one-dimensional array.
Passing Structure to function.  structure to function structure to function  Passing structure to function in C Passing structure to function in C 
1 Chapter 7 Arrays. 2 Topics 7.1 Arrays Hold Multiple Values 7.2 Accessing Array Elements 7.3 No Bounds Checking in C Array Initialization 7.5 Processing.
Computer Programming Lecture 8 Arrays. 2 switch-statement Example (3) If use press left arrowIf use press right arrow If use press up arrow If use press.
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.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 8 Arrays.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
1 Arrays and Strings Lecture: Design Problem l Consider a program to calculate class average Why?? ?
Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing.
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 06 FUNCTIONS 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER) 1.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
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.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 11: Records ( struct s)
Lecture 14: Arrays Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Arrays.
Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.
Module 1: Array ITEI222 - Advance Programming Language.
C++ Programming Lecture 16 Arrays – Part III The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Lecture 20 Polymorphism. Introduction General meaning ; the ability to take on different forms. Programming language term: –Allows an entity to take a.
1.8 Multidimensional Arrays academy.zariba.com 1.
Arrays Chapter 12. One-Dimensional Arrays If you wanted to read in 1000 ints and print them in reverse order, it would take a program that’s over 3000.
13/10/2016CS150 Introduction to Computer Science 1 Multidimensional Arrays  Arrays can have more than one column  Two dimensional arrays have two columns.
CS 1430: Programming in C++.
Arrays. Arrays are objects that help us organize large amounts of information.
FLOW OF CONTROL In C++ the program execution starts with the first statement in the main() and ends with the last statement of main(). This is called.
Two-Dimensional Data Class of 5 students Each student has 3 test scores Store this information in a two- dimensional array First dimension: which student.
Chapter 7: Arrays. 7.1 Arrays Hold Multiple Values.
Chapter 8: Arrays. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored in adjacent memory locations.
Computer Skills2 / Scientific Colleges 1 Arrays Topics to cover: Arrays Data Types One-dimensional Arrays Two-dimensional Arrays.
1 Two-Dimensional Arrays. 2 Terminology Two-dimensional arrays represent matrices A matrix contains a number of values of the same data type The values.
Topic 4 Data Structures Program Development and Design Using C++, Third Edition.
REVISION CSC 138 – Structured Programming One Dimensional Array
CS 1430: Programming in C++.
Introduction to Programming
C++ Programming Lecture 16 Arrays – Part III
Lecture 12 Oct 16, 02.
Multidimensional Arrays
CHAPTER 2 Arrays and Vectors.
INC 161 , CPE 100 Computer Programming
CHAPTER 2 Arrays and Vectors.
Presentation transcript:

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

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

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;

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

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

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

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

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

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

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

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