Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
Everything is going right in program but the arrays are not getting sorted

What I have tried:

C++
#include <iostream>

using namespace std;
int price;
int n;
int* p_array=NULL;
int* p_matrix=NULL;

void swap(int *i,int *k)
{
    int j;// js any temperory element
    *i=*k;
    *k=j;
}
void sort(int _array[],int n )
{
    int hole;
    int i;
    hole = i;
    for(int hole=1;hole<n;hole++)>
    {
        if( hole>0 && _array[hole-1]>_array[hole])
        {
            swap(_array[hole],_array[hole-1]);
            hole=hole-1;
        }
    }
}


int main()
{
    p_array=new int[n];
    p_matrix=new int[n];
    cin >> n;
    for(int i=0;i<n;i++)>
    {
        cin >> *(p_array+i) ;
    }
    sort (p_array,n);
    for(int i=0;i<n;i++)>
    {
        *(p_matrix+i)=(*(p_array+i))*(n-i);
    }
    sort(p_matrix,n);
    for(int i=0;i<n;i++)>
    {
         cout << *(p_matrix+i)<< " " ;
         cout << endl;
         cout << *(p_array+i)<< " " ;
    }
}
Posted
Updated 25-Jun-16 4:40am
v4
Comments
Andreas Gieriet 25-Jun-16 7:05am    
Compile it with all warnings on!
E.g. n is not set but you create an array with n elements...
Why do you use pointer arithmetic when you could do it with far less pain with array indices? E.g. cin >> p_array[i];
Cheers
Andi
Philippe Mori 25-Jun-16 9:08am    
- Don't use global variables.
- Initialize your variables when you declare them if initial value is known. Otherwise, declare the variable just before it initialization.
- Use appropriate spacing between operators.
- Do not start a name with an underscore (reserved for system use).
- Delete memory you allocate. Better yet, use std::vector instead of raw array.
- Prefer array notation to pointer notation.
- Use consistent spacing between functions (always 1 line or always 2 lines).

By the way, if you would have written a unit test for your swap function, you would have seen that it does not works as expected. The by using a debugger, you would have seen the problem. But for learning purpose, it is probably best to use pencil and paper.

Indentation helps a lot when you look at code: it means you can see what is goign on a lot more clearly...
But a quick glance says that the swap function:
C#
void swap(int *i,int *k)
{int j;// js any temperory element
*i=*k;
*k=j;
}
Is not going to work!
What value do you put in k each time?
Try changing it to:
C#
void swap(int *i,int *k)
    {
    int j;
    j = *i;
    *i = *k;
    *k = j;
    }
And that at least will work better!

In future, use the debugger: it would have shown you that problem in seconds, instead of the minutes it took you to ask here, then the time you had to wait for us to see the question, then the time for you to get an email that there is an answer...
 
Share this answer
 
Comments
Member 12566145 25-Jun-16 7:15am    
still NOT working :(
Michael_Davies 25-Jun-16 7:26am    
Please show the output that would help to see what is wrong, the swap function Griff shows you should work fine so there is something else in your code, why do you hole - 1 if you swap?
OriginalGriff 25-Jun-16 7:58am    
So use the debugger, and follow your code through.
It should be fairly obvious if you work out what you expect to happen, then compare that to what does happen when you single step each line.
Member 12566145 25-Jun-16 8:12am    
THANK YOU there were 2 mistakes
1 of swap function
2 of hole=hole-1 (instead hole=0 must be there)
OriginalGriff 25-Jun-16 8:25am    
You're welcome!
It's well worth playing with the debugger on little projects like this - it's a tool which will save you significant amounts of time and hair pulling when you move to more complex projects.
C++
p_array=new int[n];
p_matrix=new int[n];
cin >> n;

That will never work how you expect. You allocate two arrays based on the value of variable n before you have assigned a value to it. You need to go through your code carefully to check the logic.
 
Share this answer
 
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger allow you to see which part of your program conforms to your expectations, and which part don't. This allow you to narrow the research of bugs.
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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