Thread: For loop with a two-dimensional array

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    1

    For loop with a two-dimensional array

    Hey!

    I'm working on a program that will calculate prices for coins. I'm using a two-dimensional array to save the year, and the condition of the coin.

    Here are the basics.

    Code:
    j = 0;
    k = 0;
    
    coinCount = 2; // previously prompted
    
    for (i = 0; i < coinCount; i++)
        {
    printf("\nYear: ");
            coin[j][k] = check(a); //A function that validates the input
            k++;
    
    printf("\nCondition: ");
            coin[j][k] = check(a);
            j++;
        }
    
        j = 0;
        k = 0;
    
        // For every coin, check if condition is 1.
    
        for (i = 0; i < coinCount; i++)
        {
            if (coin[j][1] == 1)
            {
                mint = mint + (3 * coin[j][0]); // THIS doesn't work.
                j++;
            }
        }
    

    I'm trying to make it print the variable 'mint', but it only stores the values from one loop. Am I doing something terribly wrong?

    Is a two-dimensional array the way you would approach this?
    Is there a way to make "object-like" things in C?
    Like Coin1.year and Coin1.condition?

    Thank you and sorry for my bad language :)


  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    First, use "paste as text" when you paste code, then it looks like this
    Code:
      j = 0;
      k = 0;
    
      coinCount = 2;                // previously prompted
    
      for (i = 0; i < coinCount; i++) {
        printf("\nYear: ");
        coin[j][k] = check(a);      //A function that validates the input
        k++;
    
        printf("\nCondition: ");
        coin[j][k] = check(a);
        j++;
      }
    
      j = 0;
      k = 0;
    
      // For every coin, check if condition is 1.
    
      for (i = 0; i < coinCount; i++) {
        if (coin[j][1] == 1) {
          mint = mint + (3 * coin[j][0]); // THIS doesn't work.
          j++;
        }
      }

    Next, look at your loop variables.
    Code:
      for (i = 0; i < coinCount; i++) {
        if (coin[j][1] == 1) {
          mint = mint + (3 * coin[j][0]); // THIS doesn't work.
          j++;
        }
      }
    You don't need j at all.

    In fact, in all the code, you may as well stick with coin[i][0] and coin[i][1]
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by slimd View Post
    Is there a way to make "object-like" things in C?
    Like Coin1.year and Coin1.condition?
    That is a struct in C. Example

    Code:
    typedef struct {
      int year;
      char condition[100];
    } COIN;
    Code:
    COIN Coin1;
    Coin1.year = 1920;
    strcpy(Coin1.condition, "really bad");

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 12-01-2011, 04:00 AM
  2. Zero out two dimensional array
    By davo666 in forum C Programming
    Replies: 16
    Last Post: 01-08-2009, 05:28 AM
  3. Replies: 24
    Last Post: 11-11-2008, 01:39 PM
  4. Replies: 1
    Last Post: 04-25-2006, 12:14 AM
  5. two dimensional array
    By saurav sarkar in forum C Programming
    Replies: 5
    Last Post: 08-04-2002, 11:12 AM

Tags for this Thread