Practical 5 - Define Arrays 1D and 2D
Practical 5 - Define Arrays 1D and 2D
#include<stdio.h>
void main()
int myarray[10]={10,20,30,40,50,60,70,80,90,100};
Output:-
2. Write a C Program to declare and initialise (1D) array with 10 value of type
decimal
Input:-
#include<stdio.h>
void main()
float myarray[10]={10.5,20.5,30.6,40.7,50.8,60.9,70,80,90,100.1};
Output:-
first value of array is 10.500000
void main()
int myarray[10],i;
for(i=0;i<10;i++)
printf("%d\n" ,myarray[10]);
Output:-
0
9
4. Write a C Program to take input from user for (1D)array with type integer using
for loop
Input:-
#include<stdio.h>
void main()
int myarray[10],i;
for(i=0;i<5;i++)
for( i=0;i<5;i++)
printf("%d\n",myarray[i]);
Output:-
Enter array value1: 1
5
5. Write a C Program to print the sum of values of and 1D array
Input:-
#include<stdio.h>
void main()
int n,i,sum,a[100];
scanf("%d",&n);
for(i=0; i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
sum=sum+a[i];
printf("%d\n",a[i]);
Output:-
Enter the size of an array...
6. Write a C Program to declare 2D array of size 3x3, initialise values into it and
print the same
Input:-
#include<stdio.h>
void main()
int a[3][3]={{1,2,3,},{4,5,6},{7,8,9}};
int i,j;
for(i=0;i<=2;i++)
for(j=0;j<=2;j++)
printf("%d\t",a[i][j]);
printf("\n");
Output:-
1 2 3
4 5 6
7 8 9
7. Write a C Program to read no of rows and columns Of 2D array . From the user
along with its values. and print the same on screen also print it's row wise sum of
values
Input:-
#include<stdio.h>
void main()
int a[10][10],rows,cols,i,j,sum;
scanf("%d%d",&rows,&cols);
for(i=0;i<rows;i++)
for(j=0;j<cols;j++)
scanf("%d",&a[i][j]);
for(i=0;i<rows;i++)
sum=0;
for(j=0;j<cols;j++)
printf("%d\t",a[i][j]);
sum=sum+ a[i][j];
}
}
Output:-
22
24
68
2 4 Row-wise sum is 6
6 8 Row-wise sum is 14
8. Write a C Program to declare and initialise string and print the same
Input:-
#include<stdio.h>
void main()
int i;
printf("%c\n",mystring[1]);
printf("%c\n",mystring[6]);
for(i=0;i<=8;i++)
printf("%c",mystring[i]);
}
Output:-
c
kc !!coll
void main()
scanf("%d", &a[i]);
scanf("%d", &b[i]);
printf("%d\t", a[i]);
printf("\n");
printf("\n");
printf("%d\t", add[i]);
printf("\n");
printf("%d\t", sub[i]);
printf("\n");
printf("%d\t", mul[i]);
printf("\n");
printf("%d\t", div[i]);
printf("\n");
}
Output:-
Enter cell no: 1
10
First Array is
2 6 10
Second Array is
1 3 5
3 9 15
1 3 5
2 18 50
Input:-
#include <stdio.h>
void main()
scanf("%d%d",&rows,&cols);
for(i=0;i<rows;i++)
for(j=0;j<cols;j++)
scanf("%d",&a[i][j]);
for(i=0;i<rows;i++)
for(j=0;j<cols;j++)
printf("%d\t",a[i][j]);
printf("\n");
transpose[j][i]=a[i][j];
}
}
printf("%d\t",transpose[i][j]);
printf("\n");}
Output:-
Enter the number of rows and columns of 2D array...
24
68
2 4
6 8
2 6
4 8
11. Write a C Program to read 2D arrays a and b from the user of order m*n
perform addition and subtraction between these 2 arrays and print their results
Input:-
#include<stdio.h>
void main(){
printf("First Array\n");
int row1,col1;
printf("Enter no of rows:\n");
scanf("%d",&row1);
printf("Enter no of cols:\n");
scanf("%d",&col1);
int m[10][10],i,j;
printf("First Arrays Value\n");
for(i=0;i<row1;i++){
for(j=0;j<col1;j++){
printf("Enter cell %d%d:\n",i,j);
scanf("%d",&m[i][j]);
}
}
printf("Second Array \n");
int row2,col2;
printf("Enter no of rows:\n");
scanf("%d",&row2);
printf("Enter no of cols:\n");
scanf("%d",&col2);
int n[10][10];
printf("Second Arrays Value \n");
for(i=0;i<row2;i++){
for(j=0;j<col2;j++){
printf("Enter cell %d%d:\n",i,j);
scanf("%d",&n[i][j]);
}
}
printf("Printing First Array\n");
for(i=0;i<row1;i++){
for(j=0;j<col1;j++){
printf("%d\t",m[i][j]);
}
printf("\n");
}
printf("Printing Second Array\n");
for(i=0;i<row2;i++){
for(j=0;j<col2;j++){
printf("%d\t",n[i][j]);
}
printf("\n");
}
int add[10][10],sub[10][10],multi[10][10],div[10][10];
for(i=0;i<row1;i++){
for(j=0;j<col1;j++){
add[i][j]=m[i][j]+n[i][j];
sub[i][j]=m[i][j]-n[i][j];
multi[i][j]=m[i][j]*n[i][j];
div[i][j]=m[i][j]/n[i][j];
}
}
printf("\n Addition of Two Arrays is\n");
for(i=0;i<row1;i++){
for(j=0;j<col1;j++){
printf("%d\t",add[i][j]);
}
printf("\n");
}
printf("\n Subtraction of Two Arrays is n");
for(i=0;i<row1;i++){
for(j=0;j<col1;j++){
printf("%d\t",sub[i][j]);
}
printf("\n");
}
printf("\ Multiplication of Two Arrays is \n");
for(i=0;i<row1;i++){
for(j=0;j<col1;j++){
printf("%d\t",multi[i][j]);
}
printf("\n");
}
printf("\n Division of Two Arrays is\n");
for(i=0;i<row1;i++){
for(j=0;j<col1;j++){
printf("%d\t",div[i][j]);
}
printf("\n");
}
}
Output:-
First Array
Enter no of rows:
2
Enter no of cols:
2
First Arrays Value
Enter cell 00:
8
Enter cell 01:
6
Enter cell 10:
4
Enter cell 11:
2
Second Array
Enter no of rows:
2
Enter no of cols:
2
Second Arrays Value
Enter cell 00:
4
Enter cell 01:
3
Enter cell 10:
2
Enter cell 11:
1
Printing First Array
8 6
4 2
Printing Second Array
4 3
2 1
Addition of Two Arrays is
12 9
6 3
Subtraction of Two Arrays is n
4 3
2 1
Multiplication of Two Arrays is
32 18
8 2
Division of Two Arrays is
2 2
2 2
12. Write a C Program to find the largest value in 1D array of size n from the user
Input:-
#include<stdio.h>
void main()
int a[10],i,n;
scanf("%d",&n);
for(int i=0;i<n;i++)
printf("Enter number%d:",i+1);
scanf("%d",&a[i]);
if(a[0]<a[i])
a[0]=a[i];
printf("Largest element=%d",a[0]);
Output:-
Enter the size of array :5
Enter number1:6
Enter number2:8
Enter number3:9
Enter number4:1
Enter number5:5
Largest element=9
13. Write a C Program to find the smallest value in 1D array of size n from the user
Input:-
#include<stdio.h>
void main()
int a[100],s,i,n;
scanf("%d",&n);
for(int i=0;i<n;i++)
printf("Enter number%d:",i+1);
scanf("%d",&a[i]);
int s=a[0];
if(a[i]<s)
s= a[i];
Output:-
Enter the size of array :5
Enter number1:8
Enter number2:9
Enter number3:3
Enter number4:5
Enter number5:6
smallest number is =3
14. Write a C Program to find the largest value in 2D array of size n from the user
Input:-
#include<stdio.h>
void main()
int a[3][3],i,j,l;
for(i=0;i<3;i++)
scanf("%d",&a[i][j]);
l=a[0][0];
for(i=0;i<3;i++)
for(j=0;j<3;j++)
if(l<a[i][j])
l=a[i][j];
Output:-
Enter the value for matrix :
256
897
365
Largest element=9
15. Write a C Program to find the smallest value in 2D array of size n from the user
Input:-
#include<stdio.h>
void main()
int a[3][3],i,j,s;
for(i=0;i<3;i++)
scanf("%d",&a[i][j]);
s=a[0][0];
for(i=0;i<3;i++)
for(j=0;j<3;j++)
if(s>a[i][j])
s=a[i][j];
Output:-
Enter the value for matrix :
256
897
143
Smallest element=1