C program that prompts a user to enter five cities and the temperature

Page 1 of 1

4 Replies - 166 Views - Last Post: 25 February 2018 - 06:09 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=409494&amp;s=5aa77ea50456b1f8c32ba2571ad17867&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

Reputation: 0
• Posts: 2
• Joined: 25-February 18

C program that prompts a user to enter five cities and the temperature

Posted 25 February 2018 - 12:04 PM

I'm currently writing a C program that prompts a user to enter five cities and the temperatures of those cities over a span of five years and also to display the maximum of the temperatures given. So here's a function I came up with to display the maximum temperature when called;

]
```      [ int max_temp(float temps_arr[])
{
int max = 0;
for(int k = 0;k<=4;k++)
{
max = temps_arr[0];
if(max>temps_arr[k])
{
max = temps_arr[k];
printf("The maximum Temperature for city %d is %d\n", k+1, max);
}
}
}
And also how i integrated it into my program;

for(int i = 0;i<=4;i++)
{
printf("Enter city %d\n", i+1);
scanf("%s",&cities[i]);

for(int j = 0;j<=4;j++)
{
printf("Enter temperature %d\n", j+1);
scanf("%d", &temps[j]);
max_temp(temps);
}
}

return 0;

```

I'm not getting desired result so any input is appreciated!..

Is This A Good Question/Topic? 0

Replies To: C program that prompts a user to enter five cities and the temperature

#2 baavgai

• Dreaming Coder

Reputation: 7112
• Posts: 14,827
• Joined: 16-October 07

Re: C program that prompts a user to enter five cities and the temperature

Posted 25 February 2018 - 12:30 PM

Strangely, you have most of the right pieces, almost.
```// I don't like 4 as a magic number, but we'll ignore that for now
int max_temp(float temps_arr[]) {
int max = 0; // why is this 0
for(int k = 0;k<=4;k++)  {
// this would make sense outside the loop
// as it stands, it resets each iteration
max = temps_arr[0];
// this makes no sense, max == temps_arr[0] is always true here
if(max>temps_arr[k]) {
max = temps_arr[k];
// this also makes no sense, here
printf("The maximum Temperature for city %d is %d\n", k+1, max);
}
}
}

```

```// returns the index of max value
int get_max_index(float *, int size);
//...

int max_index = get_max_index(temps, 5);
// now that you have the right, position
printf("The maximum Temperature for city %s is %d\n", cities[max_index], temps[max_index]);

```

Hope this helps.

Reputation: 0
• Posts: 2
• Joined: 25-February 18

Re: C program that prompts a user to enter five cities and the temperature

Posted 25 February 2018 - 01:00 PM

i'm sorry but i don't understand your solution.

#4 baavgai

• Dreaming Coder

Reputation: 7112
• Posts: 14,827
• Joined: 16-October 07

Re: C program that prompts a user to enter five cities and the temperature

Posted 25 February 2018 - 03:25 PM

Well, it's more a suggestion than a solution. The solution is for you to find.

Since you've failed to offer a complete program, it's hard to see what you're after. If you offer that, perhaps others might chime in with suggestions that resonate better.

To me, your shown code makes little sense, saving cities as a numeric into an array for no apparent reason.

However, for completeness, here's a more fleshed out example of how you'd want to implement a max function:
```#include <stdio.h>
#include <stdlib.h>

int get_max_index(float *, int);

int main() {
float a[] = { 79.5, 89.2, 91.7, 85.3 };
int idx = get_max_index(a, sizeof(a)/ sizeof(*a));
printf("the max index is %d, value %0.2f\n", idx, a[idx]);
return 0;
}

int get_max_index(float *a, int size) {
}

```

Result:
```the max index is 2, value 91.70

```

Note, I thought the index was important, given your odd cities thing. Now I see it's nested, so perhaps simply float get_max(float *, int); might work for you.

#5 Skydiver

• Code herder

Reputation: 6062
• Posts: 20,872
• Joined: 05-May 12

Re: C program that prompts a user to enter five cities and the temperature

Posted 25 February 2018 - 06:09 PM

As an aside, as you go further with your programming career, realize that using parallel arrays or parallel data structures is often leads to a poor architecture that is a pain to debug and maintain. It's better to keep related data together. So instead of having two arrays:
```char cities[5][80];
int  temps[5];

```

this would be a better plan to have a single array:
```struct CityTemp
{
char Name[80];
int  Temp;
} CityTemps[5];

```