1

I'm struggling with some errors when I write programs using real values (float and double types). When declaring "real" as double, in watches debugging tool the value relative to this variable isn't showed in a comprehensible way. Also, the program doesn't give me the right output and I'd like to know why.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i,integer;
    double real = 0;
    double average = 0;

    printf("enter a real value: ");
    scanf("%f",&real);

    for (i=0; average < real; i++){

        printf("enter an integer: ");
        scanf("%d",&integer);
        average = float((average+integer)/(i+1));
    }
    printf("\n%d numbers have been added!", i);
    printf("\nthe average of all entered numbers is: %.2f",average);

    return 0;
}

Some variables could not correspond because I've translated it right now to make it more comprehensible for you.

I've tried using cast operators, removing them, using float and double in declaration getting no acceptable result (and program won't work either)

EDIT: program is working now, but still have some math problems, result isn't the one i was expecting https://i.stack.imgur.com/Zl9dJ.jpg

user3757339
  • 7
  • 1
  • 4

1 Answers1

1

When I compile your code with GCC, I get this:

gcc -Wall -std=gnu99 -Wextra -Werror -g -O3 -o q q.c
cc1: warnings being treated as errors
q.c: In function ‘main’:
q.c:10:5: error: format ‘%f’ expects type ‘float *’, but argument 2 has type ‘double *’
q.c:16:19: error: expected expression before ‘float’
make: *** [q] Error 1

Those warning flags are useful. To fix the error on line 10, use "%lf" like the comments said.

The error on line 16 is because float is not a function. You can use a typecast like this:

average = (float)((average+integer)/(i+1))

But that just casts the result to a float. If you want to actually do the math with floating point math instead of integer math cast to a float, you need floating point operands. The simplest thing to do is to use floating point literals:

average = (average+integer)/(i+1.0)

Actually, since average is already a double, you're going to do floating point math with this expression regardless. Though as gnometorule pointed out in the comments, this is probably not the calculation you want to do. You might want to build a sum of all the numbers entered, then divide by the number of them at the end. That will get you the mean.

You also want to print a newline at the end of your last printf:

printf("\nthe average of all entered numbers is: %.2f\n",average);

When it comes to debugging, I don't know about Watches, but gdb does this:

(gdb) p real
$1 = 7.2999999999999998
(gdb) p average
$3 = 121.14920634920635

You might want to compile with optimizations turned off to get more complete information in the debugger, but doubles should print out just fine. If you're looking at the memory that contains them, you'll want to look into [IEEE 754].

nmichaels
  • 49,466
  • 12
  • 107
  • 135
  • 1
    His formula for "average" is wrong too - at least the final printf indicates he doesn't want the strange incremental pairwise average (6, 10, 20 - average: 12; his formula - 9 1/3). He might actually mean what he coded, but I doubt that. – gnometorule Jun 19 '14 at 17:03
  • my program should ask the user to enter integers until the average exceed the real value. why my formula for average isn't working? i'm missing something... – user3757339 Jun 19 '14 at 17:35
  • @user3757339: Walk through it. If the user enters 6, 10, and 20, the mean is `(6+10+20)/3=12`. Your program will calculate `((((6/1)+10)/2)+20)/3=9.333...`. – nmichaels Jun 19 '14 at 17:39
  • @user3757339: One way to fix it would be to keep track of the sum of the numbers your user enters as you go. Then you can calculate average as `sum/(i+1.0)` whenever you need it. – nmichaels Jun 19 '14 at 17:41
  • i'm trying to change the condition in the for function, loop should now sto whether the average exceed the rational number or the user inputs 10 integers. for (i=0; i<10 || average < real; i++){ what's not working with the logic or operator? – user3757339 Jun 19 '14 at 19:20
  • @user3757339: The middle of the for loop is a "while" condition. The loop terminates when it's `false`, so by [De Morgan's law](https://en.wikipedia.org/wiki/De_Morgan%27s_laws), you probably want `&&` in there. – nmichaels Jun 19 '14 at 19:24