First of all use int main( void ) and not just main( ) since its not standard.
Don't hard code values in your program, if you want to process 20 values make a preprocessor defination at the start of your program which can easily be modified if you are asked to change the requirements. Using magic numbers as such causes a lot of confusion.
Can you give us a sample run or a dummy example of what kind of output you are expecting ? Are you required to store the occurances or just display them?
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 733
Hmm..if thats the case then all you are required to do is to use nested loops. Here is a short algorithm:Create a variable count which will keep track of the occurances of the numbers.
Start an outer loop with i as the counter or index, initialize it with 0 and continue looping till i is less than the number of elements or till all the elements have been visited.
Create an inner loop with j as the counter, initialize it in the same manner as the previous loop.
Inside the inner loop check if [I]my_array equals my_array[j] and if it does, increment the counter by one.
Close the inner for loop and after the loop completion, print out the value of counter which should be the number of times the element [I]my_arrayhas occured along with the element under consideration.
Reset the counter value back to zero for the remaining elements.
Keep looping the outer loop till all the elements have been visited.
End.
Hope it helped, bye.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 733
thanks a lot!
Just one more question. What do you mean by "and if it does, increment the counter by one"?
counter = counter + 1
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
What I mean is :
// if the element of array matches with another element in the same
// array, then increment the counter.
if( my_array[i] == my_array[j] )
{
++counter ; // counter = counter + 1
}
// my_array = {1, 2, 1, 3, 4, 5 }
// my_array[i] = my_array[0] = 1
// counter = 2 ( since 1 has a match at 0th and 2nd position )
Hope it helped, bye.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 733