Inputting values in array and outputting them in descending order

Hey guys,

I have been reading the forum for a while, but I couldn't find anything that could help to figure out my current assignment. Its a very simple program, but somehow just doesn't work for me. I'm a beginner and I hope that somebody can explain what am I doing wrong.


The program should as the user how many values she would like to enter (max 50). Then the program should ask to enter the values.
These values should be stored in double array and then printed out in descending order, using bubble sort.

I have got this code so far... Please advise if you can on what can I do to make it work.
I can only use very basic codding as well..

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
#include <iostream>

using namespace std;

int main()
{

    double nums[50];
    int size, a, b, t;

    cout << "How many values would you like to enter?\n";
    cin >> size;

    cout << "Please enter the values:\n";
    cin >> nums[size];

    for (a=1; a<size; a++)
        for (b=size-1; b>=a; b--) {
            if(nums[b-1] < nums[b]) {

                t=nums[b-1];
                nums[b-1] = nums[b];
                nums[b] = t;
                }
            }

    cout << "Sorted rray is:\n";
    cout << nums[size] << "\n";


    return 0;
}

I didn't look at the sorting code. Your input/outpus is wrong. nums[size] is the size'th element of nums array. You have to
1
2
3
4
for(int i = 0; i <size; i++){
   cout << "nums["<<i<<"] = ";
   cin >> nums[i];
}

similar thing for output.
Thank you, I fixed the input and it seems to be working fine now! :)

Can you please advise if there is anything that needs to be changed in the code, as the requirement in assignment was 50 values max that can be entered? Or nums[50] is enough for that?

Thank you
Topic archived. No new replies allowed.