The requirements are actually simple, just asking to sort a few, say 10, 20 or so, intergers in ascending order. However, array and pointers are not allowed to be used, and just the fundamental knowledge about flow control are permitted. The hints are as follows:
1. Initialize a variable F that keeps a very large number (larger than any number in the to-be -sorted batch)
2. Search among the batch for the biggest number, which is smaller F
3. Show that number on the screen
4. Save that number to F and take away the number from the batch
5. Repeat steps 2,3 and 4 until all the 5 numbers in the batch are shown on the screen

Many thanks to anyone who can help!!!

Recommended Answers

All 19 Replies

Its the simple question ... When we use to learn the C++
This type of Sorting is known as Selection sorting...

Rest you figure it out...:D

Its the simple question ... When we use to learn the C++
This type of Sorting is known as Selection sorting...

Rest you figure it out...:D

Thanks a lot

OK: "array and pointers are not allowed to be used". What is it "batch"? I don't know such type (I hope, it's not an array ;)) in C++...

Its the simple question ... When we use to learn the C++
This type of Sorting is known as Selection sorting...

Rest you figure it out...:D

Using selection sorting without using arrays, can you provide any ideas how to get the numbers into the loop, and take away the number from the batch?

OK: "array and pointers are not allowed to be used". What is it "batch"? I don't know such type (I hope, it's not an array ;)) in C++...

"batch" simply means the collection of the numbers to be sorted, that's not a keyword.

you can access them by memory addresses... so the integers should be given addresses that are consecutive. could count as pointing really.

you can access them by memory addresses... so the integers should be given addresses that are consecutive. could count as pointing really.

Thank you any way, but you didn't see the question carefully, arrays and pointers are not
allowed to be used, only the fundamental knowledge of the flow control are permited and
that's why the hints, the initialization of the large number F are for.

This website doesn't provide source code, and I understand that, however, is there anybody who manages this website can come up with some useful ideas. I'm only a
new learner of C++, and what I'm asking is
only some fundamental thing.

This website doesn't provide source code, and I understand that, however, is there anybody who manages this website can come up with some useful ideas. I'm only a
new learner of C++, and what I'm asking is
only some fundamental thing.

According to the sorting algorithm I have already told you its the Selection sorting.
But i don't think so you can possibly create the batch of no without using array or pointer.

Other crazy solutions might be using link list or objects or blah blah blah.. for just a simple sorting. :-/

If you can't use containers then it is difficult (impossible?) to use loops. You could try an elaborate sequence of if/else similar to the sequence below which sorts the three ints into ascending order without using a container or a loop. To do this for 10 variables would be absolute nonsense however.

int a, b, c;

if(a < b && a < c)
  a is smallest
     if b < c
        sequence is abc
     else 
        sequence is acb
else if(b < a  && b < c) 
  b is smallest
     if(a < c)
       sequence is bac
     else
       sequence is bca
else 
 c is samllest
    if(a < b)
      sequence is cab
    else
      sequence is cba

You could use an STL set, but I doubt that is what the point of exercise is.

Of course, "batch" is not a keyword. "Array" and "pointer" are not keywords too.
But we all know what is it an array and what is it a pointer in C++, but it seems only the author knows what is it mysterious "batch" in C++.
It's funny...

If you can't use containers then it is difficult (impossible?) to use loops. You could try an elaborate sequence of if/else similar to the sequence below which sorts the three ints into ascending order without using a container or a loop. To do this for 10 variables would be absolute nonsense however.

int a, b, c;

if(a < b && a < c)
  a is smallest
     if b < c
        sequence is abc
     else 
        sequence is acb
else if(b < a  && b < c) 
  b is smallest
     if(a < c)
       sequence is bac
     else
       sequence is bca
else 
 c is samllest
    if(a < b)
      sequence is cab
    else
      sequence is cba

I guess its not the healthy choice according to him as he says many no...
I'm doubting about the the question.:-/

I agree with you ArkM. :)
I wan't to ask what container have to be used to do such thing...

And don't tell me sets.. stuff like my mathematics teacher does..:D

Actually this is one exercise for coming programing text, and This is the professor's reply

to some of the students, anyway he will post the answer soon, thx everybody

The presence of F is to enable you putting the steps 2 - 4 in to the action statements in a loop. That's the job I gave you. Think of it.

I think to follow the method suggested, the difficult part should be: How do you take away the number from the batch?

Wait for a while, I'll let you have the suggested solution later and before the Programming Test 1.


taco wrote:
Are we only allowed to use the knowledge we learnt so far? thx..

YES, only knowledge you learn so far.
In particular, do NOT use array and pointers. I find some students not able to write the program if arrays are not allowed to be used. Hope you are not one of them.

Of course, "batch" is not a keyword. "Array" and "pointer" are not keywords too.
But we all know what is it an array and what is it a pointer in C++, but it seems only the author knows what is it mysterious "batch" in C++.
It's funny...

Regard it as collection, it has no special meaning.

Here's a possible strategy to display the values in a batch of variables in ascending order.

int F;  // at the end of the loop F will hold the smallest value in the batch on each given loop through the varaibles in the batch.  Each time through the loop F is initially assigned a value too high to be smaller than the largest value in the batch.

int G = 35000; //a large value

//batch size known at compile time
int numVariables = 3;

//a batch of variables with known starting values at compile time
int a = 9;
int b = 4;
int c = 1;

//algorithym
for(int i = 0; i < numVariables; ++i)
{
   //assign F a large value
   F = G;

   //find smallest value in batch this time through
   if(a < F)
    F = a;
   if(b < F)  
    F = b;
   if(c < F)
    F = c;
   
   //display smallest value this time through 
   cout << F << ' ';

   //remove current smallest variable from further consideration by making it too large to every be smallest variable again
   for(int j = 0; j < numValues; ++j)
   {
     if(a == F)
       a = G;
     if(b == F)
       b = G;
     if(c == F)
       c = G;
    }

    //reset F to be too high to be smallest value remaining in batch
    F = G;
}

Should be expandable to any batch size as long as the size is known at compile time. The original values of a, b, and c are lost, so if you want to keep them intact for some reason then use copies of the original values in the algorithm.

Solution for 10 to be sorted numbers:

#include <iostream>
using namespace std;
void descend (float,float,float,float,float,float,float,float,float,float);
float max (float,float,float,float,float,float,float,float,float,float);

int main ()
{
	float f1=1,f2=1,f3=1,f4=1,f5=1,f6=1,f7=1,f8=1,f9=1,f10=1;
	cout << "Please enter 10 floating-point numbers\n";
	cin>>f1;
	cin>>f2;
	cin>>f3;
	cin>>f4;
	cin>>f5;
	cin>>f6;
	cin>>f7;
	cin>>f8;
	cin>>f9;
	cin>>f10;// Get the numbers

	descend (f1,f2,f3,f4,f5,f6,f7,f8,f9,f10);
	return 0;
}

void descend (float d1,float d2,float d3,float d4,float d5,float d6,float d7,float d8,float d9,float d10)
{
	int i;
	float F = 1.0;
	float small = 3.4e-38;
	cout << "\n\nNumbers in descending order\n";
	for (i=0;i<10;i++)
	{
		F = max (d1,d2,d3,d4,d5,d6,d7,d8,d9,d10);
		if (d1 == F)
			d1 = small;
		else if (d2 == F)
			d2 = small;
		else if (d3 == F)
			d3 = small;
		else if (d4 == F)
			d4 = small;
		else if (d5 == F)
			d5 = small;
		else if (d6 == F)
			d6 = small;
		else if (d7 == F)
			d7 = small;
		else if (d8 == F)
			d8 = small;
		else if (d9 == F)
			d9 = small;
		else if (d10 == F)
			d10 = small;
	}
}

float max (float d1,float d2,float d3,float d4,float d5,float d6,float d7,float d8,float d9,float d10)
{
	float max;
	max = d1;
	if (max < d2)
		max = d2;
	if (max < d3)
		max = d3;
	if (max < d4)
		max = d4;
	if (max < d5)
		max = d5;
	if (max < d6)
		max = d6;
	if (max < d7)
		max = d7;
	if (max < d8)
		max = d8;
	if (max < d9)
		max = d9;
	if (max < d10)
		max = d10;
	cout << max << endl;
	return max;
}

It's actually a little confusing in the hints that F is initiallized a very large number while it's just all right as long as it's initialized.

Thanks, everybody, especially lerner, he got the idea.

Looks like you are back to the age before the array. :d

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.