Translate

Labels

Showing posts with label ARRAY PROGRAMS. Show all posts
Showing posts with label ARRAY PROGRAMS. Show all posts

Friday, 5 April 2013

PROCESSING ONE DIMENSIONAL ARRAY

TO SEARCH FOR A NUMBER IN A LIST OF NUMBERS

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10},n,i,f,s;
clrscr();
printf("Enter the no of numbers \n");
scanf("%d",&n);
printf("Enter %d numbers \n",n);
for(i=0;i<n;i++)
scanf("%d",a[i]);
printf("Enter the number to be searched \n");
scanf("%d",&s);
/* searching for s in a begins */ 
f=0;
for(i=0;i<n;i++)
if(a[i]==s)
{
f=1;
break;
}
/*searching for s in a ends*/
printf("\n List of elements \n");
for(i=0;i<n;i++)
printf("%d",a[i]);
printf("\n Enter to be searched \n");
printf("\n s=%d \n",s);
if(f==1)
printf("%d is found",s);
else
printf("%d is not found",s);
getch();
}
OUTPUT
Enter the no of numbers
5
Enter 5 numbers
1   2   3   4   5
Enter the number to be searched 
3
List of elements 
1   2   3   4   5
Elements to be searched
3
3 is found
Explanation
a is declared to be array of int type of size 10.Maximum 10 values can be read into the array.n,i,s and f are declared to be variable of int type.n is to collect the number of values to  be read into a, which lies within 1 and 10.i is to traverse all the elements of a.s is to collect a value which is to be searched.f is to be determine whether s is found in a or not.It acts as a flag variable.The procedure of searching for s in a is implemented in the following segment of the program.
f=0;
for(i=0;i<n;i++)
if(a[i]==s)
{
f=1;
break;
}
before searching begins,f is set to zero with the assumption that s is not available in a.During the course of searching that is inside the body of the for loop, if s is found t match with any element in the array, f is set to 1  and loop is excited. If s takes 1 then it means s  is found in a.If f retains 0,we conclude that s s not fond in a.The value of f would thus tell us whether s is found in a or not.  

Thursday, 4 April 2013

PROCESSING ONE DIMENSIONAL ARRAY

TO ACCEPT A LIST OF NUMBERS INTO A 1-d ARRAY, FIND THEIR SUM, AVERAGE AND DISPLAY THEM.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,n, sum;
float avg;
clrscr();
printf("Enter no.of elements [1-10]\n");
scanf("%d",&n);
printf("Enter %d numbers \n ",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("The number are \n");
for(i=0;i<n;i++)
printf("%4d",a[i]);
/*Finding the sum and average begins*/
sum=0;
for(i=0;i<n;i++)
sum+=a[i];
avg=(float)sum/n;
/*Finding the sum and average ends*/
printf("\n Sum=%d\n",sum);
printf("\n Average=%f\n",avg);
getch();
}
OUTPUT
Enter no.of elements[1-10]
5
Enter 5 numbers
1   2   3   4   5
The numbers are
1   2   3   4   5
Sum=15
Average=3.000000
Explanation
a is declared to be an array of int type and of size 10.Maximum 10 numbers can be written into the array.n,i and sum are declared to be variable of int type.n is to collect the number of values to be read into the array a,which lies within 1 and 10.i is to traverse all values in a sum is to collect the sum of the values in a avg is declared to be a variable of float type and it is to collect the average of values in a.
In the process of finding the sum of all the values initially sum is set to 0.A for loop is used select each number in the array and it added to sum with the statement.
sum+=a[i]
The statement
avg=(float)sum/n;
on its execution assign average value to avg. Note that we have used type casting to convert sum forcibly to float to get the exact result.Otherwise the expression (sum/n) produces only integral part of the quotient since both sum and n are integers.Both sum and avg are then displayed 

Wednesday, 3 April 2013

PROCESSING ONE DIMENSIONAL ARRAYS

TO ILLUSTRATE DECLARATION, INITIALIZATION OF A ONE-DIMENSIONAL ARRAY AND DISPLAYS ITS ELEMENTS.

#include<stdio.h>
#include<conio.h>
void main()
{
int i,a[5]={2,3,5,6,7};
clrscr();
printf("The elements of array a \n");
for(i=0;i<=4;i++)
printf("%3d",a[i]);
getch();
}
OUTPUT:
The element of array a
2   3   5   6   7
Explanation
a is declared to be an array of int type and of size five and also is initialized while it is declared.i is a variable of int type and it is used to traverse the elements of a.
To display the elements of a,we have used for loop to repeatedly call printf().The loop variable i is made to range from 0 to 4.When i takes 0, the first element is displayed.When i takes 1,the second element is displayed and so on.  

Tuesday, 1 January 2013

STORING THE NAMES IN ARRAY USING C PROGRAM

#include<stdio.h>
#include<conio.h>
void main()
{
char a[10][20];
int i,j;
clrscr();
printf("Enter any names: \n");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%s",a[i]);
printf("Print the names in each line:\n");
for(i=0;i<n;i++)
printf("%s\n",a[i]);
getch();
}

Saturday, 29 December 2012

ARRANGING THE NUMBER IN DESCENDING ORDER USING ARRAY

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,j,n,temp;
clrscr();
printf("Enter the number of elements ");
scanf("%d",&n);
printf("Enter the number one by one ");
for (i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
for(i=0;i<n;i++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("Descending order:\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
getch();
}