2D Array

Hi there,

I would like to receive some help and directions on how to start writing code for this particular project. I am completely new and would really want to learn how I go about doing this.

Write a complete C++ program that stores integer elements in a 2D array, multiplies the elements by a given number, and displays the contents of the 2D array on the screen in a tabular format. The 2D array should be of size 3 x 3. Declare the 2D array within main using const values for the ROWS and COLS.



Write a function called multiplyArray, which accepts a 2D array of integers, its size, and the number you are multiplying the elements by as arguments. The function should not return anything. The function will multiply the elements of the 2D array and store them in the same array.



In main you will prompt the user to enter the initial elements of the array. You will call the multiplyArray function within main and print the new elements of the array in main.



You must use nested loops to store the integers in the array and to print the contents of the array in main.

The output looks something like this:

*********** 2D Arrays ************
********* By: Name **************

Enter values for your array, one row at a time.
1 2 3
4 5 6
7 8 9
Enter a number to multiply your 2D array by: 2
The 2D array multiplied by 2 is:
2 4 6
8 10 12
14 16 18
Press any key to continue . . .


EDIT:
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
#include <iostream>

using namespace std;

int main()
{
	int ar[3][3];
	int n;

	cout << "Enter values for your array, one row at a time" << endl;
	cin >> n;
	int *ar = new int[n];


	for (int row = 0; row < 3; row++)
	{
		for (int col = 0; col < 3; col++)
		{
			ar[row][col] = row + col;
		}
		cout << endl;
	}

	return 0;
}
void multiplyArray(int ar[][3], int size)
{

}


EDIT: I do not know how to prompt the user to enter the initial elements of the array. After typing in 1 2 3, it stops and won't let me type the rest i.e 4, 5, 6, 7, 8, 9.
Last edited on
1
2
3
cout << "Enter values for your array, one row at a time" << endl;
cin >> n;
int *ar = new int[n];


Hey. Im not sure what you're trying to do here but it does not look right.

You've already created an array of 3x3. It's called "ar" - int ar[3][3];
However, the instructions tells you to create 2 const variables, Declare the 2D array within main using const values for the ROWS and COLS.

So you can do this to create the array -

1
2
3
const int ROW = 3;
const int COL = 3;
int arr[ROW][COL];


Now the next step is to enter the values. For that, you need a nester for-loop that where both run 3 times each, so its 9 in total.

1
2
3
4
5
6
7
8
9
cout << "Enter the values of the array seperated by return: " << endl;

for (int i = 0; i < ROW; i++)
{
	for (int j = 0; j < COL; j++)
	{
		cin >> arr[i][j];
	}
}


Now the last step is to multiply it. I will let you figure this one out by yourself. Your function looks correct, now all you have to do. Is use the same nested-forloop, but instead of prompting the user to enter values, you simply multiply the existing values.

Dont forget this - Write a function called multiplyArray, which accepts a 2D array of integers, its size, and the number you are multiplying the elements by as arguments.

So the function should also take in the multiplier, which you will have to ask the user for in the main function.

Goodluck!
Hi, @TarikNeaj.
I'm likely to be asking a stupid question, but...
"Enter the values of the array seperated by return: "

Is it possible that you meant "separated by spaces" ?
Last edited on
Hey. No I meant return button, meaning the enter button. You have to press that between each number, because std::cin stops reading after it sees a space.
Hello Sentoo,

You should compile your program before posting and remove as many errors as you can first. Your code has:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

using namespace std;

int main()
{
// This part not needed.
#include <iostream>

using namespace std;

int main()
{


You have two "int main()"s and you can only have one.

On line 13 you define int ar[3][3]; and then on line 18 you define int *ar = new int[n];. Line 18 is a redefinition of "ar" even-though you are trying to make a pointer is does not work and there is no use for it in the program.

Line 17 should be inside the inner for loop so you can assign the user input to the array "ar".

As TarikNeaj pointed out you need to re-read your specs and understand them better.

Hope that helps,

Andy
EDIT:

#include <iostream>

using namespace std;

void multiplyArray(int ar[][3], int size, int mult);

int main()
{
	const int ROW = 3;
	const int COL = 3;
	int ar[ROW][COL];

	cout << "Enter values for your array, one row at a time " << endl;

	for (int i = 0; i < ROW; i++)
	{
		for (int j = 0; i < COL; i++)
		{
			cin >> ar[i][j];
		}
	}

	int userNum;

	cout << "Enter a number to multiply your 2D array by: ";
	cin >> userNum;

	return 0;
}
void multiplyArray(int ar[][3], int size, int mult)
{
	for (int i = 0; i < size; i++)
	{
		for (int j = 0; j < size; j++)
		{
			mult = ar[i][j] * ar[i][j];
		}
		cout << endl;
	}
}

Okay so, I have changed my code and followed everyone's advice and managed to make it cleaner and make the first part work. However, I am still stuck on the multiplyArray function and do not know how I would put it in main.
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
#include <iostream>

using namespace std;

void multiplyArray(int ar[][3], int size, int mult);

int main()
{
	const int ROW = 3;
	const int COL = 3;
	int ar[ROW][COL];

	cout << "Enter values for your array, one row at a time " << endl;

	for (int i = 0; i < ROW; i++)
	{
		for (int j = 0; j < COL; j++) // Should be j < COL. Not i < COL. Should also be j++ not i++.
		{
			cin >> ar[i][j];
		}
	}

	int userNum;

	cout << "Enter a number to multiply your 2D array by: ";
	cin >> userNum;

	// Call the function and give it the arguments.
	multiplyArray(ar, ROW, userNum); 

	// The array is now multiplied, this nested for loop prints the array out.
	for (int i = 0; i < ROW; i++)
	{
		for (int j = 0; j < COL; j++) // Should be j < COL. Not i < COL. Should also be j++ not i++.
		{
			cout << ar[i][j] << " ";
		}

		cout << endl;
	}

	system("pause");
	return 0;
}

// This function should simply multiply the contents of the array with the "mult"
void multiplyArray(int ar[][3], int size, int mult)
{
	for (int i = 0; i < size; i++)
	{
		for (int j = 0; j < size; j++)
		{
			ar[i][j] = ar[i][j] * mult;
		}
	}
}
@TarikNeaj Thank you so much for the help! The program is working as intended now.
Topic archived. No new replies allowed.