Upload
umer-aftab
View
18
Download
0
Embed Size (px)
DESCRIPTION
ghf
Citation preview
CSC 141: Introduction to Computer Programming Lecture 13: ArraysDr. Tayyaba Anees*
Recap//4 bytes longint v;int size = sizeof(v);printf(%d, size );
float v;int size = sizeof(v);printf(%d, size );//8 bytes longlong v; double v;
//1 byte longchar v;Unary operator sizeof is used to calculate the size of any datatype
Size of an Arrayint arr[] = { 1, 2, 3, 4, 5, 6 };int size = sizeof( arr ) / sizeof( arr[0] );
How many cells in an array are filled with values?int c[10] ={2, 4, 5}; size = 10; element = 3 int c[5] ={2, 4}; size = 5; element = 2 int c[3] ={0, 2, 4}; size = 3; element = 3
Count filled cells of an array:Initialize array with some markerThen count those elements which are not equal to that marker
Finding maximum value in arrayLet us develop its algorithm/logic: Declare and assign values in array Save first value in a variable max_value Compare max_value with the second value Save the greater value in max_value and repeat the step 2 again
Finding maximum value in arraymain(){int values[5], i, max;printf("Enter 5 numbers\n");for( i = 0; i < 5; i++ ) scanf("%d", &values[i] );
max_value = values[0];for( i = 1; i < 5; ++i ){ if( values[i] > max_value ) max_value = values[i];}
With max function#include int max( int [] );
int max( int values[] ){int max_value, i;max_value = values[0];for( i = 1; i < 5; ++i ) if( values[i] > max_value ) max_value = values[i];return max_value;}IT IS NOT A COPY, BUT THE ORIGINAL.Well learn more in pointers
Usage in main()main(){int values[5], i, m;
printf("Enter 5 numbers\n");for( i = 0; i < 5; ++i )scanf("%d", &values[i] );
m = maximum( values );printf("\nMaximum value is %d\n", m );}
Prints input sequence backwardsTake numbers input from user repeatedly and store them in array.Stop when user inputs 0.Print the contents of array in reverse order.
#include#define SIZE 20void mian(){ int i, n, nums[SIZE] ; for(n = 0 ; n < SIZE ; n++) {int input ;scanf(%d, &input);if (input==0) break ;num[n] = input ; }
for(i = n-1 ; n >= 0 ; i--)printf(%d, nums[i]);}User input
2-Dimensional Arrays
Arrays of Arrays
TableConsider the following 5*2 tableRows * Columns
Student roll noStudent marks190280378498545
Table vs Matrix1902803784985 45[0][0]
190280378498545
In C, we can represent it in 2-d arraymain( ){int st_rec[5][2] = {{ 1, 90 },{ 2, 80 },{ 3, 78 },{ 4, 98 },{ 5, 45}};}st_rec[cols][rows]
190280378498545
Accessing 2-D array10,0900,121,0 801,132,0 782,143,0 983,154,0 454,1
1[0][0]90[0][1]2[1][0]80[1][1]3[2][0]78[2][1]4[3][0]98[3][1]5[4][0]45[4][1]
Accessing 2-D arrayvoid main(){ int st_rec[5][2] ; st_rec[0][0] = 1 ; st_rec[0][1] = 90 ; st_rec[1][0] = 2 ; st_rec[1][1] = 80 ; st_rec[2][0] = 3 ; st_rec[2][1] = 78 ; .... }
2-D arrayC stores it linearly: 1 90 2 80 3 78 4 98 5 45 The compiler interprets it as an array of 5 arrays of 2 ints | 1 90 | 2 80 | 3 78 | 4 98 | 5 45 |
Different 2-D arraysint x[2][3]
int x[4][6]
`
Addition of Matrices
Addition of MatricesRule: Addition of two matrices is only possible if both matrices are of same size.
Suppose two matrices A and B is of same size m X nSum of two matrices is defined as:(A + B)ij = Aij + BijWhere 1 i m and 1 j n
What we want to doDefine a matrix A and fill it from user inputDefine a matrix B and fill it from user inputPrint the contents of both matrices on screenAdd them and print the result