150 likes | 394 Views
Tutorial 8. Concordia university Shruti Rathee. Today we will cover exercise on arrays.
E N D
Tutorial 8 Concordia university ShrutiRathee
Question1 . Write a program that asks the user to type 10 integers of an array. The program will then display either "the array is growing", "the array is decreasing", "the array is constant", or "the array is growing and decreasing."
solution • #include <iostream> #define SIZE 10 using namespace std; int main() { int numbers[SIZE]; bool increase=false, decrease=false; cout<<"Welcome, enter integers to know the nature of sequence."<<endl; for(inti=0; i < SIZE; i++) //get values for numbers[] array from user { cout<<"numbers["<<i<<"] = "; }
for(inti=0; i<SIZE-1; i++) // to check the nature of values { if(numbers[i+1]>numbers[i]) increase = true; if(numbers[i+1]<numbers[i]) decrease = true; } // print result if(increase && decrease) cout<<"Array is Increasing & Decreasing"; else if(increase) cout<<"Array is Increasing"; else if(decrease) cout<<"Array is Decreasing"; else if(!(increase && decrease)) cout<<"Array is Constant"; return 0; //successful termination }
Question2 . Write a program that asks the user to type 10 integers of an array. The program will then sort the array in descending order and display it.
solution #include <iostream> using namespace std; constint N=10; int main() { int a[N],i,nb,tmp; for(i=0;i<N;i++) { cout << "Please enter an integer: "; cin >> a[i]; } do { nb=0; for (i=0;i<N-1;i++) if (a[i]>a[i+1]) { tmp=a[i];a[i]=a[i+1];a[i+1]=tmp; nb++; } } while(nb!=0); cout << "The sorted array:" << endl; for (i=0;i<N;i++) cout << "a[" << i << "] = " << a[i] << endl; return 0; }
Question 3. Write a program which takes 2 arrays of 10 integers each, a and b. c is an array with 20 integers. The program should put into c the appending of b to a, the first 10 integers of c from array a, the latter 10 from b. Then the program should display c.
solution #include <iostream> using namespace std; constint N=10; int main() { int a[N],b[N],c[2*N],i; cout << "Enter table a:" << endl; for (i=0;i<N;i++) { cout << "Please enter an integer: "; cin >> a[i]; } cout << "Enter table b:" << endl; for (i=0;i<N;i++) { cout << "Please enter an integer: "; cin >> b[i]; } for (i=0;i<N;i++) c[i]=a[i]; for (i=0;i<N;i++) c[i+N]=b[i]; cout << "Table c:" << endl; for (i=0;i<2*N;i++) cout << c[i] << " "; cout << endl; return 0; }
Take home question (This is for complex understanding of arrays) • Using two-dimentional arrays, write a function (and a corresponding program to test it) which multiplies an mxn matrix of integers by an nxr matrix of integers. Use global constant declarations before the main program to give test values for m, n and r. Example input/output might be: INPUT FIRST (2x2) MATRIX: Type in 2 values for row 1 separated by spaces: 3 4 Type in 2 values for row 2 separated by spaces: 5 7 INPUT SECOND (2x2) MATRIX: Type in 2 values for row 1 separated by spaces: 1 1 Type in 2 values for row 2 separated by spaces: 2 2 3 4 5 7 TIMES 1 1 2 2 EQUALS 11 11 19 19
solution #include<iostream> usingnamespacestd; constint M = 2; constint N = 2; constint R = 2; /* Function to multiply matrices */ voidmatrix_mult(int a[][N], int b[][R], int answer[][R], inta_rows); /* Function to work out a single entry in the answer matrix */ intentry_for_row_and_column(int row, int column, int a[][N], int b[][R]); /* Function to input N column matrix */ voidinput_N_column_matrix(int a[][N], inta_rows); /* Function to input R column matrix */ voidinput_R_column_matrix(int a[][R], inta_rows); /* Function to display N column matrix */ voiddisplay_N_column_matrix(int a[][N], inta_rows); /* Function to display R column matrix */ voiddisplay_R_column_matrix(int a[][R], inta_rows);
/* START OF MAIN PROGRAM */ int main() { int a[M][N]; int b[N][R]; int answer[M][R]; /* input the two matrices to be multiplied */ cout << "INPUT FIRST (" << M << "x" << N << ") MATRIX:\n"; input_N_column_matrix(a, M); cout << "INPUT SECOND (" << N << "x" << R << ") MATRIX:\n"; input_R_column_matrix(b, N); /* multiply them */ matrix_mult(a, b, answer, M); /* and display the answer */ cout << "\n"; display_N_column_matrix(a, M); cout << "TIMES\n"; display_R_column_matrix(b, N); cout << "EQUALS\n"; display_R_column_matrix(answer, M); return 0; } /* END OF MAIN PROGRAM */
/* DEFINITION OF FUNCTION matrix_mult */ voidmatrix_mult(int a[][N], int b[][R], int answer[][R], inta_rows) { for (int row = 0 ; row < a_rows ; row++) for (int column = 0 ; column < R ; column++) { answer[row][column] = entry_for_row_and_column(row, column, a, b); } } /* END OF FUNCTION DEFINITION */ /* DEFINITION OF FUNCTION matrix_mult */ intentry_for_row_and_column(int row, int column, int a[][N], int b[][R]) { int total = 0; for (int count = 0 ; count < N ; count++) { total += a[row][count] * b[count][column]; } return total; } /* END OF FUNCTION DEFINITION */ /* DEFINITION OF FUNCTION input_N_column_matrix */ voidinput_N_column_matrix(int a[][N], inta_rows) { for (int row = 0 ; row < a_rows ; row++) { cout << "Type in " << N << " values for row " << row + 1 << " separated by spaces: "; for (int column = 0 ; column < N ; column++) cin >> a[row][column]; } } /* END OF FUNCTION DEFINITION */
/* DEFINITION OF FUNCTION input_R_column_matrix */ voidinput_R_column_matrix(int a[][R], inta_rows) { for (int row = 0 ; row < a_rows ; row++) { cout << "Type in " << R << " values for row " << row + 1 << " separated by spaces: "; for (int column = 0 ; column < R ; column++) cin >> a[row][column]; } } /* END OF FUNCTION DEFINITION */ /* DEFINITION OF FUNCTION display_N_column_matrix */ voiddisplay_N_column_matrix(int a[][N], inta_rows) { for (int row = 0 ; row < a_rows ; row++) { cout << " "; for (int column = 0 ; column < N ; column++) { cout.width(5); cout << a[row][column] << " "; } cout << "\n"; } } /* END OF FUNCTION DEFINITION */ /* DEFINITION OF FUNCTION display_R_column_matrix */ voiddisplay_R_column_matrix(int a[][R], inta_rows) { for (int row = 0 ; row < a_rows ; row++) { cout << " "; for (int column = 0 ; column < R ; column++) { cout.width(5); cout << a[row][column] << " "; } cout << "\n"; } } /* END OF FUNCTION DEFINITION */
Referance for more questions • http://www.doc.ic.ac.uk/~wjk/C++Intro/RobMillerE6.html • http://en.wikibooks.org/wiki/C%2B%2B_Programming/Exercises/Static_arrays/Pages