Array
Array
1/20
ARRAYS
An array is a collection of elements of the same type that are
referenced by a common name.
int main(void)
{
int studMark1, studMark2, studMark3,
studMark4, …, …, studMark998, stuMark999,
studMark1000;
…
…
return 0;
}
By using an array, we just declare like this,
int studMark[1000];
This will reserve 1000 contiguous memory locations for
storing the students’ marks.
Graphically, this can be depicted as in the following
figure.
This absolutely has simplified our declaration of the
variables.
array_element_data_type array_name[array_
size];
char cName[30];
For example:
int Arr[]={1,3,5,6,8};
printf(“%d\t%d\n”,Arr[1],Arr[2]);
Output: 3 5
ARRAY EXAMPLE
Take 10 integer input from user and store then in an
array and find the sum of all numbers stored in array.
#include<stdio.h>
int main(){
int i,sum=0,arr[10];
for(i=0;i<10;i++)
scanf(“%d”,&arr[i]);
for(i=0;i<10;i++)
sum+=arr[i];
printf(“Sum of input integers is %d\n”,sum);
return 0;
}
Summarize the response of a survey.
Input: Response of the survey, can be in the range
between 0 and 10. Assume the population size to
be 40.
Output: Frequency of each response.
#include<stdio.h>
#define SIZE 40
#define ANS 11
int main(void) {
int response[SIZE];
int freq[ANS] = {0};
int i;
for(i=0; i< SIZE; i++){
scanf(“%d”,&response[i]);
++freq[response[i]];
}
for(i=0;i<ANS;i++)
printf("Frequency of %d is %d\n",i,freq[i]);
}
TWO DIMENSIONAL/2D ARRAYS
For examples,
int xInteger[3][4];
float matrixNum[20][25];
}
Output: 1600101376 1600101392 1600101408
1600101424 1600101440
LIST OF STUDENTS AND THEIR SUBJECT
MARKS
Marks
Students 10 23 31 11
20 43 21 21
12 22 30 13
30 31 26 41
13 03 41 15
int main(void)
{
int i, j;
double total;
int marks[ROW][COL]= { 10, 23, 31, 11, 20, 43, 21, 21,12,
22, 30, 13, 30, 31, 26, 41,13, 03, 41, 15 };
for(i = 0; i < ROW ; i++)
{
total = 0.0;
for (j=0; j<COL; j++)
total+=marks[i][j];
printf("Average of student %d is %f\n", i, total/4.0);
}
}
INITIALIZATION OF 2D ARRAY
int disp[2][4] = { {10, 11, 12, 13}, {14, 15, 16, 17} };
OR
int disp[2][4] = { 10, 11, 12, 13, 14, 15, 16, 17};
/* Valid declaration*/
int abc[2][2] = {1, 2, 3 ,4 }
/* Valid declaration*/
int abc[][2] = {1, 2, 3 ,4 }
/* Invalid declaration – you must specify second
dimension*/
int abc[][] = {1, 2, 3 ,4 }
/* Invalid because of the same reason mentioned above*/
int abc[2][] = {1, 2, 3 ,4 }
For array storing string
void main()
{
static int array[10][10];
int i, j, m, n;
printf("Enter the order of the matrix \n");
scanf("%d %d", &m, &n);
printf("Enter the coefiicients of the matrix\n");
for (i = 0; i < m; ++i){
for (j = 0; j < n; ++j){
scanf("%d", &array[i][j]);
}
}
printf("The given matrix is \n");
for (i = 0; i < m; ++i){
for (j = 0; j < n; ++j){
printf(" %d", array[i][j]);
}
printf("\n");
}
printf("Transpose of matrix is \n");
for (j = 0; j < n; ++j){
for (i = 0; i < m; ++i){
printf(" %d", array[i][j]);
}
printf("\n");
}
}
#include <stdio.h>
int main()
{
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter number of columns (between 1 and 100): ");
scanf("%d", &c);
printf("\nEnter elements of 1st matrix:\n");
for(i=0; i<r; ++i) {
for(j=0; j<c; ++j) {
printf("Enter element a%d%d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
}
printf("Enter elements of 2nd matrix:\n");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j) {
printf("Enter element a%d%d: ",i+1, j+1);
scanf("%d", &b[i][j]);
}
// Adding Two matrices
for(i=0;i<r;++i)
for(j=0;j<c;++j) {
sum[i][j]=a[i][j]+b[i][j];
}
// Displaying the result
printf("\nSum of two matrix is: \n\n");
for(i=0;i<r;++i)
for(j=0;j<c;++j) {
printf("%d ",sum[i][j]);
if(j==c-1) { printf("\n\n"); }
}
return 0;
}
#include <stdio.h>
int main() {
int a[10][10], b[10][10], result[10][10], r1, c1, r2, c2, i,
j, k;
printf("Enter rows and column for first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and column for second matrix: ");
scanf("%d %d",&r2, &c2);
while (c1 != r2) {
printf("Error! Not compatible for multiplication\n");
}
printf("\nEnter elements of matrix 1:\n");
for(i=0; i<r1; ++i)
for(j=0; j<c1; ++j) {
printf("Enter elements a%d%d: ",i+1, j+1);
scanf("%d", &a[i][j]);
}
}
result[i][j]+=a[i][k]*b[k][j];
}
// Displaying the result
printf("\nOutput Matrix:\n");
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j) {
printf("%d ", result[i][j]);
}
return 0;
}