Help me DMA and sorting strings

Hi, I am supposed to use Dynamic Memory Allocation to create the names and scores arrays, but I am not sure if I did it right or not. Also, I can sort the scores from highest to lowest, but the names will just appear as I have inputted them, anyone know how to sort the names? Thanks!
--------------------------------------------------------------------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <string>

using namespace std;

void sort(string name[],int a[], int size);
void display(string name[], int a[], int size);
void initializeData(string name[], int a[], int size);

int main (){
    
    int size;
    int *a = new int[size];
    string *name = new string[size];
    
    cout<<"How many scores will you enter?: ";
    cin>>size;
    
    initializeData(name, a, size);
    sort(name, a, size);
    display(name, a, size);
    
    return 0;
}

void sort(string name[],int a[], int size){
    for(int j=0; j<size; j++){
        for(int k=0; k<size; k++){
            if(a[k]<a[k+1]){
                int temp = a[k];
                a[k] = a[k+1];
                a[k+1] = temp;
            }
        }
    }
}

void display(string name[], int a[], int size){
    cout<<"Top Scorers: "<<endl;
    for(int l=0; l<size; l++){
        cout<<name[l]<<": "<<a[l]<<endl;
    }
}

void initializeData(string name[], int a[], int size){
    for(int i=0; i<size; i++) {
        
        cout<<"Enter the name for score#"<<i+1<<": ";
        cin>>name[i];
        cout<<"Enter the score for score#"<<i+1<<": ";
        cin>>a[i];
    }
    cout<<endl;
}


--------------------------------------------------------------------------------
Example code:

How many scores will you enter?: 3
Enter the name for score#1: John
Enter the score for score#1: 10
Enter the name for score#2: Kim
Enter the score for score#2: 29
Enter the name for score#3: Bob
Enter the score for score#3: 15

Top Scorers:
John: 29
Kim: 15
Bob: 10
--------------------------------------------------------------------------------

As you can see, the scores didn't correspond with the name.
Last edited on
You are sorting scores but not names.
BTW if you used code tags and some style of indentation there would no doubt be more people willing to help.
Topic archived. No new replies allowed.