Skip to main content

C-Programming Program 5(a)

5 (a) Write a C program to find both the largest and smallest number in a list of integers.

Algorithm 


1. Start

2. Read n
3. Set i:=0
4. Repeat steps 5,6 until i >= n , go to step 6
5. Read a[i]
6. Set i:=i+1. 
7. Set large:=small:=a[0]
8. Set pos1:=pos2:=0
9. Set i:=1
10. Repeat steps 11 to13 until i >= n , go to step 14
11. If a[i] > large then set large:=a[i] ,pos1:=i 
12. If a[i]<small then set small:=a[i] , pos2:=i
13. Set i:=i+1
14. Write pos1,large
15. Write pos2,small
16. Set i:=0
17. Repeat steps 18 to 25 until i >= n , go to step 26
18. Set j:=i+1
19. Repeat steps 20 to 24 until j >= n , go to step 25
20. If a[i] <= a[j] go to step 26
21. temp:=a[i]
22. a[i]:=a[j]
23. a[j]:=temp
24. Set j:=j+1 
25. Set i:=i+1
26. Set i:=0
27. Repeat steps 28 to 29 until i >= n , go to step 30
28. Write a[i]
29. Set i:=i+1
30. Stop

Flowchart





Program


#include<stdio.h>

#include<conio.h>
#include<math.h>
void main()
{
  int a[10],b[10],i,j,n,large,small,pos1=1,pos2=1,temp;
  clrscr();
  printf("Enter how many values u want:");
  scanf("%d",&n);
  printf("Enter elements into array a:\n");
   for(i=0;i<n;i++)
    {
      scanf("%d",&a[i]);
    }
   large=small=a[0];
   for(i=0;i<n;i++)
    {
      if(a[i]>large)
       {
large=a[i];
pos1=i+1;
       }
      else if(a[i]<small)
       {
small=a[i];
pos2=i+1;
      }
    }
   printf("\nthe largest element is:%d\tpos=%d",large,pos1);
   printf("\nthe small element is:%d\tpos=%d",small,pos2);
    for(i=0;i<n-1;i++)
     {
       for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
 {
  temp=a[i];
  a[i]=a[j];
  a[j]=temp;
 }
}
     }
     printf("\the sorting order is given below:");
     for(i=0;i<n;i++)
      printf("%d",a[i]);
 getch();
 }

Comments

Popular posts from this blog

C-Programming with Record

C-Programming Programs Syllabus Click for full record  1.a) Write a C program to ask the user to enter one char ( Upper-Case letter) check  whether user  entered a Upper-case letter or not(by using relational and logical operators) and then if user has  entered a Upper-case letter  convert into a Lower-case letter? ( hint: Upper-case means capital letters,  use ASCII information to check for Upper-case and convert) b) Write a  C program to ask the user to enter two integers and apply all arithmetic operations on those  print the corresponding values?(hint : +,-,*,/,%)     c) Write a C program to Determine the ranges of char, short, int and long int variables both signed and  Unsigned   By using sizeof operator(ii) By printing appropriate values from standard header (limits.h )   2.a) Write a Program to Find the Roots of a Quadratic Equation using if else and Switch statements. b) Write a Program which Gener...

OOPS CPP Program-14

14. Write a program to find transpose of a given matrix of mxn size using unary operator overloading #include<iostream> using namespace std; class matrix {         int x[5][5],m,n; public:         matrix(){ }         matrix(int a,int b)         {                m=a;n=b;         }         void get();         void put();         matrix operator !(); }; void matrix::get() {         int i,j;         cout<<"\n Enter a matrix of order"<<m<<"x"<<n<<"\n";                for(i=0;i<m;i++)                    for(j=0;j<n;j++)                        ...

OOPS CPP Program-15

15. Write a program to concatenate one string to another using binary operator overloading. #include<iostream> #include<string.h> using namespace std; class str  {         char *st; public:         str(int len, char *s)         {                  int l=strlen(s);                st= new char[l];                st=s;          }         str()         {         }         void put();         str operator +(str); }; str str :: operator +(str s)  {  /*str abc; abc.st=strcat(st, s.st); return abc;*/         st=strcat(st, s.st);         return *this; } void str :: put() {   cout<<...