arrays

This is a discussion on arrays within the C++ Programming forums, part of the General Programming Boards category; I am new to all this so I hope I'm posting this correctly. While studing about arrays I came across ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    5

    arrays

    I am new to all this so I hope I'm posting this correctly. While
    studing about arrays I came across a program that has code in it
    that I don't recognize or understand. I wondered if it was old or why I have never seen it. The program will run on my complier but I don't understand why. I am trying to write a similiar program for homework and would like for someone to explain this co#include<stdio.h>
    #include<conio.h>

    main()
    {
    // clrscr();-----WHAT IS THIS?
    int A[4][4]={1,2,3,4,0,3,5,7,2,4,6,8,9,8,7,6},row,I,J,column, trow,tcolumn,
    dia,rdia,ckboard,reverse;
    row=0;
    trow=0;
    for(J=0;J<=3;J++)
    {
    for(I=0;I<=3;I++)
    {
    printf("%2d",A[J][I]);--IS PRINTF THE SAME AS COUT?AND WHAT DOES %2D MEAN?
    row=row+A[J][I];
    }
    printf("\n");
    printf("Row=%d\n\n",row);
    trow=trow+row;
    row=0;
    }
    printf("Total of the row=%d\n\n",trow);
    trow=0;
    {
    tcolumn=0;
    column=0;
    for(I=0;I<=3;I++)
    {
    for(J=0;J<=3;J++)
    {
    printf("%2d",A[I][J]);
    column=column=column+A[I][J];
    }
    printf("\n");
    printf("column=%d\n\n",column);
    tcolumn=tcolumn+column;
    column=0;
    }
    return 0;
    }

    }de to me. I don't have MSDN. Thanks for any help
    JBLEA

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    no, printf() is not the same as cout, although they do similar things. printf() is the function used in the C language to print data to the screen, which is the same process that is accomplished by cout in C++. printf() works fine in C++ (until further notice anyway). Rather than using class modifiers like setw(), setprecision, hex, binary, ios::right, etc., printf() uses a string of symbols called a format string following the % sign to do the same thing. Since I don't use C much, I won't swear to the next piece of informtion, but I believe the string %2d means the variable written to the screen will be an integer with precision of 2 significant figures, although I may be wrong. Some people prefer to use printf() and the format string when printing formatted output and some people prefer to use cout and it's modifies/etc. The choice is yours. If you are going to be serious about programming in C++ you should be aware of printf() and know where to go to figure out what it is doing because you are likely to see it pop up in code you are given, even if you don't use it in code you write.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    clrscr is a non-standard function that clears the screen, making it all blank, usually in preparation for some output. If you have problems with your code because of the function, your compiler probably doesn't support it so you can just get rid of it.

    printf is one of a family of C routines for displaying formatted output to the screen. The %2d indicates that the function is to display the given number, A[J][I], in a two character wide field (at least). This would be equivalent of using setw(2) stream manipulator or cout.width(2).
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    5
    Thanks for the info. Now if I wanted to add the rows and or columns in a two dimensional array, how would I go about it? I keep getting an error that says cannot convert from 'int[16]' to'int'
    when I use a for statement and try to add totalrow=totalrow+row;
    or row[j]=row[j]+row[j+1];
    I have tried everything that we have used up to this point and I am lost. Our teacher will not help us anymore. He says we are on our own. All we have done with arrays is sort. This is the first problem where we have had to work with two dimensional and make them do something. We are suppose to add each row and then the total rows, add each column and the total columns, add diagonally, reverse diagonally, add every other one(like a checkerboard), and then flip the data and add again. We are on spring break and mine has been bad, any help would be appreciated.
    And NO I am not going to be serious with c++, I'm just trying to survive and get out of this class. I do NOT want to program. I don't have the logic or the patience. I admire anyone who does, but it is not for me.
    Thanks,
    Jblea

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    array[4][4];
    for(i = 0; i < 4; i++)
    {
    for(j = 0; j < 4; j++)
    {
    array[i][j] = i + j;
    }
    }

    for(i = 0; i < 4; i++)
    {
    for(j = 0; j < 4; j++)
    {
    cout << array[i][j];
    }
    cout << endl;
    }

    //add all values in column 3 of array
    total = 0;
    for(i = 0; i < 4; i++)
    {
    total += array[i][2];
    }

    cout << total;

    Now write out the array in tabular form using pencil and paper, do the math yourself, and prove that the system works.

    Then change the column total by changing just index above. Then determine how to make totals for all. Then think of a way to store the column and row totals in the table itself by expanding the size of the table appropriately.

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    5
    Thanks, I know what Thursday of my spring break will consist of. My day will be filled working on this. Thanks for all your help.
    jblea

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I can think of worse thinks to do, like cleaning out the garage.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function to read in two arrays
    By ssmokincamaro in forum C Programming
    Replies: 7
    Last Post: 11-12-2008, 06:59 AM
  2. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 03:24 PM
  3. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 09:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 08:24 PM

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