ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I've been working on one of my homework assignments which asks me to write a program to input 10 non-zero integers and then evaluate them and display the smallest number.This is the code I have right now for the first three digits, but every time I run it tells me the smallest number is 0. Anyone help?
>
> short minimum, number1, number2, number3
> printf("enter a number between 1 and 3\t");
> scanf("%d", &number1);
>
> printf("enter another number");
> scanf("%d", &number2);
> if(number 1 > number2) {
> (minimum = number1);
> }
> else {
> (minimum = number2); }
>
> printf("enter another number");
> scanf("%d", number3);
> if(number3 > minimum) {
> (minimum = number3);
> }
> else {
> (minimum = minimum);
> }
> printf("the smalles number is %d\n", minimum);
> return 0;
> }
Click here to see the post LQ members have rated as the most helpful post in this thread.
The first thing I noticed is that you're using > when you should be using <.
Furthermore, you need to declare the variables as int and not short. scanf("%d") expects an integer, which is larger than a short. When you pass it a short, it writes an int's worth of data into the short, thus corrupting the data stored in the other variables.
As you know, we don't do homework assignments for you - we try to provide help.
There were a number of logical errors, and a number of typos. I reformatted your code below, used "code blocks" (to preserve formatting), and posted my compile command (note "-Wall", "-pedantic" and "-g": they're your friends ) and execution results.
Please compare with your original code.
Code:
#include <stdio.h>
int
main (int argc, char *argv[])
{
int minimum, number1, number2, number3;
printf ("enter a number between 1 and 3: ");
scanf ("%d", &number1);
printf ("enter another number: ");
scanf ("%d", &number2);
if (number1 < number2) {
minimum = number1;
}
else {
minimum = number2;
}
printf ("enter another number: ");
scanf ("%d", &number3);
if (number3 < minimum) {
minimum = number3;
}
else {
minimum = minimum;
}
printf("the smallest number is %d\n", minimum);
return 0;
}
Quote:
gcc -g -Wall -pedantic -o hmk hmk.c
./hmk
enter a number between 1 and 3: 3
enter another number: 2
enter another number: 1
the smallest number is 1
One thing I *didn't* do is check the return value of "scanf()". If it isn't "1" (the #/expected items), then scanf() failed and you should handle the error. Just FYI...
Note, too, the difference between "short" and "int" ... and the fact that compiling with "-Wall" and "pedantic" (or the equivalent high warning levels for your compiler) will flag this "logical error". That was the reason you were getting "0"
Well i know you are not gonna do my homework, and that's not what I was asking for, I was asking for help. Now everything seems to be working right. I removed the curly brakets like you did and changed it "short" to "int" and that seems it made it. Thank you
If I'm comparing the posts accurately, paulsm4 removed parentheses ( ) rather than curly brackets { } and did so only for readability. That change should not affect the behavior of the program.
Quote:
Originally Posted by paulsm4
Q: Do you understand *why* "short" vs. "int" was causing 0?
I sure don't understand that!
j360 should understand why short is wrong. But in combination with the other error of using > instead of < it is very hard to see how the value 0 was reached. (If the only error were short instead of int, the 0 is easily understood).
There is generally no good reason to try to understand why a memory clobber bug causes the specific results it produces. It is just important to recognize that there is a memory clobber bug and it should be corrected. So you taught the right lesson, I just found the "causing 0" part of it a bit misleading.
Quote:
Originally Posted by j360
write a program to input 10 non-zero integers and then evaluate them and display the smallest number.This is the code I have right now for the first three digits,
Hopefully, when you switch from three to ten, you will not simply code more of the same.
I'm sure the instructor would have picked a smaller number if that were the expected solution.
Clearly part of the intended lesson is to use a loop rather than excess source code when you want to do the same thing over and over.
It was still a good idea for you to nail down the details of scanf and comparison while working with a smaller number and no loop. But now that this is done, figure out how to use a loop for the real job.
When I compiled with full warnings ("-Wall -pedantic", in the case of gcc), I saw this:
Code:
gcc -Wall -pedantic hmk.c
hmk.c: In function `main':
hmk.c:9: warning: int format, different type arg (arg 2)
hmk.c:12: warning: int format, different type arg (arg 2)
hmk.c:21: warning: int format, different type arg (arg 2)
That led me to this:
Code:
...
short minimum, number1, number2, number3;
...
printf ("enter a number between 1 and 3: ");
scanf ("%d", &number1); /* Line 9 */
printf ("enter another number: ");
scanf ("%d", &number2); /* Line 12 */
if (number1 < number2) {
minimum = number1;
}
else {
minimum = number2;
}
printf ("enter another number: ");
scanf ("%d", &number3); /* Line 21 */
if (number3 < minimum) {
minimum = number3;
...
As you can see, "scanf()" was told to expect an "int" (e.g. 32 bits), but it was being given a "short" (16 bits).
Whoops.
I was hoping to encourage the OP to use compiler warnings, so that he'd regard them as his Friend. Dugan later modified his reply to state this explicitly; I was hoping the OP might try to figure it out himself.
IMHO .. PSM
PS:
Another solution would be to change scanf()'s format string, instead of changing the variable type:
Code:
man 3 printf =>
Quote:
PRINTF(3) Linux Programmer's Manual PRINTF(3)
NAME
printf, fprintf, sprintf, snprintf, vprintf, vfprintf, vsprintf,
vsnprintf - formatted output conversion
SYNOPSIS
#include <stdio.h>
int printf(const char *format, ...);
...
The length modifier
Here, `integer conversion' stands for d, i, o, u, x, or X conversion.
hh A following integer conversion corresponds to a signed char or
unsigned char argument, or a following n conversion corresponds
to a pointer to a signed char argument.
h A following integer conversion corresponds to a short int or
unsigned short int argument, or a following n conversion correā
sponds to a pointer to a short int argument.
l (ell) A following integer conversion corresponds to a long int
or unsigned long int argument, or a following n conversion corā
responds to a pointer to a long int argument, or a following c
conversion corresponds to a wint_t argument, or a following s
conversion corresponds to a pointer to wchar_t argument.
ll (ell-ell). A following integer conversion corresponds to a long
long int or unsigned long long int argument, or a following n
conversion corresponds to a pointer to a long long int argument.
...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.