Hi,

I am new to the forum. I am stuck on this assignment I have to do.
I think i have defined the array suitably enough, I just cant implement the sorting specified in the question.
Any help would be greatly appreciated.

Here is the question:

Create a program in which a user is asked for:
How many values (s)he wants to enter (maximum 50);
Asks for the values and stores them into an array of double.
Sorts the values in ascending order according to the following algorithm, where size is the number of doubles to be sorted:
for i=0..size-2
check all the values between position i and size-1 to find the smallest one
swap the smallest value and the one in position i
Prints the sorted array to the console.

#include <iostream>

using namespace std;

int main()
{
    //Declaring Variables
    int size;
    double i;

    //Title Screen
    cout << "Welcome to Number Sorter 1.0\n" << endl;
    cout << "This program sorts values in ascending order." << endl;
    cout << "First you will be asked how many values you wish to enter." << endl;
    cout << "Then you will be asked to enter those values." << endl;
    cout << "The program will then print the sorted array.\n" << endl;

    //Size Entry
    do {
    cout << "So, please enter the size of your array with the maximum being 50: ";
    cin >> size;
    } while (size > 50 || size < 0);

    //Value Entry
    double *arr = new double[size];
    cout << "\nNow fill your array by entering " << size << " values (press enter after each value): " << endl;

     for (int i=0; i < size; i++) {
      cin >> arr[i];
     }
     cout << "\nYou entered: " << endl;

     for (int i=0; i < size; i++)
      cout << arr[i] << " ";
     cout << endl;
     delete [] arr;

    return 0;
}

Sorts the values in ascending order according to the following algorithm, where size is the number of doubles to be sorted:
for i=0..size-2
check all the values between position i and size-1 to find the smallest one
swap the smallest value and the one in position i

That's a description of selection sort (wikipedia page). Your code looks fine aside from missing the sorting step, so now it's really just a matter of understanding selection sort and then implementing it.

Thanks for the reply Narue.

I shall try implementing the selection sort now i will post the results here in a little while.

Thanks Narue

Ok so i read that wiki and i am still not sure how to combine the code from there into mine with the specified algorithm. The problem is i just dont know where to start. If you could give just direction I will try to proceed. Thanks in advance

#include <iostream>

using namespace std;

int main()
{
    int size;
    double i;

    do {
        cout << "So, please enter the size of your array with the maximum being 50: ";
        cin >> size;
    } while (size > 50 || size < 0);

    double *arr = new double[size];
    
    cout << "\nNow fill your array by entering " << size << " values (press enter after each value): " << endl;

    for (int i=0; i < size; i++) {
        cin >> arr[i];
    }
    
    /*
        Selection sort algorithm
    */
    for (int i = 0; i < size - 1; i++) {
        // Find the index (min) of the smallest item
        // Swap arr[min] with arr[i]
    }
    
    cout << "\nYou entered: " << endl;

    for (int i=0; i < size; i++)
        cout << arr[i] << " ";
        
    cout << endl;
    
    delete [] arr;
}

I am sorry Narue, I simply do not have a clue how to do this. Is it possible for you do do a bit, I feel a bit guilty asking but i really cant do it. Thanks Narue

I truly am, eternally confuzzled

We're talking about four lines of code. I can't do any more without solving the problem for you, which won't accomplish anything. What have you tried so far?

Ok, I understand.

How do I find the index (min) of the smallest item?

How do I find the index (min) of the smallest item?

Use a loop to find the smallest item, and save its index as you go:

from i to size
    if a(i) < a(min)
        min = i
loop

Ok so something like this:

/*
        Selection sort algorithm
     */
    for (int i = 0; i < size - 1; i++) {

    if a(i) < a(min)
        min = i
    loop
        // Swap arr[min] with arr[i]
    }

except i get the error expected ( before a

Sorry Narue I have to go to work. "Il be back"

Ok so something like this:

/*
        Selection sort algorithm
     */
    for (int i = 0; i < size - 1; i++) {

    if a(i) < a(min)
        min = i
    loop
        // Swap arr[min] with arr[i]
    }

except i get the error expected ( before a

What I posted was pseudocode, not valid C++...

Narue,

Sorry I haven't been able to get back here sooner, I have been so busy.
Basically I just have too much college stuff to do at the moment to be able to adequately learn what needs to be done. The assignment is due tomorrow and i am still nowhere near to completing it.
Would it be too much to ask could you give me the code that I am missing?
I am also not sure what the difference is between pseudocode and valid c++.

Thanks for your help

Would it be too much to ask could you give me the code that I am missing?

Yes. Like I said, we're talking about four lines of code. If I gave you anything else, you'd be done.

I am also not sure what the difference is between pseudocode and valid c++.

Pseudocode doesn't compile while valid C++ does. But pseudocode is close enough to show the logic of an algorithm such that one can translate it into C++.

I realise that it would defeat the purpose of me learning something if you gave me the code, and I understand that you are reluctant to do so. I would be of the same opinion if I were in your position, however I am just trying to convey that this is a unique situation where I just need the 'answer', as I don't have time to study it.

I apologise if I am coming across as impatient or rude Narue, and I know I am taking a massive shortcut by just asking for the answer.

Thanks

I am just trying to convey that this is a unique situation where I just need the 'answer', as I don't have time to study it.

That's not unique at all. Students with poor time management skills panicking about bad grades are a dime a dozen. The way I see it, even if you fail the assignment you'll still learn a valuable lesson: don't expect other people to bail you out.

Now, if that attitude seems harsh, consider that in the real world if you don't get the job done, someone else will. All too often I've been in the unenviable position of picking up the slack of a coworker who didn't learn how to manage time. I'm not quite as forgiving about things that tend to affect me directly.

Far too harsh considering you know nothing of the circumstances.
And this isn't just some student looking to be bailed out.
And take a bit more care next time before you put it down to 'poor time-management skills' as a complete generalisation, as it isn't.
Seriously, chill out. This isn't yours or anybody else's work place.
I shouldn't need to explain to you that this is a community driven forum, and I have come here looking for an answer to a question that I simply can't do. Period.

Anyway, thanks for your time

Far too harsh considering you know nothing of the circumstances.

That's an unwarranted assumption. Though if you want to explain the circumstances, we'll see if I know nothing about them. I'm willing to apologize if you have a legitimate reason for failing to complete your assignment and then asking for the answer to be handed to you on a silver platter.

And this isn't just some student looking to be bailed out.

Prove it.

And take a bit more care next time before you put it down to 'poor time-management skills' as a complete generalisation, as it isn't.

Prove it. The way I see it, either you mismanaged your time, or you bit off more than you can chew. With both cases the fault is yours and my previous statements apply.

I shouldn't need to explain to you that this is a community driven forum, and I have come here looking for an answer to a question that I simply can't do. Period.

Sorry dude, you'll get no sympathy from me. I did all of your work for you except the extremely trivial step of taking obvious pseudocode and turning it into basic C++. If you truly can't accomplish such a simple feat, your remaining options are to give up programming, or cheat your way through to a degree. And I refuse to help you cheat.

Anyway, thanks for your time

Wasted time, apparently.

its all good it got it!

Hi,

I'm new to c++ trying to read as many bits of code as possible. I'm a bit confused by this piece of code.

double *arr = new double[size];

Why is there an asterix in front of arr? Is it something to do with a pointer? Please forgive my ignorance as I am only starting out on my programming journey.

Thanks for the help in advance.

Yes I believe a pointer. Sorry I don't know more....
Just starting myself

No worries. Thanks for the reply.

Why is there an asterix in front of arr? Is it something to do with a pointer?

Yes, that's it exactly. In this specific case, arr is a pointer to double. Memory is then allocated to the pointer such that it can be used as a simulated array of double. The reason a pointer is used instead of an array is because in C++ array sizes must be constant at compile time while this program requires the size to be input by the user at runtime.

Thanks for the reply Narue!

I've actually tried to solve the problem that was proposed in this thread. I've used some of phr33k code and then used bubble sort to sort the numbers. However when I run it it's not sorting the numbers properly. Not sure where I've went wrong.

#include <iostream>
#include <cstdlib>


using namespace std;

int main()
{
     //Declaring the variables
    int size;
    int a,b,t;

    cout << "\t\tValue Sorter\n" << "\n"; //This is the title
    cout << "First you will be asked how many values you wish to enter.\n";
    cout << "Then you will be asked to enter these values.\n";
    cout << "The program will then print the sorted array in ascending order.\n" << "\n";

    //this do loop will set the size of the array.
        do
        {
            cout << "Please enter the amount of values you want to sort (max 50): ";
            cin >> size;
        }while (size > 50 || size < 0);


    cout << "\nYou have chosen to enter " << size << " values.\n";

    //I will now look for the values to be stored in the array
    double *arr = new double[size];
    cout << "Please input each value (Press enter after each value):\n";

    for (t=0; t < size; t++)
    {
        cin >> arr[t];
    }

     cout << "\nYou entered: " << endl;

     for (int t=0; t < size; t++)
     {
        cout << arr[t] << " ";
     }

    for (a=1; a<size; a++)
        {
            for (b=size-1; b>=a; b--)
            {
                if (arr[b-1] > arr [b])
                /*which means if out of order
                exhange the vlaues*/
                t = arr[b-1];
                arr[b-1] = arr[b];
                arr [b] = t;
            }
        }

        //this will display the sorted array
        cout << "\nThe sorted values are:\n";
        for(t=0; t<size; t++)
        {
            cout << arr[b] << " ";
        }



return 0;
}

If you're using the output to determine if the sort works, that would be a big issue. The loop always prints the same value because you print arr[b] instead of arr[t] . ;)

Sorry me again :-D.

I've tidied the code up a little but still can't get the bubble sort to work.
My code is below. I'm still going to work away on it. Any help is much appreciated.

#include <iostream>
#include <cstdlib>


using namespace std;

int main()
{
     //Declaring the variables
    int size;
    int a,b,t;

    cout << "\t\tValue Sorter\n" << "\n"; //This is the title

    //this do loop will set the size of the array.
        do
        {
            cout << "Please enter the amount of values you want to sort (max 50): ";
            cin >> size;
        }while (size > 50 || size < 0);


    cout << "\nYou have chosen to enter " << size << " values.\n";

    //I will now look for the values to be stored in the array
    double *arr = new double[size];
    cout << "Please input each value (Press enter after each value):\n";

        for (t=0; t < size; t++)
        {
            cin >> arr[t];
        }

    cout << "\nYou entered: " << endl;

        for (int t=0; t < size; t++)
        {
            cout << arr[t] << " ";
        }

        for (a=0; a<size; a++)
        {
            for (b=size-1; b>=a; b--)
            {
                if (arr[b-1] > arr [b])
                /*which means if out of order
                exhange the vlaues*/
                t = arr[b-1];
                arr[b-1] = arr[b];
                arr [b] = t;
            }
        }

    //this will display the sorted array
    cout << "\nThe sorted values in ascending order are:\n";
        for(t=0; t<size; t++)
        {
            cout << arr[t] << " ";
        }

return 0;
}

Ok.

Fixed it! I like fixing problems :-D

The problem was in the piece of code below.

//this is the bubble sort
        for (a=1; a<size; a++)
        {
            for (b=size-1; b>=a; b--)
            {
                if (arr[b-1] > arr [b])
                {
                /*which means if out of order
                exhange the vlaues*/
                t = arr[b-1];
                arr[b-1] = arr[b];
                arr [b] = t;
                }
            }
        }

Firstly I changed the condition of the for loop to a=1 and then I put the parenthesis in for the if loop.

Think I solved it. Let me know. Thanks.

The only problem with the code is it won't sort numbers with decimal places. This is my code as it looks below. Really sorry about all the posts. I feel like I'm spamming the thread. Sorry.

#include <iostream>
#include <cstdlib>


using namespace std;

int main()
{
     //Declaring the variables
    int size;
    int a,b,t;

    cout << "\t\t\tValue Sorter\n" << "\n"; //This is the title.

    //this do loop will set the size of the array.
        do
        {
            cout << "Please enter the amount of values you want to sort (max 50): ";
            cin >> size;
        }while (size > 50 || size < 0);


    cout << "\nYou have chosen to enter " << size << " values.\n";

    //I will now look for the values to be stored in the array
    double *arr = new double[size];
    cout << "Please input each value (Press enter after each value):\n";

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

    cout << "\nYou entered: " << endl;

        for (int t=0; t < size; t++)
        {
            cout << arr[t] << " ";
        }

    cout << "\n";

        //this is the bubble sort
        for (int a=0; a<size; a++)
        {
            for (int b=size-1; b>=a; b--)
            {
                if (arr[b-1] > arr [b])
                {
                /*which means if out of order
                exhange the vlaues*/
                t = arr[b-1];
                arr[b-1] = arr[b];
                arr [b] = t;
                }
            }
        }

    //this will display the sorted array
    cout << "\nThe sorted values in ascending order are:\n";
        for(t=0; t<size; t++)
        {
            cout << arr[t] << " ";
        }

    cout << "\n";

return 0;
}

Only noticed ur reply there narue! Sorry! I get a bit rambly when I'm working on a problem nearly typing before I think :-D. I think I solved the issue apart from it not sorting decimal numbers. Any ideas about that? Thanks for the help. C++ is my first foray into the world of programming nd trying to do it on my own can be tough.

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.