Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of threads: 5430
Number of posts: 16951

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
How to compare integers Posted by RadiationX on 23 Mar 2007 at 8:42 AM
I'm trying to solve this problem in chapter 1 of a C programming book. I want this program to read in 5 integers then determine and print the largest and smallest. My problem is I don't want it to print everything just the smallest and largest values. Is my comparison of the integers correct?



#include <stdio.h>
#include <stdlib.h>

int main()
{
int a,b,c,d,e;

printf("enter your five numbers and I'll tell you something\n");
scanf("%d%d%d%d%d", &a,&b,&c,&d,&e);

/* if(a>b)
printf("%d\n",a);

if(a>c)
printf("%d\n",a);

*/
if(a>b,c,d,e)
{
printf("%d\n",a);
}
if(a<b,c,d,e)
{
printf("%d\n",a);
}
if(b>a,c,d,e)
{
printf("%d\n",b);
}
if(b<a,c,d,e);
{
printf("%d\n",b);
}
if(c>a,b,d,e);
{
printf("%d\n",c);
}
if(c<a,b,d,e)
{
printf("%d\n",c);
}
if(d>a,b,c,e)
{
printf("%d\n",d);
}
if(d<a,b,c,e)
{
printf("%d\n",d);
}
if(e>a,b,c,d)
{
printf("%d\n",e);
}
if(e<a,b,c,d)
{
printf("%d\n\n",e);
}

Report
Re: How to compare integers Posted by MT2002 on 23 Mar 2007 at 3:46 PM
This message was edited by MT2002 at 2007-3-23 15:47:6


: I'm trying to solve this problem in chapter 1 of a C programming book. I want this program to read in 5 integers then determine and print the largest and smallest. My problem is I don't want it to print everything just the smallest and largest values. Is my comparison of the integers correct?
:
:
:
: #include <stdio.h>
: #include <stdlib.h>
:
: int main()
: {
: int a,b,c,d,e;
:
: printf("enter your five numbers and I'll tell you something\n");
: scanf("%d%d%d%d%d", &a,&b,&c,&d,&e);
:
: /* if(a>b)
: printf("%d\n",a);
:
: if(a>c)
: printf("%d\n",a);
:
: */
: if(a>b,c,d,e)
: {
: printf("%d\n",a);
: }
: if(a<b,c,d,e)
: {
: printf("%d\n",a);
: }
: if(b>a,c,d,e)
: {
: printf("%d\n",b);
: }
: if(b<a,c,d,e);
: {
: printf("%d\n",b);
: }
: if(c>a,b,d,e);
: {
: printf("%d\n",c);
: }
: if(c<a,b,d,e)
: {
: printf("%d\n",c);
: }
: if(d>a,b,c,e)
: {
: printf("%d\n",d);
: }
: if(d<a,b,c,e)
: {
: printf("%d\n",d);
: }
: if(e>a,b,c,d)
: {
: printf("%d\n",e);
: }
: if(e<a,b,c,d)
: {
: printf("%d\n\n",e);
: }
:
:
if(b>a,c,d,e)

I dont think this is what you want. (Im surprised it compilied,
actually)

You have to specify all of your conditional tests. In the above if,
for example:


// if (b>a, c, d, e)  <Incorrect
if (b>a && b>c && b>d && b>e) < should be this


On a side note, all of those if's are really ugly..

A better method would be to store the numbers in a list,
and sort them using a sorting algorithm. This is just something
to think about

Hope this helps;

Report
Re: How to compare integers Posted by RadiationX on 23 Mar 2007 at 7:05 PM
: This message was edited by MT2002 at 2007-3-23 15:47:6

:
: : I'm trying to solve this problem in chapter 1 of a C programming book. I want this program to read in 5 integers then determine and print the largest and smallest. My problem is I don't want it to print everything just the smallest and largest values. Is my comparison of the integers correct?
: :
: :
: :
: : #include <stdio.h>
: : #include <stdlib.h>
: :
: : int main()
: : {
: : int a,b,c,d,e;
: :
: : printf("enter your five numbers and I'll tell you something\n");
: : scanf("%d%d%d%d%d", &a,&b,&c,&d,&e);
: :
: : /* if(a>b)
: : printf("%d\n",a);
: :
: : if(a>c)
: : printf("%d\n",a);
: :
: : */
: : if(a>b,c,d,e)
: : {
: : printf("%d\n",a);
: : }
: : if(a<b,c,d,e)
: : {
: : printf("%d\n",a);
: : }
: : if(b>a,c,d,e)
: : {
: : printf("%d\n",b);
: : }
: : if(b<a,c,d,e);
: : {
: : printf("%d\n",b);
: : }
: : if(c>a,b,d,e);
: : {
: : printf("%d\n",c);
: : }
: : if(c<a,b,d,e)
: : {
: : printf("%d\n",c);
: : }
: : if(d>a,b,c,e)
: : {
: : printf("%d\n",d);
: : }
: : if(d<a,b,c,e)
: : {
: : printf("%d\n",d);
: : }
: : if(e>a,b,c,d)
: : {
: : printf("%d\n",e);
: : }
: : if(e<a,b,c,d)
: : {
: : printf("%d\n\n",e);
: : }
: :
: :
:
: if(b>a,c,d,e)
: 

: I dont think this is what you want. (Im surprised it compilied,
: actually)
:
: You have to specify all of your conditional tests. In the above if,
: for example:

:

: // if (b>a, c, d, e)  <Incorrect
: if (b>a && b>c && b>d && b>e) < should be this
: 

:
: On a side note, all of those if's are really ugly..
:
: A better method would be to store the numbers in a list,
: and sort them using a sorting algorithm. This is just something
: to think about
:
: Hope this helps;
:

:
Thanks for the reply. However the book wants me to write this program without using &&. This is a chapter one exercise so technically I'm not supposed to know about sorting algorithms or even the IF-Else technique.
I see how your technique will work, but is there an even 'simpler' method of doing this?
Report
Re: How to compare integers Posted by AsmGuru62 on 23 Mar 2007 at 7:57 PM
So, you supposed to sort without if/else and any other technique?!..
Nice!..

Report
Re: How to compare integers Posted by RadiationX on 23 Mar 2007 at 8:03 PM
: So, you supposed to sort without if/else and any other technique?!..
: Nice!..
:

:
yes, do you have any ideas?
Report
Re: How to compare integers Posted by diffeecult on 24 Mar 2007 at 8:24 AM
This message was edited by diffeecult at 2007-3-24 13:11:26

: : So, you supposed to sort without if/else and any other technique?!..
: : Nice!..
: :

: :
: yes, do you have any ideas?
:

This works unless you enter a number out of range, but you could increase that if you wanted to.
Hope this helps,

diffeecult

#include <stdio.h>
#include <stdlib.h>

int main()
{
int a, b, c, d, e, largest, smallest;

printf("enter your five numbers and I'll tell you something\n");
scanf("%d%d%d%d%d", &a,&b,&c,&d,&e);
	
/* Set really low and high numbers to test against */
largest  = -32000;
smallest = 32000;
    
/* Find the largest number */
if(largest < a)
    largest = a;
if(largest < b)
    largest = b;
if(largest < c)
    largest = c;
if(largest < d)
    largest = d;
if(largest < e)
    largest = e;
	
/* Find the smallest number */
if(smallest > a)
    smallest = a;
if(smallest > b)
    smallest = b;
if(smallest > c)
    smallest = c;
if(smallest > d)
    smallest = d;
if(smallest > e)
    smallest = e;
	
/* Print the values */
printf("\nThe largest is %d and the smallest is %d",largest, smallest);
		
return 0;
}


Uh Oh,
Never mind. I just realized I used if. I don't know how you could do it without any comparisons.

diffeecult

Report
Just an attempt Posted by Mohammad Rast on 29 Mar 2007 at 10:36 AM
Forgive for the loops!

#include <iostream>
using namespace std;

#include <string>



#define Num_of_Nums 7 // number of numbers for comparison

int main()
{
	
	int numbers[Num_of_Nums]= {12,-9,-9,0,34,0,7}; // 'Num_of_Nums' random numbers
	int comp=1, i, j;

	// Finding Minimum...

	for (i=0; i<Num_of_Nums && comp>0; i++) // for each number...
		for (j=0, comp=-1; j<Num_of_Nums && comp<=0; j++) // compare each number with others...
			comp = numbers[i] - numbers[j]; // if comp==0 or <0 then it can be the minimum, otherwise test next number

	cout << "The minimum is : " << numbers[i-1] << endl;

	// Finding Maximum...

	comp=-1; // any negative number

	for (i=0; i<Num_of_Nums && comp<0; i++)
		for (j=0, comp=1; j<Num_of_Nums && comp>=0; j++)
			comp = numbers[i] - numbers[j];

	cout << "The maximum is : " << numbers[i-1] << endl;

	return 0;
}

--------------------------------------
- I don't have Time To waste The Time!

Report
Re: How to compare integers Posted by shrinivasrathi on 3 Apr 2007 at 7:42 AM
: I'm trying to solve this problem in chapter 1 of a C programming book. I want this program to read in 5 integers then determine and print the largest and smallest. My problem is I don't want it to print everything just the smallest and largest values. Is my comparison of the integers correct?
:
:
:
: #include <stdio.h>
: #include <stdlib.h>
:
: int main()
: {
: int a,b,c,d,e;
:
: printf("enter your five numbers and I'll tell you something\n");
: scanf("%d%d%d%d%d", &a,&b,&c,&d,&e);
:
: /* if(a>b)
: printf("%d\n",a);
:
: if(a>c)
: printf("%d\n",a);
:
: */
: if(a>b,c,d,e)
: {
: printf("%d\n",a);
: }
: if(a<b,c,d,e)
: {
: printf("%d\n",a);
: }
: if(b>a,c,d,e)
: {
: printf("%d\n",b);
: }
: if(b<a,c,d,e);
: {
: printf("%d\n",b);
: }
: if(c>a,b,d,e);
: {
: printf("%d\n",c);
: }
: if(c<a,b,d,e)
: {
: printf("%d\n",c);
: }
: if(d>a,b,c,e)
: {
: printf("%d\n",d);
: }
: if(d<a,b,c,e)
: {
: printf("%d\n",d);
: }
: if(e>a,b,c,d)
: {
: printf("%d\n",e);
: }
: if(e<a,b,c,d)
: {
: printf("%d\n\n",e);
: }
:
:

HI friend ,
there is some prob with ur code.
when u compare a>b,c,d,e
this comparision is eqvlent to a>e. this due to comma(,) operator in C.
Read K&RC or some othr book for details abt comma operator.
I wud probably send u soln to this tomrw.
Report
Re: How to compare integers Posted by clivenew on 5 Apr 2007 at 7:35 AM
: I'm trying to solve this problem in chapter 1 of a C programming book. I want this program to read in 5 integers then determine and print the largest and smallest. My problem is I don't want it to print everything just the smallest and largest values. Is my comparison of the integers correct?
:
:
:
: #include <stdio.h>
: #include <stdlib.h>
:
: int main()
: {
: int a,b,c,d,e;
:
: printf("enter your five numbers and I'll tell you something\n");
: scanf("%d%d%d%d%d", &a,&b,&c,&d,&e);
:
: /* if(a>b)
: printf("%d\n",a);
:
: if(a>c)
: printf("%d\n",a);
:
: */
: if(a>b,c,d,e)
: {
: printf("%d\n",a);
: }
: if(a<b,c,d,e)
: {
: printf("%d\n",a);
: }
: if(b>a,c,d,e)
: {
: printf("%d\n",b);
: }
: if(b<a,c,d,e);
: {
: printf("%d\n",b);
: }
: if(c>a,b,d,e);
: {
: printf("%d\n",c);
: }
: if(c<a,b,d,e)
: {
: printf("%d\n",c);
: }
: if(d>a,b,c,e)
: {
: printf("%d\n",d);
: }
: if(d<a,b,c,e)
: {
: printf("%d\n",d);
: }
: if(e>a,b,c,d)
: {
: printf("%d\n",e);
: }
: if(e<a,b,c,d)
: {
: printf("%d\n\n",e);
: }
:
:
i'm also finding a solution for the question. I have the same book and u CAN use if statement but not else statement. Can anyone give a solution?
Report
Re: How to compare integers Posted by mmas on 22 Mar 2011 at 3:47 AM
hey,i have writ this bit of code for comparing five it and print smallest and largest int.but i have assign LG = 0; only if i assign s =0 ; the output is every time 0 do i need to assign this value for s or with out this its working well ...i just start my programing practice.



#include <stdio.h>

int main(void)

{
int num1 ,num2,num3,num4,num5,s,LG;/* s means smallest and LG for largest*/

printf(" enter five integers: ");

scanf("%d%d%d%d%d",&num1,&num2,&num3,&num4,&num5);
LG = 0;

if(LG < num1 )
{
LG = num1;
}
if( LG < num2)
{
LG = num2;

}
if(LG < num3 )
{
LG = num3;

}
if( LG < num4 )
{
LG = num4;
}
if(LG < num5)
{
LG = num5;
}
printf("the lagest is %d\n",LG);


if( s > num1 )
{
s = num1;
}
if ( s > num2 )
{
s = num2;
}
if( s > num3 )
{
s = num3;
}
if( s > num4 )
{
s = num4;
}
if(s > num5 )
{
s = num5;
}
printf("The smallest is %d\n",s);
return 0;

}





Report
Re: How to compare integers Posted by nebgast on 24 Mar 2011 at 5:18 PM
Just to point out and clear up confusion about the following:

if ( a < b, c, d, e )


This if equates to

if( e )


this is because of operator precedence.

First: < operator is left to right associative.
Second: , operator is left to right associative and has the absolute lowest precedence level meaning that no matter what operators you have the comma operator will occur last. Also the right hand side is the final value for any given , operator.

so we have

a < b, c, d, e

which is
( ( ( ( a < b ), c ), d ), e )
( ( ( ( true/false ), c ), d ), e )
( ( ( c ), d ), e )
( ( d ), e )
( e )

Report
Re: How to compare integers Posted by Newb1001 on 1 Apr 2011 at 9:33 AM
Int main ()
{
int a, b, c, d, e, r, r2;

cout<< "Enter 5 integers."<<endl;
cin>>a>>b>>c>>d>>e;

if (a<b&c&d&e);

if (b<a&c&d&e);

if (c<a&b&d&e);

if (d<a&b&c&e);

if (e<a&b&c&d);

r= (a,b,c,d,e);
cout<<r;

return 0;
}

Newb1001
Edit: I thought this worked, but did't.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.