2 Replies - 222 Views - Last Post: 04 February 2018 - 05:22 PM Rate Topic: -----

#1 cupcake007   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 04-February 18

Need help displaying sorted scores in descending order

Posted 04 February 2018 - 01:33 PM

My assignment is to create a program that takes inputs of names and scores from the user and then sorts the scores in descending order using arrays. I believe I have everything figured out except for getting my program to actually display the scores after they have been sorted.

I get no error messages, but when it is time to display the scores, the scores generate endlessly, all with the value of zero. They should stop generating after x number of scores.

Thanks so much.


using namespace std;

void initializeArray(string*, int*, int);
void sortData(string*, int*, int);
void displayData(const string*, const int*, int);

int main()

    int SIZE;

    string *name;
    name = new string[SIZE];
    int *score;
    score = new int[SIZE];

    initializeArray(name, score, SIZE);

    sortData(name, score, SIZE);

    displayData(name, score, SIZE);

void initializeArray(string names[], int scores[], int size)

    cout<<"how many scores will you enter? ";
    cin>> size;

    for(int count = 0; count<size; count++)
        cout<<"name number "<<count+1<<": ";
        cin>> names[size];
        cout<<"score number "<<count+1<<": ";
        cin>> *(scores + count);

void sortData(string names[], int scores[], int size)

    int temp;
    bool swap;
        swap = false;
        for(int count=0; count < (size-1); count++)  
            if(scores[count] > scores[count+1])
                temp = scores[count];
                scores[count] = scores[count+1];
                scores[count+1] = temp;
                swap = true;
    }while(swap);//while there is a bool swap
void displayData(const string names[], const int scores[], int size)
    for(int count = 0; count<size; count++)  
        cout<<"name "<<count<<": "<< scores[count]<<endl;

This post has been edited by ndc85430: 04 February 2018 - 01:34 PM
Reason for edit:: Fixed broken code tags.

Is This A Good Question/Topic? 0
  • +

Replies To: Need help displaying sorted scores in descending order

#2 jimblumberg   User is offline

  • member icon

Reputation: 5537
  • View blog
  • Posts: 17,143
  • Joined: 25-December 09

Re: Need help displaying sorted scores in descending order

Posted 04 February 2018 - 01:47 PM

What is the value held in SIZE when you try to dynamically allocate memory for those pointers?

In fact where do you ever assign a value to SIZE in main()?

By the way all caps is usually reserved for compile time constant variables.

Why are you passing SIZE into your initialization function? You don't use the value and you don't seem to return how many scores you entered so that you can use this value in your other functions.

Was This Post Helpful? 1
  • +
  • -

#3 tony jay   User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 50
  • Joined: 31-January 18

Re: Need help displaying sorted scores in descending order

Posted 04 February 2018 - 05:22 PM

Error messages form the compiler are the least of your problems!
In fact you would be far better off right now if the compiler was catching your numerous logic errors!
jimblumberg has highlighted a few but there are many more.

I completely get that you are probably a beginner - obviously if you knew what you were doing then you would not have posted this code as it is...

My suggestion is to revisit the design of your entire project here.
Things to consider:
Why are you using two arrays to capture your data?
(Are there some weird restrictions on what C++ constructs you can use?)
Would it not be better to create a struct that can contain both name and score?

Avoid using pointers if you can - this especially goes for function parameters, use references instead, and const references if that function does not, and should not, alter the data - function displayData is a case in point.

Also, arrays are pretty poor data structures to use in open code - especially in your case where the number of data entries is dynamic.
Think about using vectors instead.
It is easy enough to create a vector using a struct as its data type.
Vectors have a lot of advantages over C-style arrays - get familiar with them, and use them as you default data structure.

Because the name and score are linked by the struct all the logic that is needed in your program from , entering data, sorting it, and outputting the data is much easier compared to the illogical mess your program currently represents.

I am not a 30 year old veteran/guru in C++. In many respects I am probably not that far ahead of you.
What seems really hard right now will soon become pretty routine...
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1