dj2k - 1 year ago 50

C Question

I am currently doing practice with programming (particularly with C Programming) and I am trying out an end of chapter problem which has this description:

"Use a single-subscripted array to solve the following problem.

Read in 20 numbers, each of which is between 10 and 100, inclusive. **As each number is read**, print

it only if it’s not a duplicate of a number already read. Provide for the “worst case” in which all 20

numbers are different. Use the smallest possible array to solve this problem"

Here is what I have so far:

`#include <stdio.h>`

#include <stdbool.h> //used this library for the ability to use true and false

#define SIZE 20

bool searchArray(int integer, unsigned int j); //prototype for the searchArray function

int main (void)

{

bool print;

int numbers[20];

unsigned int i;

for (i = 0; i < SIZE; i++)

{

scanf("%d", &numbers[i]);

print = searchArray(numbers[i], i); //receives true or false

if (print == true)

{

printf("\nNumber at element %d: %d\n", i, numbers[i]);

}

else if (print == false)

{

printf("\nThis element has a copy in the array already\n");

}

}

}

bool searchArray(int integer, unsigned int j) //function that helps decide whether or not to print out the integer

{

static int numberscopy[20];

unsigned int n;

numberscopy[j] = integer;

for (n = 0; n < SIZE; n++)

{

if (numberscopy[n] == numberscopy[j] && n != j) // for making sure that the inputted integer is equal to any element that's currently in the array and making sure it's not including the very same integer that was just input on it's first appearance

{

return false;

}

return true;

}

}

EXPLANATION: What I did in this program was that I defined a function called searchArray which based on the inputs of the current array element and array index return a boolean value (true or false). In my for loop in the main function, I used scanf to receive integers from the user one at a time. Each of those integer elements are put into an array index in an array called "numbers".

Then, both the array element and the current index of the array that the program is on is put into the searchArray function. Within the searchArray function, each element is input into the function's local 20 element array called "numberscopy" (where in that array each number is put in is based on the current value of i that gets passed the searchArray). Then within the searchArray function, there is a for loop which searches through all the current elements in the numberscopy array and checks if that integer that was just put in is already in the numberscopy array. If found, it returns "false". If not, then "true" gets automatically returned instead.

I thought this would work, but the problem is that it seems to only be able to detect duplicates of the very first integer that is input on each run. For an example, if a number 25 is input first, then a 32 next, then a 25 again and then a 32 again, it properly detects the 25 is already in the array when I put 25 in the second time, but for the 32, it doesn't seem to detect it and prints 32 when it's put in again. The example here

Anyone know why this is not working like I thought it would?

Answer Source

```
for (n = 0; n < SIZE; n++)
{
if (numberscopy[n] == numberscopy[j] && n != j)
{
return false;
}
}
return true;
}
```

instead of

```
for (n = 0; n < SIZE; n++)
{
if (numberscopy[n] == numberscopy[j] && n != j)
{
return false;
}
return true;
}
}
```