This is the exercise I have in codelab. I've been going over and over, and just can't see how to do it without an array. Must be something simple that I am missing. Any suggestions?
_____
Assume you have a int variable n that has already been declared and initialized. Its value is the number of integers that need to be read in from standard input and printed out in sorted (ascending) order, each on a line by itself. Furthermore, there are no duplicates in the input and every number to be read is a non-negative value that is less than n's value.

In this exercise you may not use any array (or fancy STL collection such as a vector). You may declare a variable or two as needed. With these restrictions, read the n values and print them out as required onto standard output.
_____

## All 6 Replies

Hard to say if this falls within the

... a variable or two as needed.

requirement but you can construct a binary search tree which, by it's properties, is always sorted. Printing the sorted items requires only an inorder traversal of the tree.

Nathan: That thread never came out with correct solution.

L7Sqr: I am taking introductory class to c++, and not familiar with binary search tree. there must be some simple code?!

okay, n is int, declared and initialized. values entered belong to [0;value(n)).
no duplicates. what required is to read them from standard input (which is simple 'for' loop), and print them out in ascending order (this is where i am lost).

here is the code i came out with, but it's wrong somewhere:

``````int inpt;
for (int i=0; i<n; i++) {
cin>>inpt;
int small=inpt;
for (int j=i; j<n; j++) {
cin>>inpt;
if (inpt<small)
small=inpt;
}
cout<<small<<endl;
}
``````

This is the code from the post I linked to.

``````#include <iostream>
using namespace std;

int main()
{
int n = 10, inpt;

for( int i = 0; i < n; i++ )
cin >> inpt;

for( int i = 0; i < n; i++ )
cout << i << endl;

return 0;
}
``````

What this does is that it reads in the n number of integers. Then it displays to the console integers in the range 0 -> n-1. The problem says "there are no duplicates in the input and every number to be read is a non-negative value that is less than n's value.". Since there are no duplicates and all numbers are less than n all you need to do is display 0 to n-1.

oh, thank you :-)!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.