program in C to calculate the average , average and quantity of odd numbers

Asked

Viewed 321 times

1

The point is this

:Make a C program that reads from the user an integer vector with n positions in the range [5, 200]. Repeat the value of n until a valid value is entered by user. After reading the n values typed by the user some statistics should be extracted from this vector: a) Print the Vector Average on the screen; b) Print the median of the vector on the screen; c) Print on the screen the number of odd numbers and pairs of the vector.

Note: The real numbers must be printed on the screen with precision of two houses decimal;

Assume that the vector is already typed ordered by the user; The simple arithmetic mean corresponds to , where x corresponds to each element vector with n positions. The calculation of the median of ordered data of samples of size n may be performed as follows: if n is odd, the median shall be the central element . If n is even, the median will be the result of the simple mean between the elements and +1;

I tried to solve but I’m not used to this precise system of typing variables in the C language, here is my code (this very precarious):

   #include <stdio.h>

   int main(){

int comeco =0, fim =0;
int v[1000];
int tam = fim - comeco;
int m = 0,md, impar = 0;
float media, mediana;

do{

printf("Digite um numero no intervalo de 5 a 200");
scanf("%i %i", &comeco, &fim);
getchar();

}while(comeco < 5 || fim > 200);

int i = 0;

for (int j = comeco; j < fim; j++) {
    v[i] = j;
    i+=1;
    if(v[i] % 2 != 0){
        impar += 1;
        }
}
for (int me = 0; me < tam; me++) {

    m += v[me];
}
media = (float)m/(float)tam;
printf("%.2f\n", media);
printf("%i impar \n", impar );

if(tam % 2 == 0){
    md = (tam/2);
    mediana = ((float)v[md]+ (float)v[md-1])/2;
}else{
    md=(tam/2);
    mediana=(float)v[md];
}
printf("%.2f", mediana);

}

  • From the statement I understand that you should read the n elements of the vector and not assign a sequential value from beginning to end.

  • Yes, I should do a reading of the n positions in the vector but the values have to be in the range of 5-200. assigns two variables ("start" and "end") for me to be able to attach the values in the loop starting from position 0, in the case of the variable "tam" which is the end - the sequence start is only for me to be able to do the average and median accounts with the right positions in the array. I’m pretty sure it’s a conversion error of variable types but I’m not finding where exactly the error is....

  • I believe that there are several problems of understanding. Reading the statement understand that you should read the amount of values to be treated (n, or in your case tam) and then read these n values where each read value must be in [5,200] (it is a single value and not a pair of values). Note that where you declared int tam = fim - comeco; the value of tam will be zero as it is immediately after int comeco =0, fim =0;.

  • this question has the algorithm for media and median: https://answall.com/questions/490264/diminuir-o-c%c3%b3digo/490322#490322

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.