For loop with a two-dimensional array

This is a discussion on For loop with a two-dimensional array within the C Programming forums, part of the General Programming Boards category; Hey! I'm working on a program that will calculate prices for coins. I'm using a two-dimensional array to save the ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    1,098
    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, 03:00 AM
  2. Zero out two dimensional array
    By davo666 in forum C Programming
    Replies: 16
    Last Post: 01-08-2009, 04:28 AM
  3. Replies: 24
    Last Post: 11-11-2008, 12: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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21