954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Input sorting without arrays, pointers, or vectors

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.


This is a direct quote from an exercise. Seems like a step backwards, but this is what my professor has assigned.

In fact, in the previous three questions I solved similar problems using arrays. He just wants it done without one now.

We are not required to write the entire program, just the piece that would solve the provided question.

Any guidance would be appreciated.

nightcrew
Newbie Poster
22 posts since Oct 2011
Reputation Points: 27
Solved Threads: 1
 

Below are the solutions I came up with for the previous two questions.

The first asked you to sort n random inputs that could range from 0 to 49 in order without displaying duplicates using a boolean array:

int counter, x;

for (counter = 0; counter < 50; counter++){
    wasReadIn[counter] = false;}
for (counter = n; counter > 0; counter--){
    cin >> x;
    if (x > -1 && x < 50)
        wasReadIn[x] = true;}
for (counter = 0; counter < 50; counter++){
    if (wasReadIn[counter] == true)
        cout << counter << " ";}


The second asked you do perform a similar task, except this time duplicates could be stored and output using an integer array:

int counter, duplicates, x;

for (counter = 0; counter < 50; counter++){
    wasReadIn[counter] = 0;}
for (counter = n; counter > 0; counter--){
    cin >> x;
    if (x > -1 && x < 50)
        wasReadIn[x] += 1;}
for (counter = 0; counter < 50; counter++){
    if (wasReadIn[counter] >= 1){
        for (duplicates = wasReadIn[counter]; duplicates > 0; duplicates--)
            cout << counter << " ";}}


Now this question is again asking for a very similar output, but I just don't even know where to begin without using an array. Thought posting the previous questions might shed some light on the original post.

nightcrew
Newbie Poster
22 posts since Oct 2011
Reputation Points: 27
Solved Threads: 1
 
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
 

The exercise is run through a server, so I don't think I can use files for I/O unless the program references a file name for me (which it normally does if files can be used).

nightcrew
Newbie Poster
22 posts since Oct 2011
Reputation Points: 27
Solved Threads: 1
 

This sounds like a trick question to me because if n was 50 or something and I gave the input 20, 30, 21, 49, 1, 35, ... 0 how would you be able to sort that without having a variable for each input.

To "sort" a bunch of input in ascending order with n numbers and no duplicates can be done by using a for() loop

for( int i = 0; i < n; i++ )
	cout << i << endl;
sfuo
Practically a Master Poster
656 posts since Jul 2009
Reputation Points: 164
Solved Threads: 99
 
how would you be able to sort that without having a variable for each input.


This is the very question that has been puzzling me since I first laid eyes on the problem.

Probably something simple I'm missing here that I'll kick myself for later.

nightcrew
Newbie Poster
22 posts since Oct 2011
Reputation Points: 27
Solved Threads: 1
 

Output the numbers to the printer
Cut the numbers from the paper
Sort them on the table
Tape them together when sorted.
:icon_twisted:

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

If there's no upper bound on n, then you need a data structure that can grow without bound. If you can't use arrays or pointers, then you must find some other data structure that grows without bound. If you're not allowed to use any such data structure, then the only solution I can see is to use recursion to store the information you need on the call stack somehow. I don't immediately see how, but I also don't see any other way to do it.

Either that or there's some other aspect of the problem that you haven't told us.

arkoenig
Master Poster
703 posts since Jun 2010
Reputation Points: 359
Solved Threads: 109
 

I thought the same as sfuo wrote.

Caligulaminus
Junior Poster
106 posts since Feb 2011
Reputation Points: 72
Solved Threads: 30
 

The original post is exactly, word for word, what the question states. I have not excluded any details. To hold back information would not be beneficial in my attempt to solve the problem.

nightcrew
Newbie Poster
22 posts since Oct 2011
Reputation Points: 27
Solved Threads: 1
 

I want to try the external merge sort with files.

Probably the only way. I'll update if it works (which it probably will).

nightcrew
Newbie Poster
22 posts since Oct 2011
Reputation Points: 27
Solved Threads: 1
 

The only thing I can think of is that maybe you are to use a stream iterator and loop through it outputting what you need each iteration. This seams like a very nonsense question.

NathanOliver
Veteran Poster
1,084 posts since Apr 2009
Reputation Points: 215
Solved Threads: 189
 

Oh, wait a minute, I got it. It is indeed a trick question. sfuo is right. Read the conditions carefully.

arkoenig
Master Poster
703 posts since Jun 2010
Reputation Points: 359
Solved Threads: 109
 

I took a crack at using a file for sorting.

I keep entering an infinite loop on the test object, and if I plug the code into Bloodshed and build my own main, it skips the while loop at line 26 no matter what numbers I enter.

So, this method may work... I'm obviously just going about it the wrong way if it does though. The class I am taking is an introduction to C++, so just keep that in mind when breaking down what I've used.

ofstream outputFile;
ifstream inputFile;
int counter, number, minimum;

minimum = n;
outputFile.open("Random_Numbers.txt");
for(counter = n; counter > 0; counter--)
{
    cin >> number;
    outputFile << number << endl;
}
outputFile.close();
number = 0;
do
{
    counter = 0;
    inputFile.open("Random_Numbers.txt");
    while (inputFile >> number)
    {
        if (number < minimum)
            minimum = number;
    }
    inputFile.close();
    inputFile.open("Random_Numbers.txt");
    outputFile.open("temp_File.txt");
    while (inputFile >> number)
    {
        if(number != minimum)
        {
            outputFile << number << endl;
            counter++;
        }
    }
    inputFile.close();
    outputFile.close();
    remove("Random_Numbers.txt");
    rename("temp_File.txt","Random_Numbers.txt");
    if (counter > 0)
        cout << minimum << endl;
}while (counter > 0);
nightcrew
Newbie Poster
22 posts since Oct 2011
Reputation Points: 27
Solved Threads: 1
 

Yeah but if you think about it a file is pretty much a big array of characters so if he would let you do that then thats a joke.

sfuo
Practically a Master Poster
656 posts since Jul 2009
Reputation Points: 164
Solved Threads: 99
 

Pretty much.

But, I might as well try it. I've got no other leads to follow, so if anyone can help me identify where I've gone wrong with my code, it would be much appreciated.

nightcrew
Newbie Poster
22 posts since Oct 2011
Reputation Points: 27
Solved Threads: 1
 

OK, let me state this more explicitly. If I have n non-negative integers, each of which is less than n, then that defines the set of integers [0..n-1]. All the code needs to do is read in the n integers and write out the integers 0 to n-1. It doesn't matter what order they appeared in the input file.

kriviere
Newbie Poster
1 post since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

Since it says there are no duplicate inputs I'm guessing (and there is no way to check) there will be no need for checking if a duplicate number was entered. You can throw on a check to restrain input from [0,n).

This is what I came up with "1 day ago" according to this thread.

#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;
}
sfuo
Practically a Master Poster
656 posts since Jul 2009
Reputation Points: 164
Solved Threads: 99
 

I guess I was trying to be too serious about it...

I'll try some simple for loops when I get home tonight.

I do appreciate the input from everyone. If this is a trick question... Oh, so mad. ;)

nightcrew
Newbie Poster
22 posts since Oct 2011
Reputation Points: 27
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: