Array in C Language
Array is a collection of similar data types under a single name that stores data in contiguous memory location. it is a null-terminated string, that means array of characters terminated by null. Array of characters called string.
This program feels the need of array.
main()
{
int roll1, rool2, roll3;
clrscr();
printf("\n Enter the roll 1= ");
scanf("%d",&roll1);
printf("\n Enter the roll 2= ");
scanf("%d",&roll2);
printf("\n Enter the roll 3= ");
scanf("%d",&roll3);
printf("\n The roll1 is = %d",roll1);
printf("\n The roll1 is = %d",roll2);
printf("\n The roll1 is = %d",roll3);
getch();
}
Simple Array Program
main()
{
int roll[3];
clrscr();
printf("\n Enter the roll 1= ");
scanf("%d",&roll[0]);
printf("\n Enter the roll 2= ");
scanf("%d",&roll[1]);
printf("\n Enter the roll 3= ");
scanf("%d",&roll[2]);
printf("\n The roll1 is = %d",roll[0]);
printf("\n The roll1 is = %d",roll[1]);
printf("\n The roll1 is = %d",roll[2]);
getch();
}
Array with for loop.
In this program array of 3 elements defined, i.e. takes input with the for loop and gives output also with for loop.
main()
{
int roll[3];
clrscr();
for(i=0;i<=2;i++)
{
printf("\n Enter the roll= ");
scanf("%d",&roll[i]);
}
for(i=0;i<=2;i++)
{
printf("\n The roll1 is = %d",roll[i]);
}
getch();
}
Display array and total of that array.
#include<stdio.h>
#include<conio.h>
main()
{
int a[5], i, s=0;
clrscr();
for(i=0;i<5;i++)
{
printf("\n Enter the number= ");
scanf("%d",&a[i]);
s=s+a[i];
}
for(i=0;i<5;i++)
{
printf("\n The value of a is= ",a[i]);
}
printf("\n The total of numbers is=",s);
getch();
}
Sorting of an array
#incldue<stdio.h>
#include<conio.h>
main()
{
int a[5],i,j,t;
clrscr();
for(i=0;i<=4;i++)
{
printf("\n Enter the numbers= ");
scanf("%d",&a);
}
for(j=0;j<=4;j++)
{
for(i=0;i<=4;i++)
{
if(a[i])>a[i+1])
t=a[i];
a[i]=a[i+1];
a[i+1]=t;
}
}
printf("\n The sorted array is = ");
for(i=0;i<=4;i++)
{
printf("%d",a[i]);
}
getch();
}
Two-D Array
#include<stdio.h>
#include<conio.h>
main()
{
int n[3][3],i,j;
clrscr();
for(i=0;i<=3;i++)
{
for(j=0;j<=3;j++)
{
printf("\n Enter the elements of array= ");
scanf("%d",&n[i][j]);
}
}
for(i=0;i<=3;i++)
{
for(j=0;j<=3;j++)
{
printf("\n The array element are %d= ",n[i][j]);
printf("\n");
}
}
getch();
}
Qus:-Enter three roll numbers and print them.
Ans:- int roll1, roll2, roll3;
Qus:- Enter three roll numbers using array without for loop.
Ans:- int roll[3];
Qus:- Enter three roll numbers and print them using Array with for.
Ans:- int roll[3], i;
Qus:- Enter the array and calculate the sum of that array.
Ans:- s=s+a[i];
Qus:- Enter the array and set their sorting.
Ans:- if(a[i])>a[i+1]); t=a[i]; a[i]=a[i+1]; a[i+1]=t;
Qus:- Enter 2-D array and print them.
Ans:- for(i){for(j)};
No comments:
Post a Comment