Download presentation
Presentation is loading. Please wait.
Published byBrianne Wilkins Modified over 8 years ago
1
Consultation Hours
2
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)
3
Arrays
4
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.
5
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.
6
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.
7
Array Declaration and Initialization The elements of the array occupy adjacent locations in memory.
9
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; }
10
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");
11
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]; }
12
Bubble Sort
15
Third Iteration
18
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; }
19
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; }
20
Stepij a[j]> a[j+1]a[0]a[1]a[2]a[3]a[4] 10 54321 200 54321 300T54321 4,5,600 45321 201 45321 301T45321 01 43521 202 43521 302T43521 02 43251 203 43251 303T43251 03 43215 204 43215 114 43215 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; }
21
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] 114 43215 210 43215 310T43215 4,5,610 34215 211 34215 311T34215 11 32415 212 32415 312T32415 12 32145 213 32145 123 32145
22
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] 123 32145 220 32145 320T32145 4,5,620 23145 221 23145 321T23145 21 21345 222 21345 132 21345 230 21345 330T21345 30 12345 231 12345 141 12345
23
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
24
2-D Array
25
Initializing two dimensional array
28
Two Dimensional Array
29
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]; }
30
Appendix
31
Practice Questions Addition of Matrices Matrix Multiplication Average and Statistics
32
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
33
Matrix Multiplication
34
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
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)
36
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; }
37
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; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.