Arrays 1
Arrays 1
Arrays 1
Arrays 1
Why Arrays?
● Read ages for
● 5 people, print the respective ages and their
average
● int a1, a2, a3, a4, a5;
● Read values for a1 to a5 ... scanf 5 times
● Print values for a1 to a5 ... printf 5 times
Arrays 2
Why Arrays?
● Read ages for
● 5 people, print the respective ages and their average
● int a1, a2, a3, a4, a5;
● Read values for a1 to a5 ... scanf 5 times
● Print values for a1 to a5 ... printf 5 times
● Neat way : use loop
● Problem with loop is that different variables are needed to
store the ages of different persons
● Loop will have to store and print different variables at every
iteration
● Elegant way : use arrays to store the ages
Arrays 3
Example
#include <stdio.h>
void main()
{
/* Declaration : datatype, name, size */
int age[5];
int i,n;
float sum=0;
...
}
Arrays 4
Example
for(i=0 ; i< n ; i++) {
printf(“Age: “);
/* accessing array elements */
scanf(“%d”, &age[i]);
sum += age[i];
}
printf(“The ages input are:\n”);
for(i=0 ; i<n ; i++)
printf(“%d\n”, age[i]);
printf(“The average is: %f\n”, sum / n);
Arrays 5
● Initialisation of single dimensional array
int a[5] = { 13, 14, 15, 22, 23 };
This makes a[0]=13, a[1] = 14, ...
● Warning: C does not check validity of subscripts when you
use arrays. Can lead to runtime error.
int a[40];
a[50]=11; /* no compile time error */
● Name of array: also refers to the memory location of the
first number in the array
scanf(“%f”, &f[0]); is same as
scanf(“%f”, f);
Arrays 6
Find largest and smallest element
Arrays 7
Find largest and smallest element
/* Initialisation */
#include <stdio.h>
large = a[0];
main() small = a[0];
{
int i, n; /* largest & smallest element */
float a[50], large, small; for (i=1; i<n ; i++) {
if (a[i] > large)
large = a[i];
printf("Size of vector? ");
else if (a[i] < small)
scanf("%d", &n);
small = a[i];
}
printf("\nElements? :\n"); printf("\n Largest: %f\n", large);
for (i=0 ; i<n ; i++) printf("\nSmallest: %f\n", small);
scanf("%f", &a[i]); }
Arrays 8
Insert an element into a vector
Arrays 9
Insert an element into a vector
#include <stdio.h>
printf("\n Position of insertion?:
");
main()
scanf("%d", &pos);
{
int i, j, k, n, pos;
/* pushing down elements */
int a[50], item;
for (k=n ; k >= pos ; k--)
a[k] = a[k-1];
printf("Size of vector? ");
/* insert item */
scanf("%d", &n);
a[--pos] = item;
Arrays 10
Multidimensional Arrays
● Useful to store two dimensional data structures
● Example: Matrix
● Array will have two subscripts
● one for row and one for column
● float m[10][20];
● 10 rows, 20 columns
● First row, first column: m[0][0]
● Last row, last column: m[9][19]
● Memory storage: first row - all columns, then second row - all
columns, ...
● m[0][0], m[0][1], ... m[0][19], then
● m[1][0], m[1][1], ...m[1][19], then m[2][0]...
Arrays 11
● Accessing array elements m[i][j] = row i, column j
● Initialising
int two_d[3][4] = {{1, 2, 3, 4},
{2, 3, 4, 5},{5, 6, 7, 8}};
● Can be written as: [ no first subscript ]
int two_d[ ][4] =
{ {1, 2, 3, 4}, {2, 3, 4, 5}, {5, 6, 7, 8} };
Can we keep the first subscript and omit the second ?
Arrays 12
Passing arrays to functions
#include <stdio.h>
/* function definition */
const int rollno = 3; void display(int studmarks[3][3])
const int subjects = 3; {
int r, s;
/* function declaration */ for (r=0 ; r<rollno ; r++)
void display (int marks[3][3]); {
printf("\nRollno %d,marks:",r);
main() for (s=0 ; s<subjects ; s++)
{ printf("%5d",studmarks[r][s]);
/* array initialisation */ } /* end for */
int marks[3][3] = {{10, 10, 10},
printf("\n");
{20, 20, 20}, {30, 30, 30} };
} /* end function */
Arrays 13
Passing arrays to functions
#include <stdio.h>
/* function definition */
const int rollno = 3; void display(int studmarks[3][3])
const int subjects = 3; {
int r, s;
/* function declaration */ for (r=0 ; r<rollno ; r++)
void display (int marks[3][3]); {
printf("\nRollno %d,marks:",r);
main() for (s=0 ; s<subjects ; s++)
{ printf("%5d",studmarks[r][s]);
/* array initialisation */ } /* end for */
int marks[3][3] = {{10, 10, 10},
printf("\n");
{20, 20, 20}, {30, 30, 30} };
} /* end function */
/* function declarations */
void read_mat(int a[10][10], int row, int col);
void write_mat(int a[10][10], int row, int col);
main()
{
int a[10][10], b[10][10], c[10][10];
int i, j, m, n;
int ip, k, p, q;
Arrays 16
Matrix Multiplication
printf("Input number of rows and columns for matrix A: ");
scanf("%d", &m);
scanf("%d", &n);
if( n != k )
{
printf("Cols of Matrix A must be = rows of Matrix B");
printf("Cannot multiply matrices!\n");
}
Arrays 17
Matrix Multiplication
else
{
printf("Matrices can be multiplied.\n");
printf("Resultant matrix is %d X %d\n", m, q);
Arrays 18
n
ip q j
m k
ip
Matrix A Matrix B
Arrays 19
/* Multiply the two matrices */
for(i=0; i<m ; i++)
{ for(j=0; j<q ; j++)
{ c[i][j] = 0;
for(ip=0; ip<n ; ip++)
{
c[i][j] = c[i][j] + a[i][ip] * b[ip][j] ;
} /* end for ip */
} /* end for j */
} /* end for i */
Arrays 20
Read/write matrix functions
/* Function to write a
/* Function to read a
matrix */
matrix */
} printf("\n");
}
}
Arrays 21
Strings : Arrays of Characters
● “I study in IITG”
● String enclosed in double quotes
● Stored as ASCII codes of each charater
● Ended with the ASCII code 0: null: '\0'
● char month1[ ] =
{'j','a','n','u','a','r','y',0};
● char month1[ ] = “january”;
● Array of strings is a two dimensional array
Arrays 22
string.h
● strcat() : concatenate two strings. Answer in the
first string.
● strcmp() : compare two strings using ASCII code
● < 0 : if first string < second
● = 0 : if first string = second
● > 0 : if first string > second
● strcpy() : copies second string on the first
including null character
● strlen() : Number of characters in the string,
excluding null character
Arrays 23
Example
#include <stdio.h>
#include <string.h>
if (cmpResult < 0)
printf("str1 < str2 ");
void main()
{
else if (cmpResult == 0)
Arrays 24