Consultation Hours
Mubashir: – Tuesday from 12:30 to 1:30 with ease of Students. Zohaib – Wednesday b/w 9:30 -10:30 Location: TA Room (next to HOD Office)
Arrays
There are situations in which we would want to store more than one value at a time in a single variable. For example, suppose we wish to arrange the percentage marks obtained by 100 students in ascending order. – Construct 100 variables to store percentage marks obtained by 100 different students, i.e. each variable containing one student’s marks. – Construct one variable (called array or subscripted variable) capable of storing or holding all the hundred values.
it would be much easier to handle one variable than handling 100 different variables. Moreover, there are certain logics that cannot be dealt with, without the use of an array.
Array Declaration and Initialization in C int num[6]={2,4,12,5,45,5}; int n[]={2,4,12,5,45,5}; float p[]={1.5,2.5,3}; int k[3]; k[0]=1; k[1]=2; k[2]=3; type variable_name[lengthofarray]; double height[10]; float width[20]; int min[9]; char name[20]; All elements of the array should be of the same type In C Language, arrays starts at position 0.
Array Declaration and Initialization The elements of the array occupy adjacent locations in memory.
9 Arrays - No bounds checking // An incorrect program. Do Not Execute! int main() { int crash[10], i; for(i=0; i<100; i++) crash[i]=i; return 1; }
Linear/Sequential Search int a[]={1,2,5,4,7}; int key=4,n=5,flag=0; for(i=0; i<= n-1; i++){ if(a[i] == key){ flag=1; break; } if(flag == 0) printf("\nThe number is not in the list"); else printf("\nThe number is found");
Finding Min and Max Number // start min and max off as equal to the first number min = nums[0]; max = nums[0]; // iterate through nums int i; for(i = 1; i < nums_length; ++i) { // update max, if necessary if( nums[i] > max ) { max = nums[i]; } // update min, if necessary if(nums[i] < min) { min = nums[i]; }
Bubble Sort
Third Iteration
Bubble Sort for(i=0; i<n-1; i++){ // number of iterations for(j=0; j<(n-i-1); j++){ // number of pair wise comparisons if(array[j]>array[j+1]){ temp = array[j+1]; array[j+1] = array[j]; array[j] = temp; }
Bubble Sort 1. for(i=0; i<n-1; i++){ // number of iterations 2. for(j=0; j<(n-i-1); j++){ // number of pair wise comparisons 3. if(array[j]>array[j+1]){ 4. temp = array[j+1]; 5. array[j+1] = array[j]; 6. array[j] = temp; }
Stepij a[j]> a[j+1]a[0]a[1]a[2]a[3]a[4] T ,5, T T T for(i=0; i<n-1; i++){ 2. for(j=0; j<(n-i-1); j++){ 3. if(array[j]>array[j+1]){ 4. temp = array[j+1]; 5. array[j+1] = array[j]; 6. array[j] = temp; }
1. for(i=0; i<n-1; i++){ 2. for(j=0; j<(n-i-1); j++){ 3. if(array[j]>array[j+1]){ 4. temp = array[j+1]; 5. array[j+1] = array[j]; 6. array[j] = temp; } Stepij a[j]> a[j+1]a[0]a[1]a[2]a[3]a[4] T ,5, T T
1. for(i=0; i<n-1; i++){ 2. for(j=0; j<(n-i-1); j++){ 3. if(array[j]>array[j+1]){ 4. temp = array[j+1]; 5. array[j+1] = array[j]; 6. array[j] = temp; } Stepij a[j]> a[j+1]a[0]a[1]a[2]a[3]a[4] T ,5, T T
Homework Trace Bubble Sort algorithm for the following input – Directly (show comparisons and swaps) – Using tracing table (show variables also) Input: {2,6,12,5,9} Submission – Handwritten – Start of next Class
2-D Array
Initializing two dimensional array
Two Dimensional Array
Matrix Addition int i, j; for (i = 0; i < NRows; i++) { for (j = 0; j < NCols; j++) { C [i][j] = A [i][j] + B [i][j]; }
Appendix
Practice Questions Addition of Matrices Matrix Multiplication Average and Statistics
Matrix Multiplication It is defined between two matrices only if the number of columns of the first matrix is the same as the number of rows of the second matrix. If A is an m-by-n matrix and B is an n-by-p matrix, then their product A×B is an m-by-p matrix given by
Matrix Multiplication
int i = 0; int j = 0; int k = 0; for(i = 0; i < 2; i++) // rows of matrix 1 for( j = 0; j < 4; j++) // columns of matrix 2 for( k = 0; k < 3; k++) // col of m1,row of m2 a3[i][j] += a1[i][k] * a2[k][j];
35 Computing Mean, Median and Mode Using Arrays Mean – Average (sum/number of elements) Median – Number in middle of sorted list – 1, 2, 3, 4, 5 (3 is median) – If even number of elements, take average of middle two Mode – Number that occurs most often – 1, 1, 1, 2, 3, 3, 4, 5 (1 is mode)
Bubble Sort for(x=0; x<n; x++){ // number of iterations for(y=x+1; y<n; y++){ // number of pair wise comparisons if(array[y]>array[y+1]){ temp = array[y+1]; array[y+1] = array[y]; array[y] = temp; }
Matrix Multiplication int i, j, k; int sum; for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { sum = 0; for (k = 0; k < N; k++) { sum += A[i][k] * B[k][j]; } C[i][j] = sum; }