Thread: Nested Loop

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    55

    Nested Loop

    In the code below I want x to increment up by one but every time it finishes the nested loop it resets itself to 0. How do I change it so that it keeps it's value and continues to add one to itself? (My plan is storing the variables from the 1-d array(A) into the 2-d array (pairs))

    Code:
    x = 0;
    
    for(r = 0; r<=HowMany; r++);
    {
    for(c = 0; c <=1; c++)
    {
    pairs[r][c] = A[x];
    x++;
    }
    }
    Full Code
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int main(void)
    
    {
    
    FILE *fp;
    
    
    fp = fopen("test.dat", "r");
    
    if (fp == NULL)
      {
      printf("File test.dat failed to open\n");
      exit (1);
      }
    
    int r, c, x, i, HowMany;
    int pairs[r][c];
    int A[1000];
    
    i = 0;
    
    fscanf(fp, "%d", &x);
    
    while ( !feof(fp) )
    
      {
    
       A[i] = x;
    
       i++;
    
       fscanf(fp, "%d", &x);
    
      }
    
    HowMany = i + 1;
    
    printf("There is %d: \n", HowMany);
    
    printf("%d\n", A[0]);
    printf("%d\n", A[1]);
    printf("%d\n", A[2]);
    printf("%d\n", A[3]);
    printf("%d\n", A[4]);
    printf("%d\n", A[5]);
    printf("%d\n", A[6]);
    printf("%d\n", A[7]);
    
    x = 0;
    
    for(r = 0; r<=HowMany; r++);
    {
    for(c = 0; c <=1; c++)
    {
    pairs[r][c] = A[x];
    x++;
    }
    }
    
    printf("%d ", pairs[0][0]);
    printf("%d\n", pairs[0][1]);
    printf("%d ", pairs[1][0]);
    printf("%d\n", pairs[1][1]);
    printf("%d ", pairs[2][0]);
    printf("%d\n", pairs[2][1]);
    printf("%d ", pairs[3][0]);
    printf("%d\n", pairs[3][1]);
    
    
    
    
    return 0;
    
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    x = 0;
    for(r = 0; r<=HowMany; r++);
    {
        for(c = 0; c <=1; c++)
        {
            pairs[r][c] = A[x];
            x++;
        }
    }
    I think you mean c resets itself every time. X does not. On that note, why do you even use an inner loop, since you only have c as 0 or 1?
    Code:
    for(x = r = 0; r<=HowMany; r++);
    {
        pairs[r][0] = A[x++];
        pairs[r][1] = A[x++];
    }

    Quzah.
    Last edited by quzah; 02-14-2011 at 04:12 PM.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    55
    But, in my attempt to store the 1-d array into the 2-day array the output ends up being this...

    A[1,2,5,7,2,3,9,6]

    pairs [0][0] = 1
    pairs [0][1] = 2
    pairs [1][0] = 1
    pairs [1][1] = 2
    etc.

    Instead of the correct way like this

    pairs [0][0] = 1
    pairs [0][1] = 2
    pairs [1][0] = 5
    pairs [1][1] = 7
    etc.

    How do I correct this problem?

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Some quick notes first:
    Quote Originally Posted by DuckCowMooQuack View Post
    Code:
    int r, c, x, i, HowMany;
    int pairs[r][c];
    You're using r and c here before they've been initialized, meaning pairs could be any size.

    Code:
    while ( !feof(fp) )
    Don't use foef to control a loop: Cprogramming.com FAQ > Why it's bad to use feof() to control a loop.

    Neither of those seem to be the source of the error though. I modified your pairs declaration to be pairs[500][2], and modified your nested loop as follows (note I'm only going half way to HowMany):
    Code:
    int pairs[500][2];
    ...
    
    for(r = 0; r < HowMany / 2; r++); {
        for(c = 0; c < 2; c++) {
            printf("pairs[%d][%d] = A[%d] = %d\n", r, c, x, A[x]);
            pairs[r][c] = A[x];
            x++;
        }
    }
    Here's the amazing output I got (input file contains numbers 20 to 27 in order):
    There is 9:
    20
    21
    22
    23
    24
    25
    26
    27
    pairs[4][0] = A[0] = 20
    pairs[4][1] = A[1] = 21
    0 0
    0 0
    0 0
    0 0
    Yes, for some reason, you initialize r = 0 and by the time you get in that loop it's 4, causing your erroneous output. Then I noticed this:
    Code:
    for(r = 0; r < HowMany / 2; r++); {
    Ditch that semicolon.

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    55
    Thanks! Ok, now is there any suggestion as to what algorithm is best for sorting? I'm going to attempt to sort the numbers from smallest to largest by first number in the pair and then by the second number in the pair...

    Example:

    Original pairs, unsorted - (1,2)(5,7)(2,3)(9,6)
    Sorted by first number in pairs - (1,2)(2,3)(5,7)(9,6)
    Sorted by second number in pairs - (1,2)(2,3)(9,6)(5,7)

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quicksort is pretty good. C has a built in function qsort() that uses that algo. You only need to provide a comparison function. Google should turn up some good examples and tutorials, and as always, come back if you need more specific help.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by DuckCowMooQuack View Post
    In the code below I want x to increment up by one but every time it finishes the nested loop it resets itself to 0. How do I change it so that it keeps it's value and continues to add one to itself? (My plan is storing the variables from the 1-d array(A) into the 2-d array (pairs))

    Code:
    for(r = 0; r<=HowMany; r++);
    Every place you have a for() loop... remove the trailing semicolon.

  8. #8
    Registered User
    Join Date
    Jan 2011
    Posts
    55

    Sorting...

    I kind of have a hang of bubble sort but I'm kind of stuck right now with my code...I don't know how to print it out sorted.

    FULL CODE
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int main(void)
    
    {
    
    FILE *fp;
    
    
    fp = fopen("test.dat", "r");
    
    if (fp == NULL)
      {
      printf("File test.dat failed to open\n");
      exit (1);
      }
    
    int r, c, x, i, HowMany;
    r = 0;
    c = 0;
    int pairs[500][2];
    int A[1000];
    
    i = 0;
    
    fscanf(fp, "%d", &x);
    
    while ( !feof(fp) )
    
      {
    
       A[i] = x;
    
       i++;
    
       fscanf(fp, "%d", &x);
    
      }
    
    A[i] = x;
    
    HowMany = i + 1;
    
    printf("There is %d: \n", HowMany);
    
    printf("%d\n", A[0]);
    printf("%d\n", A[1]);
    printf("%d\n", A[2]);
    printf("%d\n", A[3]);
    printf("%d\n", A[4]);
    printf("%d\n", A[5]);
    printf("%d\n", A[6]);
    printf("%d\n", A[7]);
    
    x = 0;
    
    for(r = 0; r<=HowMany; r++)
    {
    for(c = 0; c <=1; c++)
    {
    pairs[r][c] = A[x];
    x++;
    }
    }
    
    printf("%d ", pairs[0][0]);
    printf("%d\n", pairs[0][1]);
    printf("%d ", pairs[1][0]);
    printf("%d\n", pairs[1][1]);
    printf("%d ", pairs[2][0]);
    printf("%d\n", pairs[2][1]);
    printf("%d ", pairs[3][0]);
    printf("%d\n", pairs[3][1]);
    
    
    int j,temp,temp2;
    c = 0;
    r = 0;
    
    for(i = 0; i <= HowMany; ++i)
    {
       for(j = 0; j <= HowMany - 1; ++j)
       {
          if(pairs[c][r] > pairs[c+1][r])
          {
             temp = pairs [c][r];
             temp2 = pairs[c][r+1];
             pairs[c][r] = pairs[c+1][r];
             pairs[c][r+1] = pairs[c+1][r+1];
             pairs[c+1][r] = temp;
             pairs[c+1][r+1] = temp2;
          }
       }
    }
    
    printf("%d ", pairs[0][0]);
    printf("%d\n", pairs[0][1]);
    printf("%d ", pairs[1][0]);
    printf("%d\n", pairs[1][1]);
    printf("%d ", pairs[2][0]);
    printf("%d\n", pairs[2][1]);
    printf("%d ", pairs[3][0]);
    printf("%d\n", pairs[3][1]);
    
    return 0;
    
    }
    Here's what I'm looking at specifically...
    Code:
    int j,temp,temp2;
    c = 0;
    r = 0;
    
    for(i = 0; i <= HowMany; ++i)
    {
       for(j = 0; j <= HowMany - 1; ++j)
       {
          if(pairs[c][r] > pairs[c+1][r])
          {
             temp = pairs [c][r];
             temp2 = pairs[c][r+1];
             pairs[c][r] = pairs[c+1][r];
             pairs[c][r+1] = pairs[c+1][r+1];
             pairs[c+1][r] = temp;
             pairs[c+1][r+1] = temp2;
          }
       }
    }
    
    printf("%d ", pairs[0][0]);
    printf("%d\n", pairs[0][1]);
    printf("%d ", pairs[1][0]);
    printf("%d\n", pairs[1][1]);
    printf("%d ", pairs[2][0]);
    printf("%d\n", pairs[2][1]);
    printf("%d ", pairs[3][0]);
    printf("%d\n", pairs[3][1]);
    The idea of it is to take pairs of numbers (Example: (1,2) (5,7) (2,3) (9,6) ) and sort them by the first number from lowest to highest so the example pairs would look like this...

    (1,2) (2,3) (5,7) (9,6)

    I think I have the logic down for the most part but I don't know where to go from here...like how to print it...

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You need to pay closer attention to your loops:
    Code:
        for(i = 0; i <= HowMany; ++i) {
            for(j = 0; j <= HowMany - 1; ++j) {
                if(pairs[c][r] > pairs[c+1][r]) {
    EDIT: And you should stick to one convention. [rows][columns] is the more common one, but you're switching your indexes around between [r][c] and [c][r]. And HowMany is too many in this case it's 9, you have 8 actual numbers and only 4 pairs. You need to fix your use, or misuse of it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. URGENT - while loop, nested while
    By S!lveRdu$t in forum C++ Programming
    Replies: 2
    Last Post: 11-15-2009, 05:22 PM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  4. Nested for loop...search & display a list within a list
    By chadsxe in forum C++ Programming
    Replies: 13
    Last Post: 07-20-2005, 01:34 PM
  5. output from nested while loop
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 08-22-2002, 09:30 AM