0

Hi guys,

Could someone help me on this, the problem is:
Using array, Ask for 10 integer inputs then display the frequency distribution of the inputs. say,

Example Output:

Enter input 1: 2
Enter input 2: 2
Enter input 3: 3
Enter input 4: 4
Enter input 5: 5
Enter input 6: 6
Enter input 7: 5
Enter input 8: 4
Enter input 9: 2
Enter input 10: 1

2s - 3
3s - 1
4s - 2
5s - 2
6s - 1
1s - 1

Here's my own code:

#include<iostream.h>
#include<conio.h>

void print_array(int a[], int length)
{
    cout << "Enter the number of elements of the array:" << endl;
    for(i=0;i<length;i++)
    {
	cin >> a[i];
    }

    for(i=0;i<length;i++)
    {
	cout << "There are ____ " << a[i] << "s Integers" << endl;
    } 
		
}


void main()

{

    clrscr();
    int input_num[]={0,0,0,0,0,0,0,0,0,0};
    print_array(input_num,10);
    
getch();

}

Thanks in advanced.

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by VernonDozier
0

There are a few ways of doing this. First, do you have an allowable range that the input must be in (say 0 to 9)? If so, you can set up an array to keep track of the number of occurences:

int occurences[10];
// initialize all elements to 0.

for (int i = 0; i < 10; i++)
   occurences[input_num[i]]++;

// occurences[2] now holds number of times 2 shows up in input_num[] array

This won't work if there is not a definite range for the input or if the input can be negative.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.