6 Replies - 38973 Views - Last Post: 17 September 2006 - 03:33 AM Rate Topic: -----

#1 browngod2002  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • 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

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#3 browngod2002  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 DeeViLiSh  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#5 born2c0de  Icon User is offline

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

Reputation: 180
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#6 browngod2002  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • 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!!!!

View PostDeeViLiSh, 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

View Postborn2c0de, 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.

Was This Post Helpful? 0
  • +
  • -

#7 DeeViLiSh  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • 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> :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1