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;
}

Dani AI

Generated

A few small but critical fixes turn the posted program into a working, robust solution. The original code only reads a single slot (cin >> nums[size]), so there is nothing meaningful to sort; the temporary swap variable was declared as int which will truncate double values; and the requested count is never validated (so negative or >50 values can corrupt memory). correctly pointed out the need for a per-element input loop, and was right that there was “nothing to sort.” ’s suggestion to keep the array sorted on insert is a valid alternative (insertion-style logic) and worth considering for small, incremental inputs.

A simple, safer bubble-sort example (descending) with input-validation and correct swap type:

#include <iostream>
#include <limits>

int main() {
    const int MAX = 50;
    double arr[MAX];
    int n;

    std::cout << "How many values (1-" << MAX << ")? ";
    if (!(std::cin >> n) || n < 1 || n > MAX) {
        std::cerr << "Invalid count.\n";
        return 1;
    }

    std::cout << "Enter " << n << " numbers:\n";
    for (int i = 0; i < n; ++i) {
        while (!(std::cin >> arr[i])) {
            std::cin.clear();
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            std::cout << "Please enter a valid number: ";
        }
    }

    for (int i = 0; i < n - 1; ++i) {
        bool swapped = false;
        for (int j = 0; j < n - 1 - i; ++j) {
            if (arr[j] < arr[j + 1]) {
                double tmp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = tmp;
                swapped = true;
            }
        }
        if (!swapped) break;
    }

    std::cout << "Sorted (descending):\n";
    for (int i = 0; i < n; ++i) std::cout << arr[i] << '\n';
    return 0;
}

Troubleshooting notes: always validate the requested size (1..MAX), use a double temp for swapping, print every element in a loop, and handle invalid numeric input. For larger or unknown sizes prefer std::vector<double> and std::sort with a descending comparator, or implement insertion-on-insert if maintaining a sorted list during input is desired.

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.