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..

#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];

// bubble sort code

    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 array is:\n";
    cout << nums[size] << "\n";

    return 0;
}

Recommended Answers

All 5 Replies

It has nothing to do with your sort. You have nothing TO sort. You need to create a loop and read an element at a time into your array, THEN sort it.

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

The above lines would only accept 1 value....nums. Use a loop instead:

for(int i=0;i<size;i++){
cin>>nums[i];
}

wow that really did help!
Can you please also advise if I need to do something about maximum 50 values? Or double nums[50]; is enough?

My final code turned out to be this:

#include <iostream>

using namespace std;

int main()
{

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

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

    cout << "Please enter the values:\n";
    for (i=0; i<size; i++)
    cin >> nums[i];

    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";
    for (t=0; t<size; t++)
    cout << nums[t] << "\n";


    return 0;
}

Just going to throw this out there, but a lot of times, it is easier to sort as you add new elements, instead of waiting till after you add all your elements.

#include<iostream>

using namespace std;

void main()
{
    cout<<"You're a dick head!";



}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.