It has been a long time since i do C++ programming and I am stuck on this question. I don't remember how to solve this kind of question. haha

Part 1:
Develop a C++ program which asks the user for the choice of either 3x3 or 4x4 2-dimensional array (use 'if' statement). once the choice is made, create either:

two 3x3 or arrays (A and B) by assigning random numbers, (0 to 100), to the elements of the arrays or

two 4x4 or arrays (C and D) by assigning random numbers, (0 to 100), to the elements of the arrays.

use integer variables.

Part 2:
Continue the C++ program by multiplying the created arrays "A and B" or "C and D" (depending on the user's choice) to produce array E(3x3) or F(4x4).

use 'for' loops.

Recommended Answers

All 8 Replies

In order to help you please put some code you already created.
After that I can point where do you have problems and help you solwing them.
Regards, Mike.

Here are some codes I had done. It only work when I choose A. when I try to choose B, it end the program.

#include <iostream>
using namespace std;

int main(){

    char choice;

    cout<<"Enter 'A' =3x3 or 'B' =4x4: ";
    cin>>choice;


    if (choice=='A' || choice=='a'){
        int arrA[3][3];
        cout<<"You have chose a 3x3 2 dimensional array. \n"<<endl;
        cout<<"Enter 9 random numbers (1-100 only): "<<endl;

        for (int a=0; a<3; a++){
            for (int b=0; b<3; b++){
                cin>>arrA[a][b];
            }   
        }
        for (int a=0; a<3; a++){
            for (int b=0; b<3; b++){
                if (arrA[a][b] > 100){
                    break;
                    cout<<"Enter numbers 1 to 100 only! Follow instructions as stated above. "<<endl;
                }
            }
        }
    }
    else if (choice== 'B' || choice=='b'){

    }
    else if (choice != 'A'|| choice != 'a' && choice != 'B' || choice != 'b'){
        cout<<"Choose A/a or B/b only. "<<endl;
    }
}

anyone? help me solve the problem in my codes. why doesn't it work when I choose B and also it stop working as well after I entered random numbers for the array.

It stops working because you have no code in those sections. If you choose 'B' you will execute this code:

else if (choice== 'B' || choice=='b') {

}

Which of course does nothing. Similarly after you create your arrays you don't do anything with them, so again it stops working.

Meanwhile I doubt you are intended to manually enter a bunch of numbers. You are likely supposed to generate them. To generate a random number between 'low' and 'high' you can do this:

int random = (rand() % (high-low)) + low

You can google search for "rand() C++" for more information.

I redo my codes. Here is the new one. The problem now is all the arrays are displayed after i made i a choice. How can I fix this?

#include <iostream>
using namespace std;

int main()
{
    int choice;

    cout << "Enter your choice 3x3 or 4x4 :";
    cin>> choice;

    if (choice==3)
        cout<< "You have chose 3x3 array" << endl;

            int arrayA[3][3] = {{3,8,14},{13,3,20},{7,16,19}};

                for (int i=0; i<3; i++)
                {
                    cout<< endl; // to create a new row
                    for (int j=0; j<3; j++)
                    {
                    cout << arrayA[i][j] << " ";
                    }   
                }

            int arrayB[3][3] = {{16,12,9},{27,12,15},{20,11,40}};

                for (int i=0; i<3; i++)
                {
                    cout<< endl; // to create a new row
                    for (int j=0; j<3; j++)
                    {
                    cout << arrayB[i][j] << " " ;
                    }   
                }
    if (choice==4)
        cout<< "You have chose 4x4 array \n" << endl;
            int arrayC[4][4] = {{2,11,13,5},{6,11,17,33},{2,7,27,10},{10,36,12,8}};

                for (int i=0; i<4; i++)
                {
                    cout<< endl; // to create a new row
                    for (int j=0; j<4; j++)
                    {
                    cout << arrayC[i][j] << " ";
                    }   
                }

            int arrayD[4][4] = {{8,4,7,9},{12,1,13,28},{16,5,24,36},{20,15,9,3}};

                for (int i=0; i<4; i++)
                {
                    cout<< endl; // to create a new row
                    for (int j=0; j<4; j++)
                    {
                    cout << arrayD[i][j] << " ";
                    }   
                }
    return 0;
}

can someone check the new codes? what is wrong with the code, why all the arrays are displayed?

Control statements need:
1) an identifier (if, else, switch, for, while, etc) indicating this is a control statement
2) a condition (or conditions) that must be met for the following body of code to run
3) a body of code to run

To write the body of code you generally put an opening curly brace right after the closing parenthesis of the conditional. The body of code for the conditional then continues until the closing curly brace is found.

However, there is an exception. If there is a valid statement after the closing parenthesis of the condition, then the compiler will assume that the body of the conditional statement is a single line only, and not look for the curly braces that usually enclose the body of code.

The first thing to do is to fix that type of problem and see what happens.

Emphasized Text Here

nice informition...thanks a lot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.