I think this is what you are after
main()
{
//prompt user for yearly temperature of the five cities
const int numYears = 5;
char cities[5][20];
float temps[25];
int i;
printf("Please enter the yearly temperature of the five cities\n");
for (i = 0; i <= 4; i++)
{
printf("Enter city %d \n", i + 1);
scanf("%s", &cities[i]);
printf("Enter temperatures for city %d\n", i + 1);
for (int j = (i * numYears); j < (i * numYears) + numYears; j++)
{
printf("For year %d\n", (j % numYears) + 1);
scanf("%f", &temps[j]);
}
}
return 0;
}
It inputs 5 cities and 5 temps per city, storing the 5 * 5 total temps in an array of 25 ints
So I fixed a few things. You were re-using the same loop counter i
for all your loops. You should use a different one for each loop, and the general preference with modern C compilers is to declare the loop counter within the loop so
for (int i = 0;
instead of
int i = 0
.
.
for (i = 0;
You also had too many loops, you need two, not three. Each inner loop gets its number of executions multiplied by the outer loop, so an outer loop that executes 5 times, will cause an inner loop of 25 to be executed a total of 125 times. So you really need just an outer loop of 5 (one for each city), and an inner loop of 5 (one for each of the 5 temps for each city), for a total of 5 cities and 25 temps.
And this code was not correct at all, it really did nothing
if (i == 5) {
continue;
}
Finally, for the printf
that displays the year number it is asking for, I use the %
operation to have it say the years as 1-5, instead of saying the years as 1-25. %
may be something you have not been exposed to yet, but it is a very powerful operation for cycling through things in a repeated pattern. If you have questions on what it is doing, certainly go look it up, or feel free to ask a follow-up question.
%
is the modulus operation. It returns the remainder when its operands are divided. For example,
3 % 5 = 3
and 7 % 5 = 2
It is a very useful operation when you want to cycle through a set of numbers with a repeating pattern. In your year case, you stored 5 sets of 5 integers as an array of 25 integers. If we simply did
for (int j = (i * numYears); j < (i * numYears) + numYears; j++)
printf("For year %d\n", j);
you would be prompting the user to input values for years 0 - 24, not what you want. You could use
for (int j = (i * numYears); j < (i * numYears) + numYears; j++)
printf("For year %d\n", j + 1);
that would prompt the user for years 1 - 25, a little clearer, but still not the best. By using
for (int j = (i * numYears); j < (i * numYears) + numYears; j++)
printf("For year %d\n", (j % numYears) + 1);
the prompt asks for years 1-5 when j
goes from 0 - 4, then resets and changes the prompt to years 1-5 again when j
goes from 6 - 10. This is because
(1 % 5) = (6 % 5) = 1
Note we only use this when prompting for the year, we still use j
when storing the value so we actually fill up the array positions 0 - 24.
I apologize if I haven't done the best job explaining how it works, I'm sure there are plenty of tutorials if you search around a bit.