# How to compare numbers to determine the highest

Page 1 of 1

## 6 Replies - 43040 Views - Last Post: 17 September 2006 - 03:33 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=18645&amp;s=ef8377dc339d280aebf02d87d993f545&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 browngod2002

• New D.I.C Head

Reputation: 4
• Posts: 27
• Joined: 16-September 06

# How to compare numbers to determine the highest

Posted 16 September 2006 - 02:31 PM

need help trying to find the two highest numbers input by a user.

```/*********************************************************/
/**																							   */
/** Author: Matthew Schofield														 */
/** Purpose : This program will take in 10 numbers and					*/
/**		   compare them and then display the two						   */
/**		   highest numbers.														   */
/*********************************************************/

#include "stdafx.h"
#include <stdio.h>
/*int get_max(int num1, int num2, int num3, int num4, int num5, int num6,int num7, int num8, int num9, int num10);*/

/*#using <mscorlib.dll>*/

/*using namespace System;*/

int _tmain()
{
int i;
/*int int1;
int int2;
int int3;
int int4;
int int5;
int int6;
int int7;
int int8;
int int9;
int int10;*/
int num[10];

/*get_max(int num1, int num2, int num3, int num4, int num5, int num6,int num7, int num8, int num9, int num10);*/

for (i = 0; i <= 9; ++i)
{
printf("Enter a number: ");
scanf("%d", num[i]);

}

getchar();

return 0;
}

/*int get_max(int num1, int num2, int num3, int num4, int num5, int num6,int num7, int num8, int num9, int num10)
{
int i;
for (i = 0; i<=10; i++)
{
if (num1 > num2)
return num1;
else
return num2;
if (num3 > num4)
return num3;
else
return num4;
if (num5 > num6)
return num5;
else
return num6;
if (num7 > num8)
return num7;
else
return num8;
if (num9 > num10)
return num9;
else
return num10;
}
}*/

```

edit: moved to C/C++, modified title to be more descriptive ~ jayman9

Is This A Good Question/Topic? 1

## Replies To: How to compare numbers to determine the highest

• g+ + -o drink whiskey.cpp

Reputation: 248
• Posts: 13,507
• Joined: 12-July 02

## Re: How to compare numbers to determine the highest

Posted 16 September 2006 - 03:10 PM

Could you specify the problem you are encountering?

### #3 browngod2002

• New D.I.C Head

Reputation: 4
• Posts: 27
• Joined: 16-September 06

## Re: How to compare numbers to determine the highest

Posted 16 September 2006 - 03:30 PM

I am having a problem looping through the array and then comparing the numbers in the array to see which two are the biggest numbers. I get some kind of number that is not one of the original numbers. Or I get nothing.

### #4 DeeViLiSh

Reputation: 6
• Posts: 175
• Joined: 25-July 06

## Re: How to compare numbers to determine the highest

Posted 17 September 2006 - 01:07 AM

Your get_max() functions is wrong since there is no possibility of knowing whether 2 is higher than 8 or 4.
Ex : Numbers entered : 1; 4; 3; 5; 9; 4; 3; 9; 0; 2
According to your program, 3 is the highest number since 1!>4 is respected but not 4!>3

In your loop, I suggest you use arrays instead of "num1" and "num2", etc.

It limits the manipulation of loops processing each array.

```int get_max(int num[])
{
int highest;
int a

for(a = 0; a!= 10; ++a){
if(strcmp(num[a], num[a+1]) > 0)
if(highest < num[a])
highest = num[a];
}

return highest;
}
```

I'm doing this without my IDE so I'm not sure if num[a] will be considered a const char * string. If not, convert int num[] to string num.

Note : the declaration of the get_max() function needs to be after the for loop in int _tmain()

This post has been edited by DeeViLiSh: 17 September 2006 - 01:10 AM

### #5 born2c0de

• printf("I'm a %XR",195936478);

Reputation: 183
• Posts: 4,667
• Joined: 26-November 04

## Re: How to compare numbers to determine the highest

Posted 17 September 2006 - 02:16 AM

Use arrays, implement any Sorting Algorithm (Check out the Code Snippets Section) and print the first two elements in the Array.

### #6 browngod2002

• New D.I.C Head

Reputation: 4
• Posts: 27
• Joined: 16-September 06

## Re: How to compare numbers to determine the highest

Posted 17 September 2006 - 03:30 AM

thank you that helped alot!!!!

DeeViLiSh, on 17 Sep, 2006 - 01:07 AM, said:

Your get_max() functions is wrong since there is no possibility of knowing whether 2 is higher than 8 or 4.
Ex : Numbers entered : 1; 4; 3; 5; 9; 4; 3; 9; 0; 2
According to your program, 3 is the highest number since 1!>4 is respected but not 4!>3

In your loop, I suggest you use arrays instead of "num1" and "num2", etc.

It limits the manipulation of loops processing each array.

```int get_max(int num[])
{
int highest;
int a

for(a = 0; a!= 10; ++a){
if(strcmp(num[a], num[a+1]) > 0)
if(highest < num[a])
highest = num[a];
}

return highest;
}
```

I'm doing this without my IDE so I'm not sure if num[a] will be considered a const char * string. If not, convert int num[] to string num.

Note : the declaration of the get_max() function needs to be after the for loop in int _tmain()

thank you i tried it that way also and it works just fine

born2c0de, on 17 Sep, 2006 - 02:16 AM, said:

Use arrays, implement any Sorting Algorithm (Check out the Code Snippets Section) and print the first two elements in the Array.

### #7 DeeViLiSh

Reputation: 6
• Posts: 175
• Joined: 25-July 06

## Re: How to compare numbers to determine the highest

Posted 17 September 2006 - 03:33 AM

I'm happy you've solved your problem

Welcome to </Dream.In.Code>