an easy program.

I created an easy program,but i want to know if i should make it better,and if can i how?

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

int main()
{
int a,b,c;
cout<<"Give the numbers:";
cin>>a>>b>>c;
cout<<endl;

if(a>b && a>c)
{
    cout<<"a is bigger";
}
if(b>a && b>c)
{
    cout<<"b is bigger";
}
if(c>b && c>a)
{
    cout<<"c is bigger";
}
}
You could improve the indentation. It makes the program easier to read.
You could add error checks. What should happen if the input of a, b and c fails?
Your program is invalid! If a and b are equal for example to 10 and c is less than a and b and equal for example to 5 your program outputs nothing!:)
Last edited on
OK,if you can send me your changes to see in a code.
i made it,so what else?
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
#include <iostream>
using namespace std;

int main()
{
int a,b,c;
cout<<"Give the numbers:";
cin>>a>>b>>c;
cout<<endl;

if(a>b && a>c)
{
    cout<<"a is bigger";
}
else if(b>a && b>c)
{
    cout<<"b is bigger";
}
else if(c>b && c>a)
{
    cout<<"c is bigger";
}
else{
cout<<"nothing";
}
}


I mean,if there is any keyword that know which number is bigger,so not use if.is there?

(like it is the {rand()} for random number).
Last edited on
@vlad from moscow: I don't see why that makes it invalid. It was supposed to return the biggest element, and there is no one.

@OP: you may use an array to store the numbers and then just std::max_element( begin(array), end(array) );
However If there are several with the max value it will output the first one.
@ne555
@vlad from moscow: I don't see why that makes it invalid. It was supposed to return the biggest element, and there is no one.


The program returns nothing. It reports whether there is the biggest number. But if a user does not get the answer he will be confused.
He does get the answer. The program ends with no output, so there wasn't an element that would be biggest.

Suppose a program that diff two files, ¿what would be the output if the two files are identical?
No output - is it an answer?! If you ask somebody question and that somebody has said nothing you will think that you have gotten the answer?! And what about if that somebody is simply deaf?!
Last edited on
No output - is it an answer?
Yep. Another example, the "everything's OK" alarm.
In my opinion absense of a message confuses the user because he does not know whether the program executed successfully or it did something wrong. It is a situation of uncertainty.
If your program terminates with error, then it will return an error code.
It is only your fantasiy.
Hmm.. The eternal debate rages on.

With vlad-my-world-is-windows pitted against ne555-i-use-unix in this instalment.

http://flylib.com/books/en/2.506.1.42/1/

Btw, borges-my-world-is-unix would much rather have programs (or for that matter people) who have nothing to say, to actually say nothing. Not shout 'Now listen to me carefully: I have nothing to say'
In my opinion, I think skarla (44) 's program is asking users to input three int numbers and then output the biggest one of them, am I right ?
so you wanna ask if there is a better way to get the biggest one , how to optimize the program, maybe you can use (x>y?x:y) to optimize it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*********************************
题目:输入三个整数,然后求出最大的那个并且输出它的值,
输入:三个整型数字;
输出:三个整数中最大的一个;
**********************************/

#include <iostream>
using namespace std;
void main()
{
	//变量定义
	int a,b,c;
	//输出要用户输入的信息
	cout<<"Give the 3 numbers separated with space :"<<endl;
	
	cin>>a>>b>>c;
	cout<<endl;
	int m = (  c >= (a >= b ? a : b) ? c : ( a >= b ? a : b ) );
	cout<<"the biggest one is :"<<m<<endl;
	system("pause");
}


this is my first reply ,if i do something wrong please forgive me.
Last edited on
Less readability with no improvement in performance is certainly not an optimization.
Topic archived. No new replies allowed.