In an array in C is a group of data items of the same data type that share a common name. Although, An array in C is also known as a ‘Subscripted Variable‘. Hence, A subscript is also called an index. Therefore, C subscripts start at 0 (zero) and cannot be negative.

There are two types of arrays :

  1. One dimensional arrays (Vectors).
  2. Multi-dimensional array (Matrices).
  3. One-dimensional array :

Declaration of One-dimensional Array

Syntax : datatype arrayname[size]

Where datatype: The type of data stored in the array.

arrayname: Name of the array.

size: Maximum number of an element that array can hold.

Example: char subjects[5]

This represents the subject’s size of 5.

Initialization of One-dimensional Arrays

You can initialize the elements of array one by one.

Syntax : arrayname[index] = value;

Array in c

accept ten numbers and printing all even numbers from them.

#include<stdio.h>
void main()
{
int x[10], p;
printf("Enter any ten numbers: \n");
for(p=0;p<10;p++)
scanf("%d", &x[p]);
printf("Even numbers are as follows: \n");
for(p=0;p<10;p++)
{
if(x[p]%2==0)
printf(" %d", x[p]);
}
}
Output:
Enter any ten numbers: 
23
65
12
12 
23 
56
12
10
32
40
Even numbers are as follows: 
12 12 56 12 10 32 40

Program to find maximum within 3*3 matrix.

#include<stdio.h>
void main()
{
int x[3][3], p, q, max;
printf("Enter the elements of matrix: \n");
for(p=0;p<3;p++)
{
for(q=0;q<3;q++)
scanf("%d", &x[p][q]);
}
max=x[0][0];
printf("The matrix is as follows: \n");
for(p=0;p<3;p++)
{
for(q=0;q<=3;q++)
scanf(" %d", &x[p][q]);
}
for(p=0;p<3;p++)
{
for(q=0;q<3;q++)
{
if(x[p][q]>max)
max=x[p][q];
}   
printf("\n"); 
}
printf("Maximum number in the matrix is: %d", max);
}
Output:
Enter the elements of matrix: 
23 65 12
12 23 56
12 10 32
The matrix is as follows: 
23 65 12
12 23 56
12 10 32
Maximum number in the matrix is: 65

C Program to multiply two 3 * 3 matrices.

#include<stdio.h>
void main()
{
int x[3][3], y[3][3], z[3][3], p, q, r, add;
printf("Enter the elements of first matrix: \n");
for(p=0;p<3;p++)
{
for(q=0;q<3;q++)
scanf("%d", &x[p][q]);
}
printf("Enter the elements of second matrix: \n");
for(p=0;p<3;p++)
{
for(q=0;q<3;q++)
scanf("%d", &y[p][q]);
}
printf("The matrix x is as follows: ");
for(p=0;p<3;p++)
{
printf("\n");
for(q=0;q<3;q++)
printf(" %d", x[p][q]);
}
printf("\nThe matrix y is as follows: ");
for(p=0;p<3;p++)
{
printf("\n");
for(q=0;q<3;q++)
printf(" %d", y[p][q]);
}
for(p=0;p<3;p++)
{
for(q=0;q<3;q++)
{
add=0;
for(r=0;r<3;r++)
add=add + x[p][r]*y[r][q];
z[p][q]=add;
}
}
printf("\nProduct of matrices is as follows: ");
for(p=0;p<3;p++)
{
printf("\n");
for(q=0;q<3;q++)
printf(" %d", z[p][q]);
}
}
OUTPUT:
Enter the elements of first matrix: 5 2 0 6 1 9 5 7 4 
Enter the elements of second matrix: 8 3 6 7 1 0 5 1 2
The matrix x is as follows: 
5 2 0
6 1 9
5 7 4
The matrix y is as follows: 
8 3 6
7 1 0
5 1 2
Product of matrices is as follows: 
54 17 30
100 28 54
109 26 38

C Program to sort n number of arrays.

#include<stdio.h>
void main()
{
int arr[30], p, q, len, temp;
printf("Enter number of elements: \n");
scanf("%d", &len);
printf("Enter the elements: \n");
for(p=0;p<len;p++)
scanf("%d", &arr[p]);
for(p=0;p<len-1;p++)
{
for(q=p+1;q<len;q++)
{
if(arr[p]>arr[q])
{
temp=arr[p];
arr[p]=arr[q];
arr[q]=temp;
}
}
}
printf("Elements after sorting: \n");
for(p=0;p<len;p++)
printf("%d\n",arr[p]);
}
Output:
Enter number of elements:
4
Enter the elements:
23
12
5
6
Elements after sorting:
5
6
12
23

Program to swap i’th row with j’th row or i’th column with j’th column.

#include<stdio.h>
void main()
{
int arr[30][30], p, q, r, c, ch, r1, r2, c1, c2, temp;
printf("Enter the row and column of the matrix: \n");
scanf("%d %d", &r, &c);
printf("Enter the elements: \n");
for(p=0;p<r;p++)
for(q=0;q<c;q++)
scanf("%d",&arr[p][q]);
while(1)
{
for(p=0;p<r;p++)
{
for(q=0;q<c;q++)
printf("%5d ", arr[p][q]);
printf("\n");
}
printf("1.Row swap\n2.Column swap\n3.Exit\nChoice?: \n");
scanf("%d", &ch);
switch(ch)
{
case 1:
printf("Enter 2 rows to swap: \n");
scanf("%d %d", &r1, &r2);
for(p=0;p<c;p++)
{
temp=arr[r1][p];
arr[r1][p]=arr[r2][p];
arr[r2][p]=temp;
}
break;
case 2:
printf("Enter 2 columns to swap: \n");
scanf("%d %d", &c1, &c2);
for(p=0;p<c;p++)
{
temp=arr[p][c1];
arr[p][c1]=arr[p][c2];
arr[p][c2]=temp;
}
break;
}
if(ch==3)
break;
}
}
OUTPUT:
Enter the row and column of the matrix:
3 3
Enter the elements:
2 3 5 64 8 9 7 6 5
2     3     5
64     8     9
7     6     5
1.Row swap
2.Column swap
3.Exit
Choice?:
1
Enter 2 rows to swap:
0 2
7     6     5
64     8     9
2     3     5
1.Row swap
2.Column swap
3.Exit
Choice?:
2
Enter 2 columns to swap:
2 1
7     5     6
64     9     8
2     5     3
1.Row swap
2.Column swap
3.Exit
Choice?:
3

Program to merge two sorted arrays into third array.

#include<stdio.h>
void main()
{
int p, q, r, temp, z[10];
int x[5]={2, 44, 65, 78, 99};
int y[6]={-56, -1, 3, 55, 100, 109};
printf("X = { ");
for(p=0;p<5;p++)
printf("%4d,", x[p]);
printf("}\nY = { ");
for(p=0;p<6;p++)
printf("%4d,",y[p]);
for(p=0, q=0, r=0;r<11;r++)
{
if(p==5)
{
z[r]=y[q];
q++;
continue;
}
if(q==6)
{
z[r]=x[p];
p++;
continue;
}
if(z[p]<y[q])
{
z[r]=x[p];
p++;
}
else
{
z[r]=y[q];
q++;
}
}
printf("}\nZ = {");
for(p=0;p<11;p++)
printf("%4d,", z[p]);
printf("}\n");
}
Output:
X = {    2,  44,  65,  78,  99,}
Y = {  -56,  -1,   3,  55, 100, 109,}
Z = { -56,   2,  -1,  44,  65,   3,  78,  55,  99, 100, 110,}

Construct C Program to fill 5 x 5 matrix as follows:

1. Upper right corner with -1’s

2. Lower left corner with +1’s

3. Left to right diagonal with 0’s

#include <stdio.h>
void main() 
{
int m[5][5], p, q;
clrscr();
for(p=0;p<5;p++)
{
for(q=0;q<5;q++)
{
if(p>q)
m[p][q]=1;
if(p==q)
m[p][q]=0;
if(p<q)
m[p][q]=-1;
}
}
printf("Matrix is as follows: \n");
for(p=0;p<5;p++)
{
for(q=0;q<5;q++)
{
printf(" %d", m[p][q]);
}
printf("\n");
}
getch();
}
OUTPUT:
Matrix is as follows: 
0 -1 -1 -1 -1
1  0 -1 -1 -1
1  1  0 -1 -1
1  1  1  0 -1
1  1  1  1  0

Determine a Program to print total sales of each item by salesgirl, the total sale of each salesgirl, grand total of all items.

#include<stdio.h>
#define S_GIRL 4
#define ITEM 5
void main()
{
int abc[S_GIRL][ITEM], prosale[ITEM]={0, 0, 0, 0, 0};
int girlsale[S_GIRL]={0, 0, 0, 0}, p, q, r=0;
for(p=0;p<S_GIRL;p++)
{
for(q=0;q<ITEM;q++)
{
printf("\nEnter sale of %dth item by %dth sales girl: \n", q+1, p+1);
scanf("%d",&abc[p][q]);
girlsale[p]+=abc[p][q];
r+=abc[p][q];
}
}
printf("\n-----Sale database-----\n");
printf("\t \t: Item 1    Item 2     Item 3    Item 4    Item 5\n");
for(p=0;p<S_GIRL;p++)
{
printf("\nSales girl %2d: ", p+1);
for(q=0;q<ITEM;q++)
printf(" %5d      ", abc[p][q]);
}
printf("\n-----Grand total sale of ABC company is: %d-----\n", r);
printf("\n-----Total sale of each product-----\n");
for(p=0;p<ITEM;p++)
{
for(q=0;q<S_GIRL;q++)
{
prosale[p]+=abc[q][p];
}
printf("\nTotal sale of %dth product: %d\n", p+1, prosale[p]);
}
printf("\n-----Total sale of each sles girl-----\n");
for(p=0;p<S_GIRL;p++)
{
printf("\nTotal sale of %dth sales girl: %d\n", p+1, girlsale[p]);
}
}
Output:
Enter sale of 1th item by 1th sales girl:
2
Enter sale of 2th item by 1th sales girl:
4
Enter sale of 3th item by 1th sales girl:
5
Enter sale of 4th item by 1th sales girl:
6
Enter sale of 5th item by 1th sales girl:
7
Enter sale of 1th item by 2th sales girl:
8
Enter sale of 2th item by 2th sales girl:
9
Enter sale of 3th item by 2th sales girl:
4
Enter sale of 4th item by 2th sales girl:
5
Enter sale of 5th item by 2th sales girl:
6
Enter sale of 1th item by 3th sales girl:
41
Enter sale of 2th item by 3th sales girl:
74
Enter sale of 3th item by 3th sales girl:
7
Enter sale of 4th item by 3th sales girl:
8
Enter sale of 5th item by 3th sales girl:
7
Enter sale of 1th item by 4th sales girl:
8
Enter sale of 2th item by 4th sales girl:
8
Enter sale of 3th item by 4th sales girl:
7
Enter sale of 4th item by 4th sales girl:
8
Enter sale of 5th item by 4th sales girl:
43
-----Sale database-----
: Item 1    Item 2     Item 3    Item 4    Item 5
Sales girl  1:      2           4           5           6           7
Sales girl  2:      8           9           4           5           6
Sales girl  3:     41          74           7           8           7
Sales girl  4:      8           8           7           8          43
-----Grand total sale of ABC company is: 267-----
-----Total sale of each product-----
Total sale of 1th product: 59
Total sale of 2th product: 95
Total sale of 3th product: 23
Total sale of 4th product: 27
Total sale of 5th product: 63
-----Total sale of each sles girl-----
Total sale of 1th sales girl: 24
Total sale of 2th sales girl: 32
Total sale of 3th sales girl: 137
Total sale of 4th sales girl: 74

Write a program to count total number of failed, 1st class, 2nd class, 3rd class students.

#include<stdio.h>
void main()
{
int arr[30], p, q, len, cnt[5]={0, 0, 0, 0, 0};
printf("Enter the number of students: \n");
scanf("%d", &len);
printf("Enter the marks out of 100 of %d students: \n", len);
for(p=0;p<len;p++)
{
scanf("%d", &arr[p]);
switch(arr[p]/10)
{
case 0:
case 1:
case 2:
case 3: 
cnt[0]++;
break;
case 4: 
cnt[1]++;
break;
case 5: 
cnt[2]++;
break;
case 6: 
cnt[3]++;
break;
case 7: 
if(arr[p]<75)
cnt[3]++;
else 
cnt[4]++;
break;
case 8:
case 9:
cnt[4]++;
break;
}
}
printf("The marks are: \n");
for(p=0;p<len;p++)
printf("%d ", arr[p]);
printf("Results are: \n");
printf("The total distinction students are: %d\n", cnt[4]);
printf("The total first class students are: %d\n", cnt[3]);
printf("The total second class students are: %d\n", cnt[2]);
printf("The total third class students are: %d\n", cnt[1]);
printf("The total failed students are: %d\n", cnt[0]);
} 
OUTPUT:
Enter the number of students: 5
Enter the marks out of 100 of 5 students: 46 78 57 62 31
The marks are: 
46 78 57 62 31 
Results are: 
The total distinction students are: 1
The total first class students are: 1
The total second class students are: 1
The total third class students are: 1
The total failed students are: 1

Write a program to find maximum element in given rows and minimum element in given column.

#include<stdio.h>
void main()
{
int arr[30][30], p, q, r, c, temp, ch, r1, r2, c1, c2;
printf("Enter the rows and column of the matrix: \n");
scanf("%d%d", &r, &c);
printf("Enter the elements: \n");
for(p=0;p<r;p++)
for(q=0;q<c;q++)
scanf("%3d", &arr[p][q]);
while(1)
{
for(p=0;p<r;p++)
{
for(q=0;q<c;q++)
printf("%3d", arr[p][q]);
printf("\n");
}
printf("\n1. Row 2. Column 3. Exit\n Choice?\n");
scanf("%d", &ch);
switch(ch)
{
case 1: 
printf("Enter rows to find maximum element\n");
scanf("%d", &r1);
temp=arr[r1][0];
for(p=1;p<c;p++)
{
if(temp<arr[r1][p])
temp=arr[r1][p]; 
}
printf("Maximum element of %dth row is: %d\n", r1, temp);
break;
case 2:
printf("Enter column to find minimum element\n");
scanf("%d", &c1);
temp=arr[0][c1];
for(p=1;p<r;p++)
{
if(temp>arr[p][c1])
temp=arr[p][c1]; 
}
printf("Minimum element of %dth column is: %d\n", c1, temp);
break;
}
if(ch==3)
break;
}
}
Output:
Enter the rows and column of the matrix: 3 3
Enter the elements: 31 57 20 62 13 74 36 42 85
31 57 20
62 13 74
36 42 85
1. Row 2. Column 3. Exit
Choice?
1
Enter rows to find maximum element
1
Maximum element of 1th row is: 74
31 57 20
62 13 74
36 42 85
1. Row 2. Column 3. Exit
Choice?
2
Enter column to find minimum element
2
Minimum element of 2th column is: 20
31 57 20
62 13 74
36 42 85
1. Row 2. Column 3. Exit
Choice?
3

Program to replace an element of matrix by 0 if it is greater than 25.

#include <stdio.h>
void main() 
{
int p, q, x[10][10], r, c;
printf("Enter size of matrix: (row/column:\n");
scanf("%d%d", &r, &c);
for(p=0;p<r;p++)
{
for(q=0;q<c;q++)
{
scanf("%d", &x[p][q]);
}
}
printf("Matrix after replacement is as follows: \n");
for(p=0;p<r;p++)
{
for(q=0;q<c;q++)
{
if(x[p][q]>25)
x[p][q]=0;
}
}
for(p=0;p<r;p++)
{
for(q=0;q<c;q++)
{
printf("%2d", x[p][q]);
}
printf("\n");
}
}
OUTPUT:
Enter size of matrix: (row/column: 3 3 
Matrix after replacement is as follows: 34 5 7 12 43 64 19 3 27
0 5 7
12 0 0
19 3 0

Program to fill a 5X5 matrix as follows:

1. Upper left corner with +1’s

2. Lower right corner with -1’s

3. Right to left diagonal with 0’s

#include<stdio.h>
void main()
{
int x[5][5], p, q, r;
printf("Enter elements in A matrix: \n");
for(p=4, r=0;p>0;p--, r++)
for(q=0;q<p;q++)
x[r][q]=1;
for(q=0, p=4;q<5;q++, p--)
x[p][q]=0;
for(p=1, r=1;p<5;p++, r++)
for(q=5-p;q<5;q++)
x[r][q]=-1;
for(p=0;p<5;p++)
{
for(q=0;q<5;q++)
printf("%6d", x[p][q]);
printf("\n");
}
} 
Output:
Enter elements in A matrix: 
3 6 2 5 2
6 8 1 0 3 
4 2 7 5 1
8 3 5 9 0
2 1 4 6 3 
1 1 1 1 0
1 1 1 0-1
1 1 0-1-1
1 0-1-1-1
0-1-1-1-1

By Admin

Leave a Reply

Your email address will not be published. Required fields are marked *