Array Program Help

I am trying to create a program that will read up too 1000 non-negative numbers and print out only the distinct numbers, using arrays. (Example: If user enters: 1 1 5 5 2 2. Program returns: 1 5 2). The program runs but won't print out the distinct numbers. I am trying to modify my readNumbers function so that it will only add unique numbers to the array. The program works so far, but it prints out all of the numbers the user enters.

Last edited on
Here's a requirements question...

In readNumbers, do you have to store every number that is entered?

For example, if I enter in 1,000 1's, do you have to know I entered in 1 1000 times?

As a side note, you may want to add a condition to your while loop that makes sure count doesn't go over the ARRAY_SIZE.
@tscott8706: nope! I only need to store it if the number has not been entered before! So if you were to enter 1,000 1's. The program just needs to return: 1.
Ok, so if I understand correctly, there may be a simple way to do this in your readNumbers function, before you do

 
list[count] = userInput;


Loop through your entire array up to count, and make sure userInput is not already in one of those array elements. If it is already in the list, don't add it again.

So something like (pseudocode):

1
2
3
4
bool isElementInList(const int list[], int count, int newNum)
{
    // Loop through list up to count and see if number is in it.  Return true or false.
}


And in your readNumbers function...
1
2
3
4
5
6
// Only add userInput if it isn't already in list.
if (!isElementInList( /* args */))
{
    list[count] = userInput;
    count++;
}


Let me know if that works or if that doesn't make sense.
Last edited on
Hey, so I think I might have a general idea of what you're saying, can you check to see if I have the right idea? My program does run, but after I put in the numbers, it doesn't print anything out. Any ideas?
Last edited on
You're getting there. There's a few things I notice in your program:

* You aren't passing any arguments to your function on line 30. You need to pass in the list/count/and user input.
* In your isElement in list function, your loop needs to go up to the count, not the newNum. You want to loop over all the previous inputs to the array, which goes up to the count.
* In the isElementInList function, you don't need to do the comparison to 0.
* The newNum is just the new input (not an element in the array).
* Also make sure to return a value if you don't enter the loop at all. (Can you guess what value you need to return?)

For example, you may pass in a list of {1, 2, 3, 4}, a count of 4, and a newNum of 4 (which is what the user input). This should return true because list[3] == 4.

Let me know if any of this doesn't make sense.
Last edited on
Hey, so I think I'm getting it to almost work. It finally starts to print out something. But here's the thing: If I enter something like: 1 1 3 3 2 -1. The program returns: 1 3 3 2 and if I enter: 1 5 3 3 2 -1. The program returns: 1 5 3 3 2. So it seems like that the program doesn't store a duplicate entry for the first number, but then after that it stops doing that (sorry if that doesn't much sense, but hopefully my example gives you a better idea of what i'm talking about). Other than that though, here is what I have, and I'm slightly confused on your last bullet point D:
Last edited on
Ok, let's step through what the program is doing.

The problem occurs when it inserts that 2nd 3.

When we call isElementInList that time, it is called with list of {1, 3}, count of 2, and newNum of 3.

Try walking through what that function does with those inputs on a piece of paper. If you really understand what's going on, my next statement should make sense (and you hopefully will see the solution).

* We enter the loop.
* list[0] is 1.
* newNum is 3.
* So we fail that if statement.
* We then return false from the else statement.

Should we immediately be returning false there? No. We want to compare all the other elements of the array.

So rather than what we want... comparing list[0] == 3, list[1] == 3 (and list[1] == 3 would evaluate to true), we just do list[0] == 3 then immediately return false.

So where can you move that return false statement to make this work?
- Hint: it also solves my last bullet point I was talking about.
- And another hint: wherever you move it, just make sure that the whole loop gets to execute, all the way up until i == count. So if any of the elements in the array match, you want to return true. You only want to return false if all elements in the array do not match.
Hey, so I'm pretty sure I got what you meant, and I tested my program and it works the way it should. SO i'm pretty sure I got it, I just want you to check it to make sure!
Last edited on
Looks good. Great job!
Sweet, thanks so much helping me through this! You're a life saver! :)
Topic archived. No new replies allowed.