0

I'm doing an assignment for a class and it's fairly routine. I've done this before in Java so it's not entirely new to me, however, I'm not entirely sure what's wrong here.

Basically I need to enter information for several row/columns and then print the sum of the column, all of that isn't particularly difficult. However, every time I print my array's contents everything is 1, no matter what.

I'm not sure what's wrong, if someone can tell me why I'd appreciate it.

Oh, forgot to mention, I am using g++ for compiling.

#include <iostream>
#include <cstdio>
using namespace std;

const int ROWS = 4;
const int COLUMNS = 3;

void setupMatrix() {
    // Setup matrix
    double array[ROWS][COLUMNS];

    // Needed? Wouldn't think so.
    // for (int i = 0; i < COLUMNS; i++) {
    //      for (int j = 0; j < ROWS; j++) {
    //              array[i][j] = 0.0;
    //      }
    // }

    // Get array information from user
    for (int i = 0; i < COLUMNS; i++) {
            printf("Row %d\n", (i + 1));
            for (int j = 0; j < ROWS; j++) {
                    printf("Column %d ", (j + 1));
                    cin >> array[i][j];
            }
    }

    // Print array so I can see what's up first
    for (int i = 0; i < COLUMNS; i++) {
            // Row & column separation
            printf("\n");
            for (int j = 0; j < ROWS; j++) {
                    printf("%d ", array[j][i]);
            }
    }

}

int main () {
    setupMatrix();

    return 0;
}
3
  • homework tag is now deprecated Commented Feb 4, 2013 at 12:08
  • What should I use instead, if anything? Commented Feb 4, 2013 at 12:12
  • @Casey Nothing. Whether this is homework is irrelevant in the current form of the question (this is a good thing). Commented Feb 4, 2013 at 12:15

2 Answers 2

5

First dimension of the array is ROWS and second is COLUMNS, you got the cycles order wrong. Also to print double use format specifier %lf in the printf. I don't see why you decided to use printf when you read using cin, better use cout for consistency and as this is the typical way to print in C++

Sign up to request clarification or add additional context in comments.

5 Comments

are you sure %lf should be used? lf is for long double according to some sources
@DariuszWawer AFAK %f is float, %lf is double and %llf(or %Lf on other compilers) is long double. Tested through many years of experience.
Thanks so much. I had completely forgotten to double check the proper way to print doubles with printf. Would never have thought of that, same goes for the column and row sizes. Just curious, is using printf considered bad practice? As I like to use it where I can since it's a lot quicker to write statements with it than cout.
@CaseyWeed I personally use printf quite often for the exact same reason, because I do a lot of IO operations. I don't think it's a bad practise just saying you would not have done the error had you used cout.
Ah, ok, I see. For homework/classwork I might just use cout first and verify that everything written is sane, and change it to printf afterwards.
1

You are using:

array[i][j]);

for "cin"and

array[j][i]);

for print: use cout<<array[j][i]);

1 Comment

Woops. Didn't notice that. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.