Sei sulla pagina 1di 25

CSC 141: Introduction to

Computer Programming

Lecture 13: Arrays
Dr. Tayyaba Anees
1
Recap
//4 bytes long
int v;
int size = sizeof(v);
printf(%d, size );

float v;
int size = sizeof(v);
printf(%d, size );
//8 bytes long
long v;
double v;

//1 byte long
char v;
Unary operator sizeof is used to calculate the size of any
datatype
Size of an Array
int 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 marker
Then count those elements which are not equal
to that marker

Finding maximum value in
array
Let 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 array
main(){
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 <stdio.h>
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 backwards
Take numbers input from user repeatedly
and store them in array.
Stop when user inputs 0.
Print the contents of array in reverse order.
#include<stdio.h>
#define SIZE 20
void 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
Table
Consider the following 5*2 table
Student roll no Student marks
1 90
2 80
3 78
4 98
5 45
Rows * Columns
Table vs Matrix
1 90
2 80
3 78
4 98
5 45
[0][0]
1 90
2 80
3 78
4 98
5 45
In C, we can represent it in 2-d array
main( )
{
int st_rec[5][2] = {
{ 1, 90 },
{ 2, 80 },
{ 3, 78 },
{ 4, 98 },
{ 5, 45}
};
}
1 90
2 80
3 78
4 98
5 45
st_rec[cols][rows]
Accessing 2-D array
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]
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 array
void 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 ;
....
}
Accessing 2-D array
void main(){
int st_rec[][2] = {
{ 1, 90 }, { 2, 80 }, { 3, 78 },
{ 4, 98 }, { 5, 45} };
for(int i=0 ; i<5 ; i++){
for(int j=0; i<2 ; j++)
printf(%d, st_rec[ i ][ j ] ) ;
printf(\n);
}
}
2-D array
C 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 arrays
int x[2][3]


int x[4][6]
`
Addition of Matrices
Addition of Matrices
Rule: 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 n
Sum of two matrices is defined as:
(A + B)
ij
= A
ij
+ B
ij
Where 1 i m and 1 j n
What we want to do
Define a matrix A and fill it from user input
Define a matrix B and fill it from user input
Print the contents of both matrices on
screen
Add them and print the result

#include<stdio.h>
void main(){
int A[3][3], B[3][3], C[3][3] ;
printf("Enter a 3*3 matrix");
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
scanf("%d",&A[i][j]);

printf("\nEnter another 3*3 matrix");
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
scanf("%d",&B[i][j]);
printf("\nThe first matrix is\n");
for(int i=0; i<3; i++){
printf("\n");
for(int j=0; j<3; j++)
printf("%d\t", A[i][j]);
}


printf("\nThe second matrix is\n");
for(int i=0;i<3;i++){
printf("\n");
for(int j=0; j<3; j++)
printf("%d\t", B[i][j]);
}
for(int i=0 ; i<3 ; i++)
for(int j=0 ; j<3 ; j++)
C[i][j] = A[i][j]+B[i][j];

printf("\nThe addition of two matrices is\n");
for(int i=0; i<3; i++){
printf("\n");
for(int j=0; j<3; j++)
printf("%d\t", C[i][j]);
}

Potrebbero piacerti anche