ONE-DIMENSIONAL ARRAYS
Arrays
An array is like a large data variable that can store multiple data items of
the same type.
Arrays are a series of elements (variables) of the same type placed
consecutively in memory that can be individually referenced by adding
an index to a unique name.
X
X[0]
Each individual
element is referred to by
the array name and the
subscript (or index).
Index starts with 0 and ends
with is the size-1;
Arrays
We can store 5 values of type int without having to declare 5 different
variables each one with a different identifier. Instead of that, using an
array we can store 5 different values of the same type, int for example,
with an unique identifier.
Example: Use an array called Arr to contain 5 integer values of type int.
Arr
int
The cells are numbered from 0 to 4 since in arrays the first index is
always 0, independently of its length .
billy
0
0
1
0
2
0
3
0
4
0
int
But additionally, when we declare an Array, we have the possibility to assign initial
values to each one of its elements using curly brackets { }.
Example:
int billy [5] = { 16, 2, 77, 40, 12071 };
6
Solution
#include <stdio.h>
int main()
{
int A[10];
int i;
printf("Enter 10 integer values: ");
for (i=0;i<10;i++)
scanf("%d",&A[i]);
/*Displaying back the values */
printf("Elements input (in reverse order): ");
for (i=0;i<10;i++)
printf("%d,",A[9-i]);
printf("\n");
//for (i=9;i>=0;i--)
// printf("%d, ", A[i]);
//printf("\n");
return 1;
}
10
11
Solution
#include <stdio.h>
int main()
{
int i;
float A[10];
int maxposn;
float max;
printf("Input 10 integer numbers:
", n);
maxposn=0;
max=0;
for (i=0;i<n;i++)
{
scanf("%f",&A[i]);
if(i==0)
max=A[i];
else
if (A[i]>max)
{
max=A[i];
maxposn=i;
}
}
printf("largest is %.2f at posn
%d\n", max, maxposn);
}
12
13
14
15
Solution
#include <stdio.h>
int main()
{
char name[20];
char ch;
int i,count;
count = 0;
printf("Input name: ");
scanf("%s", name);
i=0;
while ((name[i]!='\0') && (i<20))
{ ch=name[i];
if ((ch=='A') || (ch=='E')|| (ch=='I') ||(ch=='O')|| (ch=='U'))
count++;
i++;
}
printf("No. of vowels is %d\n",count);
return 1;
}
16
18
strcpy
strcpy(x,name);
strcmp
if(strcmp(name,x)==0)
printf(same\n);
else
printf(not same\n);
19
22
TWO-DIMENSIONAL ARRAYS
23
Example
int a[3][4];
We have defined a table with 3 rows and 4
columns
24
25
a[0][0]
a[1][0]
a[2][0]
a[0][1]
a[1][1]
a[2][1]
a[0][2]
a[1][2]
a[2][2]
a[0][3]
a[1][3]
a[2][3]
row 0
row 1
row 2
26
a[0][0]
a[1][0]
a[2][0]
a[0][1]
a[1][1]
a[2][1]
a[0][2]
a[1][2]
a[2][2]
a[0][3]
a[1][3]
a[2][3]
27
30
#include <stdio.h>
void printarray (int arg[][3], int rows);
int main ()
{
int M[5][3];
int row,col;
for(row=0;row<5;row++)
{
printf("3 numbers for Row %d : ",(row+1));
for(col=0;col<3;col++)
scanf("%d",&M[row][col]);
}
printarray(M,5);
return 1;
}
32
33
34
35
Class Exercise
Write a program that declares a 2dimensional array to store marks for 15
students, in 6 modules for each student. The
program also declares a 1-dimensional array
to store average marks (in 6 modules) for 15
students.
Make use of functions:
To input marks of 15 students in six modules
To output marks of 15 students in six modules
To output average marks of students
# include <stdio.h>
for(x=0;x<15;x++)
{
sum=0;
for(y=0;y<6;y++)
sum=sum+Marks[x][y];
Avg[x]=sum/6;
}
output_average(Avg,15);
return 1;
}
40