Originally Posted by
SDH
Here is the code that I am trying to work on.
I have changed nu to number just so it is easier to read.
The input is -
5
1 1 1 7 2
The output is -
Enter the number of elements in array
Enter 5 numbers
1 is present at location 1.
1 is present at location 2.
1 is present at location 3.
2 is present at location 5.
7 is present at location 4.
10 is present 5 times in array.
But I would like the output to read, "1 is present 3 times in array"
"2 is present 1 times in array"
7 is present 1 times in array"
C99Tutorial already stated how to do this. You glossed over that post, without really understanding it!
Code:
#include<stdio.h>
int main(void)
{
//added i to the list of int variables, and initialize count1 to all zero's
int array[100], i, search, c, n, count = 0, number, count1[100]={0};
printf("Enter the number of elements in array\n");
scanf("%d",&n);
printf("Enter %d numbers\n", n);
for ( c = 0 ; c < n ; c++ )
scanf("%d",&array[c]);
//new: tricky counting
for(i=0;i<n;i++) { //for every number
count1[array[i]]++; //if array[i] is N, then count1[N]++
}
//new: reporting
for(i=0;i<100;i++) { //scan the whole range of numbers
if(count1[i]) //there was at least one with this value
printf("%d is present %d times in array.\n",i,count[i]);
else
printf("%d was not in the array\n",i);
}
return 0;
}
Don't feel badly though - it's tricky!