GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 23-Jun-2005, 23:47
zidanefreak01 zidanefreak01 is offline
Invalid Email Address
 
Join Date: Jun 2005
Posts: 12
zidanefreak01 is on a distinguished road
Question

sorting numbers through array


i have problem with sorting numbers through array. pls help me...

requirements:

1. the user will input an array of 10 numbers.
2. a menu will appear after the input has done. and you will choose either the numbers will appear in ascending or descending order.

here's the code and i don't know if its correct
CPP / C++ / C Code:
for (ctr=1;ctr<=9;ctr++)
	{
		cout<<"Enter No."<<ctr<<": ";
		cin>>x[ctr];
	}

	cout<<"\n\n\n";

	cout<<"M E N U\n";
	cout<<"[a] ascending\n";//sorts the numbers in ascending order
	cout<<"[d] descending\n";//sorts the numbers in descending order
	cout<<"[x] exit\n";//exit the program
	cout<<"Enter Your Choice: ";
	cin>>choice;

	if (choice=='a' || choice=='A')
	{
		cout<<"the ascending order is:";
		sort(x[ctr],maxsize);// there was an error message that told me  ' sort ' : function does not take two parameters
	}
	else if (choice=='d' || choice=='D')
	{
		cout<<"the descending order is:";
	}
	else
		cout<<"the choice you entered is not valid";

}
void sort(int arr[],int size) 
{ 
     int last = size - 2; 
     int changes = 1; 

     while (last >= 0 && changes) 
     { 
           changes = 0; 

           for (int k = 0; k <= last; k++) 
                 if (arr[k] < arr[k+1]) 
                 { 
                       swap(arr[k],arr[k+1]); // there was an error message that told me ' swap ' : function does not take two parameters

                       changes = 1; 
                 } 
           last--; 
     } 
}
void swap(int& x,int& y)
{
	int temp;
	temp=x;
	x=y;
	y=temp;
}
  #2  
Old 24-Jun-2005, 06:15
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 961
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Quote:
Originally Posted by zidanefreak01
CPP / C++ / C Code:
	if (choice=='a' || choice=='A')
	{
		cout<<"the ascending order is:";
		sort(x[ctr], maxsize);// there was an error message that told me  ' sort ' : function does not take two parameters
	}

You call the sort function with the second parameter being an int (I think; maxsize is an int, right?). But your function expects an address to an int:

CPP / C++ / C Code:
void swap(int& x,int& y)

That is the problem. When you call it the second time, you won't get an error, because that is correct (syntactically). Decide what you are trying to do, and then perform the appropriate changes.

Kind regards,
Lucian
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #3  
Old 26-Jun-2005, 02:38
maprich maprich is offline
Member
 
Join Date: May 2005
Posts: 163
maprich has a spectacular aura aboutmaprich has a spectacular aura about
The problem with the original code is probably as simple as this:
Quote:
Originally Posted by zidanefreak01
CPP / C++ / C Code:
  if (choice=='a' || choice=='A')
  {
    cout<<"the ascending order is:";
    sort(x[ctr],maxsize);  // there... zip
  }
and
CPP / C++ / C Code:
void sort(int arr[],int size) 
{
now you can see that in the function body the arguments are of types (int[],int) while in the function call (s)he uses (int,int) as arguments...
seems that the "x[ctr]" is merely a typing accident and if written simply "x" the whole problem goes away.

But when I am writing this the topic is already 2 days old and probably expired already. This forum is just too quiet I guess.
  #4  
Old 26-Jun-2005, 03:41
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 961
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Quote:
Originally Posted by maprich
now you can see that in the function body the arguments are of types (int[],int) while in the function call (s)he uses (int,int) as arguments...
seems that the "x[ctr]" is merely a typing accident and if written simply "x" the whole problem goes away.

Man, did I mess up!

I was in such a rush, I misquoted the function headers, and got it all wrong. Thanks, maprich for correcting my mistake.

Best regards,
Lucian
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
 
 

Recent GIDBlogWriting a book by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with an array using pointers glulu76 C++ Forum 1 01-May-2005 12:21
duplicate numbers in array? njypa C++ Forum 1 27-Feb-2005 17:08
template comiling problems - need expert debugger! crq C++ Forum 1 01-Feb-2005 22:26
Sorting a 2D array c++ mike3340 C++ Forum 4 15-Dec-2003 14:35

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:23.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.