how to use 2D array, 'if' statement and 'for' loop

I have no idea on how to make a 3x3 and 4x4 2D array and also using the 'if' statement. I also need to use 'for' loops for this assignment. My question are what functions do i have to use in order to make the arrays? what are the steps to use 'if' statement and 'for' loops? Here is the assignment question.

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.
Last edited on
I'm having trouble in checking the inputs of the user if it is still 1-100...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#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;
	}
}
Last edited on
okay, I know already how to create a 2D array of 3x3 or 4x4 and i also understand how to use for loop but i still don't know how to use the 'if' statement. I tried the code you gave but it only work when I chose A not when I chose B. Then the question said assign random numbers from 0-100, do I actually need to use rand() or srand() ?
Last edited on
This is my new codes. The problem is all the arrays are displayed after i made a choice. How can I fix this? Then how can I label the arrays? I mean, after I run it, the screen will display
"----ArrayA----
3 8 14
13 3 20
7 16 19 " Something like this.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#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? Please~~
It almost seems as simple as adding braces ('{}') around your code... Right after the if statements and then again after your for loops.
Topic archived. No new replies allowed.