SlideShare a Scribd company logo
Page No. 1
ARRAY 1D
/*Write a C program which calculate and print sum
and average of elements of an array*/
#include<stdio.h>
int main()
{
int arr[50],n,i,sum=0;
float avg;
printf("enter the no. of elementn");
scanf("%d",&n);
printf("enter elementn");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
for(i=0;i<n;i++)
sum=arr[i]+sum;
avg=(float)sum/n;
printf("nElement sum is = %d",sum);
printf("nElement average is = %f",avg);
return 0;
}
/*Write a C Program to print the elements of array
in reverse order*/
#include<stdio.h>
int main()
{
int arr[50],i,n;
printf("Enter size of array : ");
scanf("%d",&n);
printf("Enter elements of array : n");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
printf("Array in Reverse Order : ");
for(i=n-1;i>=0;i--)
{
printf("%d ",arr[i]);
}
return 0;
}
/*Write a C program to print elements of an array
greater than average*/
#include<stdio.h>
int main()
{
int ar[10],i,n,s=0,c=0;
float avg;
printf("enter the limit:n");
scanf("%d",&n);
printf("enter the elementsn");
for(i=0;i<n;i++)
{
scanf("%d",&ar[i]);
s=s+ar[i];
}
avg=(float)s/n;
printf("average is %.2f",avg);
for(i=0;i<n;i++)
{
if(ar[i]>avg)
{
printf("n%d is grater than avg",ar[i]);
c++;
}
}
printf("nTotal Elements greater than avg are %d",c);
return 0;
}
/*Write a C Program to store the elements of array
into another array in reverse order*/
#include<stdio.h>
int main()
{
int arr[50],rev[50],i,n,j;
printf("Enter size of array : ");
scanf("%d",&n);
printf("Enter elements of array : n");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
j=n-1;
for(i=0;i<n;i++)
{
rev[i]=arr[j--];
}
printf("Array in Reverse Order : ");
for(i=0;i<n;i++)
{
printf("%dt",rev[i]);
}
return 0;
}
/*Write a c Program to search an element in a 1D
array*/
#include<stdio.h>
int main()
{
int arr[50],i,n,key,c=0;
Page No. 2
printf("Enter size of array : ");
scanf("%d",&n);
printf("Enter elements of array : n");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
printf("Enter Element to Be Searched : ");
scanf("%d",&key);
for(i=0;i<n;i++)
{
if(arr[i]==key)
{
printf("Element found at %d
indexn",i);
c++;
}
}
if(c==0)
printf("Element Not Found");
return 0;
}
/*Write a c program to find maximum and minimum
element in 1-D array */
#include<stdio.h>
int main()
{
int arr[50],n,i,min,max;
printf("Enter no. of elements:");
scanf("%d",&n);
printf("Enter %d elements in array",n);
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
min=max=arr[0];
for(i=0;i<n;i++)
{
if(arr[i]>max)
max=arr[i];
else if(arr[i]<min)
min=arr[i];
}
printf("nThe max element is:%d",min);
printf("nThe min element is:%d",max);
return 0;
}
/*Write a c program to perform sorting on 1-D array
*/
#include<stdio.h>
int main()
{
int arr[50],n,i,j,temp;
printf("Enter no. of elements:");
scanf("%d",&n);
printf("Enter %d elements in array",n);
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
for(i=0;i<n;i++)
{ for(j=0;j<n-1;j++)
{
if(arr[j]>arr[j+1])
{ temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
printf("Array Elements after Sorting");
for(i=0;i<n;i++)
{
printf("n%d",arr[i]);
}
return 0;
}
/* Write a c program to find maximum and minimum
element in 1-D array by sorting technique */
#include<stdio.h>
int main()
{
int arr[50],n,i,j,temp;
printf("Enter no. of elements:");
scanf("%d",&n);
printf("Enter %d elements in array",n);
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
//sorting array in ascending order
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
for(i=0;i<n;i++)
{
printf("%d ",arr[i]);
}
printf("nThe maximum element is:%d",arr[n-1]);
Page No. 3
printf("nThe minimum element is:%d",arr[0]);
printf("nThe second maximum element
is:%d",arr[n-2]);
printf("nThe second minimum element
is:%d",arr[1]);
return 0;
}
Page No. 4
ARRAY-2D
#include<stdio.h>
int main()
{
int row,col, ar[100][100],i,j;
printf("Enter row and column of array : ");
scanf("%d%d",&row,&col);
printf("Enter the elements of arrayn");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&ar[i][j]);
}
}
printf("The elements of arrayn");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{ if(i==j)
printf(" %d ",ar[i][j]);
sum=sum+ ar[i][j];
}
printf("n");
}
return 0;
}
/*Write a c Program to find addition of two 2d
array*/
#include<stdio.h>
int main()
{
int arr1[50][50],arr2[50][50],sum[50][50];
int i,j,r1,r2,c1,c2;
printf("Enter row and column of 1st array : ");
scanf("%d%d",&r1,&c1);
printf("Enter elements of array 1st array: n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&arr1[i][j]);
}
}
printf("Enter row and column of 2nd array : ");
scanf("%d%d",&r2,&c2);
printf("Enter elements of array 2nd array: n");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
scanf("%d",&arr2[i][j]);
}
}
if(r1==r2&&c1==c2)
{
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
sum[i][j]=arr1[i][j]+arr2[i][j];
}
}
printf("Sum Of Two Matrices : n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("%dt",sum[i][j]);
}
printf("n");
}
}
else
printf("Addition Not Possible");
return 0;
}
/*Write a C Program to print the upper and lower
triangle in a matrix*/
#include<conio.h>
#include<stdio.h>
#define row 3
#define col 3
int main()
{
int ar[row][col],i,j,n;
printf("Enter the elements of arrayn");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&ar[i][j]);
}
}
printf("The elements of arrayn");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf(" %d ",ar[i][j]);
}
printf("n");
}
if(row==col)
{
printf("The lower triangular matrixn");
for(i=0;i<row;i++)
Page No. 5
{
for(j=0;j<col;j++)
{
if(j<=i)
{
printf(" %d
",ar[i][j]);
}
}
printf("n");
}
printf("The upper triangular matrixn");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(j>=i)
{
printf(" %d ",ar[i][j]);
}
else
{
printf(" ");
}
}
printf("n");
}
}
else
printf("Can't calculate the upper/lower triangle in
matrix");
return 0;
}
/*Write a C Program to find row sum & column sum
*/
#include<stdio.h>
int main()
{
int mat[50][50];
int i,j,r,c,rsum,csum,d1=0,d2=0;
printf("Enter row and column of an array : ");
scanf("%d%d",&r,&c);
printf("Enter elements of array an array: n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&mat[i][j]);
}
}
//Code to print elements
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf(" %d",mat[i][j]);
}
printf("n");
}
//Row sum
for(i=0;i<r;i++)
{
rsum=0;
for(j=0;j<c;j++)
{
rsum=rsum+mat[i][j];
}
printf("Row Sum %dn", rsum);
}
//Column Sum
for(i=0;i<c;i++)
{
csum=0;
for(j=0;j<r;j++)
{
csum=csum+mat[j][i];
}
printf("Column Sum %dn",csum);
}
return 0;
}
//logic for individual right diagonal
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{ if(i==j)
{
printf(" %d ",ar[i][j]);
}
Else
{
Printf(“ “);
}
}
printf("n");
}
//logic for individual left diagonal
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{ if(i+j=row-1)
{
printf(" %d ",ar[i][j]);
}
Page No. 6
Else
{
Printf(“ “);
}
}
printf("n");
}
Write a C Program to add the elements of both
diagonals of a user defined matrix (method 1)
#include<stdio.h>
int main()
{
int n,i,j,r,c,arr[100][100],dsum=0;
printf("Enter the number of rows and columns.");
scanf("%d%d",&r,&c);
if(r==c)
{
for(i=0;i<=r-1;i++)
{
for(j=0;j<=c-1;j++)
{
scanf("%d",&arr[i][j]);
}
}
for(i=0;i<=r-1;i++)
{
for(j=0;j<=c-1;j++)
{
if((i==j)||(i+j==r-1))
{
dsum=dsum+arr[i][j];
}
}
}
printf("The sum of elements of both diagonals is
%d.",dsum);
}
else
{
printf("The sum of diagonals cannot be
calculated.");
}
return 0;
}
Write a C Program to add the elements of both
diagonals of a user defined matrix (method 2)
#include<stdio.h>
int main()
{
int n,i,j,r,c,arr[100][100],dsum=0;
printf("Enter the number of rows and columns.");
scanf("%d%d",&r,&c);
if(r==c)
{
for(i=0;i<=r-1;i++)
{
for(j=0;j<=c-1;j++)
{
scanf("%d",&arr[i][j]);
}
}
for(i=0;i<=r-1;i++)
{
dsum=dsum+arr[i][i];
}
for(i=0;i<=r-1;i++)
{
dsum=dsum+arr[i][r-1-i];
}
printf("The sum of elements of both diagonals is
%d.",dsum);
}
else
{
printf("The sum of diagonals cannot be
calculated.");
}
return 0;
}

More Related Content

Similar to Array Programs.pdf

Cpds lab
Cpds labCpds lab
PCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.pdfPCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.pdf
Ashutoshprasad27
 
PCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docxPCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docx
Ashutoshprasad27
 
1D Array
1D Array1D Array
1D Array
A. S. M. Shafi
 
Common problems solving using c
Common problems solving using cCommon problems solving using c
Common problems solving using c
ArghodeepPaul
 
Basic C Programming Lab Practice
Basic C Programming Lab PracticeBasic C Programming Lab Practice
Basic C Programming Lab Practice
Mahmud Hasan Tanvir
 
ADA FILE
ADA FILEADA FILE
ADA FILE
Gaurav Singh
 
C Programming Example
C Programming ExampleC Programming Example
C Programming Example
PRATHAMESH DESHPANDE
 
Cpd lecture im 207
Cpd lecture im 207Cpd lecture im 207
Cpd lecture im 207
Syed Tanveer
 
programs on arrays.pdf
programs on arrays.pdfprograms on arrays.pdf
programs on arrays.pdf
sowmya koneru
 
(Meta 5) ejemplo vectores dev c++
(Meta 5) ejemplo vectores dev c++ (Meta 5) ejemplo vectores dev c++
(Meta 5) ejemplo vectores dev c++ Eli Diaz
 
(Meta 5) ejemplo vectores 2 dev c++
(Meta 5) ejemplo vectores 2 dev c++ (Meta 5) ejemplo vectores 2 dev c++
(Meta 5) ejemplo vectores 2 dev c++ Eli Diaz
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Er Ritu Aggarwal
 
cpract.docx
cpract.docxcpract.docx
cpract.docx
PRATIKSHABHOYAR6
 
C file
C fileC file
C Programming Language Part 8
C Programming Language Part 8C Programming Language Part 8
C Programming Language Part 8
Rumman Ansari
 
C Programming Exam problems & Solution by sazzad hossain
C Programming Exam problems & Solution by sazzad hossainC Programming Exam problems & Solution by sazzad hossain
C Programming Exam problems & Solution by sazzad hossain
Sazzad Hossain, ITP, MBA, CSCA™
 

Similar to Array Programs.pdf (20)

Cpds lab
Cpds labCpds lab
Cpds lab
 
PCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.pdfPCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.pdf
 
PCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docxPCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docx
 
SaraPIC
SaraPICSaraPIC
SaraPIC
 
1D Array
1D Array1D Array
1D Array
 
Common problems solving using c
Common problems solving using cCommon problems solving using c
Common problems solving using c
 
Ds
DsDs
Ds
 
C lab manaual
C lab manaualC lab manaual
C lab manaual
 
Basic C Programming Lab Practice
Basic C Programming Lab PracticeBasic C Programming Lab Practice
Basic C Programming Lab Practice
 
ADA FILE
ADA FILEADA FILE
ADA FILE
 
C Programming Example
C Programming ExampleC Programming Example
C Programming Example
 
Cpd lecture im 207
Cpd lecture im 207Cpd lecture im 207
Cpd lecture im 207
 
programs on arrays.pdf
programs on arrays.pdfprograms on arrays.pdf
programs on arrays.pdf
 
(Meta 5) ejemplo vectores dev c++
(Meta 5) ejemplo vectores dev c++ (Meta 5) ejemplo vectores dev c++
(Meta 5) ejemplo vectores dev c++
 
(Meta 5) ejemplo vectores 2 dev c++
(Meta 5) ejemplo vectores 2 dev c++ (Meta 5) ejemplo vectores 2 dev c++
(Meta 5) ejemplo vectores 2 dev c++
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
 
cpract.docx
cpract.docxcpract.docx
cpract.docx
 
C file
C fileC file
C file
 
C Programming Language Part 8
C Programming Language Part 8C Programming Language Part 8
C Programming Language Part 8
 
C Programming Exam problems & Solution by sazzad hossain
C Programming Exam problems & Solution by sazzad hossainC Programming Exam problems & Solution by sazzad hossain
C Programming Exam problems & Solution by sazzad hossain
 

Recently uploaded

一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
VENKATESHvenky89705
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
AJAYKUMARPUND1
 
Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
Kerry Sado
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
manasideore6
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Dr.Costas Sachpazis
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
ongomchris
 
Building Electrical System Design & Installation
Building Electrical System Design & InstallationBuilding Electrical System Design & Installation
Building Electrical System Design & Installation
symbo111
 
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdfTutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
aqil azizi
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
Amil Baba Dawood bangali
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
ydteq
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
JoytuBarua2
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
FluxPrime1
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
Intella Parts
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
Osamah Alsalih
 
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERSCW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
veerababupersonal22
 
AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
BrazilAccount1
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
Massimo Talia
 

Recently uploaded (20)

一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
 
Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
 
Building Electrical System Design & Installation
Building Electrical System Design & InstallationBuilding Electrical System Design & Installation
Building Electrical System Design & Installation
 
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdfTutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
 
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERSCW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
 
AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
 

Array Programs.pdf

  • 1. Page No. 1 ARRAY 1D /*Write a C program which calculate and print sum and average of elements of an array*/ #include<stdio.h> int main() { int arr[50],n,i,sum=0; float avg; printf("enter the no. of elementn"); scanf("%d",&n); printf("enter elementn"); for(i=0;i<n;i++) { scanf("%d",&arr[i]); } for(i=0;i<n;i++) sum=arr[i]+sum; avg=(float)sum/n; printf("nElement sum is = %d",sum); printf("nElement average is = %f",avg); return 0; } /*Write a C Program to print the elements of array in reverse order*/ #include<stdio.h> int main() { int arr[50],i,n; printf("Enter size of array : "); scanf("%d",&n); printf("Enter elements of array : n"); for(i=0;i<n;i++) { scanf("%d",&arr[i]); } printf("Array in Reverse Order : "); for(i=n-1;i>=0;i--) { printf("%d ",arr[i]); } return 0; } /*Write a C program to print elements of an array greater than average*/ #include<stdio.h> int main() { int ar[10],i,n,s=0,c=0; float avg; printf("enter the limit:n"); scanf("%d",&n); printf("enter the elementsn"); for(i=0;i<n;i++) { scanf("%d",&ar[i]); s=s+ar[i]; } avg=(float)s/n; printf("average is %.2f",avg); for(i=0;i<n;i++) { if(ar[i]>avg) { printf("n%d is grater than avg",ar[i]); c++; } } printf("nTotal Elements greater than avg are %d",c); return 0; } /*Write a C Program to store the elements of array into another array in reverse order*/ #include<stdio.h> int main() { int arr[50],rev[50],i,n,j; printf("Enter size of array : "); scanf("%d",&n); printf("Enter elements of array : n"); for(i=0;i<n;i++) { scanf("%d",&arr[i]); } j=n-1; for(i=0;i<n;i++) { rev[i]=arr[j--]; } printf("Array in Reverse Order : "); for(i=0;i<n;i++) { printf("%dt",rev[i]); } return 0; } /*Write a c Program to search an element in a 1D array*/ #include<stdio.h> int main() { int arr[50],i,n,key,c=0;
  • 2. Page No. 2 printf("Enter size of array : "); scanf("%d",&n); printf("Enter elements of array : n"); for(i=0;i<n;i++) scanf("%d",&arr[i]); printf("Enter Element to Be Searched : "); scanf("%d",&key); for(i=0;i<n;i++) { if(arr[i]==key) { printf("Element found at %d indexn",i); c++; } } if(c==0) printf("Element Not Found"); return 0; } /*Write a c program to find maximum and minimum element in 1-D array */ #include<stdio.h> int main() { int arr[50],n,i,min,max; printf("Enter no. of elements:"); scanf("%d",&n); printf("Enter %d elements in array",n); for(i=0;i<n;i++) { scanf("%d",&arr[i]); } min=max=arr[0]; for(i=0;i<n;i++) { if(arr[i]>max) max=arr[i]; else if(arr[i]<min) min=arr[i]; } printf("nThe max element is:%d",min); printf("nThe min element is:%d",max); return 0; } /*Write a c program to perform sorting on 1-D array */ #include<stdio.h> int main() { int arr[50],n,i,j,temp; printf("Enter no. of elements:"); scanf("%d",&n); printf("Enter %d elements in array",n); for(i=0;i<n;i++) { scanf("%d",&arr[i]); } for(i=0;i<n;i++) { for(j=0;j<n-1;j++) { if(arr[j]>arr[j+1]) { temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } } } printf("Array Elements after Sorting"); for(i=0;i<n;i++) { printf("n%d",arr[i]); } return 0; } /* Write a c program to find maximum and minimum element in 1-D array by sorting technique */ #include<stdio.h> int main() { int arr[50],n,i,j,temp; printf("Enter no. of elements:"); scanf("%d",&n); printf("Enter %d elements in array",n); for(i=0;i<n;i++) { scanf("%d",&arr[i]); } //sorting array in ascending order for(i=0;i<n;i++) { for(j=0;j<n-1;j++) { if(arr[j]>arr[j+1]) { temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } } } for(i=0;i<n;i++) { printf("%d ",arr[i]); } printf("nThe maximum element is:%d",arr[n-1]);
  • 3. Page No. 3 printf("nThe minimum element is:%d",arr[0]); printf("nThe second maximum element is:%d",arr[n-2]); printf("nThe second minimum element is:%d",arr[1]); return 0; }
  • 4. Page No. 4 ARRAY-2D #include<stdio.h> int main() { int row,col, ar[100][100],i,j; printf("Enter row and column of array : "); scanf("%d%d",&row,&col); printf("Enter the elements of arrayn"); for(i=0;i<row;i++) { for(j=0;j<col;j++) { scanf("%d",&ar[i][j]); } } printf("The elements of arrayn"); for(i=0;i<row;i++) { for(j=0;j<col;j++) { if(i==j) printf(" %d ",ar[i][j]); sum=sum+ ar[i][j]; } printf("n"); } return 0; } /*Write a c Program to find addition of two 2d array*/ #include<stdio.h> int main() { int arr1[50][50],arr2[50][50],sum[50][50]; int i,j,r1,r2,c1,c2; printf("Enter row and column of 1st array : "); scanf("%d%d",&r1,&c1); printf("Enter elements of array 1st array: n"); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) { scanf("%d",&arr1[i][j]); } } printf("Enter row and column of 2nd array : "); scanf("%d%d",&r2,&c2); printf("Enter elements of array 2nd array: n"); for(i=0;i<r1;i++) { for(j=0;j<c2;j++) { scanf("%d",&arr2[i][j]); } } if(r1==r2&&c1==c2) { for(i=0;i<r1;i++) { for(j=0;j<c1;j++) { sum[i][j]=arr1[i][j]+arr2[i][j]; } } printf("Sum Of Two Matrices : n"); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) { printf("%dt",sum[i][j]); } printf("n"); } } else printf("Addition Not Possible"); return 0; } /*Write a C Program to print the upper and lower triangle in a matrix*/ #include<conio.h> #include<stdio.h> #define row 3 #define col 3 int main() { int ar[row][col],i,j,n; printf("Enter the elements of arrayn"); for(i=0;i<row;i++) { for(j=0;j<col;j++) { scanf("%d",&ar[i][j]); } } printf("The elements of arrayn"); for(i=0;i<row;i++) { for(j=0;j<col;j++) { printf(" %d ",ar[i][j]); } printf("n"); } if(row==col) { printf("The lower triangular matrixn"); for(i=0;i<row;i++)
  • 5. Page No. 5 { for(j=0;j<col;j++) { if(j<=i) { printf(" %d ",ar[i][j]); } } printf("n"); } printf("The upper triangular matrixn"); for(i=0;i<row;i++) { for(j=0;j<col;j++) { if(j>=i) { printf(" %d ",ar[i][j]); } else { printf(" "); } } printf("n"); } } else printf("Can't calculate the upper/lower triangle in matrix"); return 0; } /*Write a C Program to find row sum & column sum */ #include<stdio.h> int main() { int mat[50][50]; int i,j,r,c,rsum,csum,d1=0,d2=0; printf("Enter row and column of an array : "); scanf("%d%d",&r,&c); printf("Enter elements of array an array: n"); for(i=0;i<r;i++) { for(j=0;j<c;j++) { scanf("%d",&mat[i][j]); } } //Code to print elements for(i=0;i<r;i++) { for(j=0;j<c;j++) { printf(" %d",mat[i][j]); } printf("n"); } //Row sum for(i=0;i<r;i++) { rsum=0; for(j=0;j<c;j++) { rsum=rsum+mat[i][j]; } printf("Row Sum %dn", rsum); } //Column Sum for(i=0;i<c;i++) { csum=0; for(j=0;j<r;j++) { csum=csum+mat[j][i]; } printf("Column Sum %dn",csum); } return 0; } //logic for individual right diagonal for(i=0;i<row;i++) { for(j=0;j<col;j++) { if(i==j) { printf(" %d ",ar[i][j]); } Else { Printf(“ “); } } printf("n"); } //logic for individual left diagonal for(i=0;i<row;i++) { for(j=0;j<col;j++) { if(i+j=row-1) { printf(" %d ",ar[i][j]); }
  • 6. Page No. 6 Else { Printf(“ “); } } printf("n"); } Write a C Program to add the elements of both diagonals of a user defined matrix (method 1) #include<stdio.h> int main() { int n,i,j,r,c,arr[100][100],dsum=0; printf("Enter the number of rows and columns."); scanf("%d%d",&r,&c); if(r==c) { for(i=0;i<=r-1;i++) { for(j=0;j<=c-1;j++) { scanf("%d",&arr[i][j]); } } for(i=0;i<=r-1;i++) { for(j=0;j<=c-1;j++) { if((i==j)||(i+j==r-1)) { dsum=dsum+arr[i][j]; } } } printf("The sum of elements of both diagonals is %d.",dsum); } else { printf("The sum of diagonals cannot be calculated."); } return 0; } Write a C Program to add the elements of both diagonals of a user defined matrix (method 2) #include<stdio.h> int main() { int n,i,j,r,c,arr[100][100],dsum=0; printf("Enter the number of rows and columns."); scanf("%d%d",&r,&c); if(r==c) { for(i=0;i<=r-1;i++) { for(j=0;j<=c-1;j++) { scanf("%d",&arr[i][j]); } } for(i=0;i<=r-1;i++) { dsum=dsum+arr[i][i]; } for(i=0;i<=r-1;i++) { dsum=dsum+arr[i][r-1-i]; } printf("The sum of elements of both diagonals is %d.",dsum); } else { printf("The sum of diagonals cannot be calculated."); } return 0; }