I need to write a function for the following code that passes two values into it from the following code. These are my instructions for the function:
Use an IF – ELSE – IF – ELSE statement to compare the two numbers and then display one of three messages:
The two numbers are equal.
The first number _ was larger than the second number _.
The second number _ was larger than the first number _.
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
//Declaration Block
int firstNum;
int secondNum;
//User Input Block
cout << "Please enter a number: ";
cin >> firstNum;
cout << "Please enter a second number: ";
cin >> secondNum;
//Processing Block
if (firstNum == secondNum)
{
cout << "The two numbers are equal ";
}
elseif (firstNum >= secondNum)
{
cout << "The first number " << firstNum << "was larger than the second number " << secondNum << endl;
}
elseif (secondNum >= firstNum)
{
cout << "The second number " << secondNum << "was larger than the first number " << firstNum << endl;
}
system("pause");
return 0;
}
// prototype on top of main (prototype always have a semicolon, and prototypes declare the function (step 1)
void largeandsmall( int x, int y);
// call is
int main()
{
largeandsmall(firstNum, secondNum);
}
//definition under main (definition always have { }(step 2)
void largeandsmall( int x, int y)
{
if (firstNum == secondNum)
{
cout << "The two numbers are equal ";
}
elseif (firstNum > secondNum)
{
cout << "The first number " << firstNum << "was larger than the second number " << secondNum << endl;
}
else
{
cout << "The second number " << secondNum << "was larger than the first number " << firstNum << endl;
}
}
//call this after your user input (function call in main() ( step 3))
// firstNum is assigned to x in the function
//secondNum is assigned to y in the function
//you use firstNum and secondNum instead of x and y is because
//the function uses x and y
//your main uses firstNum and secondNum
// your user inputs and a number you put the numbers into the function by calling it.
remove the = from the last 2 else
you only need
if (equal numbers)
else if then (first is larger)
else to the remaining ont (second is larger)
It's just a basic programming and logic class, not a exact c plus plus class, that's just the language we're using as a base. The first response helped the most
small unreadable code that does the same thing is not improved.
big, bloated code that does too much is not improved.
You want readable code that resolves the issue without wasted work. Modern program sacrifices wasted work for re-use and readability more often than not.