# Thread: Finding and returning a common value from an array of 8 integers

1. ## Finding and returning a common value from an array of 8 integers

Hello all,

I am new to this forum and C Programming. One of my hw assignments is to write code that returns a value that is repeated more than once in an array of 8 integers.

For example:

Numbers: 30 12 100 33 2 2 1 40
Output: 2

Here is my code so far:

Code:
#include <stdio.h>

int main(void){
int i;
int j;
int arr[8];
int count;
int maxCount = 0;
int maxValue = 0;

printf("Enter 8 values between 1-100: ");

for(i = 0; i < 8; i++){
scanf("%d", arr[i]);                                  /* ERROR */
}

for(j = 0; j < 8; j++){
count = 0;
for(i = 0; i < 8; i++){
if(arr[i] == arr[j]){
count++;
}
if(count > maxCount){
maxCount = count;
}
}

maxValue = arr[i];
}

printf("Number: %d", maxValue);
}
When I compile this code (on Vim), I get an error when I print the array of 8 above (see ERROR above).

2. The argument you give to scanf must be a pointer to a location that you want to store the read data to. arr[i] is an integer, not a pointer. You can get the pointer to arr[i] with the address-of operator &.

Code:
scanf("%d", &arr[i]);
You always need to put & in front of any non-pointer variables you pass to scanf.

3. In your loop to find repeated values, you will get false hits since arr[i] is always equal to itself.

What is supposed to happen if there are two distinct repeated values in the array? For example 30 12 12 30 10 11 13 14

4. Originally Posted by ejoshi
When I compile this code (on Vim), I get an error when I print the array of 8 above (see ERROR above).