1

This is relatively basic but I'm trying to write a function that sorts an array in C++ and I don't want to do it the regular way. I made a loop to find the greatest number in array, store it as "max", save max as the current value in another array and then change the value to zero so that by the time the loop runs the next time, the previous max number will no longer be the maximum, letting the next highest take its place but for some reason, this doesn't work. what did I do wrong?

#include <iostream>
using namespace std;

void mysort(int arr[10]){
    int max = 0;
    int high[10];
    int count;




    for (int j=0; j<10; ++j){

            for(int i=0; i<10; ++i){
            if (arr[i]> max){
            max= arr[i];

            count=i;

        }

        }
    cout<<"the "<< count+1<<" largest is: "<< max<<"\n";    
    high[j]= max;
    *arr[count]= 0;

    }
}
main(){

    int pass[10] = {1,2,3,4,5,6,7,8,9,0};
    mysort(pass);

}
7
  • 1
    Your resource for learning C++ is either very out of date, or just extremely poor. The int in int main() is not optional. Just like the void in void mysort. Commented Oct 26, 2017 at 8:42
  • what is the meaning of "doesnt work" ? When you are using 0 as placeholder you cannot have 0 in your input, otherwise you cannot distinguish between a placholder and a 0 input. Also <0 wont work, because then you would find 0 as max Commented Oct 26, 2017 at 8:42
  • That line *arr[count]= 0;, what do you think it does ? Commented Oct 26, 2017 at 8:42
  • I assume you did it as an exercise, in general I strongly suggest you use std::vector<type> instead of an array. Also, this type of sorting algorithm is very inefficient. Commented Oct 26, 2017 at 8:54
  • A slightly saner way of sorting is to move the last entry to sorted entry instead of resetting it and to decrement the number of entries in the unsorted array. Do pass the length of the array to the function if you don't want to use containers or begin/end iterators and make sure your result can be used afterwards.. Commented Oct 26, 2017 at 9:51

2 Answers 2

1

Your max never gets reset, so on first iteration it gets set to 9 and stays that for the rest of the execution

#include <iostream>
using namespace std;

void mysort(int arr[10]){
//    int max = 0; **this was the problem**
int high[10];
int count;




for (int j=0; j<10; ++j){
    int max = 0;
    for(int i=0; i<10; ++i)
    {
            if (arr[i]> max)
            {
                    max= arr[i];
                    count=i;

            }

    }
    cout<<"the "<< count+1<<" largest is: "<< max<<"\n";
    high[j]= max;
    arr[count]= 0;

    }
}

int main(){**you should also add return type to int main**

    int pass[10] = {1,2,3,4,5,6,7,8,9,0};
    mysort(pass);

}
Sign up to request clarification or add additional context in comments.

Comments

0

Your inner loop always set max to 9 on its final iteration, and your outer loop only ever reads max once the inner loop is completed, so high[j] will always be 9.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.