Saturday, 28 May 2016

Q: Write a program that inputs ten integers in an array. It displays the number of occurrences of each numbers in the array as follows:
    3 is stored 4 times in the array
    1 is stored 1 times in the array

Program:

#include<iostream.h>
#include<conio.h>
void main()
{
   int array[10],i,j,c,n;
   cout<<"Enter integers: ";
   for(i=0;i<10;i++)
   cin>>array[i];
   for(i=0; i<10; i++)
   {
     if (array[i]== -1)
     continue;
     n=array[i];
     c=1;
     for(j=i+1 ; j<10; j++)
      {
        if (array[j]==n)
        {
         c++;
         array[j]=-1;
         }
     }
    cout<<n<<" is stored "<<c<<" times in the array<<endl;
    }
getche();
}

No comments:

Post a Comment