LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-25-2011, 12:43 PM   #1
j360
Member
 
Registered: Jan 2010
Location: Maryland
Distribution: Ubuntu
Posts: 58

Rep: Reputation: 0
Cool C programming help


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.
Old 02-25-2011, 12:52 PM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,223

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
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.

Last edited by dugan; 02-25-2011 at 01:08 PM.
 
2 members found this post helpful.
Old 02-25-2011, 12:54 PM   #3
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

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"

Last edited by paulsm4; 02-25-2011 at 01:07 PM.
 
Old 02-25-2011, 02:47 PM   #4
j360
Member
 
Registered: Jan 2010
Location: Maryland
Distribution: Ubuntu
Posts: 58

Original Poster
Rep: Reputation: 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

Last edited by j360; 02-25-2011 at 02:48 PM.
 
Old 02-25-2011, 03:15 PM   #5
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

More importantly -

Q: Do you understand *why* "short" vs. "int" was causing 0?

Q: Do you understand what Dugan said about "<" vs. ">", and why?

Also:

* Please continue pasting code snippets (thank you - it is EXTREMELY helpful!)

* Please use code blocks in the future (it makes your code snippets MUCH easier to read )

* Please mark the thread "solved", with you resolution, when you resolve the problem.

Thank you in advance .. PSM

Last edited by paulsm4; 02-25-2011 at 03:17 PM.
 
1 members found this post helpful.
Old 02-26-2011, 07:44 AM   #6
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by j360 View Post
I removed the curly brakets like you did
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 View Post
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 View Post
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.

Last edited by johnsfine; 02-26-2011 at 07:56 AM.
 
Old 02-26-2011, 11:02 AM   #7
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Johnsfine -

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. ...
... scanf ("%hd", &number1); ... scanf ("%hd", &number2); ... scanf ("%hd", &number3);

Last edited by paulsm4; 02-27-2011 at 12:23 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:09 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration