Arrays Multidimensional initialize display Sorting Part II Multidimensional

  • Slides: 38
Download presentation
Arrays Multi-dimensional initialize & display Sorting Part II

Arrays Multi-dimensional initialize & display Sorting Part II

Multidimensional Arrays a everything about one dimensional arrays applies a all elements of the

Multidimensional Arrays a everything about one dimensional arrays applies a all elements of the same data type a just need additional sets of [ ] a a 3 -D array has rows, columns, and rank

Multidimensional Arrays int scores [4] [3] [2]; -24 // assume loaded with 1 for(row=0;

Multidimensional Arrays int scores [4] [3] [2]; -24 // assume loaded with 1 for(row=0; row<4; row++) { for(col=0; col<3; col++) { for(rank=0; rank<2; rank++) cout<<setw(6)<<scores[row][col][rank]; cout<<endl; // new line for each

Multidimensional Arrays for(row=0; row < 4; row++) { cout <<endl; // start a new

Multidimensional Arrays for(row=0; row < 4; row++) { cout <<endl; // start a new line for(col=0; col < 3; col++) { for(rank=0; rank < 2; rank++) cout<<setw(6)<<nums[row][col][rank]; ][rank] cout<<endl; // start a new line } }

Parallel Arrays Used when related data is of different data types. grade% of class

Parallel Arrays Used when related data is of different data types. grade% of class A 28 B 40 C 29 D 9 E 14 parallel arrays = two or more arrays in which elements with corresponding indexes are related *

Parallel Arrays for(row… for(col… { cout >> “Enter id#”; cin >> id[row][col]; cout >>

Parallel Arrays for(row… for(col… { cout >> “Enter id#”; cin >> id[row][col]; cout >> “Enter grade”; cin >> grade[row][col]; }

Sum a Row void main(void) { double nums [3][4] = {1, 2, 3, 4,

Sum a Row void main(void) { double nums [3][4] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; double sum. Row(double [3] [4]); // prototype } cout << “The sum of row 3 is “ << sum. Row(nums)<<endl; //function call

Sum a Row double sum. Row(double ary[3][4]) { int col; double total=0; } for(col

Sum a Row double sum. Row(double ary[3][4]) { int col; double total=0; } for(col = 0; col < 4; col++) total +=+= ary[2][col]; total ary[ ][col]; //enter row #-1 return total; output The sum of row 3 is 42

Sum a Column void main(void) { double nums [3][4] = {1, 2, 3, 4,

Sum a Column void main(void) { double nums [3][4] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 12}; double sum. Col(double [3] [4]); // prototype } cout << “The sum of column 3 is “ << sum. Col(nums) << endl; //function call

Sum a Column double sum. Col(double ary[3][4]) { int row; double total=0; } for(row

Sum a Column double sum. Col(double ary[3][4]) { int row; double total=0; } for(row = 0; row<3; row++) total +=+= ary[row][2]; total ary[row][ ]; //enter col #-1 return total; output The sum of column 3 is 21

Sum All Rows void sum. Row(double ary[3][4]) { int row, col; double total=0; 1

Sum All Rows void sum. Row(double ary[3][4]) { int row, col; double total=0; 1 for(row = X 0; row < 3; row++) { total = 0; // each row total begins at 0 for(col = 0; col<4; col++) total += ary[row][col]; // enter row # cout << "The sum of row "<<row+1 << " is "<<total<<endl; }

Sum All Columns void sum. Col(double ary[3][4]) { int row, col; double total=0; 1

Sum All Columns void sum. Col(double ary[3][4]) { int row, col; double total=0; 1 3 for(col = X 0; col < X 4; col++) { total = 0; // each col. total begins at 0 for(row = 0; row < 3; row++) total += ary[row][col]; //enter col # cout << "The sum of col " << col+1 << " is " << total << endl; } *

Sum Each Diagonal Just for fun! A 2 -D array has the same number

Sum Each Diagonal Just for fun! A 2 -D array has the same number of rows as col. 1. How to sum the diagonals - upper left to lower right? 2. How to sum the diagonals - upper right to lower left?

Sorting Internal Sorts [for small data sets] selection bubble (exchange) External Sorts [for large

Sorting Internal Sorts [for small data sets] selection bubble (exchange) External Sorts [for large data sets]

Selection Sort index (k) sm_index 21 13 9 15 17 0 2 swap 21,

Selection Sort index (k) sm_index 21 13 9 15 17 0 2 swap 21, 9 9 13 21 15 17 1 1 swap 13, 13 9 13 21 15 17 2 3 swap 21, 15 9 13 15 21 17 3 4 swap 21, 17 9 13 15 17 21

Selection Sort void sort(double [5]); void swap(double [5], int); // prototypes void main(void) {

Selection Sort void sort(double [5]); void swap(double [5], int); // prototypes void main(void) { int index; double my_list[ ] = {21, 13, 9, 15, 17}; sort(my_list); // function call cout<<"n. The sorted array is: n"; for(index=0; index<5; index++) cout<<'t'<<my_list[index]<<endl; }

Selection Sort void sort(double test. Array[5]) { int n, k, sm_index, moves=0; } double

Selection Sort void sort(double test. Array[5]) { int n, k, sm_index, moves=0; } double smallest; for(k=0; k<4; k++) // size-1 = number of passes { smallest=test. Array[k]; sm_index=k; for(n=k+1; n<5; n++) // size = # elem. to look at if(test. Array[n]<smallest) { smallest=test. Array[n]; sm_index=n; } swap(test. Array, sm_index, k); // call to swap() }

Selection Sort void swap(double test. Array[5], int smaller, int pass) { // pass =

Selection Sort void swap(double test. Array[5], int smaller, int pass) { // pass = current position: k int moves; double temp; temp=test. Array[pass]; test. Array[pass]=test. Array[smaller]; test. Array[smaller]=temp; moves++; } // not needed for swap

Bubble Sort 21 13 9 25 17 Put smaller first 13 21 9 25

Bubble Sort 21 13 9 25 17 Put smaller first 13 21 9 25 17 Put smaller first 13 9 21 25 17 No change 13 9 21 25 17 Put smaller first

Bubble Sort 13 9 21 17 25 Begin again and put smaller first 9

Bubble Sort 13 9 21 17 25 Begin again and put smaller first 9 13 21 17 25 No change 9 13 21 17 25 Put smaller first 9 13 17 21 25

A Bubble Sort Function void bubble_sort(int array[ ], int length) { int j, k,

A Bubble Sort Function void bubble_sort(int array[ ], int length) { int j, k, flag=1, temp; for(j=1; j<=length && flag; j++) { flag=0; // false for(k=0; k < (length-j); k++) { if (array[k+1] > array[k]) // > low to high { temp=array[k+1]; // swap array[k+1]= array[k]; array[k]=temp;

Array Review a is an ordered sequence of data of the same type a

Array Review a is an ordered sequence of data of the same type a can be of any valid data type a can be 1 -, 2 -, or multi- dimensional a must be declared before used a can be assigned and initialized a element numbering starts at zero

Array Review a use for loops to access (nested for multidimentional) a can be

Array Review a use for loops to access (nested for multidimentional) a can be passed back and forth between functions when sent to functions the actual values are manipulated - not a copy (passed by reference) a

Array Review -1 Write a C++ program that adds equivalent elements of the two-dimensional

Array Review -1 Write a C++ program that adds equivalent elements of the two-dimensional arrays named first and second. Both arrays should have two rows and three columns. For example, element [1][2] of the resulting array should be the sum of first[1][2] and second[1][2]. first 16 18 23 54 91 11 40 70 sum second 70 100 24 52 77 110 70 16 19 59 *

Array Review - 2 Write a C++ program that finds and displays the maximum

Array Review - 2 Write a C++ program that finds and displays the maximum value in a two-dimensional array of integers. It should also show the maximum value’s subscripts. The array should be declared as a four-by-five array of integers and initialized. data: 16, 22, 99, 4, 18, -258, 4, 101, 5, 98, 105, 6, 15, 2, 45, 33, 88, 72, 46, 3

Array Review - 3 Write a program which picks the elements in ascending order

Array Review - 3 Write a program which picks the elements in ascending order from a two dimensional matrix (4 by 5) and puts them in a single dimensional array. Display the single dimensional array.

Array Review - 4 There is a 3 by 5 array of grades. Write

Array Review - 4 There is a 3 by 5 array of grades. Write a program which reads the 15 grades and displays the number of grades below 60, number of grades in the 60's, number or grades in the 70's, number of grades in the 80's, and number of grades 90 or better.

Array Review - 5 Write a program that finds and displays the maximum value

Array Review - 5 Write a program that finds and displays the maximum value and its indicies in a twodimensional array of integers. The array should be declared as a 10 -row by 20 column array of integers in main().

Array Review - 6 a There is an array of three students each with

Array Review - 6 a There is an array of three students each with four exam scores. Assume the scores are known and are: {77, 68, 86, 73}, {96, 87, 89, 78}, {70, 90, 86, 81}. Create a program which will display the lowest grade, the highest grade and the average of the grades to two decimal places.

Array Review - 6 b #include <iostream. h> #include <iomanip. h> const int STUDENTS

Array Review - 6 b #include <iostream. h> #include <iomanip. h> const int STUDENTS = 3; const int EXAMS = 4; int mini (int [][EXAMS], int); int maxi (int [][EXAMS], int); float average(int [], int); void print. Array(int [][EXAMS], int);

Array Review - 6 c void main(void) { int student. Grades[STUDENTS][EXAMS] = {{77, 68,

Array Review - 6 c void main(void) { int student. Grades[STUDENTS][EXAMS] = {{77, 68, 86, 73}, {96, 87, 89, 78}, {70, 90, 86, 81}}; cout << "The array is: " << endl; print. Array(student. Grades, STUDENTS, EXAMS); cout<<endl<<"Lowest grade: " << mini (student. Grades, STUDENTS, EXAMS) << endl << "Highest grade: " << maxi (student. Grades, STUDENTS, EXAMS)<<endl; for (int person = 0; person < STUDENTS; person++) cout << "The average grade for student " << person << " is " <<setiosflags(ios: : fixed | ios: : showpoint)<<setprecision(2) << average(student. Grades[person], EXAMS) << endl; }

Array Review - 6 d int mini(int grades[][EXAMS], int maxi(int grades[][EXAMS], int pupils, int

Array Review - 6 d int mini(int grades[][EXAMS], int maxi(int grades[][EXAMS], int pupils, int tests) { { int low. Grade = 100; int high. Grade = 0; for (int i = 0; i < pupils; i++) for (int j = 0; j < tests; j++) if (grades[i][j] < low. Grade) if (grades[i][j] > high. Grade) low. Grade = grades[i][j]; high. Grade = grades[i][j]; return low. Grade; return high. Grade; } }

Array Review - 6 e float average(int set. Of. Grades[], void prt. Array(int grades[][EXAMS],

Array Review - 6 e float average(int set. Of. Grades[], void prt. Array(int grades[][EXAMS], int tests) int pupils, int tests) { { int total = 0; cout << " [0] [1] [2] [3]"; for (int i = 0; i < tests; i++) for (int i = 0; i < pupils; i++) { cout << endl<< "student. Grades[" total += set. Of. Grades[i]; << i << "] "; return (float) total / tests; } for (int j = 0; j < tests; j++) cout << setiosflags(ios: : left) << setw(5) << grades[i][j]; }

Array Review - 6 f function call: average(student. Grades[person], EXAMS) float average(int set. Of.

Array Review - 6 f function call: average(student. Grades[person], EXAMS) float average(int set. Of. Grades[], int tests) { int total = 0; for (int i = 0; i < tests; i++) total += set. Of. Grades[i]; return total

Common Errors ; Not declaring the array ; First element is called zero; last

Common Errors ; Not declaring the array ; First element is called zero; last element is one less than the number of elements ; Out of range subscripts - no warning ; Error in the for loop - check the counter ; Not initializing the array

Common Errors ; Aggregate operations not allowed ; Omitting array size - permitted only

Common Errors ; Aggregate operations not allowed ; Omitting array size - permitted only when declared as a formal parameter initialized in the declaration ; If array is /* in */ only, declare the formal parameter as const to prevent accidental modification

Debugging array subscripts recheck array size in declaration, initialization, and for loops Prevention -

Debugging array subscripts recheck array size in declaration, initialization, and for loops Prevention - plan first! Valuation tables Display values with cout C++ Debugger

End Note I really hate this darn machine, I wish they would sell it.

End Note I really hate this darn machine, I wish they would sell it. It never does quite what I want, But only what I tell it.