0

I'm writing a C program that is meant to return the temperature of five cities and I'm quite stuck. I created a for loop to loop through the array of cities I created and another one to loop through another array of temperatures.
Here's the code:

// program to accept yearly temperature and display the max and minumum temp of five cities
#include <stdio.h>
int main() {
    //prompt user for yearly temperature of the five cities

    char cities[5][20];
    float temps[25];
    int i = 0;
    printf("Please enter the yearly temperature of the five cities\n");
    for (i; i <= 4; i++) {
      printf("Enter city %d \n", i + 1);
      scanf("%s", & cities[i]);

      for (i; i <= 4; i++) {
        printf("Enter temperatures for city %d\n", i + 1);

        for (i; i <= 24; i++) {
          printf("For year %d\n", i + 1);

          scanf("%f", & temps[i]);
          if (i == 5) {
            continue;
          }
        }
      }
    }
    return 0;
}

The problem I'm having is that when I run the code, instead of the third loop to continue running if i = 5, it just continues. here's a screenshot. page1page2

so as you can see the code just keeps running till twenty five and then just ends. it would be helpful if any one tells me what i'm doing wrong. thanks.

alk
  • 69,737
  • 10
  • 105
  • 255
  • 1
    You are using the same variable for both inner and outer loop - that cannot work well – UnholySheep Feb 25 '18 at 10:00
  • 2
    For pure textual output, it is *highly preferred* to just copy that actual text into your question, not as images. See also [your previous question](https://stackoverflow.com/q/48928606/2564301), where someone else helpfully re-typed everything. – Jongware Feb 25 '18 at 10:04
  • 1
    Why is iit always 'i', 'j' etc? What is wrong with, say, 'cityIndex' and 'tempIndex'? – Martin James Feb 25 '18 at 10:06
  • 1
    @MartinJames: or in this case 'i', 'i', 'i' ... – Jongware Feb 25 '18 at 10:07
  • thanks for that @usr2564301 i'll take that in mind –  Feb 25 '18 at 13:10

2 Answers2

2

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.

Stephen Docy
  • 4,738
  • 7
  • 18
  • 31
  • please can you explain more about the % operation –  Feb 25 '18 at 13:15
  • and can you please explain what you did there in the second for loop ? the one with the j % numYears @StephenDocy –  Feb 25 '18 at 13:21
  • because it's giving me an error code that's saying it's only allowed in c11 –  Feb 25 '18 at 13:28
2

You need to declare a variable for each loop. If you wanted three different i variables you should have written:

// program to accept yearly temperature and display the max and minimum temp of five cities
#include <stdio.h>
int main() {
    //prompt user for yearly temperature of the five cities

    char cities[5][20];
    float temps[25];
    printf("Please enter the yearly temperature of the five cities\n");
    for (int i = 0; i <= 4; i++) {
      printf("Enter city %d \n", i + 1);
      scanf("%s", & cities[i]);

      for (int i = 0; i <= 4; i++) {
        printf("Enter temperatures for city %d\n", i + 1);

        for (int i = 0; i <= 24; i++) {
          printf("For year %d\n", i + 1);

          scanf("%f", & temps[i]);
          if (i == 5) {
            continue;
          }
        }
      }
    }
    return 0;
}

Note that this code is only valid in C99. Earlier versions of the language requires you to use different variable names in for loops.

However in the provided code above, your continue statement will have no effect since the condition i == 5 is the last instruction in the loop. If you want to terminate the loop you should use break and not continue. In this case I'd define the boundaries of that loop's i variable to be i < 5 and get rid of the if statement altogether.
You should learn about variable scoping in C. Each i variable declared here is a whole different variable, as opposed to your code which reuses the same i variable all over again which results in early termination of the program.
I suggest you to always declare a variable for each for loop so your code will always be clearer.

The code you provided is also algorithmically wrong. What you want is to repeat the collection process of temperatures for of the five cities five times. The total iteration number should be 25 in that case because a for loop requires O(n) iterations and a nested for loop requires O(n^k) iterations where k is the number of for nested loops. Each loop reoccurs 5 times so 5^2 = 25.

the_drow
  • 18,571
  • 25
  • 126
  • 193
  • What happens is you add `-Wshadow` to your compiler string? (yes, it's valid, but is it smart?) – David C. Rankin Feb 25 '18 at 10:26
  • I'm not sure I follow. Are you suggesting to add -Wshadow to his compilation options? It's probably a good idea to compile with -Wall generally speaking. – the_drow Feb 25 '18 at 10:41
  • If you add `-Wshadow` the compiler will warn about your shadowing of `i` between the loops. As mentioned, it is valid, but can lead to error if a value of `i` is changed outside of the intended scope. – David C. Rankin Feb 25 '18 at 10:43