0
# include <iostream>
using namespace std;

void init(int *Ptr, const int &s);
void display(int *Ptr, const int &s);
void sort(int *aPtr, int s);
void swap(int *v1Ptr, int *v2Ptr);
int index_of_next_smallest(int *aPtr, int *SiPtr, int s);


int main()
{
    int size, count;
    int *nPtr;
    
    cout << "Enter the size: ";
    cin >> size;
    
    nPtr = new int[size];
    
    
    init(nPtr, size);
    display(nPtr, size);
    
    sort(nPtr, size);
    
    cout << endl;
    display(nPtr, size);
    
    char ch;
    cin >> ch;
}
///////////////////////////////////////////
void init(int *Ptr, const int &s)
{
     for(int x=0; x<s; x++)
     {
        *Ptr++=rand()%101;
     }
}        
///////////////////////////////////////////
void display(int *Ptr, const int &s)
{
     int x=0;
     while(x<s)
     {
        cout << *Ptr++ << " ";
        x++;
     }
}
////////////////////////////////////////////
void sort(int *aPtr, int s)
{
     int *ionsPtr;
     int *indexPtr=aPtr;
     for(int index=0; index<s-1; index++)
     {
      ionsPtr=index_of_smallest(aPtr, indexPtr, s);
      swap(indexPtr, ionsPtr);
      indexPtr++;
     }
}     
////////// /////////////////////////////////
void swap(int *v1Ptr, int *v2Ptr)
{
     int temp;
     temp=*v2Ptr;
     *v2Ptr=*v1Ptr;
     *v1Ptr=temp;
}
/////////////////////////////////////////////
int index_of_next_smallest(int *aPtr, int *SiPtr, int s)
{
     int min=indexPtr, index_of_min=indexPtr;
     for(int index=indexPtr+1; index<s; index++)
        if(indexPtr<min)
        {
           min=indexPtr;
           index_of_min=indexPtr;
        }
        
     return index_of_min;           
}

this is my code and i have a little compile error that i cant seem to fix if someone can help me that would be appreciate

3
Contributors
7
Replies
8
Views
7 Years
Discussion Span
Last Post by Fbody
Featured Replies
  • 1

    I would have to so some more-thorough tracing, but at first blush you appear to be attempting to swap elements with themselves rather than with different elements. The net result would be no change. I suggest you take a closer look at your sort and index_of..... functions. You may want … Read More

0

What's the error? No one can help if they don't know what the error is or where to start...

0

I'd say the error is a failure to pay attention. You use a different name for the index_of_next_smallest function when calling it, and neglected to declare indexPtr everywhere it's used

0

hey guys im putting my code back cuz i have another problem my numbers wont swap places. if someone can help me that would be great cuz it is due tomarrow

# include <iostream>
using namespace std;

void init(int *Ptr, const int &s);
void display(int *Ptr, const int &s);
void sort(int *aPtr, int s);
void swap(int *v1Ptr, int *v2Ptr);
int * index_of_next_smallest(int *aPtr, int *SiPtr, int s);


int main()
{
    int size, count;
    int *nPtr;
    
    cout << "Enter the size: ";
    cin >> size;
    
    nPtr = new int[size];
    
    
    init(nPtr, size);
    display(nPtr, size);
    
    sort(nPtr, size);
    
    cout << endl;
    display(nPtr, size);
    
    char ch;
    cin >> ch;
}
///////////////////////////////////////////
void init(int *Ptr, const int &s)
{
     for(int x=0; x<s; x++)
     {
        *Ptr++=rand()%101;
     }
}        
///////////////////////////////////////////
void display(int *Ptr, const int &s)
{
     int x=0;
     while(x<s)
     {
        cout << *Ptr++ << " ";
        x++;
     }
}
////////////////////////////////////////////
void sort(int *aPtr, int s)
{
     int *ionsPtr;
     int *indexPtr=aPtr;
     for(int index=0; index<s-1; index++)
     {
      ionsPtr=index_of_next_smallest(aPtr, indexPtr, s);
      swap(indexPtr, ionsPtr);
      indexPtr++;
     }
}     
////////// /////////////////////////////////
void swap(int *v1Ptr, int *v2Ptr)
{
     int temp;
     temp=*v2Ptr;
     *v2Ptr=*v1Ptr;
     *v1Ptr=temp;
}
/////////////////////////////////////////////
int * index_of_next_smallest(int *aPtr, int *indexPtr, int s)
{ 
     int min=*indexPtr, index_of_min=*indexPtr; //these need to be defined as pointers
     for(int index=*indexPtr+1; index<s; index++)
        if(index<min)
        {
           min=index;
           index_of_min=index;
        }
        
     return indexPtr;           
}
1

I would have to so some more-thorough tracing, but at first blush you appear to be attempting to swap elements with themselves rather than with different elements. The net result would be no change.

I suggest you take a closer look at your sort and index_of..... functions.

You may want to look at your sorting algorithm too. Your algorithm seems a little odd to me. This may help.

Edited by Fbody: n/a

Comments
great link! Bookmarked
0

ok so is there a way to fix it and can u help me do that please

0

it help if i was using different notation but im using poiner notation so im at a loss of wat to do

0

it help if i was using different notation but im using poiner notation so im at a loss of wat to do

Start hand tracing your code. You should see what is happening if you pay attention to it.

I've run it through a debugger and that is in fact the case. You'll have to take a closer look at your index_of_next_smallest function and see what you can do differently. You seem to be attempting some sort of pointer arithmetic, but I'm not positive. In addition, I believe you have assigned incorrect values to min and s.

I do not have the time to correct this for you. Plus, I'm not really supposed to per the forum rules.

Edited by Fbody: n/a

This article has been dead for over six months. Start a new discussion instead.
Be sure to adhere to our posting rules.