how do I solve this exercise in c / c ++

-1

In the end I still have to figure out which stores are showing a price within the media, someone helps me;

#include<bits/stdc++.h>
struct difsites{
    double preco;
    char loja[50];
    char site[50];
};
int main(){
    int i,j;
    double soma, media;
    struct difsites MP;
    for(i=0;i<6;i++){
        printf("Insira o nome da loja: ");
        scanf(" %50[^\n]s", &MP.loja);
        printf("Insira a URL: ");
        scanf(" %50[^\n]s", &MP.site);
        printf("Insira o valor do computador: ");
        scanf(" %lf", &MP.preco);
        soma+=MP.preco;
        media=soma/6.0;
    }
    printf("Preco medio: R$%.2lf\n",media);
}
    
asked by anonymous 23.06.2018 / 03:56

1 answer

3

First error in your code is in the variable "sum", we should initialize it as 0 if we are going to increment from it, because of the memory garbage.

double soma = 0, media;

For you to store the stores, I recommend creating a vector of structures. Staying like this

struct difsites MP[6];

The second error is at the time of calculating the media, you will only calculate the average when the cycle is finished.

 for(i=0;i<6;i++){  //Le cada loja do vetor, começando da posicao 0 até 5
        printf("Insira o nome da loja: ");
        scanf(" %50[^\n]s", &MP[i].loja);
        printf("Insira a URL: ");
        scanf(" %50[^\n]s", &MP[i].site);
        printf("Insira o valor do computador: ");
        scanf(" %lf", &MP[i].preco);

        soma = soma + MP[i].preco; //Soma todos os preços das lojas
    }

    media = soma/6.0;

Then to check if the store is within the average it is only to cycle again with a condition if less than average, display the price.

printf("Media do preco = %lf\n",media);
    for(i=0; i <6; i++){
        if(MP[i].preco <= media){
            printf("Loja %s | Preco =  R$%.2lf\n",MP[i].loja,MP[i].preco);
        }
    }
    
23.06.2018 / 05:27