-1

This software may calulate the min e max temperature of some cities entered by keybord, but I've some problems with the scanf or the printf, because it shows the ram register value instood the effictive data. I don't know why my simple c script don't work any idea? (may the problems is on the scanf %g of the &rilevazioni[i].temp)

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

typedef struct {
    char nome [30] ;
    double temp;
}meteo;

int main(int argc, char* argv[]) {
    meteo rilevazioni[20];
    meteo min, max;
    double media = 0;
    int i = 0;
    int app = 0;
    int n;
    printf("quante citta' vuole inserire? (max 20 citta')\n");
    scanf("%d",&n);
    printf("\n Inseriamo le rilevazioni");
    for (i = 0; i < n; i++)
    {
        printf("\n Inserisci il nome : ");
        scanf("%s", rilevazioni[i].nome);
        printf("\n Inserisci la temperatura : ");
        fflush(stdin);
        scanf("%g", &rilevazioni[i].temp);
        if (i == 0)
            min = max = rilevazioni[i];
        else
            if (min.temp > rilevazioni[i].temp)
                min = rilevazioni[i];
            else if (max.temp < rilevazioni[i].temp)
                max = rilevazioni[i];
        media += rilevazioni[i].temp;
    }
    fflush(stdin);
    media = media / n;
    printf("\n La citta' con temperatura minima e': %s con una temperatura di: %g gradi ", min.nome, min.temp);
    printf("\n La citta' con temperatura massima e': %s con una temperatura di: %g gradi ", max.nome, max.temp);
    printf("\n Le citta' con temperatura maggiore rispetto alla media (%f) sono:", media);
    for (i = 0; i < n; i++)
    {
        if (rilevazioni[i].temp > media)
        printf("\n La citta' : %s ha una temperatura di: %g gradi ", rilevazioni[i].nome, rilevazioni[i].temp);
    }
}
  • 2
    [`fflush(stdin);` is undefined behaviour, don't do it.](https://stackoverflow.com/a/38325926/2173917) – Sourav Ghosh Dec 09 '20 at 11:58
  • Please make sure you have a more descriptive title in your question so contributors can look for your post, and your questions become more "searchable" for future reference – chris Dec 09 '20 at 13:55

2 Answers2

1

Compiling your code will GCC and warnings on (gcc temp.c -Wall), it says:

test.c:25:17: warning: format ‘%g’ expects argument of type ‘float *’, but argument 2 has type ‘double *’

And indeed, %g expects a float*, if you want to read a double use %lg.

This is confusing, because there is no such distinction when calling printf(), but that is because when calling printf all arguments suffer default promotion, and all float values are promoted to double. But with scanf() you are passing pointers to values, so that promotion does not apply and you must be extra careful.

rodrigo
  • 94,151
  • 12
  • 143
  • 190
0

This code can help you ,I compile my code with GCC

I found the index of min & max temperature (i_min & i_max) and I print the two cities which they have max_temperature & min_temperature like this

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

typedef struct meteo{
    char nome [30] ;
    double temp;
}meteo;

int main(int argc, char* argv[]) {


    double media = 0;
    int i = 0, app = 0,n=0;
    do
    {
         printf("quante citta' vuole inserire?\n");
         scanf("%d",&n);
    }while(n<1||n>20);

    meteo rilevazioni[n];
    printf("\n Inseriamo le rilevazioni");
    double min, max;
    int i_min=0,i_max=0,k=0;
    
    for (i = 0; i < n; i++)
    {
        printf("\n Inserisci il nome : ");
        scanf(" %s", rilevazioni[i].nome);
        printf("\n Inserisci la temperatura : ");
        scanf("%lf", &rilevazioni[i].temp);
        if (i == 0)
        {
            min = max = rilevazioni[0].temp;
        }
        else
        {k++;
            if (rilevazioni[k].temp<min)
            {
                i_min=k;
                min = rilevazioni[k].temp;
            }
            if (rilevazioni[k].temp > max)
            {
                i_max=k;
                max = rilevazioni[k].temp;
            }
        }
    }

printf("\n La citta' con temperatura minima e': %s con una temperatura  : %lf gradi ", rilevazioni[i_min].nome, rilevazioni[i_min].temp);
printf("\n La citta' con temperatura massima e': %s con una temperatura : %lf gradi ", rilevazioni[i_max].nome, rilevazioni[i_max].temp);

}
MED LDN
  • 684
  • 1
  • 5
  • 10