I stuck with exerise in a Learning C book that I got. If I have the
numbers as input. How can I determine which of the three is the
smallest number, largest number and the range. Or would it be simpler
to just find the range of the three numbers.
Thanks in advance :D 7 6686
Tweaxor wrote: I stuck with exerise in a Learning C book that I got. If I have the numbers as input. How can I determine which of the three is the smallest number, largest number and the range. Or would it be simpler to just find the range of the three numbers.
Thanks in advance :D
#include <stdio.h>
int min(int num1, int num2);
int max(int num1, int num2);
int main()
{
int int1, int2, int3;
printf("Enter three integers, separated by spaces: ");
scanf("%d%d%d", &int1, &int2, &int3);
printf("The minimum is %d\n", min (int1, (min (int2, int3))));
printf("The maximum is %d\n", max (int1, (max (int2, int3))));
printf("The range is %d\n", max (int1, (max (int2, int3)))
- min (int1, (min (int2, int3))));
return 0;
}
int min(int num1, int num2)
{
if (num1 < num2)
return num1;
else
return num2;
}
int max(int num1, int num2)
{
if (num1 > num2)
return num1;
else
return num2;
}
Steve Zimmerman wrote: Tweaxor wrote:
I stuck with exerise in a Learning C book that I got. If I have the numbers as input. How can I determine which of the three is the smallest number, largest number and the range. Or would it be simpler to just find the range of the three numbers.
Thanks in advance :D
Homework solution snipped. Are you going to do this guy's day-job for him
too if he ever graduates?
printf("The range is %d\n", max (int1, (max (int2, int3))) - min (int1, (min (int2, int3))));
Sloppy, but not sloppy enough to get the guy a low mark if that's what you
had in mind.
OP: now that Mr Zimmerman has trashed any value that might have remained in
your homework assignment, see if you can work out ON YOUR OWN why the above
printf assumes too much.
--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
On Tue, 23 Sep 2003 07:00:46 +0000 (UTC), in comp.lang.c , Richard
Heathfield <do******@address.co.uk.invalid> wrote: Homework solution snipped. Are you going to do this guy's day-job for him too if he ever graduates?
As long as I also get his pay cheque... :-)
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
"Tweaxor" <ph****@yahoo.com> wrote in message
news:1c**************************@posting.google.c om... I stuck with exerise in a Learning C book that I got. If I have the numbers as input. How can I determine which of the three is the smallest number, largest number
Use the comparison operators: '<' means 'less than',
'>' means 'greater than.
if(a < b)
printf("%d is less than %d\n", a, b);
and the range. Or would it be simpler to just find the range of the three numbers.
Not sure what you mean by 'range'. The range of
numbers between the highest and lowest consists
of all values between them (and including them if
your definition of 'range' means 'inclusive').
The range of possible values for a given numeric
type can be obtained from a macro of <limits.h>:
printf("Range of type 'int' is from %d to %d inclusive.\n",
INT_MIN, INT_MAX);
-Mike
Richard Heathfield <do******@address.co.uk.invalid> spoke thus: printf("The range is %d\n", max (int1, (max (int2, int3))) - min (int1, (min (int2, int3))));
OP: now that Mr Zimmerman has trashed any value that might have remained in your homework assignment, see if you can work out ON YOUR OWN why the above printf assumes too much.
I'm having a tough time figuring out what it assumes that's inadvisable. max
and min always return integers, so the program will never crash. And as far
as I can tell, it always produces correct results... so help me out here...
--
Christopher Benson-Manica | Jumonji giri, for honour.
ataru(at)cyberspace.org |
"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
news:bk**********@chessie.cirr.com... Richard Heathfield <do******@address.co.uk.invalid> spoke thus:
printf("The range is %d\n", max (int1, (max (int2, int3))) - min (int1, (min (int2, int3)))); OP: now that Mr Zimmerman has trashed any value that might have remained
in your homework assignment, see if you can work out ON YOUR OWN why the
above printf assumes too much.
I'm having a tough time figuring out what it assumes that's inadvisable.
max and min always return integers, so the program will never crash.
It might not crash (actually nobody said it would, but it could),
but will it always give accurate results even when 'int1', 'int2'
and 'int3' each have a valid value? What output do you get from
the following (at the risk of further annoying Richard :-) ) :
[compile with implementation where sizeof(int) >= 4]
#include <limits.h>
#include <stdio.h>
int min(int lhs, int rhs)
{
return lhs < rhs ? lhs : rhs;
}
int max(int lhs, int rhs)
{
return lhs > rhs ? lhs : rhs;
}
int main()
{
int int1 = 500000000;
int int2 = -1000000000;
int int3 = -2000000000;
double dmin = int3; /* used below */
double dmax = int1; /* " " */
printf("INT_MIN == %11d\n", INT_MIN);
printf("INT_MAX == %11d\n", INT_MAX);
printf("int1 == %11d\n"
"int2 == %11d\n"
"int3 == %11d\n",
int1, int2, int3);
printf("max(%11d, (max(%11d, %11d))) == %11d\n"
"min(%11d, (min(%11d, %11d))) == %11d\n",
int1, int2, int3, max(int1, (max(int2, int3))),
int1, int2, int3, min(int1, (min(int2, int3))));
printf("The range is %11d\n", max (int1, (max (int2, int3)))
- min (int1, (min (int2, int3))));
printf("%.0f - %.0f == %.0f\n", dmax, dmin, dmax - dmin);
return 0;
}
-Mike
.. And as far as I can tell,
How far did you test? :-)
it always produces correct results...
Not *always*.
so help me out here...
See above.
-Mike
Mike Wahler <mk******@mkwahler.net> spoke thus: How far did you test? :-)
Okay, I didn't ;) Boy, that was more subtle than I thought, but I probably
still should have seen it... *sheepish*
Not *always*.
Just put in the documentation not to use MAX_INT ;)
--
Christopher Benson-Manica | Jumonji giri, for honour.
ataru(at)cyberspace.org | This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Gerrit Holl |
last post by:
Hi,
is it proper to compare booleans? It is possible, of course, because
they're compatible with numbers, but booleans aren't truly numbers. I'm
tempted to write:
return cmp(self.extends,...
|
by: John |
last post by:
Hi,
I encountered a strange problem while debugging C code for a
Windows-based application in LabWindows CVI V5.5, which led me to
write the test code below. I tried this code with a different...
|
by: John Smith |
last post by:
This code for the comparison of fp types is taken from the C FAQ.
Any problems using it in a macro?
/* compare 2 doubles for equality */
#define DBL_ISEQUAL(a,b)...
|
by: Pugi! |
last post by:
hi,
I am using this code for checking wether a value (form input) is an
integer and wether it is smaller than a given maximum and greater then
a given minimum value:
function...
|
by: nw |
last post by:
Hi,
I'd like to compare 2 floating point numbers within a given error. I'd
rather not use a absolute error but one related to the number of
values that can be represented between the two floats....
|
by: shana07 |
last post by:
I need help on comparing lines in my output file...
How to delete the second line in text file (FileReader) and print out the first line for the same fileno?
I put here sample of my output file:...
|
by: Thomas Kowalski |
last post by:
Hi everyone,
To determine equality of two doubles a and b the following is often
done:
bool isEqual ( double a, double b ) {
return ( fabs (a-b) < THRESHOLD );
}
But this a approach usually...
|
by: Carramba |
last post by:
Hi!
is there a better/faster way to compare mantissas of to real number then
in following code?
#include <stdio.h>
#include <stdlib.h>
int main(void) {
float a,b;
int test;
|
by: eman.abu.samra |
last post by:
Hi all,
i have encountered the strangest behavior. Check out this simple
program:
#include <stdio.h>
int main()
{
double time = 1;
|
by: saneman |
last post by:
I have a function:
int F(double a)
{
if (a = =1.0)
{
return 22;
}
return 44;
}
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
| |