Thread: Need help with code

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    6

    Need help with code

    code is intended to ask user for how many numbers are to be entered, then display the min, max and average.

    everything is working correctly besides the Minimum (random number appears), and the average.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    int main(void)
    {
       float sum, min = 0, max = 0, mean = 0;
       double x = 0, y = 0;
       int i = 0, test = 0, test1 = 0;
    
    
    do{
             printf("How many values are to be entered?\n");
             test1 = scanf("%f", &sum);
    
    
             if(test1 == 0 || sum < 0)
             {
                while(getchar() != '\n');
                printf("INPUT ERROR!\n");
                test1 = 0;
             }
    
    
    }
    while(test1 == 0);
    
    
             printf("\n");
    
    
    for(i = 1; i <= sum; i++)
    
    
            do{
                printf("Value %d: ", i);
                test = scanf("%lf", &x);
    
    
            if(test == 0)
            {
             while(getchar() != '\n');
             printf("INPUT ERROR!\n");
            }
             }
    while(test == 0);
    
    
          y = y +x;
          mean = y / sum;
    
    
          if(i == 1){
             min = x;
             max = x;
          }
    
    
          if(x > max){
             max = x;
          }
          else if (x < min){
             min = x;
          }
    
    
       printf("The minimum value is %g, ",min);
       printf("the maximum value is %g, ",max);
       printf("and the average value is %g\n",mean);
    
    
    return(0);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,165
    It's a good example of why indentation matters.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main(void)
    {
      float sum, min = 0, max = 0, mean = 0;
      double x = 0, y = 0;
      int i = 0, test = 0, test1 = 0;
    
      do {
        printf("How many values are to be entered?\n");
        test1 = scanf("%f", &sum);
        if (test1 == 0 || sum < 0) {
          while (getchar() != '\n');
          printf("INPUT ERROR!\n");
          test1 = 0;
        }
      }
      while (test1 == 0);
    
    
      printf("\n");
      for (i = 1; i <= sum; i++)
        do {
          printf("Value %d: ", i);
          test = scanf("%lf", &x);
          if (test == 0) {
            while (getchar() != '\n');
            printf("INPUT ERROR!\n");
          }
        }
        while (test == 0);
    
      y = y + x;
      mean = y / sum;
      if (i == 1) {
        min = x;
        max = x;
      }
    
      if (x > max) {
        max = x;
      } else if (x < min) {
        min = x;
      }
    
      printf("The minimum value is %g, ", min);
      printf("the maximum value is %g, ", max);
      printf("and the average value is %g\n", mean);
    
      return (0);
    }
    Your for loop scope ends at line 33.
    But it looks like you meant for it to end at line 46.

    The problem?
    Line 24 has no opening brace, so the for loop only executes the next logical statement - the do while loop.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    6

    Reply

    Thank you very much. i figured it was an indentation problem, but the more i messed with it the worse it got. again thank you!

    Quote Originally Posted by Salem View Post
    It's a good example of why indentation matters.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main(void)
    {
      float sum, min = 0, max = 0, mean = 0;
      double x = 0, y = 0;
      int i = 0, test = 0, test1 = 0;
    
      do {
        printf("How many values are to be entered?\n");
        test1 = scanf("%f", &sum);
        if (test1 == 0 || sum < 0) {
          while (getchar() != '\n');
          printf("INPUT ERROR!\n");
          test1 = 0;
        }
      }
      while (test1 == 0);
    
    
      printf("\n");
      for (i = 1; i <= sum; i++)
        do {
          printf("Value %d: ", i);
          test = scanf("%lf", &x);
          if (test == 0) {
            while (getchar() != '\n');
            printf("INPUT ERROR!\n");
          }
        }
        while (test == 0);
    
      y = y + x;
      mean = y / sum;
      if (i == 1) {
        min = x;
        max = x;
      }
    
      if (x > max) {
        max = x;
      } else if (x < min) {
        min = x;
      }
    
      printf("The minimum value is %g, ", min);
      printf("the maximum value is %g, ", max);
      printf("and the average value is %g\n", mean);
    
      return (0);
    }
    Your for loop scope ends at line 33.
    But it looks like you meant for it to end at line 46.

    The problem?
    Line 24 has no opening brace, so the for loop only executes the next logical statement - the do while loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-19-2012, 01:58 PM
  2. Replies: 1
    Last Post: 03-10-2010, 11:28 AM
  3. Replies: 14
    Last Post: 04-01-2008, 02:23 AM
  4. Having trouble translating psudeo-code to real-code.
    By Lithorien in forum C++ Programming
    Replies: 13
    Last Post: 10-05-2004, 07:51 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM

Tags for this Thread