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.

Slides:



Advertisements
Similar presentations
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
Advertisements

Arrays Programming COMP102 Prog. Fundamentals I: Arrays / Slide 2 Arrays l An array is a collection of data elements that are of the same type (e.g.,
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
 2003 Prentice Hall, Inc. All rights reserved Introduction Arrays –Structures of related data items –Static entity (same size throughout program)
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Chapter 6 Control Structures.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
Chapter 9: Arrays and Strings
Computer Science 1620 Arrays. Problem: Given a list of 5 student grades, adjust the grades so that the average is 70%. Program design: 1. read in the.
Review of C++ Basics.
Chapter 9: Arrays and Strings
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
Chapter 7 Arrays C++ Programming, Namiq Sultan1 Namiq Sultan University of Duhok Department of Electrical and Computer Engineering Reference: Starting.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
What is the out put #include using namespace std; void main() { int i; for(i=1;i
Chapter 7: Arrays. Outline Array Definition Access Array Array Initialization Array Processing 2D Array.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
Chapter 8 Arrays and Strings
February 11, 2005 More Pointers Dynamic Memory Allocation.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
1 Lecture 8 Arrays Part II Sorting Arrays Sorting data  Important computing application  Virtually every organization must sort some data Massive.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Input a number #include using namespace std; int main() { int num; cout num; return 0; }
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
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?? ?
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
Chapter 7: Arrays. Outline Array Definition Access Array Array Initialization Array Processing 2D Array.
Review for Final Exam. Contents 5 questions (20 points each) + 1 bonus question (20 points) – Basic concepts in Chapters 1-4 – Chapters 5-9 – Bonus: Chapter.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
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.
Lecture 14: Arrays Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Lecture 8 – Array (Part 1) FTMK, UTeM – Sem /2014.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
Array. Array is a group of data of the same type. Array elements have a common name –The array as a whole is referenced through the common name Individual.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 19: Container classes; strings.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
Chapter 7: Arrays. 7.1 Arrays Hold Multiple Values.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Arrays Hold Multiple Values 7.1.
Computer Skills2 / Scientific Colleges 1 Arrays Topics to cover: Arrays Data Types One-dimensional Arrays Two-dimensional Arrays.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
Objectives You should be able to describe: One-Dimensional Arrays
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
New Structure Recall “average.cpp” program
Programming Funamental slides
Data type List Definition:
Review for Final Exam.
CISC181 Introduction to Computer Science Dr
CS150 Introduction to Computer Science 1
Engineering Problem Solving with C++, Etter
Review for Final Exam.
Arrays Lecture 11.
Presentation transcript:

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 C++ program to do the following: Read in and print the data Find the average mark and the highest mark, Find and print how many marks are above, how many marks are below, and how many marks are equal to the class average. Assume, there no more than 50 students in the class. The data format is as follows: a number 'num', called the parameter, saying how many marks there are for the class, followed by that many marks. Sample Data:

First Draft of Program /* Program to process exam grades */ #include using namespace std; int main() { int num,sum=0; int mark; double avgmark; cout << "Enter the number of marks: "; cin >> num; if (num > 0) cout << "There are “ << num << “ marks" <<endl; else { cout << "Invalid number of marks" << endl; exit(1); } // enter marks and find average mark for (int count = 1; count <= num; count++) { cout << "Enter a mark: "; cin >> mark; sum += mark; } avgmark = sum/num; // THIS IS WRONG!! cout <<endl<< "The average is “ << avgmark <<endl; return 0; }

Type Casting Casting is a method of explicitly requesting type conversion. Example: Change an integer in a real number avgmark = (double)sum / num; Example of Improper Casting: //casting is too late avgmark = (double)(sum / num);

Type Casting To correct the error we must change the statement avgmark = sum/num; to avgmark = (double)sum/num;

Continuing the Program Now that we calculated the average mark: What was the first mark? The second? The last? Which mark is closest to the average? How many marks are above, below, or equal to the average?

Arrays An array is a collection of elements of the same type, referenced by a single identifier. The individual elements are referenced through an index value into the array. The index is referred to as the subscript of the array. Array Declaration: data_type identifier[num_elements]; int num[5]; Array num num[0] num[1] num[2] num[3] num[4]

Using the Array num[0] = 3; num[1] = 15; num[2] = -2; num[3] = num[0] + num[1]; num[4] = num[0] * num[1] + num[2]; Array num num[0] 3 num[1] 15 num[2] -2 num[3] 18 num[4] 43

Subscript of the Array as a Variable Example: for (i = 0; i < 5; i++) num[i] = i * 3; Array num num[0] 0 num[1] 3 num[2] 6 num[3] 9 num[4] 12

Initializing Array Elements Example int num[5] = {5,11,-1,17,-32}; Array num num[0] 5 num[1] 11 num[2] num[3] 17 num[4] -32

Partial Array Initialization Example double sales[3] = {123.45, }; Array num num[0] num[1] num[2]

Interpretation of Subscripts /* Collect sales data for years 2004, 2005, 2006 */ double sales[3] = {0,0,0}; double amount; int year,numtrans; cout << "Enter the number of transactions: "; cin >> numtrans; for (int count = 0; count < numtrans; count++) { cout << "Enter the year: "; cin >> year; cout << "Enter the amount of sales for the year: "; cin >> amount; sales[year ] = amount; } for (year = 2004; year <= 2006; year++); cout << "sales for “ << year << “ are " <<sales[year-2004] << endl;

Array of char char message[20] = {'w','e','i','r','d'}; int i = 0; message[5] = ' '; // this assigns a blank to message[5] message[6] = 's'; message[7] = 't'; message[8] = 'u'; message[9] = 'f'; message[10] = 'f'; message[11] = '\0'; // this assigns a null character while (message[i] != '\0') { cout << message[i]; i++; } //output: weird stuff cout << endl; for (int i = 0; message[i] != '\0'; i++) cout << message[i]; //output: weird stuff cout << endl; cout << message << endl; //output: weird stuff Note: The last example will print the characters in the array message until it hits the null chatacter. The string within message is called a null terminated string.

Second Version of the Program const int SIZE = 50; int main() { int num,sum=0; int mark[SIZE]; double avgmark; cout << "Enter the number of marks: "; cin >> num; if (num > 0 && num <= SIZE) cout << "There are “ << num << “ marks" <<endl; else { cout << "Invalid number of marks" << endl; exit(1); } // enter marks and find average mark for (int count = 0; count < num; count++) { cout << "Enter a mark: "; cin >> mark[count]; cout << mark[count] << endl; sum += mark[count]; } avgmark = (double)sum/num; cout <<endl<< "The average is “ << avgmark <<endl; return 0; } WHAT CODE WOULD YOU WRITE TO FIND THE NUMBER OF GRADES ABOVE, BELOW AND EQUAL TO avgmark?

Arrays as Parameters of Functions Problem: Write a function called sumarray that finds the sum of the elements of an array of 'n' integers. Function Prototype: int sumarray(int [], int); Function Header: int sumarray(int numbers[], int n) The Function sumarray(): /* Function sumarray() * Input: * numbers - an array of integers * n - the number of elements in the array * Process: finds the sum of the first n elements in the numbers array. * Output:: returns the sum to the calling function. */ int sumarray(int numbers[ ], int n) { int sum=0; for (int count = 0; count < n; count++) sum += numbers[count]; return(sum); } Function Usage: int sum; sum = sumarray(mark,num);

Arrays as Parameters of Functions The Function avgarray(): /* Function avgarray() * Input: * numbers - an array of integers * n - the number of elements in the array * Process: calls sumarray to find the sum of the first n elements and then divides by n to find the average. * Output: * returns the average to the calling function. */ double avgarray(int numbers[ ], int n) { return ((double)sumarray(numbers,n)/n); } Function Prototype: double avgarray(int [], int) Function Usage: double avgmark; avgmark = avgarray(mark,num);

Changing an Array Within a Function The Function readdata(): /* Function readdata() * Input: * numbers - an array to be filled * n - a reference to the number of elements in the array * the parameters are uninitialized upon entry * Process: * reads n and reads n values into the array * Output: * the filled numbers array * n - the number of elements in the array */ Sending an array as a parameter to a function, sends the actual location of the array in memory. Consequently, changes made to the array within the function are retained upon return.

Changing an Array Within a Function void readdata(int numbers[ ], int &n) { cout << "Enter the number of marks: "; cin >> n; if (n > 0 && n <= SIZE) cout << "There are “ << n << “ marks" <<endl; else { cout << "Invalid number of marks" << endl; exit(1); } // enter the marks for (int count = 0; count < n; count++) { cout << "Enter a mark: "; cin >> numbers[count]; } return; }

Revised Version of the Main Program /* Program to process exam grades */ #include using namespace std; const int SIZE = 50; /* Function Prototypes */ void readdata(int [], int &); int sumarray(int [], int); double avgarray(int [], int); int main() { int num; int mark[SIZE]; double avgmark; // call function to read the marks readdata(mark,num); // print the mark array for (int count = 0; count < num; count++) cout<<“mark[“<<count<<“]=”<<mark[count] << endl; // find and print the average mark avgmark = avgarray(mark,num); cout <<endl<< "The average is “ << avgmark <<endl; return 0; }

Finding the Largest Element of an Array Algorithm: Initially, set the first element of the array as the largest. Compare the current largest element to the next element within the array. If the next is larger, designate that element as the largest so far. Repeat this process until all elements of the array have been compared to the largest so far. Whichever element is the largest after the last comparison, is the largest element of the entire array.

Finding the Largest Element of an Array /* Function findmax() * Input: * numbers - an array of integers * n - the number of elements in the array * Process: * finds the largest value in the array * Output: * returns the maximum value within the array */ int findmax(int numbers[], int n) { int largest_so_far; largest_so_far = numbers[0]; for (int count = 1; count < n; count++) if (largest_so_far < numbers[count]) largest_so_far = numbers[count]; return(largest_so_far); }

Finding Elements Above, Below, or Equal to a Threshold /* Function countmarks() * Input: * numbers - an array of integers * n - the number of elements in the array * avgnum - the average value of the array elements * num_below - a reference to the number of elements of the array that are below the average * num_above - a reference to the number of elements of the array that are above the average * num_equal - a reference to the number of elements of the array that equal to the average * Process: * counts the number of elements that are above, below, and equal to avgnum. * Output: * num_below - the number of elements below average * num_above - the number of elements above average * num_equal -the number of elements equal to the average */

Finding Elements Above, Below, or Equal to a Threshold void countmarks(int numbers[], int n, double avgnum, int &num_below, int &num_above, int &num_equal) { num_below=num_above=num_equal=0; //initialize for (int count = 0; count < n; count++) if ((double)numbers[count] < avgnum) num_below++; else if ((double)numbers[count] > avgnum) num_above++; else num_equal++; return; }

Another Revision of the Main Program /* Program to process exam grades */ #include using namespace std; const int SIZE = 50; /* Function Prototypes */ void readdata(int [ ], int &); int sumarray(int [ ], int); double avgarray(int [ ], int); int findmax(int [ ], int); void countmarks(int [ ], int, double, int &, int &, int &);

Another Revision of the Main Program int main() { int num; int mark[SIZE]; double avgmark; int numabove,numbelow,numequal,hi_mark; // call function to read the marks and then print the array readdata(mark,num); // print the mark array for (count = 0; count < num; count++) cout<<“mark[“<<count<<“]=”<<mark[count] << endl; // find and print the average mark avgmark = avgarray(mark,num); cout <<endl<< "The average is “ << avgmark <<endl;

Another Revision of the Main Program // find and print the highest mark hi_mark = findmax(mark,num); cout << "The highest mark is “ << hi_mark << endl; // classify marks w.r.t. average mark countmarks(mark,num,avgmark,numbelow,numabove,numequal); cout << "Number of marks below the average is " << numbelow << endl; cout << "Number of marks above the average is " <<numabove << endl; cout << "Number of marks equal to the average is " <<numequal << endl; return 0; }

Using cin.eof() to Check for the End of a Data Set Note: When cin successfully reads in a data value, the method cin.eof() returns 0 (false). When cin reaches the end of an input stream (e.g., end of a file), then cin.eof() returns 1 (true). The Function readdata(): void readdata(int numbers[ ], int &n) { // read and count the number of marks n = 0; cout << "Enter the marks - end with EOF: " << endl; cin >> numbers[n]; while (!cin.eof()) { n++; cin >> numbers[n]; } return; }

Alternate way to Check for the End of a Data Set Note: When cin successfully reads in a data value, it return 1 (true). When cin reaches the end of an input stream (e.g., end of a file), then it returns 0 (false). The Function readdata(): /*... */ void readdata(int numbers[ ], int &n) { // read and count the number of marks n = 0; cout << "Enter the marks - end with EOF: " << endl; while (cin >> numbers[n]) n++; return; }

Examples Write a generic function to add up the values of an array and return the sum Write a generic function to search an array for the largest value and return via reference the largest value and its subscript

Two Dimensional Arrays Problem: Assume that each student in a class has four grades, rather than one, representing marks on four exams. The instructor wishes to find various statistics: The average mark on each exam. The highest and lowest mark on each exam. Each student's average over all four exams. Solution: To solve this problem efficiently, we need a two-dimensional array.

Two Dimensional Arrays Two-Dimensional Array Declaration Syntax: data_type identifier [num_rows][num_columns]; Example of a 3 x 6 array: int number[3][6]; //This is the declaration Example Usage: number[0][0] = 95; number[0][5] = -27; number[1][3] = 17; number[2][1] = 68;

Processing a Two-Dimensional Array in a Main Program #include using namespace std; const int MAXSIZE = 50; const int NUMEXAMS = 4; int main() { int grade[MAXSIZE][NUMEXAMS]; int class_size; cout << “How many students in the class? "; cin >> class_size; for (int stnum = 0; stnum < class_size; stnum++) { cout << “Type in four grades for student " << stnum<< endl; for (int exam = 0, exam < NUMEXAMS; exam++) cin >> grade[stnum][exam]; cout<<"The grades for student “<<stnum<< “ were:"; for (int exam = 0, exam < NUMEXAMS; exam++) cout << “ ” << grade[stnum][exam]; cout << endl; } return 0; }

Processing a Two-Dimensional Array in a Main Program Function Prototype: void findstudentavg(int [][NUMEXAMS], int); Function Usage: findstudentavg(grade,class_size); ! The Function findstudentavg(): /* Function findstudentavg() * Input: * grade - a 2-dimensional array of grades * NUMEXAMS - numbers of exams for each student * class_size - number of students in the class * Process: * finds each student's average * Output: * prints each student's average */

Processing a Two-Dimensional Array in a Main Program void findstudentavg(int grade[][NUMEXAMS], int class_size) { int sum; double avg; for (int stnum = 0; stnum < class_size; stnum++) { sum = 0; for (int exam = 0; exam < NUMEXAMS; exam++) sum += grade[stnum][exam]; avg = (double)sum/NUMEXAMS; cout<<"Student “<<stnum<<“ had an average of “<< avg << endl; } return; }

Processing Down a Column of a Two-Dimensional Array Function Prototype: void findexamavg(int [][NUMEXAMS], int); Function Usage: findexamavg(grade,class_size); The Function findexamavg(): /* Function findexamavg() * Input: * grade - a 2-dimensional array of grades * NUMEXAMS - numbers of exams for each student * class_size - number of students in the class * Process: * finds the class average on each exam * Output: * prints the class average on each exam */

Processing Down a Column of a Two-Dimensional Array void findexamavg(int grade[][NUMEXAMS], int class_size) { int sum; double avg; for (int exam = 0; exam < NUMEXAMS; exam++) { sum = 0; for (int stnum = 0; stnum < class_size; stnum++) sum += grade[stnum][exam]; avg = (double)sum/class_size; cout << "Exam “ << exam <<“ had a class average of “ << avg << endl; } return; }

Multi-dimensional Arrays Multi-Dimensional Arrays: data_type identifier[num_dim_1][num_dim_2]...[num_dim_n];