0

Say that I have array MatrixA which is 2x3 and is filled to 0. How do I set up my loop so that values would be read into it but if the user only enters 3 values, the remaining 3 in MatrixA are 0?

Edit:

const int Q = 2;
const int S = 3;
int matrixA[Q][S] = {0};
for(int i = 0; i < Q; i++){
        for(int j = 0; j < S; j++){
            cin >> matA[i][j];
        }
    }
7
  • 1
    Do you know how to access them? And show your current code. Commented Jan 23, 2014 at 3:25
  • Your loop is fine to me. Commented Jan 23, 2014 at 3:38
  • How will you know that the user ended entering values??? Commented Jan 23, 2014 at 3:43
  • That is part that is confusing me Rashad. Commented Jan 23, 2014 at 3:44
  • You can use end of file. (Thats f6 or ctrl+z). If the input is end of file stop scanning. :) Commented Jan 23, 2014 at 3:49

3 Answers 3

1

Initialize array MatrixA with 0s and then accept values from user. Code would be something like below:

int iOuter = 0;
int iInner = 0;
for (iOuter = 0; i <2; iOuter++)
   for (iInner = 0; i < 3; iInner++)
   {
      MatrixA[iOuter][iInner] = 0;
      cin >> MatrixA[iOuter][iInner];
   }
Sign up to request clarification or add additional context in comments.

Comments

0

Id rather you have code for me to work off of for my answer but assuming your question and statement I will say this. Have a loop in which counts the amount of times the user inputs a number. When the user enters three numbers; Initialize a row variable in which takes the value of 2. Then loop it based on the amount of times the user imputed numbers with a for loop or any other method you choose.

Also to answer your question about the remaining 3 numbers I have provided a link below about the array uninitialized indexes

Why is int array not initialized to zeros in C++??

Comments

0
const int Q = 2;
const int S = 3;
int flag = 0;
int matrixA[Q][S] = {0};
for(int i = 0; i < Q; i++){
    for(int j = 0; j < S; j++){
        matA[i][j] = 0;
    }
 }

for(int i = 0; i < Q; i++){
    for(int j = 0; j < S; j++){
        if(getline(cin, matA[i][j])){

        }
        else{
            flag = 1;
            break;
        }
    }
    if(flag == 1){
        break;
    }
}

Let me know if that helps.. :)

Comments

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.