SKILL ROCKS TECH THIRD QUESTION ANSWER

Q3 find the frequency of 1 to 9 from an array of size 100 having value 1 to 9 randomly arranged
Conditions:
1. use only one loop.
2. do not use counters.
3. most efficiently 

Answer

suppose if array of 100 with random values between 1 to 9 were given then  simply answer is

for(i=0;i<100;i++)
{
          count[ arr[i] ]++; // arr[i] is given array and count[] is frequency counter.
}


complete program is like this




#include<conio.h>
#include<iostream.h>
#include<stdlib.h>
void main()
{
    int arr[100],count[10],i,j;
    clrscr();
    for(i=0;i<20;i++)
    {
        arr[i]=random(10);

    }
    for(i=0;i<20; i++)
    {
        cout<<arr[i];
        if(i%10==0)
        cout<<endl;
    }
    for(j=0;j<10;j++)
    count[j]=0;
    for(i=0;i<20;i++)

        count[arr[i]]++;

       for(i=0;i<10;i++)
    {
        cout<<"\nfreq of "<<i<<" : is "<<cont[i];
    }
    getch();
}

Comments

Popular posts from this blog

SKILL ROCKS TECH QUESTIONS

C Program for Analog Clock Design

Php Check is date or not