0% found this document useful (0 votes)
21 views

Array 1

Uploaded by

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

Array 1

Uploaded by

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

//Input/Output of 1D array

#include<stdio.h>
void main()
{
int a[5],i;
printf("Enter the array elements:\n");
for(i=0;i<5;i++)
{
printf("Enter a[%d]:",i);
scanf("%d",&a[i]);
}
printf("The array elements you entered:\n");
for(i=0;i<5;i++)
{
printf("%d\t",a[i]);
}
}
//Program to calculate the sum and average of array elements
#include<stdio.h>
void main()
{
int marks[10],i,sum=0;
float avg;
for(i=0;i<10;i++)
{
printf("Enter marks[%d]:",i);
scanf("%d",&marks[i]);
sum=sum+marks[i];
}
/*for(i=0;i<10;i++)
{
sum=sum+marks[i];
}*/
printf("Sum=%d",sum);
avg=sum/10.0;
printf("\nAverage=%f",avg);
}
/*Program to calculate the sum of those elements in an array which are exactly divisible by 2 but not
by 3*/
#include<stdio.h>
void main()
{
int marks[10],i,sum=0;
float avg;
for(i=0;i<10;i++)
{
printf("Enter marks[%d]:",i);
scanf("%d",&marks[i]);
if(marks[i]%2==0&&marks[i]%3!=0)
sum=sum+marks[i];
}
printf("Sum=%d",sum);
}
/*Program to calculate the STANDARD DEVIATION of the marks obtained by n students in a class*/
#include<stdio.h>
#include<math.h>
void main()
{
int marks[20],i,n;
float sd,sum1=0,sum2=0;
printf("Enter the number of students:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter marks[%d]:",i);
scanf("%d",&marks[i]);
sum1=sum1+marks[i];
sum2=sum2+(marks[i]*marks[i]);
}
sd=sqrt((sum2/n)-(sum1/n)*(sum1/n));
printf("Standard Deviation=%f",sd);
}
//Input/Output in 2D array
#include<stdio.h>
void main()
{
int marks[3][3],i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Enter marks[%d][%d]:",i,j);
scanf("%d",&marks[i][j]); // input
}
}
printf("The array you entered is:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",marks[i][j]); // output
}
printf("\n");
}
}
//WAP to caculate the sum of diagonal elements in any 2D array
#include<stdio.h>
void main()
{
int marks[3][3],i,j,sum=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Enter marks[%d][%d]:",i,j);
scanf("%d",&marks[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==j)
sum=sum+marks[i][j];
}
}
printf("Sum=%d",sum);
}
/*WAP to calculate the sum of each row in any 2D array of size m*n .
[HW] WAP to input the marks obtained by m students in n different subjects. Calculate and display
the total marks obtained by each student.*/
#include<stdio.h>
void main()
{
int marks[5][5],i,j,sum,m,n;
printf("Enter the row size and column size:");
scanf("%d%d",&m,&n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("Enter marks[%d][%d]:",i,j);
scanf("%d",&marks[i][j]);
}
}
for(i=0;i<m;i++)
{
sum=0;
for(j=0;j<n;j++)
{
sum=sum+marks[i][j];
}
printf("\nSum of row %d = %d",i+1,sum);
}
}
/*WAP to input any two 2D arrays. Calculate and display the sum of two arrays. Matrix Addition*/
#include<stdio.h>
void main()
{
int a[3][3],b[3][3],i,j,c[3][3];
printf("Enter the elements of first matrix:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the elements of second matrix:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("The sum matrix is:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
//Program to calculate and display transpose of matrix of order 4*3
#include<stdio.h>
#include<conio.h>
void main()
{
int a[4][3],t[3][4],i,j;
printf("Enter the elements of 4*3 matrix:\n");
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("The matrix you entered is:\n");
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
{
t[j][i]=a[i][j];
}
}
printf("The Transpose of matrix you entered is:\n");
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
printf("%d\t",t[i][j]);
}
printf("\n");
}
getch();
}
MATRIX MULTIPLICATION
1 2 3 1 2
4 5 6 3 4
7 8 9 3*3 5 6 3*2

1*1+2*3+3*5 1*2+2*4+3*6
4*1+5*3+6*5 4*2+5*4+6*6
7*1+8*3+9*5 7*2+8*4+9*6 3*2
#include<stdio.h>
void main()
{
int a[3][3],b[3][2],c[3][2],i,j,k;
printf("Enter the elements of first matrix:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the elements of second matrix:\n");
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("The product matrix is:\n");
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
c[i][j]=0;
for(k=0;k<3;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
printf("%d ",c[i][j]);
}
printf("\n");
}
}
/*SORTING OF ARRAY ELEMENTS:
(Arranging array elements in Ascending/Descending Order)
Consider, int a[]={5,4,3,2,1};How to arrange the elements in Ascending Order:
a[0]a[1]a[2]a[3]a[4]
5 4 3 2 1
First Iteration:(i=0)
5 4 3 2 1 a[0]>a[1] CODE: for(i=0;i<5-1;i++)
4 5 3 2 1 a[0]>a[2] {
3 5 4 2 1 a[0]>a[3] for(j=i+1;j<5;j++)
2 5 4 3 1 a[0]>a[4] {
15432 if(a[i]>a[j])
Second Iteration:(i=1) {
1 5 4 3 2 a[1]>a[2] t=a[i];
1 4 5 3 2 a[1]>a[3] a[i]=a[j];
1 3 5 4 2 a[1]>a[4] a[j]=t;
12543 }
Third Iteration:(i=2) }
1 2 5 4 3 a[2]>a[3] }
1 2 4 5 3 a[2]>a[4]
12354
Fourth Iteration:(i=3)
1 2 3 5 4 a[3]>a[4]
1 2 3 4 5 */
#include<stdio.h>
void main()
{
int a[ ]={2,5,7,1,9},i,j,t;
for(i=0;i<5-1;i++)
{
for(j=i+1;j<5;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
printf("The sorted array is:\n");
for(i=0;i<5;i++)
{
printf("%d ",a[i]);
}
//printf("Second Largest=%d",a[5-2]);
}
/*Program to pass 1D array to a function and return the sum of its elements.
[HW]WAP to input the array elements of size n and pass it to a function that returns
the standard deviation of its elements*/
#include<stdio.h>
int func(int[],int);
void main( )
{
int arr[]={1,2,3,4,5},s;
s=func(arr,5);
printf("Sum of array elements=%d",s);
}
int func( int x[],int n) //x[0]=arr[0],x[1]=arr[1].....x[i]=arr[i]
{
int i,sum=0;
for(i=0;i<n;i++)
{
sum=sum+x[i];
}
return sum;
}
/*WAP to input an array of size n and pass it to a function that arranges its elements in descending
order*/
#include<stdio.h>
void calculate(int[],int);
void main()
{
int a[20],n,i;
printf("Enter the value of n:");
scanf("%d",&n);
printf("Enter the array elements:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
calculate(a,n); //a=&a[0];
printf("The Sorted Array is:\n");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
// printf("\nThird highest element=%d",a[2]);
// printf("\nThird lowest element=%d",a[n-3]);

}
void calculate(int x[],int n)
{
int i,j,t;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(x[i]<x[j])
{
t=x[i];
x[i]=x[j];
x[j]=t;
}
}
}
}
//Passing 2D array to a function: Example
#include<stdio.h>
void display(int[ ][3], int, int); // function declaration
void main ()
{
int a[3][3],i,j;
printf("Enter array elements:\n");
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
printf("Enter a[%d][%d]:",i,j);
scanf("%d",&a[i][j]);
}
}
display(a,3,3); // Function call
}
void display (int b[][3], int m, int n) // Function definition: b[i][j]=a[i][j]
{
int i,j;
printf ("the entered array is:\n");
for (i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf ("%d\t",b[i][j]);
}
printf ("\n");
}
}

/*WAP to input a square matrix of size n*n and pass it to a function that returns the sum of its
diagonal elements.*/
#include<stdio.h>
int display(int[][5],int); // function declaration
void main ()
{
int a[5][5],i,j,n,s;
printf("Enter the value of n:");
scanf("%d",&n);
printf("Enter array elements:\n");
for (i=0;i<n;i++)
{
for (j=0;j<n;j++)
{
printf("Enter a[%d][%d]:",i,j);
scanf("%d",&a[i][j]);
}
}
s=display(a,n); // Function call
printf("Sum of diagonal elements=%d",s);
}
int display (int b[][5],int x) // Function definition
{
int i,j,sum=0;
for (i=0;i<x;i++)
{
for(j=0;j<x;j++)
{
if(i==j)
sum=sum+b[i][j];
}
}
return sum;
}

/*WAP to input a 2D array of size m*n and pass it to a function that squares its elements. Display the
updated array from the main function*/
#include<stdio.h>
#include<math.h>
void display(int[][5],int,int); // function declaration
void main()
{
int a[5][5],i,j,m,n;
printf("Enter the value of m and n:");
scanf("%d%d",&m,&n);
printf("Enter array elements:\n");
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
printf("Enter a[%d][%d]:",i,j);
scanf("%d",&a[i][j]);
}
}
printf("The input array is:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
display(a,m,n); // Function call
printf("The updated array is:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
}
void display (int b[][5],int x,int y) // Function definition
{
int i,j;
for (i=0;i<x;i++)
{
for(j=0;j<y;j++)
{
b[i][j]=pow(b[i][j],2);
}
}
}
/*WAP to input any two 2D array of size m*n and p*q and pass them to a function that calculates
their product[Matrix Multiplication].Display the result from main function*/
#include<stdio.h>
void display(int[][5],int[][5],int[][5],int,int,int); // function declaration
void main()
{
int a[5][5],b[5][5],c[5][5],i,j,m,n,p,q;
printf("Enter the value of m and n:");
scanf("%d%d",&m,&n);
printf("Enter the value of p and q:");
scanf("%d%d",&p,&q);
printf("Enter array elements of first matrix:\n");
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
printf("Enter a[%d][%d]:",i,j);
scanf("%d",&a[i][j]);
}
}
printf("Enter array elements of second matrix:\n");
for (i=0;i<p;i++)
{
for (j=0;j<q;j++)
{
printf("Enter a[%d][%d]:",i,j);
scanf("%d",&b[i][j]);
}
}
if(n==p)
{
display(a,b,c,m,n,q); // Function call
printf("The product matrix is:\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
else
printf("Matrix multiplication not possible");
}
void display (int x[][5],int y[][5],int z[][5],int d,int e,int f) // Function definition
{
int i,j,k;
for (i=0;i<d;i++)
{
for(j=0;j<f;j++)
{
z[i][j]=0;
for(k=0;k<e;k++)
{
z[i][j]=z[i][j]+x[i][k]*y[k][j];
}
}
}
}

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