Can someone help me with this assignment please


After the user inputs the variables, the program should ask if they want the numbers in ascending or descending order. Can someone guide me in the right direction, please? I know it should be something like: Cout << " Please select an order to sort by:";


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using namespace std;

void sortNum(int& num1, int& num2, int& num3);

int main ()

{
    int firstNum, secondNum, thirdNum;
    cout << "Input integer 1: ";
    cin >> firstNum;
    cout << "Input integer 2: ";
    cin >> secondNum;
    cout << "Input integer 3: ";
    cin >> thirdNum;
    
    cout << " Please select an order to sort by: /n";
    cout << "(A)scending order/n";
    cout << "(D)escending order/n";

 

 return 0;
} 
[/code]
Last edited on
This is not a homework site. We won't do your homework for you. However we are always willing to help solve problems you encountered, correct mistakes you made in your code and answer your questions.

We didn't see your attempts to solve this problem yourself and so we cannot correct mistakes you didn't made and answer questions you didn't ask. To get help you should do something yourself and get real problems with something. If your problem is "I don't understand a thing", then you should go back to basics and study again. As it is impossible to find derivative of function without knowledge in arithmetic, you cannot do more complex tasks in programming without clear understanding of basics
1
2
3
4
5
6
7
8
9
10
11
cout << " Please select an order to sort by: /n";
cout << "(A)scending order/n";
cout << "(D)escending order/n";
char order;
cin >> order;
if (order == 'A') {
  // print in ascending order
}
else {
  // print in descending order
}

Hello joser23,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

Your program deals with three numbers, but what would, you do if the requirements changed and asked you to handle 100 numbers. Three or 100 numbers the concept for dealing with either should be the same.

You need to rethink your program. I can see using for loops, if/else statements, vector and maybe a switch/case in the program. You could even put parts of the code in functions.

There are several possibilities of what to do all limited by what you know and/or what you are allowed to use.

Break the program into smaller pieces.

1. You might explain what is needed from the user.

2 Get the needed input.

3. Sort the numbers if needed.

4 Print in the correct order.

While thinking about these smaller pieces this could give you an idea of how to store the numbers.

Hope that helps,

Andy
Sorting three separate numbers can be done in a function, but that is messy.
Have you learned arrays yet?
As Handy Andy was suggesting, it is better to create an extensible solution.
Using arrays easily lends itself to an extensible solution.

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
#include <iostream>
using namespace std;

const int MAXNUM = 3;

void sortNum(int arr[], int sz)
{   //  Sort arr into ascending order
}

int main ()
{   int     num[MAXNUM];
    char    order;
    
    //  Input the numbers
    for (int i=0; i<MAXNUM; i++)
    {   cout << "Input integer " << i+1 << ": ";
        cin >> num[i]; 
    }        
    //  Sort the numbers
    sortNum (num, MAXNUM);
    
    cout << " Please select an order to sort by: /n";
    cout << "(A)scending order/n";
    cout << "(D)escending order/n";
    cin >> order;
    if (order == 'A') 
    {   //  print in ascending order
    }
    else 
    {   //  print in descending order
        //  Hint:  Use a descending for loop
    }
    return 0;
} 


Now, you can easily extend the program to sort any number of numbers simply by changing MAXNUM.

I managed to finish my program, pretty cool how it works haha. I've not learned arrays yet, but thanks for the suggestion. Thank you all for the help.
Topic archived. No new replies allowed.