Wednesday 12 September 2012

To find both the largest and smallest number in a list of integers


Description:
This program contains n number of elements, in these elements we can find the largest and smallest numbers and display these two numbers
Algorithm:
Step 1: start
Step 2: read n
Step 3: initialize i=0
Step 4: if i<n do as follows. If not goto step 5
                        Read a[i]
                        Increment i
                        Goto step 4
Step 5: min=a[0], max=a[0]
Step 6: initialize i=0
Step 7: if i<n do as follows. If not goto step 8
                        If a[i]<min
                        Assign min=a[i]
                        Increment i goto Step 7
Step 8: print min,max
Step 9: stop

Program:
#include<stdio.h>
void main()
{
  int a[10],i,n,min,max;
  clrscr();
  printf("enter the array size:");
  scanf("%d",&n);
  printf("Enter the elements of array");
  for(i=0;i<n;i++)  // read the elements of an array
  scanf("%d",&a[i]);
  min=a[0];
  max=a[0];
  for(i=0;i<n;i++)// read the elements of an array
   {
             if(a[i]<min)// check the condition for minimum value
              min=a[i];
              if(a[i]>max)//check the condition for maximum value
              max=a[i];
   }
              printf("maximum value is:%d\n",max);
              printf("minimum value is:%d\n",min);
              getch();
}
Output:
1.enter the array size:4
    Enter the elements of array 36     13           2          45
    maximum value is:45
       minimum value is:2
2.enter the array size:5
   Enter the elements of array 6    2     1          3          8
   maximum value is:8
   minimum value is:1
3.enter the array size:5
   Enter the elements of array-6    9     -9         2          5
     maximum value is:9
   minimum value is:-9
conclusion: the program is error free
VIVA QUESATIONS:
1) What is an array ?
Ans: The collection of similar elements is called array
1)      How many types of arrays are there ?
Ans: Three types. They are one dimensional ,two dimensional and multi dimensional arrys


No comments:

Post a Comment