Answer the question
In order to leave comments, you need to log in
What is the error in calculating the sum of a sequence of numbers?
I solve the problem, it is necessary to determine the sum of numbers of a non-empty sequence followed by 0. Calculate the arithmetic and geometric mean. Depends on the sum of numbers, the console shows incorrect counting results, for example, I enter 2 2 0, the compiler answer is: 11271656 .
Grateful for any help!
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
int x;
float sum = 0, prois = 1, sumg, suma, n = 0;
do
{
scanf("%f", &x);
n = n++;
sum = sum + x;
prois = prois*x;
suma = sum / n;
sumg = pow(prois, 1 / n);
} while (x != 0);
printf("Suma %d", &sum);
getch();
}
Answer the question
In order to leave comments, you need to log in
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
int n = 0;
float sum = 0, prois = 1, sumg, suma, x;
for (;;) {
scanf("%f", &x);
if (x == 0)
break;
n++;
sum = sum + x;
prois = prois*x;
}
if (n > 0) {
suma = sum / n;
sumg = pow(prois, 1. / n);
}
printf("Suma %f", sum);
getch();
}
Judging by the fact that "int x;" should be "scanf("%d", &x);". The product will always be equal to zero, since it will be multiplied by it first, and only then exit from the loop.
@jcmvbkbc
Thank you for your attention! I'm trying to print the geometric mean, but it's always 1. I can't figure it out.
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
int n = 0;
float sum = 0, prois = 1, sumg, suma, x;
for (;;) {
scanf("%f", &x);
if (x == 0)
break;
n++;
sum = sum + x;
prois = prois*x;
}
if (n > 0) {
suma = sum / n;
float step = 1 / n;
sumg = pow(prois, step);
}
printf("Suma %f", sum);
printf("Cped. arifmetich %f \n ", suma);
printf("Cped. geometri4eskaya %f", sumg);
getch();
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question