550 likes | 862 Views
CSC 138 Topic 1 : Multi-dimensional Arrays. Edited from Powerpoint Slides provided by Thomson Learning By Nor Zalina Ismail. Objectives. In this chapter, you will: Learn about arrays Explore how to declare and manipulate data into arrays
E N D
CSC 138Topic 1 : Multi-dimensional Arrays Edited from Powerpoint Slides provided by Thomson Learning By Nor Zalina Ismail
Objectives In this chapter, you will: • Learn about arrays • Explore how to declare and manipulate data into arrays • Become familiar with the restrictions on array processing • Discover how to manipulate data in a two-dimensional array C++ Programming: From Problem Analysis to Program Design, Fourth Edition
Objectives(continue) In this chapter, you will: • Learn about C-strings • Examine the use of string functions to process C-strings • Discover how to input data into—and output data from—a C-string C++ Programming: From Problem Analysis to Program Design, Fourth Edition
Arrays • Array: a collection of a fixed number of components wherein all of the components have the same data type • In a one-dimensional array, the components are arranged in a list form • Syntax for declaring a one-dimensional array: intExp evaluates to a positive integer C++ Programming: From Problem Analysis to Program Design, Fourth Edition
Arrays (continued) • Example: int num[5]; C++ Programming: From Problem Analysis to Program Design, Fourth Edition
Accessing Array Components • General syntax: where indexExp, called an index, is any expression whose value is a nonnegative integer • Index value specifies the position of the component in the array • [] is the array subscriptingoperator • The array index always starts at 0 C++ Programming: From Problem Analysis to Program Design, Fourth Edition
Accessing Array Components (continued) C++ Programming: From Problem Analysis to Program Design, Fourth Edition
Accessing Array Components (continued) C++ Programming: From Problem Analysis to Program Design, Fourth Edition
Accessing Array Components (continued) C++ Programming: From Problem Analysis to Program Design, Fourth Edition
Accessing Array Components (continued) C++ Programming: From Problem Analysis to Program Design, Fourth Edition
Processing One-Dimensional Arrays • Some basic operations performed on a one-dimensional array are: • Initializing • Inputting data • Outputting data stored in an array • Finding the largest and/or smallest element • Each operation requires ability to step through the elements of the array • Easily accomplished by a loop C++ Programming: From Problem Analysis to Program Design, Fourth Edition
Processing One-Dimensional Arrays (continued) • Consider the declaration int list[100]; //array of size 100 int i; • Using for loops to access array elements: for (i = 0; i < 100; i++) //Line 1 //process list[i]//Line 2 • Example: for (i = 0; i < 100; i++) //Line 1 cin >> list[i];//Line 2 C++ Programming: From Problem Analysis to Program Design, Fourth Edition
int max=0; for(index=0;index<10;index++) { if(sales[index]>max) largestSale=sales[index]; }
Exercise • Write a program to input 5 numbers into an array and find the highest number. • Write a program to input 4 floating numbers, find the lowest and average value. • Write a program to insert a number from 1 to 10 into an array and display the number in reverse order. • Write a program to input 6 characters into an array and display how many ‘a’ characters. C++ Programming: From Problem Analysis to Program Design, Fourth Edition
Find the output for the given program: void main() { int res[20] = {1, 2, 1, 4, 3, 5, 4, 2, 3, 5,1, 1, 3, 3, 1, 5, 3, 3, 2, 2}; int freq[6]; for (int i = 0; i < 20; i++) for (int rate = 1; rate < 6; rate++) if (res [i] == rate) freq[rate]++; cout << "Rating Frequency" << endl; for(int rate = 1; rate < 6; rate++) cout « setw(4)« rate « setw(10) « freq[rate] « endl; } **taken from October 2009 final exam C++ Programming: From Problem Analysis to Program Design, Fourth Edition
Sorting the array: int tmp; for(int outloop= 9; outloop>0; outloop--) for(int inloop=0; inloop<outloop; inloop++) if ( sales[inloop] > sales [inloop + 1] ) { tmp = sales [inloop]; sales [inloop] = sales [inloop+1]; sales [inloop+1] = tmp; } Searching the array: int searchValue=3,found=-1; for(int loc=0;loc<sales.length;loc++) { if (searchValue==sales [loc]) found=loc; } if (found!=-1) cout<<searchValue+" is found at num["+found+"]”; else cout<< searchValue+" is not found”;
Sample coding(searching) void main( ){ int sales[5]={10,7,3,5,1}; int searchValue=3,found=-1; for(int loc=0;loc<5;loc++) { if (searchValue==sales [loc]) found=loc; } if (found!=-1) cout<<searchValue<<" is found at index:“<<found; else cout<< searchValue<<" is not found”; }
Exercise(Searching and sorting) • Write a program to input a value into an array. The declaration of an array as below: int array[10]; From the array, find the value of 7 and display the index/location of value 7. 2. Write a program to input 5 characters and sort the characters into ascending order.
Sample coding(sorting) void main() { int sales[5]={10,7,3,5,1}; int tmp; for(int outloop= 4; outloop>0; outloop--) for(int inloop=0; inloop<outloop; inloop++) if ( sales[inloop] > sales [inloop + 1] ) { tmp = sales [inloop]; sales [inloop] = sales [inloop+1]; sales [inloop+1] = tmp; } cout<<“Data after sorting:”; for(int i=0;i<5;i++) { cout<<sales[i]<<“ “; } } C++ Programming: From Problem Analysis to Program Design, Fourth Edition
Exercise(Sorting) Write a program to input a value into an array. The declaration of an array as below: int array[10]; Sort the data in the array in ascending order C++ Programming: From Problem Analysis to Program Design, Fourth Edition
Array Initialization During Declaration • Arrays can be initialized during declaration • In this case, it is not necessary to specify the size of the array • Size determined by the number of initial values in the braces • Example: double sales[] = {12.25, 32.50, 16.90, 23, 45.68}; C++ Programming: From Problem Analysis to Program Design, Fourth Edition
Partial Initialization of Arrays During Declaration • The statement: int list[10] = {0}; declares list to be an array of 10 components and initializes all of them to zero • The statement: int list[10] = {8, 5, 12}; declares list to be an array of 10 components, initializes list[0] to 8, list[1] to 5, list[2] to 12 and all other components are initialized to 0 C++ Programming: From Problem Analysis to Program Design, Fourth Edition
Partial Initialization of Arrays During Declaration (continued) • The statement: int list[] = {5, 6, 3}; declares list to be an array of 3 components and initializes list[0] to 5, list[1] to 6, and list[2] to 3 • The statement: int list[25]= {4, 7}; declares an array of 25 components; initializes list[0] to 4 and list[1] to 7; all other components are initialized to 0 C++ Programming: From Problem Analysis to Program Design, Fourth Edition
Some Restrictions on Array Processing • Consider the following statements: • C++ does not allow aggregate operations on an array: • Solution: C++ Programming: From Problem Analysis to Program Design, Fourth Edition
Some Restrictions on Array Processing (continued) • The following is illegal too: • Solution: • The following statements are legal, but do not give the desired results: C++ Programming: From Problem Analysis to Program Design, Fourth Edition
More exercise on One Dimensional Arrays.. 1)Write a program to input 10 numbers into an array and find the average of these numbers. 2) Write a program to initialize the given numbers and display how many odd numbers.The numbers are: 7,11,15,3,20,18 3)Write a program to input 6 characters and find the number of vowel characters C++ Programming: From Problem Analysis to Program Design, Fourth Edition
Two-Dimensional Arrays • Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two dimensions • Sometimes called matrices or tables • Declaration syntax: where intexp1 and intexp2 are expressions yielding positive integer values, and specify the number of rows and the number of columns, respectively, in the array C++ Programming: From Problem Analysis to Program Design, Fourth Edition
Two-Dimensional Arrays (continued) C++ Programming: From Problem Analysis to Program Design, Fourth Edition
Accessing Array Components • Syntax: where indexexp1 and indexexp2 are expressions yielding nonnegative integer values, and specify the row and column position C++ Programming: From Problem Analysis to Program Design, Fourth Edition
Accessing Array Components (continued) C++ Programming: From Problem Analysis to Program Design, Fourth Edition
Two-Dimensional Array Initialization During Declaration • Two-dimensional arrays can be initialized when they are declared: • Elements of each row are enclosed within braces and separated by commas • All rows are enclosed within braces • For number arrays, if all components of a row aren’t specified, unspecified ones are set to 0 C++ Programming: From Problem Analysis to Program Design, Fourth Edition
Two-Dimensional Arrays and Enumeration Types C++ Programming: From Problem Analysis to Program Design, Fourth Edition
Two-Dimensional Arrays and Enumeration Types Example void main() { enumcarType {GM,FORD,TOYOTA,BMW,NISSAN,VOLVO}; enumcolorType {RED, BROWN, BLACK,WHITE,GREY}; intinStock[6][5]={{10,2,3,7,8},{4,9,10,12,7},{7,9,3,9,16}, {9,2,7,8,19},{10,3,4,2,5},{0,11,3,4,5}}; carType car; colorType color; cout<<"Number of stock for Ford and with Grey colour is " <<inStock[FORD][GREY]; car=TOYOTA; int max=0; for(int color=RED;color<=GREY;color++) { if(inStock[car][color]>max) max=inStock[car][color];} cout<<"\nMaximum value for TOYOTA:"<<max; getch(); } C++ Programming: From Problem Analysis to Program Design, Fourth Edition
Processing Two-Dimensional Arrays • Ways to process a two-dimensional array: • Process the entire array • Process a particular row of the array, called row processing • Process a particular column of the array, called column processing • Each row and each column of a two-dimensional array is a one-dimensional array • To process, use algorithms similar to processing one-dimensional arrays C++ Programming: From Problem Analysis to Program Design, Fourth Edition
Processing Two-Dimensional Arrays (continued) C++ Programming: From Problem Analysis to Program Design, Fourth Edition
Initialization • To initialize row number 4 (i.e., fifth row) to 0 • To initialize the entire matrix to 0: C++ Programming: From Problem Analysis to Program Design, Fourth Edition
Print • To output the components of matrix: C++ Programming: From Problem Analysis to Program Design, Fourth Edition
Input • To input data into each component of matrix: C++ Programming: From Problem Analysis to Program Design, Fourth Edition
Exercise: 1) Write a program to initialize all the data given in the table into an array. From the arrays: a)Display the data in the second column b)Display the largest value for first row in the array c)Display the sum value for the entire arrays C++ Programming: From Problem Analysis to Program Design, Fourth Edition
Sum by Row • To find the sum of row number 4 of matrix: • To find the sum of each individual row: C++ Programming: From Problem Analysis to Program Design, Fourth Edition
Sum by Column • To find the sum of each individual column: C++ Programming: From Problem Analysis to Program Design, Fourth Edition
Largest Element in Each Row and Each Column C++ Programming: From Problem Analysis to Program Design, Fourth Edition
Two dimensional arrays exercise1 Write a program to process a product details for Ant Sdn. Bhd. The company have produce 5 items that manufactured by 4 branches. Detail of the data as given: C++ Programming: From Problem Analysis to Program Design, Fourth Edition
Two dimensional arrays exercise(continue) Your task are: Input all the data provided by user into two dimensional arrays Find the total of items produced by Johor branch. Find the total number of item1 produced by all branches Display the largest number of item produced by Sarawak branch C++ Programming: From Problem Analysis to Program Design, Fourth Edition
Two dimensional arrays exercise2 Alter the program in exercise1 by involve the enum declaration and add one more output for the program: -Display the number of item1 produced by Johor C++ Programming: From Problem Analysis to Program Design, Fourth Edition
C-Strings (Character Arrays) • Character array: an array whose components are of type char • C-strings are null-terminated ('\0') character arrays • Example: • 'A' is the character A • "A" is the C-string A • "A" represents two characters, 'A' and '\0‘ C++ Programming: From Problem Analysis to Program Design, Fourth Edition
C-Strings (Character Arrays) (continued) • Consider the statement char name[16]; • Since C-strings are null terminated and name has 16 components, the largest string that it can store has 15 characters • If you store a string of length, say 10 in name • The first 11 components of name are used and the last five are left unused C++ Programming: From Problem Analysis to Program Design, Fourth Edition
C-Strings (Character Arrays) (continued) • The statement char name[16] = "John"; declares an array name of length 16 and stores the C-string "John" in it • The statement char name[] = "John"; declares an array name of length 5 and stores the C-string "John" in it C++ Programming: From Problem Analysis to Program Design, Fourth Edition
C-Strings (Character Arrays) (continued) C++ Programming: From Problem Analysis to Program Design, Fourth Edition