Simple program, simple problem

This is a discussion on Simple program, simple problem within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> int main(void){ int i,pos,num,minimum; for(i=1;i<=5;i++){ printf("Input an integer:"); scanf("%d",&num); printf("\n"); if(num<0){ minimum=num; pos=i; } } printf("The smallest ...

  1. #1
    Registered User KAUFMANN's Avatar
    Join Date
    Jan 2011
    Location
    Coimbra, Portugal
    Posts
    31

    Simple program, simple problem

    Code:
    #include <stdio.h>
    int main(void){
        int i,pos,num,minimum;
        for(i=1;i<=5;i++){
                            printf("Input an integer:");
                            scanf("%d",&num);
                            printf("\n");
                            if(num<0){
                                      minimum=num;
                                      pos=i;
                                      }
                            }
        printf("The smallest negative integer is %d and its position is %d",minimum,pos);
        printf("Have a good one\n\n");
        system("pause");
    }
    Hello there.

    So, this small little program is supposed to get 500 numbers from input, and then determine which one is the smallest negative. For instance, between 4 -2 -4 10 -23, the smallest negative number would be -2.

    However it does not seem to work because it just prints out the last negative integer which went into the program.

    Could someone enlighten me?

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,309
    You are assigning minimum to every negative number in the sequence, and pos to it's position as well. You need to track the current maximum number less than zero. Something like:
    Code:
    if num is less than zero and num is greater than the current maximum number less than zero
        max_neg = num
        pos = i
    Note you need to initialize max_neg and pos appropriately.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Since you are only testing for <0 you will get the last negative number, not the smallest one.
    Also testing if thisnum < lastnum will actually get you the most negative number not the least negative...

    Try something like this, where lneg is your result...
    Code:
    int num;
    int lneg = -1000000;
    
    if (num < 0)
      { if (abs(num) <  abs(lneg))
           lneg = num; }

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,309
    Oh yeah, and you told the compiler main will return an int, so why doesn't it. This can lead to problems.

    As for Tater's example, or mine, you probably want to initialize lnum/max_neg to INT_MIN from limits.h.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by anduril462 View Post
    Oh yeah, and you told the compiler main will return an int, so why doesn't it. This can lead to problems.

    As for Tater's example, or mine, you probably want to initialize lnum/max_neg to INT_MIN from limits.h.
    Good catch... both times!

  6. #6
    Registered User KAUFMANN's Avatar
    Join Date
    Jan 2011
    Location
    Coimbra, Portugal
    Posts
    31
    Very well then, I took Tater's suggestion, although it is not precisely what I wanted, and it worked out. Thank you guys.
    Code:
    #include <stdio.h>
    
    int main(void){
        int i,n,pos,num,limit;
        limit=-1000000;
        for(i=1;i<=5;i++){
                          scanf("%d",&num);
                          if(num<0 && num>limit){
                                   pos=i;
                                   limit=num;
                                   }
                                   }
        printf("The smallest negative integer is %d at pos=%d",limit,pos);
        system("pause");
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 02:30 PM
  3. simple login program problem
    By suckss in forum C Programming
    Replies: 11
    Last Post: 11-11-2006, 04:02 PM
  4. Problem with a simple program
    By Salgat in forum C Programming
    Replies: 10
    Last Post: 06-15-2006, 05:57 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21