C Programming - Array

C Programming - Array

ARRAY

Definition: A composite variable capable of holding multiple data of same
data type under a single variable name is called array.
Declaration of Array:
datatype array_name [size];
Example: - int marks [10]; int x[4]={1,2,3,4};
 Array size is fixed at the time of array declaration.
 Generally, we use for loop () to assign data or item in memory and also
use this loop to extract data from memory location.
Types of Array:
There are 2 types of array.
a) One Dimensional Array (1-D)
b) Two Dimensional Array (2-D)
One dimensional Array(1-D) Two dimensional Array(2-D)
1) In 1-D array only one size
is used.

1) In 2-D array two sizes are
used.

2) It is called list also. 2) It is called table or matrix.
3) Declaration:
datatype
array_name[size];

3) Declaration:
datatype
array_name[size1][size2];
4) Eg:- int mark [20]; 4) Eg:- float sale[2][3];
5) In 1-D array size denotes
either row or column.

5) In 2-D array size 1 denotes
row and size2 denotes
column.

6) One looping statement is
used to create or display
array items or data.

6) Two looping statements
are used to create or display
array items or data.

31. Write a program to read five positive number using array and find out the smallest
among them?
#include<stdio.h>
#include<conio.h>
void main( )
{
int n[5],i,min;
for(i=0;i<5;i++)
{
printf("Enter any %d numbers=",i+1);

www.alk.com.np

P a g e 103

scanf("%d",&n[i]);
}

min=n[0];
for(i=0;i<5;i++)
{

if(n[i]<min)
{
min=n[i];
}
}
printf("Smallest number =%d.",min);
getch( );
}
32. WAP to store 10 numbers and print the largest among them?
#include<stdio.h>
#include<conio.h>
void main( )
{
int n[10],i,largest;
for(i=0;i<10;i++)
{
printf("Enter any %d numbers=",i+1);
scanf("%d",&n[i]);
}
largest=n[0];
for(i=0;i<10;i++)
{
if(n[i]>largest)
{
largest=n[i];
}
}
printf("Largest Number =%d." , largest);
getch( );
}
33. Write a program to find the largest number among “n” numbers?
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,num[100],max;
printf("Enter the size of array not more than 100\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the %d number =",i+1);
scanf("%d",&num[i]);

www.alk.com.np

P a g e 104

}
max=num[0];
for(i=1;i<n;i++)
{
if(num[i]>max)
max=num[i];
}
printf("The largest number in array = %d ", max);
getch();
}
34. Write a program to sort 20 integers number in ascending order?
#include<stdio.h>
#include<conio.h>
void main( )
{
clrscr();
int i,j,n,num[100],temp;
for(i=0;i<20;i++)
{
printf("Enter the %d number =",i+1);
scanf("%d",&num[i]);
}
for(i=0;i<20;i++)
{
for(j=i+1;j<20;j++)
{
if(num[i]>num[j])
{
temp=num[i];
num[i]=num[j];
num[j]=temp;
}
}
}
printf("Series in Ascending order\n");
for(i=0;i<20;i++)
{
printf("%d \t",num[i]);
}
getch( );
}
35. Write a program to sort an array of “n” numbers in ascending order?
#include<stdio.h>
#include<conio.h>
void main( )
{
int i,j,n,num[100],temp;
printf("Enter the size of array not more than 100\n");
scanf("%d",&n);

www.alk.com.np

P a g e 105

for(i=0;i<n;i++)
{
printf("Enter the %d number =",i+1);
scanf("%d",&num[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(num[i]>num[j])
{
temp=num[i];
num[i]=num[j];
num[j]=temp;
}
}
}
printf("Series in Ascending order\n");
for(i=0;i<n;i++)
{
printf("%d \t",num[i]);
}
getch( );
}

36. Write a program to sort an array of “n” numbers in descending order?
#include<stdio.h>
#include<conio.h>
void main( )
{
clrscr();
int i,j,n,num[100],temp;

printf("Enter the size of array not more than 100\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the %d number =",i+1);
scanf("%d",&num[i]);
}

for(i=0;i<n;i++)
{

for(j=i+1;j<n;j++)
{
if(num[i]<num[j])
{
temp=num[i];
num[i]=num[j];
num[j]=temp;

}
}
}

printf("Series in Descending order\n");
for(i=0;i<n;i++)
{
printf("%d \t" , num[i]);
}
getch( );
}
37. Write a program which asks the user to input “n” terms of number and find out the
greatest and smallest number among those numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,max,a[100],min;
printf("How many numebrs=");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter %d number=",i+1);
scanf("%d",&a[i]);
}
max=a[0];
min=a[0];
for(i=0;i<n;i++)
{
if(a[i]>max)
max=a[i];
else if(a[i]< min )
min=a[i];
else
;
}
printf("Greatest number=%d\n",max);
printf("Smallest number=%d",min);
getch();
}

38. Write a program to input names of “20” numbers of students and sort them in
alphabetical order?
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{ clrscr();
char name[100][100],temp[100];
int i,j;
for(i=0;i<20;i++)
{
printf("Enter %d student name=",i+1);
scanf("%s",name[i]);
}
for(i=0;i<19;i++)
{
for(j=i+1;j<20;j++)
{
if(strcmp(name[i],name[j])>0)
{
strcpy(temp,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],temp);
}
}
}
printf("Sorted Alphabetically\n");
for(i=0;i<20;i++)
{
printf("%s\n",name[i]);
}
getch( );
}
39. Write a program to input names of “n” numbers of students and sort them in
alphabetical order?
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[100][100],temp[100];
int i,j,n;
printf("Enter numbers of students");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter %d student name=",i+1);

scanf("%s",name[i]);

}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp(name[i],name[j])>0)
{
strcpy(temp,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],temp);
}
}
}
printf("Sorted Alphabetically\n");
for(i=0;i<n;i++)
{
printf("%s\n",name[i]);
}
getch( );
}
40. Write a program which reads salary of 25 employees and count the number of
employees who are getting salary between 30,000 to 40,000?

#include<stdio.h>
#include<conio.h>
void main()
{
int salary[25],i,c=0;
for(i=0;i<25;i++)
{
printf("Enter the salary of %d employee=",i+1);
scanf("%d",&salary[i]);
}
for(i=0;i<25;i++)
{
if(salary[i]>=30000 && salary[i]<=40000)

{
c=c+1;
}
}
printf("Total number of employee who gare getting salary in the
range of 30,000 to 40,000 are=%d",c);
getch();
}

41. Write a program to add two matrices by supplying elements of matrices by user?
#include<stdio.h>
#include<conio.h>
void main()
{ clrscr();
int m1[3][3],m2[3][3],m3[3][3],r,c;
printf("Enter the elements of the first matrix ");
for(r=0;r<3;r++)
{
for(c=0;c<3;c++)
{
scanf("%d",&m1[r][c]);
}
}
printf("Enter the elements of the second matrix ");
for(r=0;r<3;r++)
{
for(c=0;c<3;c++)
{
scanf("%d",&m2[r][c]);
}
}
for(r=0;r<3;r++)
{
for(c=0;c<4;c++)
{
m3[r][c]=m1[r][c]+m2[r][c];
}
}
printf("The sum of two matrices is ");
printf("\n");
for(r=0;r<3;r++)
{
for(c=0;c<3;c++)
{
printf("%d\t",m3[r][c]);
}
printf("\n");
}
getch();
}
42. Write a program to transpose a matrix?
#include<stdio.h>
#include<conio.h>
void main()
{ clrscr();
int i,j,temp;
int a[3][3]={{1,2,3},{4,5,6},{7,8,9}};
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
temp=a[i][j];
a[i][j]=a[j][i];
a[j][i]=temp;
}
}
printf("Transpose matrixes\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%d\t",a[j][i]);
printf("\n");
}
getch();
}

Homework Questions

C-2 (Array)

1. 2.1 What is an array? Differentiate between 1-D and 2-D array.
2. Write a program to read age of 50 people and count number of people
between ages 30 to 50?
3. Write a program to read salaries of 300 employees and count the number
of employees getting salary from 10,000 to 15,000?
4. Write a program which finds the multiplication of two matrices.
5. Write a program which reads names of 100 students and sort them in
alphabetical order?