Please help me begin this question. I don't even know where to start

closed account (L6Cjy60M)



The largest value is -1


Last edited on
Start with a main function and read 3 integers into 3 variables called num1, num2, num2.
Create a variable max_value and set it to num1.
If num2 > max_value then max_value is num2.
Same for num3
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
#include<iostream>
using namespace std;


int main() {
	
	int x, y, z;

	cout << "Enter 3 numbers separated by spaces: ";

	cin >> x;
	cin >> y;
	cin >> z;

	cout << endl << "The largest number is ";

	if (x >= y && x >= z) {
		cout << x;
	}
	else if (y >= x && y >= z) {
		cout << y;
	}
	else {
		cout << z;
	}

	cout << "." << endl;

	
	return 0;
}
Last edited on
Yep, Manga's answer is straightforward and fulfills the requirement of conditionals. As a side note, the compiler will nicely optimize if you use the built-in std::max -- something like:
std::max(x, std::max(y, z));
I must admit, I haven't a clue why this works, but it seems to.

1
2
3
4
5
6
7
8
9
10
11
12
#include<iostream>
#include<algorithm>
using namespace std;

int main()
{
	int x, y, z;

	cout << "Enter 3 numbers separated by spaces: ";
	cin >> x >> y >> z;
	cout << "The largest number is " << max( { x, y, z } );
}
closed account (E0p9LyTq)
I haven't a clue why this works

Third (or fourth) version of the std::max template(s), uses an initializer list:
http://en.cppreference.com/w/cpp/algorithm/max
Last edited on
Ah, thank-you @FurryGuy!

This might actually be a surprisingly useful construct to remember.
closed account (E0p9LyTq)
@lastchance,

std::min and std::minmax look versatile and really useful. As do std::min_element, std::max_element and std::minmax_element.

I too learned something useful I didn't know about the <algorithm> library with those templates. :)

Better than the Windows API min/max "functions" (macros actually) for sure.
Yes, I've just been looking all through the <algorithm> library, too! So far, it seems to be just the min and max-related items.

These initialiser-list constructors (which came in with C++11) have some decidedly useful hidden features. The OP will never know how useful I found this thread!
@shellylohia0809, Please don't delete your post.

For the record this was the original version

The program asks user to enter 3 integer-valued inputs x, y, z separated by spaces. Then the code compares the values of x, y, z and print the largest value. Use either "if" statements or conditional operators or both. Do not use loop and array.

Here are a few samples:

Enter 3 numbers separated by spaces: 5 4 7

The largest value is 7

------------------

Enter 3 numbers separated by spaces: 5 4 4

The largest value is 5

------------------

Enter 3 numbers separated by spaces: 4 4 4

The largest value is 4

------------------

Enter 3 numbers separated by spaces: -4 -5 -1

The largest value is -1

closed account (E0p9LyTq)
Manga's answer doesn't work.
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
#include <iostream>

int main()
{
   std::cout << "Enter 3 numbers separated by spaces: ";

   int x;
   int y;
   int z;
   std::cin >> x >> y >> z;

   std::cout << "\nThe largest number is ";

   if (x >= y && x >= z)
   {
      std::cout << x;
   }
   else if (y >= x && y >= z)
   {
      std::cout << y;
   }
   else
   {
      std::cout << z;
   }

   std::cout << ".\n";
}

Enter 3 numbers separated by spaces: 1 6 2

The largest number is 6.
Last edited on
@FurryGuy...

What do you mean it did not work?

Just tested it with 1 6 2, it worked fine for me.

Although, I admit your clean up of my code does look more eloquent. :)
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>

#define far long
#define in_a_galaxy
#define there_lived_a
#define _____ cout<<guy.

using namespace std;

struct furry 
{
    string who_hated(string s) { return "for no reason.\n"; }
};

int main() 
{
    long long ago;
    in_a_galaxy far far away;
    there_lived_a furry guy;
    _____ who_hated("using namespace std");
}

for no reason.


;P

https://repl.it/repls/SecretGrimPrograms
Last edited on
closed account (E0p9LyTq)
@Manga,

I wasn't the one who said your code didn't work, that was the now closed account OP who said it.

The sneaky so-and-so even deleted all their posts to hide.

Must have been someone wanting to get free tutoring help for their homework, and not let the instructor/fellow classmates know they were cheating.
@FurryGuy

Oh I see... Pardon my confusion.

@icy1

(laughing!) Programing insults... This is why I love coders. Let us all solemnly swear we are up to no good.
Topic archived. No new replies allowed.