Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
This program is supposed to get an array( getvalue func. ) and then sort it( insertion_sort func. ), it works somehow, but it doesn't show me the result of insertion sort.

C++
//. Get values.
void getvalues(int arr[], int i)
{	
	cout<<" getvalues... ";
	int num=0;	
	while ( true )
	{
		cout<<"please enter element "<<i<<" ::: ";
		cin>>num;getchar();
		if ( num < 0 ) break;
		arr[i]=num;
		for(int j=0; j<=i; j++)
			cout<< arr[j];		
		i++;
		cout<<endl;
	}
	for(int j=0; j<i; j++)
	    cout<< arr[j];	
	getchar();
}
//.Insertion Sort.
void insertion_sort(int arr[],int length)
{
	cout<<" insertion_sort... ";
	int key = 0;
	int i=0;
	for( int j=2; j<=length; j++){
		key = arr[j];
		i = j-1;
		while (i>0 && arr[i]>key){
			arr[i+1] = arr[i];
			i=i-1;
		}
		arr[i+1] = key;
	}
}
//. Main.
int main()
{
	int* arr=new int[];
	int i=0;
	getvalues(arr,i);
	insertion_sort(arr,i);
	for(int j=0; j<i; j++)
		cout<<arr[j];
	getchar();
}


I can't find out what the problem is.
How should i fix it?
Thanks,
Posted

This code won't compile.

Your first line in the main function is broken: you cannot allocate an array without giving a size.

Secondly, your getvalues function is severly broken: You pass an array and some integer value that you modify inside the function and obviously assume that this change is visible outside the function. It is not. As a result, you pass 0 to the sorting which will not sort anything (assuming you fix the first problem above).

Your code is plain C code (beside using cin/cout), not C++ code. Use the means of C++ for your own benefit. E.g.
C++
int getvalues(std::vector<int> &items)
{
   int item = 0;
   while(item >= 0)
   {
      std::cin >> item;
      if (item >= 0) items.push_back(item);
   }
   return items.size();
}

int main()
{
    std::vector<int> items;
    if (getvalues(items)) std::sort(items.begin(), items.end());
    // do somethnig on the sorted items
    //...
}

Cheers
Andi
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 26-Jul-14 16:31pm    
5ed.
—SA
Andreas Gieriet 26-Jul-14 16:40pm    
Thanks for your 5, Sergey!
Cheers
Andi
m.r.m.40 27-Jul-14 4:38am    
Well thanks a lot andi,

i used these codes:

#include vector
push_back();
pop_back();
back();
vector[i];

vector reminds the stack data structure. is it right?

and you were right calling arrays wouldn't work, thanks for that.

i wish this ranking here had something more than 5.
Andreas Gieriet 27-Jul-14 12:26pm    
I read in another comment from you that you implement sorting yourself. This is fine to learn, but not worth the effort to use afterwards - sorting is "invented".
BTW: For inserting/removing elements other than at the back of the container, vector would not be the first choice. Remember, sorting is "expensive" action at runtime, so, chosing the right container might be crucial. In the example above, since the data is little, I chose vector for convenience only, not for performance.
To your storage question: the vector stores copies of your data.
Cheers
Andi
m.r.m.40 27-Jul-14 12:41pm    
I got it, thanks.
To get you started, concentrate on the lines
void getvalues(int arr[], int i)
and
getvalues(arr,i);
If you debug as far as the call to getvalues, look at the value of i when you return.

You might find this learning resource useful in fixing the problem cpp-tutorial 73[^]
 
Share this answer
 
Comments
Andreas Gieriet 26-Jul-14 15:25pm    
My 5, especially for the tutorial part!
Cheers
Andi
CHill60 26-Jul-14 17:52pm    
Thanks! I originally thought it was homework so I didn't want to give the OP too much, however they claim its not, so I like the extra detail in your solution
m.r.m.40 27-Jul-14 2:03am    
Thanks for the tutorial, i got it.
And this is not homework, this is self-study.
I'm starting with implementing Sorting algorithms.
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!
 
Share this answer
 
Comments
m.r.m.40 26-Jul-14 13:52pm    
This is not homework,
Andreas Gieriet 26-Jul-14 15:27pm    
Hm, it very much looks like homework. Do you do self-study? In that case, I strongly suggest to follow the advise from solution#2 to work through a C++ tutorial.
Cheers
Andi
m.r.m.40 26-Jul-14 15:36pm    
Thanks andi, actually i'm following it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900