4
answers
0
watching
118
views

Hi. This code hass errors when I add first cite. Thx

#include <stdio.h>

#include <string.h>

 

#define NUM_CITIES 5

 

struct City {

char name[20];

float precipitation;

};

 

void rank_cities(struct City cities[], int num_cities) {

struct City temp;

for (int i = 0; i < num_cities - 1; i++) {

for (int j = i + 1; j < num_cities; j++) {

if (cities[i].precipitation > cities[j].precipitation) {

temp = cities[i];

cities[i] = cities[j];

cities[j] = temp;

}

}

}

}

 

int main() {

struct City cities[NUM_CITIES];

float total_precipitation[NUM_CITIES] = {0};

float min_precipitation = 1000000, max_precipitation = -1;

int min_index, max_index;

 

for (int i = 0; i < NUM_CITIES; i++) {

printf("Enter the name of city #%d: ", i + 1);

scanf("%s", cities[i].name);

printf("Enter the amount of precipitation in mm for %s: ", cities[i].name);

scanf("%f", &cities[i].precipitation);

total_precipitation[i] = cities[i].precipitation;

 

if (cities[i].precipitation < min_precipitation) {

min_precipitation = cities[i].precipitation;

min_index = i;

}

if (cities[i].precipitation > max_precipitation) {

max_precipitation = cities[i].precipitation;

max_index = i;

}

}

 

float total = 0, average;

for (int i = 0; i < NUM_CITIES; i++) {

total += total_precipitation[i];

}

average = total / NUM_CITIES;

 

rank_cities(cities, NUM_CITIES);

 

printf("\nCity Rankings:\n");

for (int i = 0; i < NUM_CITIES; i++) {

printf("%d. %s (%.1f mm)\n", i + 1, cities[i].name, cities[i].precipitation);

}

 

printf("\nCity with minimum precipitation:\n%s (%.1f mm)\n", cities[min_index].name, min_precipitation);

 

printf("\nCity with maximum precipitation:\n%s (%.1f mm)\n", cities[max_index].name, max_precipitation);

 

printf("\nAverage precipitation across all cities: %.1f mm\n", average);

 

return 0;

}

For unlimited access to Homework Help, a Homework+ subscription is required.

Unlock all answers

Get 1 free homework help answer.
Already have an account? Log in
Already have an account? Log in
Already have an account? Log in
Avatar image
Read by 1 person
Already have an account? Log in

Related questions

Related Documents

Weekly leaderboard

Start filling in the gaps now
Log in