I'm very new to C++ and is currently taking an intro to C++ class. We've only covered up to Functions and how to use reference variables inside the function parameter.
One of the hw problem that was assigned was to write a void function that takes three parameters( num1, num2, num3) by reference and sorts their values into ascending order, so that num1 has the lowest, num2 the middle value, and num3 the highest value. For example, if user enters: 14, -4, 8, then the output should look like this:
-4
8
14
I've completed the program with a bunch of if/ else if statements but I was wondering if there was a more efficient way to sort the numbers. Bear in mind, we've only covered materials up to functions so I can't use any other new techniques that we haven't cover yet. Here is my code:
// This program will take three int parameters by reference and sorts their value into ascending order
//so that num1 has the lowest value, num2 has the middle value, and num3 has the highest value
#include <iostream>
using namespace std;
// declare function with reference parameter that with sort numbers
void sortNum(int &, int &, int &);
int main ()
{
int num1, num2, num3;
cout << "This program will ask you to enter 3 integer numbers and it will ";
cout << "sort the numbers into their ascending order. " << endl << endl;
// prompt user to enter 3 random integers
cout << "Enter the 3 integers that you wish to sort: " << endl;
cin >> num1 >> num2 >> num3;
cout << "\n" << "The ascending order for the 3 numbers you just entered are: " << endl;
// call the function sortNum
sortNum(num1,num2,num3);
}
// sortNum function definition
void sortNum (int &num1, int &num2, int &num3)
{
// declare the lowest, middle, and highest variables
int lowest, middle, highest;
You're not doing what the assignment asks. What it is asking you is to provide a function such that you may provide three arguments, which will be sorted in ascending order after the function is called. All the other stuff about getting numbers from the user and printing numbers to the screen does not belong in the sortNum function.
#include <iostream>
void sortNum(int& num1, int& num2, int& num3);
int main()
{
int firstNum, secondNum, thirdNum;
std::cout << "Enter first number: ";
std::cin >> firstNum;
std::cout << "Enter second number: ";
std::cin >> secondNum;
std::cout << "Enter third number: ";
std::cin >> thirdNum;
//call the function that you will define
sortNum(firstNum, secondNum, thirdNum);
//once this function returns, you should see the arguments you passed
//in have been reassigned in such a way that they are in ascending
//order, based on how you passed them in
std::cout << "Numbers in order: " << firstNum << ' ' << secondNum << ' ' << thirdNum << '\n';
}
void sortNum(int& num1, int& num2, int& num3)
{
//at the very beginning of this function, num1 num2 and num3 hold
//the values they were given at the time the function was called
//If the first number is bigger than the second, then they're not in
//ascending order. Swap them.
if(num1 > num2)
{
int temp = num1;
num1 = num2;
num2 = temp;
}
//do more swapping down here as appropriate to arrange the numbers
//...more code...etc...
//at the end, the numbers have been rearranged into ascending order.
//Since the numbers were passed by reference, the changes you make
//here will be seen by whoever called the function (in this case, main).
}
I had a feeling that I was doing it wrong. Even though the if/ else statements sorted the numbers. I knew there was a better way but I just couldn't see it. Thanks for clarifying the issue and providing the guidance!!!