0% found this document useful (0 votes)
12 views21 pages

Practical 5 - Define Arrays 1D and 2D

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views21 pages

Practical 5 - Define Arrays 1D and 2D

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Practical 5 – Define Arrays 1D and 2D

Name:- Mayuresh Pandey Date: 3rd Oct 2024 Roll


no:-
1. Write a C Program to declare and initialise (1D) array with 10 value of type
integer
Input:-

#include<stdio.h>

void main()

int myarray[10]={10,20,30,40,50,60,70,80,90,100};

printf("first value of array is %d\n",myarray[0]);

printf("second value of array is %d\n",myarray[1]);

printf("third value of array is %d\n",myarray[2]);

printf("fourth value of array is %d\n",myarray[3]);

printf("fifth value of array is %d\n",myarray[4]);

printf("sixth value of array is %d\n",myarray[5]);

printf("seventh value of array is %d\n",myarray[6]);

printf("eight value of array is %d\n",myarray[7]);

printf("ninth value of array is %d\n",myarray[8]);

printf("tenth value of array is %d\n",myarray[9]);

Output:-

first value of array is 10

second value of array is 20

third value of array is 30

fourth value of array is 40

fifth value of array is 50

sixth value of array is 60

seventh value of array is 70

eight value of array is 80

ninth value of array is 90


tenth value of array is 100

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};

printf("first value of array is %f\n",myarray[0]);

printf("second value of array is %f\n",myarray[1]);

printf("third value of array is %f\n",myarray[2]);

printf("fourth value of array is %f\n",myarray[3]);

printf("fifth value of array is %f\n",myarray[4]);

printf("sixth value of array is %f\n",myarray[5]);

printf("seventh value of array is %f\n",myarray[6]);

printf("eight value of array is %f\n",myarray[7]);

printf("ninth value of array is %f\n",myarray[8]);

printf("tenth value of array is %f\n",myarray[9]);

Output:-
first value of array is 10.500000

second value of array is 20.500000

third value of array is 30.600000

fourth value of array is 40.700001

fifth value of array is 50.799999

sixth value of array is 60.900002

seventh value of array is 70.000000

eight value of array is 80.000000

ninth value of array is 90.000000

tenth value of array is 100.099998


3. Write a C Program to declare and initialise (1D) array with type integer using
for loop.
Input:-
#include<stdio.h>

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++)

printf("Enter array value%d:\t",i+1);

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

printf("The values of an arrays are...\n");

for( i=0;i<5;i++)

printf("%d\n",myarray[i]);

Output:-
Enter array value1: 1

Enter array value2: 2

Enter array value3: 3

Enter array value4: 4

Enter array value5: 5

The values of an arrays are...

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];

printf("Enter the size of an array...\n");

scanf("%d",&n);

printf("Put values of an array ...\n");

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

printf("Enter array value%d:\t",i+1);

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

printf("The array values are...");

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

sum=sum+a[i];

printf("%d\n",a[i]);

printf("The sum of values of an array is%d",sum);

Output:-
Enter the size of an array...

Put values of an array ...

Enter array value1: 6

Enter array value2: 5

Enter array value3: 4

Enter array value4: 2


Enter array value5: 3

The array values are...6

The sum of values of an array is20

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;

printf("Enter the number of rows and columns of 2D array...\n");

scanf("%d%d",&rows,&cols);

printf("Put 2D array values are...\n");

for(i=0;i<rows;i++)

for(j=0;j<cols;j++)

scanf("%d",&a[i][j]);

printf("The 2D array values are...\n");

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];

printf("Row-wise sum is %d: \t\n",sum);

}
}

Output:-

Enter the number of rows and columns of 2D array...

22

Put 2D array values are...

24

68

The 2D array values are...

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()

char mystring[]={'k','c',' ','!','!','c','o','l','l','e','g','e','\0'};

char mystring1[]="kc !!!! college";

int i;

printf("%c\n",mystring[1]);

printf("%c\n",mystring[6]);

printf("Output 1: the string value in mystring variable is...\n");

for(i=0;i<=8;i++)

printf("%c",mystring[i]);

printf("\nOutput 2:%s\n\n", mystring);

printf("\nOutput 3:%s\n\n", mystring1);

}
Output:-
c

Output 1: the string value in mystring variable is...

kc !!coll

Output 2:kc !!college

Output 3:kc !!!! college

9. Write a C Program to read 2 1D arrays of type int, perform +,-,*,/ between


these 2 arrays
Input:-
#include <stdio.h>

void main()

int i, a[3], b[3];

printf("Enter First Arrays values:\n");

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

printf("\nEnter cell no: %d\n ",i+1);

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

printf("Enter Second Arrays values:\n");

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

printf("\nEnter cell no: %d\n ",i+1);

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

printf(" First Array is \n");

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

printf("%d\t", a[i]);

printf("\n");

printf("Second Array is \n");


for (i = 0; i < 3; i++) {
printf("%d\t", b[i]);

printf("\n");

int add[3], sub[3],mul[3],div[3];

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

add[i] = a[i] + b[i];

sub[i] = a[i] - b[i];

mul[i] = a[i] * b[i];

div[i] = a[i] / b[i];

printf("The Addition of Two Array is:\n");

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

printf("%d\t", add[i]);

printf("\n");

printf("The Subtraction of Two Array is :\n");

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

printf("%d\t", sub[i]);

printf("\n");

printf("The Multiplication of Two Array is :\n");

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

printf("%d\t", mul[i]);

printf("\n");

printf("The Division of Two Array is :\n");

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

printf("%d\t", div[i]);

printf("\n");
}

Output:-
Enter cell no: 1

Enter cell no: 2

Enter cell no: 3

10

Enter Second Arrays values:

Enter cell no: 1

Enter cell no: 2

Enter cell no: 3

First Array is

2 6 10

Second Array is

1 3 5

The Addition of Two Array is:

3 9 15

The Subtraction of Two Array is :

1 3 5

The Multiplication of Two Array is :

2 18 50

The Division of Two Array is :


2 2 2

10. Write a C Program to find the transpose of 2D arrays of m*n

Input:-

#include <stdio.h>

void main()

int a[10][10], transpose [10][10],rows,cols,i,j;

printf("Enter the number of rows and columns of 2D array...\n");

scanf("%d%d",&rows,&cols);

printf("Put 2D array values are...\n");

for(i=0;i<rows;i++)

for(j=0;j<cols;j++)

scanf("%d",&a[i][j]);

printf("The Matrix is...\n");

for(i=0;i<rows;i++)

for(j=0;j<cols;j++)

printf("%d\t",a[i][j]);

printf("\n");

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

for (j=0; j<cols;j++){

transpose[j][i]=a[i][j];

}
}

printf ("\nTranspose of the matrix:\n");

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

for (j=0; j<rows;j++){

printf("%d\t",transpose[i][j]);

printf("\n");}

Output:-
Enter the number of rows and columns of 2D array...

Put 2D array values are...

24

68

The Matrix is...

2 4

6 8

Transpose of the matrix:

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;

printf("Enter the size of array :");

scanf("%d",&n);

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

printf("Enter number%d:",i+1);

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

for (int i=1;i<n;++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;

printf("Enter the size of array :");

scanf("%d",&n);

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

printf("Enter number%d:",i+1);

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

int s=a[0];

for (int i=1;i<n;++i)

if(a[i]<s)

s= a[i];

printf("smallest number is =%d\n",s);

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;

printf("Enter the value for matrix :\n");

for(i=0;i<3;i++)

for(j=0; j<3; j++)

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];

printf("\n Largest element=%d",l);

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;

printf("Enter the value for matrix :\n");

for(i=0;i<3;i++)

for(j=0; j<3; j++)

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];

printf("\n Smallest element=%d",s);

Output:-
Enter the value for matrix :

256

897

143
Smallest element=1

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy