CPR Experiment 7
CPR Experiment 7
It can be used to store the collection of primitive data types such as int, char, float, etc., and
also derived and user-defined data types such as pointers, structures, etc.
datatype arrayname[size];
Where size1 is size of the first dimension(rows) and size2 is size of the second
dimension(columns)
Exercise:
1. Write a program to input 5 integers to an array and print the sum of them.
#include <stdio.h>
#include<conio.h>
void main()
{
int i,arr[5],sum=0;
clrscr();
printf("Enter 5 integers of an array :");
for(i=0;i<5;i++)
{
scanf("%d",&arr[i]);
sum=sum+arr[i];
}
printf("sum of all elements of an array =%d",sum);
getch();
}
2. Write a program to input 9 integers to a 3x3 matrix and print transpose of the same.
#include <stdio.h>
#include<conio.h>
void main()
{
int i,j,arr[3][3];
clrscr();
printf("Enter 9 integers of an array :\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&arr[i][j]);
}
}
printf("\nOriginal matrix is :\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",arr[i][j]);
}
printf("\n");
}
printf("\nIts Transpose matrix is :\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",arr[j][i]);
}
printf("\n");
}
getch();
}