Thread: I need help with inputing user values into an array!!!

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    4

    Exclamation I need help with inputing user values into an array!!!

    I have an assignment for school and im kind of confused for how to do it... What im trying to do is have a user input up to 25 positive double values and the end of the input data is indicated by the user typing anynonnumeric value. Then i have to count how many values the user inputed and find the max and min numbers.
    This is what I have so far but it isnt working :/


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main() {
        double numArray[25];
        char wordArray[10];
        int x = 0;
        int y = 0;
        double max = 0;
        int counter = 1;
    
    
        setvbuf(stdout, NULL, _IONBF, 0);
    
    
        printf("Enter up to 25 positive reals (non-numeric to stop).\n");
        scanf(" %lf %c", &numArray[x], &wordArray[y]);
    
    
        max = numArray[0];
    
    
        while (1) {
    
    
            if ((numArray[x]) > max) {
                max = numArray[x];
                break;
            }
            counter = counter + 1;
            x++;
    
    
    
    
        }
        printf("%d values were entered\n", counter);
        printf("Max Value = %lf", max);
    
    
        return EXIT_SUCCESS;
    
    
    }
    Last edited by Mary Heale; 02-05-2017 at 02:38 AM. Reason: typo

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I would recommend tackling this assignment in parts. You're going to have to store up to 25 values in numArray, using a loop, before you can really start the min-max part. In this loop, you should increment counter when the input is positive. By doing it this way, you ensure that what the user has typed so far meets your requirements, and you keep a good count.

    To read the numbers correctly, recognize that if scanf would fail to read a double for some reason it would return 0.
    Quote Originally Posted by man 3 scanf
    These functions return the number of input items successfully matched and assigned, which can be fewer than provided for, or even zero in the event of an early matching failure.
    So, you don't even need to read the non-numeric input that the user might use to stop the input loop. Just assume that anything that isn't a double is non-numeric, and stop.

    Once you have all the input, you can use another loop with counter and find the max and min.
    Code:
        while (1) {
            if ((numArray[x]) > max) {
                max = numArray[x];
                break;
            }
            counter = counter + 1;
            x++;
        }
    You shouldn't break out of the loop just because you found a bigger number than the last max. Consider if the input was in sorted order - the actual max is the last number. So it's important to check all of the numbers.
    Last edited by whiteflags; 02-05-2017 at 03:17 AM. Reason: brain fart

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. User assigned values into an Array
    By Mark Labarbara in forum C Programming
    Replies: 21
    Last Post: 11-12-2011, 11:57 PM
  2. Replies: 3
    Last Post: 11-01-2010, 08:22 PM
  3. Replies: 12
    Last Post: 04-12-2009, 05:49 PM
  4. Inputing values
    By blindleaf in forum C Programming
    Replies: 10
    Last Post: 05-07-2003, 04:23 PM
  5. How to let the user input values for an array...
    By TerranAce007 in forum C++ Programming
    Replies: 2
    Last Post: 08-24-2002, 03:54 AM

Tags for this Thread