SpK
SpK

Reputation: 33

Loop in C gone wrong

I have this code

for (i = 0; i < s; i++) // for i from 0 to Runners
{
    for (j = 0; j < 4; j++) //for j from 0 to laps
    {
        printf("\nEnter the time of lap %d for runner %d in minutes: ", j+1, i+1); // prompt for time for each runner in minutes
        while (scanf("%d", &Runnerm[i][j]) != 1) // While scanf returns not equal to 1 (value parsed == TRUE)
        {
            while ((ch = getchar()) != '\n') putchar(ch); // check if ch=character, compare last entry (\n if character)
            printf(" is not an integer.\nPlease enter only an "); // print error
            printf("integer, such as 1, 5, or 9 : \n"); // cnt print error
        }

        printf("Enter the time of lap %d for runner %d in seconds: ", j+1, i+1); //prompt for time for each runner in seconds
        while (scanf("%d", &Runners[i][j]) != 1) // While scanf returns not equal to 1 (value parsed == TRUE)
        {
            while ((ch = getchar()) != '\n') putchar(ch); // check if ch=character, compare last entry (\n if character)
            printf(" is not an integer.\nPlease enter only an "); // print error
            printf("integer, such as 1, 5, or 9 : \n"); // cnt print error
        }

        printf(" \n check 1 \n ");
        printf("\n %d minutes -- %d seconds \n", Runnerm[i][j], Runners[i][j]); // Correct Check!

        printf(" \n check 1.5 \n ");
        printf("\n %d minutes -- %d seconds \n", Runnerm[i][0], Runners[i][0]); // Incorrect Check!
        printf("\n %d minutes -- %d seconds \n", Runnerm[i][1], Runners[i][1]); // Incorrect Check!
        printf("\n %d minutes -- %d seconds \n", Runnerm[i][2], Runners[i][2]); // Incorrect Check!
        printf("\n %d minutes -- %d seconds \n", Runnerm[i][3], Runners[i][3]); // Incorrect Check!

    }
}

The problem is, when check 1.5 runs the second time, Runnerm[i][j] gets values from Runners[i+1][j]

In short, when i changes value, the previous stored value of Runnerm will get the value of the Runners that is currently being stored..

Why is that? I can't find the reason..

Edit: The "check 1" and "check 1.5" exist for the whole purpose of -checking- the values at any given time of the loop. SO, even if I delete them, the program will still be broken. ALSO when this is all fixed, the checks will be deleted, because I won't need to check the values any more. Thank you all for answering but I don't get why you suggest to do something with the checks, they are there for debugging. :\

Edit Found what caused the problem, when I used another version of the program that used fewer arrays, the problem was solved, when I started adding random un-used arrays, the problem appears. Why is that? (New problem) Could it be that the memory overlaps to the one that Runners[i][j] use?

Upvotes: 1

Views: 129

Answers (3)

Mahonri Moriancumer
Mahonri Moriancumer

Reputation: 6003

Fleshed-out the question code to an actual program:

#include <stdio.h>

int main()
  {
  int s = 3;
  int i;
  int j;
  int ch;
  int Runnerm[s][4]; //Runners / minutes
  int Runners[s][4]; //Runners / seconds

  for (i = 0; i < s; i++) // for i from 0 to Runners
     {
     for (j = 0; j < 4; j++) //for j from 0 to laps
        {
        printf("\nEnter the time of lap %d for runner %d in minutes: ", j+1, i+1); // prompt for time for each runner in minutes
        while (scanf("%d", &Runnerm[i][j]) != 1) // While scanf returns not equal to 1 (value parsed == TRUE)
           {
           while ((ch = getchar()) != '\n') putchar(ch); // check if ch=character, compare last entry (\n if character)
           printf(" is not an integer.\nPlease enter only an "); // print error
           printf("integer, such as 1, 5, or 9 : \n"); // cnt print error
           }

        printf("Enter the time of lap %d for runner %d in seconds: ", j+1, i+1); //prompt for time for each runner in seconds
        while (scanf("%d", &Runners[i][j]) != 1) // While scanf returns not equal to 1 (value parsed == TRUE)
           {
           while ((ch = getchar()) != '\n') putchar(ch); // check if ch=character, compare last entry (\n if character)
           printf(" is not an integer.\nPlease enter only an "); // print error
           printf("integer, such as 1, 5, or 9 : \n"); // cnt print error
           }

        printf("check 1 \n ");
        printf("\t %d minutes -- %d seconds \n", Runnerm[i][j], Runners[i][j]); // Correct Check!

        printf("check 1.5 \n ");
        printf("\t %d minutes -- %d seconds \n", Runnerm[i][0], Runners[i][0]); // Incorrect Check!

Added the following if(j > ... in order to not print uninitialized array values:

        if(j > 0) printf("\t %d minutes -- %d seconds \n", Runnerm[i][1], Runners[i][1]); // Incorrect Check!
        if(j > 1) printf("\t %d minutes -- %d seconds \n", Runnerm[i][2], Runners[i][2]); // Incorrect Check!
        if(j > 2) printf("\t %d minutes -- %d seconds \n", Runnerm[i][3], Runners[i][3]); // Incorrect Check!
        }
     }

  return(0);
  }

Then I compiled the code:

SLES11SP2:~/SO> gcc -Wall -o test *.c

And ran the code:

SLES11SP2:~/SO> ./test

Enter the time of lap 1 for runner 1 in minutes: 1
Enter the time of lap 1 for runner 1 in seconds: 2
check 1
   1 minutes -- 2 seconds
check 1.5
   1 minutes -- 2 seconds

Enter the time of lap 2 for runner 1 in minutes: 3
Enter the time of lap 2 for runner 1 in seconds: 4
check 1
   3 minutes -- 4 seconds
check 1.5
   1 minutes -- 2 seconds
   3 minutes -- 4 seconds

Enter the time of lap 3 for runner 1 in minutes: 5
Enter the time of lap 3 for runner 1 in seconds: 6
check 1
   5 minutes -- 6 seconds
check 1.5
   1 minutes -- 2 seconds
   3 minutes -- 4 seconds
   5 minutes -- 6 seconds

Enter the time of lap 4 for runner 1 in minutes: 7
Enter the time of lap 4 for runner 1 in seconds: 8
check 1
   7 minutes -- 8 seconds
check 1.5
   1 minutes -- 2 seconds
   3 minutes -- 4 seconds
   5 minutes -- 6 seconds
   7 minutes -- 8 seconds

Enter the time of lap 1 for runner 2 in minutes: 9
Enter the time of lap 1 for runner 2 in seconds: 10
check 1
   9 minutes -- 10 seconds
check 1.5
   9 minutes -- 10 seconds

Enter the time of lap 2 for runner 2 in minutes: 11
Enter the time of lap 2 for runner 2 in seconds: 12
check 1
   11 minutes -- 12 seconds
check 1.5
   9 minutes -- 10 seconds
   11 minutes -- 12 seconds

Enter the time of lap 3 for runner 2 in minutes: 13
Enter the time of lap 3 for runner 2 in seconds: 14
check 1
   13 minutes -- 14 seconds
check 1.5
   9 minutes -- 10 seconds
   11 minutes -- 12 seconds
   13 minutes -- 14 seconds

Enter the time of lap 4 for runner 2 in minutes: 15
Enter the time of lap 4 for runner 2 in seconds: 16
check 1
   15 minutes -- 16 seconds
check 1.5
   9 minutes -- 10 seconds
   11 minutes -- 12 seconds
   13 minutes -- 14 seconds
   15 minutes -- 16 seconds

Enter the time of lap 1 for runner 3 in minutes: 17
Enter the time of lap 1 for runner 3 in seconds: 18
check 1
   17 minutes -- 18 seconds
check 1.5
   17 minutes -- 18 seconds

Enter the time of lap 2 for runner 3 in minutes: 19
Enter the time of lap 2 for runner 3 in seconds: 20
check 1
   19 minutes -- 20 seconds
check 1.5
   17 minutes -- 18 seconds
   19 minutes -- 20 seconds

Enter the time of lap 3 for runner 3 in minutes: 21
Enter the time of lap 3 for runner 3 in seconds: 22
check 1
   21 minutes -- 22 seconds
check 1.5
   17 minutes -- 18 seconds
   19 minutes -- 20 seconds
   21 minutes -- 22 seconds

Enter the time of lap 4 for runner 3 in minutes: 23
Enter the time of lap 4 for runner 3 in seconds: 24
check 1
   23 minutes -- 24 seconds
check 1.5
   17 minutes -- 18 seconds
   19 minutes -- 20 seconds
   21 minutes -- 22 seconds
   23 minutes -- 24 seconds
SLES11SP2:~/SO> 

It seem to work fine.

Upvotes: 1

AJMansfield
AJMansfield

Reputation: 4129

Move these lines of code:

printf(" \n check 1.5 \n ");
printf("\n %d minutes -- %d seconds \n", Runnerm[i][0], Runners[i][0]);
printf("\n %d minutes -- %d seconds \n", Runnerm[i][1], Runners[i][1]);
printf("\n %d minutes -- %d seconds \n", Runnerm[i][2], Runners[i][2]);
printf("\n %d minutes -- %d seconds \n", Runnerm[i][3], Runners[i][3]);

to outside the j loop, remove the redundant prompt, and get rid of the magic constants used as loop bounds.

#define RUNNERS s
#define LAPS 4

for (i = 0; i < RUNNERS; i++)
{
    for (j = 0; j < LAPS; j++)
    {
        printf("Enter the time of lap %d for runner %d in seconds: ", j+1, i+1)
        while (scanf("%d", &Runners[i][j]) != 1)
        {
            while ((ch = getchar()) != '\n') putchar(ch);
            printf(" is not an integer.\nPlease enter only an integer, such as 1, 5, or 9 : \n");
        }

        printf(" \n check 1 \n ");
        printf("\n %d minutes -- %d seconds \n", Runnerm[i][j], Runners[i][j]);

    }

    printf(" \n check 1.5 \n ");

    for (j = 0; j < LAPS; j++)
        printf("\n %d minutes -- %d seconds \n", Runnerm[i][j], Runners[i][j]);
}

Although personally, I think that nonfunctionality is the least of your problems: your code is overcommented and soaking WET, and your output has way too many line breaks in it.

Upvotes: 1

David C. Rankin
David C. Rankin

Reputation: 84531

It appears that your printf statement is showing the next runner/next time, but recording for the runner/time before:

printf("\nEnter the time of lap %d for runner %d in minutes: ", j+1, i+1);
                                                                ^^^^ ^^^^

Why not i j

Upvotes: 2

Related Questions