Would changing the code so it says that it tries out every single number away, so would using a nested loop with a counter to add one to the value of the number searched for work?

Yes. Make your count an array count[0]...count[100], each member starting at 0. Each time you find the element with value i in array, increment count[i]. Then you have a tally of each value 0...100 that occured in array.

01-13-2013

SDH

I have done that using

Code:

for ( nu = 0; nu <10 ; nu++)

because I am only inputting values of one to ten into the program. I have go it succesfully find which number are in the input but when I would like it say how many times each one occurs. I am using the statement

Code:

printf("%d is present %d times in array.\n", nu, count);

however this doesnt work. It just counts from 1 to 5. I think it has got something to do with the %d assigned to count but I am not totally sure.
thanks

01-13-2013

c99tutorial

Make `count' an array rather than a scalar. And post more of the context because it's not clear by the for loop with `nu' that you posted.

01-13-2013

Crossfire

Quote:

Originally Posted by SDH

I have done that using

Code:

for ( nu = 0; nu <10 ; nu++)

because I am only inputting values of one to ten into the program. I have go it succesfully find which number are in the input but when I would like it say how many times each one occurs. I am using the statement

Code:

printf("%d is present %d times in array.\n", nu, count);

however this doesnt work. It just counts from 1 to 5. I think it has got something to do with the %d assigned to count but I am not totally sure.
thanks

actually the i have done that part, isnt even in the code you posted!!! so it isnt like we could tell you WHY it dont work, IF it isnt in your code!

if ( count == 0 )
printf("%d is not present in array.\n", number);
else
printf("%d is present %d times in array.\n", number, count);

return 0;
}

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"

01-14-2013

Adak

Quote:

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!

//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! :cool:

01-14-2013

SDH

Adak - I have tried to compile that but for some reason it wont work :s.

Says there is an error on line 25. : In function 'main': Line 25 ERROR: subscripted value is neither array nor pointer
compilation terminated due to -Wfatal-errors.

I have tried to solve this issue myself but with no luck, any ideas?

01-14-2013

Matticus

Code:

printf("%d is present %d times in array.\n",i,count[i]);

@SDH: It appears to be a minor typo - "count[i]" should be "count1[i]". Notice that, at the top of "main()", "count" is declared as an int, and "count1" is declared as an array of int.

01-14-2013

std10093

Quote:

Originally Posted by c99tutorial

Yes. Make your count an array count[0]...count[100],..

while the code is

Quote:

Originally Posted by SDH

Code:

...
int array[100];
...

shouldn't we say count[0] to count[99]?
Or maybe I am mistaken :)

//SHD didn't do fortunately the same mistake :)

01-14-2013

Adak

Matticus nailed it - yes, count is not an array, it should be count1[i] in that printf() statement.

01-14-2013

SDH

Thank you very much, I knew it was something simply just could not spot it myself. Thanks

01-14-2013

Adak

Quote:

Originally Posted by SDH

Thank you very much, I knew it was something simply just could not spot it myself. Thanks

As you < ahem! > noticed, that code was just "off the cuff", and has not been run (let alone tested). So be sure to test it thoroughly, yourself.

And you're welcome. :cool:

01-14-2013

SDH

I have no tried to get it to work but with characters. I am once again having problems.
Here is my code,