Thread: Storing negative number in variables.

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    3

    Question Storing negative number in variables.

    Code:
    #include <stdio.h>
    int main() {
    
        int count, input, temp, max;
    
    printf("How many: ");
    scanf("%d", &count);
    
    printf("Enter %d inputs: ", count);
        for (int i = 0; i < count; ++i) {
            scanf("%d", &input);
            if(input > temp){
                max = input;
    }
    temp = input;
    }
    
        printf("Max = %d", max);
    
        return 0;
    }
    I've wrote some code that enters 'x' amount of variables and determines which is the largest number to output. But I'm having issues with negative numbers and don't understand why.

    for example: I input -2 -5 -10 and the output is 69.
    All effort is appreciated.

    Thanks,
    Nand

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,402
    Why do you need the temp variable? It looks like you only need count, max, and input, and you need to set max to a suitable initial value (consider either setting max to the very first input, or #include <limits.h> and use something from there).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    3
    Quote Originally Posted by laserlight View Post
    Why do you need the temp variable? It looks like you only need count, max, and input, and you need to set max to a suitable initial value (consider either setting max to the very first input, or #include <limits.h> and use something from there).
    Thanks for the effort. It didn't help. The purpose of my code is to find the maximum input from any random user input of an integer.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,402
    Yes, and that's why I gave you that advice. At the moment you're comparing input with temp, which initially wasn't given an initial value, so that first comparison is rubbish. You should be comparing input with max... but max also needs an initial value, hence my advice. If you wish to ignore my advice... your loss.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2019
    Posts
    3
    Fixed it, I understand what you are saying now. Sorry for the misunderstanding.
    Code:
    #include <stdio.h>
    int main() {
    
    int count, input, max;
    
    printf("How many: ");
    scanf("%d", &count);
    
    printf("Enter %d inputs: ", count);
        for (int i = 0; i < count; ++i) {
            scanf("%d", &input);
            if(i == 0 || input > max){
                max = input;
    }
        }
    
    printf("Max = %d\n", max);
    
    return 0;
    }
    Last edited by Nand; 04-04-2019 at 10:35 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read until negative number
    By m_user in forum C Programming
    Replies: 10
    Last Post: 04-03-2009, 06:38 AM
  2. Assigning negative number to unsigned variables.
    By sar_mahesh in forum C Programming
    Replies: 10
    Last Post: 09-27-2006, 05:47 PM
  3. How can i convert negative number to positive number ?
    By winsonlee in forum C Programming
    Replies: 2
    Last Post: 05-05-2004, 08:02 AM
  4. Replies: 2
    Last Post: 01-04-2004, 05:52 PM
  5. random number between negative and positive number
    By anomaly in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2003, 08:40 AM

Tags for this Thread