0

I'm new to coding, and starting with C.

The program I am working on is getting me hung up because of user input.

I'm supposed to find the minimum and maximum value of the user's input, but nothing I've tried so far is working.

Any help would be appreciated:

#include <stdio.h>
#include <math.h>

int main() {
    int i;
    int n;
    float data;
    float sumx;
    float sumx2;
    float mean;
    float var;
    float sd;
    int min;
    int max;

    /* Get the number of data lines from the user*/
    
    printf("How many numbers do you need to enter (n)? ");
    scanf("%d", &n);
    
    while (n <= 0) {
      printf("Enter positive numbers only, please.\n");
      printf("\n");
      printf("How many numbers do you need to enter (n)? ");
      scanf("%d", &n);

    /* Initialization */

    sumx = sumx2 = 0.0;

    for ( i = 1; i <= n; i++) {
      printf("Enter data item %d: ", i);
      scanf("%f", &data);
 
      min = 0;
      max = 0;

      if(data > max)
      max = data;
      else if(data < min)
      min = data;

      sumx = sumx + data;
      sumx2 = sumx2 + (data * data);
    }

    mean = sumx / n;
    var = 1.0/(n-1) * (sumx2 - (1.0/n) * sumx * sumx);
    sd = sqrt(var);

    printf("\n");
    printf("The minimum value entered: %d\n", min);
    printf("The maximum value entered: %d\n", max);
    printf("\n")
    printf("mean = %f\n", mean);
    printf("variance = %f\n", var);
    printf("std. dev. = %f\n\n", sd);

    return 0;    
}

No matter what values are entered into the program, the min values read as 0, and the max value is whatever was inputted last.

Should the min/max values be different?

16
  • It will require assigning values to min and max. You might start with that. The first value read should be assigned to both, then adjust accordingly as-needed with each subsequent value. Commented Apr 1, 2014 at 20:56
  • Are you sure that you posted the correct program? What has mean value, variance etc to do with your question? Commented Apr 1, 2014 at 20:57
  • 3
    The while loop is always false for positive values Commented Apr 1, 2014 at 20:59
  • It's part of an assignment, just trying to teach me the basics of C. With whatever numbers are entered, it'll give the mean, and variances. As an additional computation, it states the users minimum and maximum outputs. Commented Apr 1, 2014 at 20:59
  • 1
    I don't see anything in there that even attempts to figure out the min and max... Commented Apr 1, 2014 at 21:19

2 Answers 2

2
min = 0;
max = 0;  

for ( i = 1; i <= n; i++) {
  printf("Enter data item %d: ", i);
  scanf("%f", &data);

  if(data > max)
      max = data;

  if(data < min)
      min = data;

  if(i == 1){min = data;}

  sumx = sumx + data;
  sumx2 = sumx2 + (data * data);
}

EDIT (the above code don't work for initial value of data = 0)

min = INT_MAX; //chux suggestion
max = INT_MIN;  

for ( i = 1; i <= n; i++) {
  printf("Enter data item %d: ", i);
  scanf("%f", &data);

  if(data > max)
      max = data;

  if(data < min)
      min = data;

  sumx = sumx + data;
  sumx2 = sumx2 + (data * data);
}

and correct the while loop

valter

Sign up to request clarification or add additional context in comments.

2 Comments

Rather than min = 0; max = 0;, suggest min = INT_MAX; max = INT_MIN;.
@chux Very clever. I haven't thought of that
2

You are on the right track. Your program will not compile because of some serious syntax errors.

The following is the output of diff with respect to a working version:

25,26c25,28
<       scanf(%d", &n);
< 
---
>       scanf("%d", &n);
>   
>   }
>   
29c31
<     sumx = sumx2 = 0.0
---
>     sumx = sumx2 = 0.0;
39c41
<     mean = sums / n;
---
>     mean = sumx / n;
46a49,50
>   
>   return 0;

The corrected errors are as follows:

  1. The format string in your scanf function was missing an opening parenthesis.
  2. The while loop was missing a closing bracket.
  3. The variable "sums" was undefined.

Also the compiler will expect a return value but this would most likely only generate a warning if it is missing. It is also worth noting that the while loop cannot do anything useful, but it can cause infinite recursion if the user enters a number less than or equal to zero.

The logic you need in your for loop follows:

sumx = sumx2 = 0.0;
max = 0;
min = 0;

for ( i = 1; i <= n; i++) {
  printf("Enter data item %d: ", i);
  scanf("%f", &data);

  if(data > max)
  max = data;
  else if(data < min)
  min = data;

if( i ==1 )
  min = data;

  sumx = sumx + data;
  sumx2 = sumx2 + (data * data);
}

There may still be other logic errors. If you need more help just let me know.

Good luck!

3 Comments

I appreciate the input, thank you! I edited the original post with what I'm trying to get out of the program, which is the min/max values entered by the user. The code I'm using compiles fine and mostly works, however, my min value is always 0 no matter what is entered.
You need to initialize the variables min and max outside of the loop. Otherwise it will never work because each iteration will reset the values. Also you cannot initialize the value of min to zero. It should be initialized to the largest possible value of the data type. Or else it can be initialized to the first entry.
Please note that the largest possible value of a float is much greater than the largest integer. So it is more practical in this case to initialize min to the first entry.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.