my task is to sort the input numbers from lowest to highest but i just cant make it work... i've tried different ways and I know there's a problem in my code. hope you can help me out.

thanks!

#include <iostream.h>

struct trial
{
	int age;
}

getcount[5];

void main()
{
	
	int i=0;
	int j = 1; 
	int x = 0;
	int temp;

	cout << "PLease enter 5 numbers:\n";

	for(i=0;i<5;i++)
	{

		cout << j << "number: ";
		cin  >> getcount[i].age;

		j = j+1;

		
	}
		


	for (i=0;i<5;i++)
	{
		temp = getcount[i].age;
	
		cout << "\n" << temp;

		/*if (x < temp)
		{
			x = temp;
			

			cout << "\n" << x;
		}*/
	
	}

}

if i try this still it wont sort

:'(

for (i=0;i<5;i++)
	{
	//	cout << getcount[i].age << "\n";


			if (getcount[i].age < getcount[i+1].age)
			{
				temp = getcount[i].age;
				getcount[i].age = getcount[i+1].age;
				getcount[i+1].age = temp;
			}
		}
	
		cout << "\n" << temp;

		for(i=0;i<5;i++)
		{
			cout << getcount[i].age << "\n";
		}

Your second post looks like you're trying to do Bubble sort. You have the comparison and the exchange OK, it's the looping that's lacking.

Look in your textbook, or do a quick internet search and you should find the basic algorithm for this. Post back what you do to improve this.

Comments
best source and to ask questions regarding programming!
//look at this code of sorting and understand how it works

// This program sorts an array's values into ascending order.
   #include <iostream>
   #include <iomanip>
   
  int main()
  {
     const int arraySize = 10; // size of array a
     int data[ arraySize ] = { 34, 56, 4, 10, 77, 51, 93, 30, 5, 52 };
     int insert; // temporary variable to hold element to insert

     cout << "Unsorted array:\n";

     // output original array
     for ( int i = 0; i < arraySize; i++ )
        cout << setw( 4 ) << data[ i ];

     // insertion sort
     // loop over the elements of the array
     for ( int next = 1; next < arraySize; next++ )
     {
        insert = data[ next ]; // store the value in the current element

        int moveItem = next; // initialize location to place element

        // search for the location in which to put the current element
       while ( ( moveItem > 0 ) && ( data[ moveItem - 1 ] > insert ) )
        {
           // shift element one slot to the right
           data[ moveItem ] = data[ moveItem - 1 ];
           moveItem--;
        } // end while

        data[ moveItem ] = insert; // place inserted element into the array
     } // end for

     cout << "\nSorted array:\n";

     // output sorted array
     for ( int i = 0; i < arraySize; i++ )
        cout << setw( 4 ) << data[ i ];
     cout << endl;
    return 0; // indicates successful termination
  } // end main



// RESULT
// Unsorted array:
//   34  56   4  10  77  51  93  30   5  52
// Sorted array:
//    4   5  10  30  34  51  52  56  77  93

// for deep learning about sorting algorithms learn
// http://www.eternallyconfuzzled.com/tuts/algorithms/jsw_tut_sorting.aspx

Comments
great help! thanks a lot

There are multiple sorting protocols. vmanes and sharensla mention two options, but there are more. Pick a type and research how it is done. Bubble sort is generally one of the more commonly used versions for beginners because it is relatively easy to implement.

Even more basic, however, is the incomplete declaration of getcount[], use of global variable when not indicated, use of void as return type for main() rather than int, and use of deprecated iostream.h instead of up to date iostream which doesn't use the .h extension, though it will requires accessing namespace std somehow.

:( i really dont get it... that's what they teach us in school... <iostream.h> :(


regarding sharensla's post there are some variables that i dont understand... :( like the setw

huhuhu....

If that's the instructional approach your school is using, then you don't have much choice, for right now at least. But it is out of date, and when you have completed your current instruction, then you should update your approach as well.

setw() is a stream manipulator that tells the compiler to output the data field 4 char wide and if output not that big, then pad with spaces as needed. It is a technique that can be useful to increase the readability output like creating columns, etc. It has no bearing on the sorting protocol as such.

hmmm... can anyone help me with this code? it is sorting ascending already but hmmm... with the for loop that i've used the starting point is 0 and not the first value. how can i refrain from doing that?

#include <iostream.h>

struct trial
{
	char name[10];
	char addy[6];
	int age,empid,sal;
}

getcount[5];

void main()
{
	
	int i=0;
	int j = 1; 
	int x;
	int temp;
	int surena;
	int choice;

mymenu:

	cout << "\n\n┬░Employee┬░";
	cout << "\n[1] Add Employee Record";
	cout << "\n[2] Browse";
	cout << "\n[3] Exit";
	cout << "\nPlease enter choice: ";
	cin  >> choice; 

	switch(choice)
	{
	case 1:
		{
			cout << "\n\nEnter Employee Id: ";
			cin  >> getcount[i].empid;

			cout << "Enter Employee Name: ";
			cin  >> getcount[i].name;
		
			cout << "Enter Address: ";
			cin  >> getcount[i].addy;
		
			cout << "Enter Age: ";
			cin  >> getcount[i].age;

			cout << "Enter Salary: ";
			cin  >> getcount[i].sal;

			cout << "\n\nPress enter to go back to main menu.\n\n";

			i = i + 1;		
			goto mymenu;

			break;
			
		}
	case 2:
		{
			cout << "\n\nBrowse\n";
			cout << "[1] Age\n";
			cout << "[2] Employee Id\n";
			cout << "[3] salary\n";
			cout << "Enter choice: ";
			cin  >> surena;


			switch (surena)
			{
			case 1:
	
				cout <<"Age  Emp Name  EmpID  Address  Salary\n\n";

				for (x=0;x<5;x++)
				{
					for (i=0;i<5;i++)
					{
						if (getcount[i].age > getcount[i+1].age)
						{
							temp = getcount[i].age;
							getcount[i].age =  getcount[i+1].age;
							getcount[i+1].age = temp;
						}
					}
				}

	
				for (i=0;i<6;i++)
				{
				
					cout << getcount[i].age << "\t";
					cout << getcount[i].name << "\t"; 
					cout << getcount[i].empid << "\t";
					cout << getcount[i].addy << "\t";
					cout << getcount[i].sal << "\t" <<"\n";
				
				}

			break;
		}
	case 3:
		{
			break;
		}

	}

		
	}
}

Probably, the most efficient and easy way is to use std::sort.

An example is here:
http://msdn.microsoft.com/en-us/library/ecdecxh1(VS.80).aspx

My best guess is that, for the time being, useless information follows:

There is a very short (but recursive version) of quicksort if you are interested in, or you are completely indifferent about the way of sorting.

If you have to use O(1) extra space, I would suggest heapsort.

Probably, the most efficient and easy way is to use std::sort.

An example is here:
http://msdn.microsoft.com/en-us/library/ecdecxh1(VS.80).aspx

My best guess is that, for the time being, useless information follows:

There is a very short (but recursive version) of quicksort if you are interested in, or you are completely indifferent about the way of sorting.

If you have to use O(1) extra space, I would suggest heapsort.

to be honest i'm just a newbie in C++ and that is how it is taught in my school... other way than that is something i dont know how to do... like the std... actually this is an assignment of mine that i have to pass tomorrow... but i just really dont know how to make it work.... tried different ways... followed its sequence but it just wont work... :( kinda losing hope...

the sorting that should suppose to happen is that when the user inputs values to the arrays sample:

when the user chooses 1
it will somehow save the data...

then if the user chooses 2 it will go to the browse selection so if the user chooses 1 which is the age

it will sort the data according to age and so on...

so if i input

emp num name add age salary
12345 lucky ph 24 1232
43234 dumb ph 12 4234
42343 me ph 19 2342

it will output like this if choose 1 of 2nd menu....

age empnum name add salary
12 43234 dumb ph 4234
19 42343 me ph 2342
24 12345 lucky ph 1232

so that how it should work... :(

It's becoming rather unclear what you're trying to do for option 1 of main option 2 - you call it browse, but you attempt to sort the data and then display all the data. If the purpose is to display sorted list, say so.

As you enter a record, you are updating a variable 'i', which is a count of the number of records. Good, so far. (Better if you named it something useful, like "count".)

When you sort the records, you need to use that as a limit on the loops, not necessarily the size of the array. If you've only entered 3 records, you don't want to sort 5. And you don't want to display 6 (That's one more than you allocated - not a good thing!)

So given that 'i' is the count of records, your loops for bubble sort should be

for ( x = 0; x < i - 1; x++ )
{
	for (y = 0; y < i - 1 - x; y++)
	{
		if ( getcount[y].age > getcount[y+1].age )
		{
			temp = getcount[y].age;
			getcount[y].age =  getcount[y+1].age;
			getcount[y+1].age = temp;
		}
	}
}

And a big OOPS - you are exchanging the age values only, which mixes up whose age is assigned to whom! You need to exchange full records. So, the temp variable should be

trial temp;

and then the exchange becomes

temp = getcount[y];
getcount[y] =  getcount[y+1];
getcount[y+1] = temp;

The use of the label mymenu: and goto's is something you should not get comfortable with. Use of goto is generally frowned upon, as it will lead to sloppy programming habits and lead you to create code that's hard to debug and maintain. You should be using a loop to control repetition of the main process.

for ( x = 0; x < i - 1; x++ )
{
	for (y = 0; y < i - 1 - x; y++)
	{

master, first of all i want to say thank you for helping me understand it more clearly... hmmm... i just want also to ask what does y < i - 1 - x means?

just a bit confused

and yah you're right the goto syntax is making me insane. I'm used to calling functions but i dont know why in c++ it wont work. i used that instead of goto or i guess it's just the same.. hahaha

thanks a bunchkie!

for ( x = 0; x < i - 1; x++ )
{
	for (y = 0; y < i - 1 - x; y++)
	{

master, first of all i want to say thank you for helping me understand it more clearly... hmmm... i just want also to ask what does y < i - 1 - x means?

Remember that i is the count of how many values in the array are valid. y is controlling which elements of the array we examine during this pass. y < i - 1 is the absolute limit of where we want to examine, because the comparison uses index [y+1]. So we must stop one short of the end of the data. Further limiting the loop by y < i - 1 - x makes the sort a tiny bit faster by never going back and reexamining the portion of the array that is already sorted. Leaving off the -x does not make the sort incorrect, just makes an already poor performing sort even worse.

(Note- the poor performance isn't really an issue on small data sets, say, under a few hundred items.)

temp = getcount[y];
getcount[y] =  getcount[y+1];
getcount[y+1] = temp;

master.... why is it this code wont work? :( it produces error... it says

C:\Documents and Settings\Administrator\My Documents\c++\testing.cpp(80) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'struct trial' (or there is no acceptable conversion)

thanks again for the big help!

You do have temp declared like this, right?

trial temp;

yes master, i declared that already but still it shows error. :(

Hmm. Please show the whole program as you have it now. There's more to this than meets the eye.

(and you can drop the "master" bit. Really. Unless you look like Barbara Eden did back in the 60s)

master! thank you! i found the error! i declared temp as int first then i had it trial temp; now i deleted the int temp and it works pretty fine!

thank you so much master!

mwah!

This question has already been answered. Start a new discussion instead.