Multi-dimensional arrays - basic understandin

This is a discussion on Multi-dimensional arrays - basic understandin within the C Programming forums, part of the General Programming Boards category; There is a program that I am trying to figure out and it is wrecking my head. I know it ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Location
    Yokohama
    Posts
    48

    Multi-dimensional arrays - basic understandin

    There is a program that I am trying to figure out and it is wrecking my head. I know it is pretty simple for you folks here but hopefully someone can explain it to me in terms I understand. I have reproduced the entire program below because it is very short. My notes make up for more than half of it. I thought it was best to do it this way so you can get an idea of what I understand (or think I understand) and what I don't.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        const int NUM = 10;  // Constant for number of array elements. (OK)
        /* Below, the arrays are defined. There will be two sub arrays with ten rows
        in each array. They will all be doubles. (?) */
        double amounts[2][NUM]; // Holds different currency amounts
    
        /* titles stores the data headers. Two is the number of sub arrays. Does 6
        mean there are six characters in each array eg. P,e,s,o,s and Null?  */
        char titles[2][6] = {
            {"Pesos"},
            {"Euros"}
        };
        /* The rates are declared as doubles and given values */
        double rates[] = {11.317466, 0.823849};
    
        /* i and j are created for the sole purpose of creating loops */
        unsigned int i, j; // Index variables (for loops)
    
        printf("US Dollar amounts to Mexican Peso and Euros:\n\n");
    
        /*This for loop is for the titles array and will run twice 0 and 1 and will
        print two column headers both 10 spaces wide to make them easier to look at.
        First it will print the first title and then continue to the next loop.
         */
        for (i = 0; i < 2; i++) {
    
            /* In the first loop this will print Pesos as Pesos is 0 in the array.
            In the second loop i will be incremented to 1 and will therfore print
            Euros. Before it prints Euros though it will continue to the next loop */
            printf("&#37;10s %10s\n", "Dollars", titles[i]);
            /* This is where it gets very confusing. I realize that this is the loop
            that will display all the values. It will run 10 times as NUM has a
            value of 10.  */
            for (j = 0; j < NUM; j++) {
                /* The book says that the loop refers to amounts[i][j] instead of
                amounts[i] because it has to access the scalar values of the
                multidimensional array. I think I undertand the sentence after
                because but I don't know really undestand the logic of amounts[i][j]*/
                amounts[i][j] = (j + 1) * 5.00;
    
                /* I understand that this prints the arrays but still don't really
                understand the amounts[i][j] concept. */
                printf("%10.2f %10.2f\n", amounts[i][j], (amounts[i][j] * rates[i]));
            }
            printf("\n\n");
        }
    
        getchar();
        return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, look at your two-dimensional char array:
    Code:
      0 1 2 3 4 5
    0 P e s o s \0
    1 E u r o s \0
    If you wanted to get at a specific char -- say the r -- you would need a row and a column. So, to access a specific element of your two-dimensional amounts array, you would need a row and a column.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Location
    Yokohama
    Posts
    48
    So the amounts [i][j] are the row and the column? i refers to the title and j refers to the column?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    i refers to the row, j to the column; amounts[i][j] is the value in the i'th row and the j'th column.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Location
    Yokohama
    Posts
    48
    Thanks. I had to study a bit to get my head around it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question to Arrays
    By doneirik in forum C++ Programming
    Replies: 1
    Last Post: 01-25-2005, 01:57 AM
  2. string into 2 dimensional array - basic stuff
    By mellisa in forum C++ Programming
    Replies: 11
    Last Post: 01-18-2003, 02:08 AM
  3. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 08:24 PM
  4. arrays and validation (basic stuff)
    By Fountain in forum C++ Programming
    Replies: 2
    Last Post: 12-21-2001, 03:25 PM

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