Need a little hint, or nudge in the right direction.

Hi,

After sheer frustration with this homework problem, I finally created a c++ account. Yes, it is indeed a homework problem and I'm not asking for the solution, but some type of solution. I've tried searching this forum for a hint, but wasn't much helpful.

The hw question is:
Write a full program that reads in an arbitrary sequence of integers from the standard input, and writes them to the standard output in sorted order and with all duplicates removed. You may assume the input contains at most 100 integers.

I think understand what to do on the later half, but I'm specifically getting stumped on "reads in an arbitrary sequence of integers from the standard input."

I'm thinking this has to be an array, but usually dealt with sequences with a set number of integers. Is there a way I can read of 'i' of some random input in array[i] before declaring what i is? I'm assuming no, cause I believe when you're declaring an array in the first place you'll have to set the max number of elements or you'll run into overloading issues.

In all, if there is a way I can figure out the number of elements from the arbitrary sequence, I think the next step would be easier. Thinking of transferring to another array with conditional statements (I think it's called bubble sort, not too worried about the efficiency of the program).

Any hints would be much appreciated.
A key part of the assignment you may have missed:
You may assume the input contains at most 100 integers.


With that assumption, you can easily define your array to be big enough to handle all the input without needing to know specifically how many items there are.

As for getting the input, just use a loop.
Last edited on
I initially assumed that too.

So let's say array[100], and

for (int i=0, i<100&&check=true, i++)
{
cin>>array[i]
count++//used later to bubble sort
if (array[i]=='/n')
check=false;
}

for that loop how can I make it stop when there are no more input variables? that 'if' statement is completely wrong.

I think in one iteration of my code I got it work accepting the inputs, but the output was wrong.

Original input
10
8
4
4
9
2
1

Output should be
1
2
4
8
9
10

My output
1
0
2
0
...(something, can't exactly remember).
for (int i=0, i<100&&check=true, i++)

Look close.
= is assignment
== is comparison


for that loop how can I make it stop when there are no more input variables?


You'll have to give the user some way to tell you that they're done inputting values. The simplest way might be to have a "sentinel" value mark the end of input. Something like "Input -1 when finished".

I got it work accepting the inputs, but the output was wrong.


Might be a problem with your sorting algorithm.

But tackle one problem at a time. Don't worry about sorting for now. Just get it accepting input. Once you have that working, then worry about sorting it.
Topic archived. No new replies allowed.