Biggest number

So Im a noob and all but I kinda catch on quick and I was fiddling around with some code from another post, something about putting numbers in descending order.
So i just wanted to write a simple code to further my knowledge one that incorporated if and if else statements since I haven't used them yet. I came upwith some code to tell me which number is bigger given two variables. Now this code works the way i expect and thats cool, but heres my problem ; I would like to have more than two inputs say three or four and then return the biggest number.I tried this with further if and else if statements and came away with only being able to return the greater of the first two values(but have since edited that part of code out). Now again I am a noob so I might be missing some really big things.Also Im not necessarily looking for a direct answer just some hints to point me in the right direction. well anyway heres the working code thus far....
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std; 

int main()
{
  int num1;
  int num2;

  cin >>num1;
  cin >>num2;

  if (num1>=num2)
  cout<<num1<<endl;
  else if (num2>=num1)
  cout<<num2<<endl;
return 0;
} 
If you want to use simple techniques (if statements) then try something like this:

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
35
36
#include <iostream>

int main()
{
    using namespace std;
    
    int i, j, k; 
    
    cout << "Please enter a number...\n";
    cin >> i;
    cout << "And another...\n";
    cin >> j;
    cout << "And one more...\n";
    cin >> k;
    cout << "Thanks!\n";
    
    if (i > j)
    {
          if (i > k)
          cout << i << endl;
          }
    if (j > k)
    {
          if (j > i)
          cout << j << endl;
          }
    if (k > i)
    {
          if (k > j)
          cout << k << endl;
          }
    
    char response;
    cin >> response;
    return 0;
}


It's probably not the best way of completing the task but it's a good start and fairly simple depending on your level of knowledge.
http://en.w- eh... bad idea.

First, you need some way of getting the number of variables you want to input. Then, you need an array to store the actual data in.
http://cplusplus.com/doc/tutorial/arrays/

Use a for loop to get that data. blackcoder41 gave you a link for using for loops.

You'll then need another variable to store the value that is the biggest in. Set it to zero before you enter your second loop.

Then, in another for loop, check each element of your array to see if it's bigger than the number in your variable. If it is, set your variable to that number.

After that loop, output your variable.

K bye.

-Albatross, the Semi-Great and Hardly Mighty
thanks guys, all this info was very helpful ... i was set to use a for loop once i figured the logic , and another variable ..thats what i needed lol. I am very much a beginner having only written two small programs (hello world and something mathematical where i call a couple of functions) so simple is good right now. I will try these suggestions and post the results or lack thereof lol ;)
oh wait, an array .... a lil scary ... sink or swim ;)
so this is what i walk away with when i tried to digest your post (albatross) after a long day . I think I'm on the verge of understanding but I have issues .
1stI need a way to get the number of variables I want to input(right now three variables to compare which is the biggest)
so for this I would build a "for" loop.
Then I need something to store the variable data in , that would be the array
then i need to set a variable for the biggest number (to use after the comparison?) this is also the output variable?
use a second "for" loop (this is where I am major lost, though as you can already see I was a Lil lost before the second "for" loop)
to check the array for the biggest value but it sounds like you want me to use the extra variable to do this with.
This is where I become disconnected ... I can only hope this response is not too convoluted to understand.
I really do appreciate your help.
u could chek which is the biggest number while entering the array, u declare the array, and declare an int/double big that represents, the biggest number, u take the first element of the array before the for loop for entering the other elements, and say that the first element is the biggest number. while entering u can compare if the entered element is bigger then "big" if so change the value of big. and after the for loop you just cout big.

hope u understand what i'm trying to say.

ok mzdr i'll give you a simple but efficent solution for 3 variables code which determine who's the max and the min values but for 4 and so on ,I think you'll need an array to do it which i am just learning so when i master it i'll give it a try too hope this help...;)

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
#include<iostream>

using namespace std;

int main()
{
int a=0,b=0,c=0,max=0,min=0;

cout<<"Enter A,B,C \n";

cin>>a;  cin>>b;  cin>>c;

  if(a>b) 
  {
      max=a; min=b;
  }
   else
  {
     max=b;   min=a;
  }
    if(c>max)
		max=c;
	else
		min=c;

cout<<" Max = "<<max;
cout<<"Min = "<<min<<endl;
	
	return 0;
}
zinogg there is one mistake in your code, u assume that if c is not the max then it haves to be the min, wich must not be true.
ur code works if u enter 1 2 3, or 3 2 1, but try entering 3 1 2
mzdr, try doing wath i told u. it is the quickest solution posible (and one of the easyer ones).
justAbeginner yes u are right i forget to test c if it's a min valuse so here is it:

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

int main()
{
..................
....................

    if(c>max)
		max=c;
	else if(c<min)
		min=c;

cout<<" Max = "<<max;
cout<<"Min = "<<min<<endl;
	
	return 0;
}


thank you justAbeginner for putting that
@mzdr

If you are interested in trying something like this in a more "fun" scenario, try this:
http://www.cplusplus.com/forum/articles/12974/

It's a bunch of exercises to try out and Pancake Glutton has to do with comparing numbers to find the biggest one. It's a fun way to practice your coding skills.
I think I figured it out in a simple way that makes sense to me. Agreed that if I were to use more than three inputs that an array would be necessary. I'm only a beginner trying to get an edge on school by giving myself little projects to get certain concepts. This was so I could get a handle on a simple comtrol structure(if, else) . thanks to all for the help and I'm going to post the complete code so any and all feedback is more than welcome .. i.e .. please comment lol
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
using namespace std; 
int compare ( int x, int y , int z) 
{ 
 if (x>=y) 
 {
	 if (x>=z)
	 {
		 return x;
	 }
	 else 
	 {
		 return z;
	 }
 }
 else 
 {
	 if (y>=z)
	 {
		 return y;
	 }
	 else
	 {
		 return z;
	 }
 }
} 

int main()
{
  int num1,num2,num3;
  int result;
  cout<<"please enter three numbers and I will tell you the largest number you entered"<<endl;
  cout<<endl;
  cout<<"the first number? :"<<endl;
  cin >>num1;
  cout<<endl;
  cout<<"the second number? :"<<endl; 
  cin >>num2;
  cout<<endl;
  cout<<"the thrid number? :"<<endl;
  cin >>num3;
  cout<<endl;

  result = compare(num1,num2,num3);

  cout<<"the largest number is :  "<<result<<endl;
  cout<<endl;
  cout<<endl;
  cout<<endl;
return 0;
}
//please enter three numbers and I will tell you the largest number you entered

//the first number? :
//65431

//the second number? :
//345143

//the thrid number? :
//54235

//the largest number is :  345143 

shameless bump
Do you want anything else answered? And like the others said (and for some reason I ignored) you should use arrays and then compare the integers (or whatever data type you are using) and have a counter increment to change what sections of the arrays are being compared.

Also if you want suggestions use "\n" to create a new line (in some cases). It saves room. You could also expand your function to handle many inputs without repeating if statements. I'd give you more hints but I only know how to do this efficiently in Ruby.
Last edited on
thank you
Topic archived. No new replies allowed.