Writing a code to multiply a multidimensional array by 2

I just have a question regarding an assignment that involves multiplying a multidimensional array by 2. This is what the assignment reads.

"Write a program containing a function MatrixMultiplication that takes in a multidimensional array of integers and any other necessary information and outputs the result of multiplying every element in the array by 2. You can choose the dimensions for your multidimensional array as long as each dimension is greater than 1. The array should be initialized by filling it with random numbers between 1 and 10."

All I have is this code. I'm going to use a 3x3 array, I am just not sure of the function I should use to multiply each element in the array by 2. Any help is extremely appreciated. Also, I am also asked if i can ass the number by parameter to multiply by and use that value to produce the multiplication result.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  
#include <iostream>
#include <string>
#include <cmath>

using namespace std;
int main()
{
  int array1[3][3] = { {3,5,7},
                       {1,3,2},
                       {2,4,6}  } ;
  cout << array1[3][3] * 2 << endl;
  system("pause");
  return 0;
}
    
Last edited on
I really, really need help. THank you
C++ does not have an operator to multiply an array with scalar. You are supposed to implement that with a function.

On line 9 the brackets set the size of an array. On line 12 the brackets dereference array to access single element. Different context, different meaning.

Therefore, your line 12 attempts to multiply a single element of the matrix with the scalar value 2.
Sadly, the array1[3][3] is not even an element of the matrix. Array indexing is from 0 to N-1, so the last element of your array is array1[2][2].

You have to multiply each element of the array individually. Loop over the array elements.


A function array argument foo( int bar[] ) is in reality a pointer foo( int * bar ) and the function does not know how many elements that array has. Therefore, one has to pass the size too: foo( int * bar, size_t size )
A 2D array argument is pointer to pointer: foo( int ** bar,
How would you multiply a one-dimensional array?
@ OP, to give you an idea, look at line 39 - 52.

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
"Write a program containing a function MatrixMultiplication 
that takes in a multidimensional array of integers and any 
other necessary information and outputs the result of 
multiplying every element in the array by 2. You can choose 
the dimensions for your multidimensional array as long as 
each dimension is greater than 1. The array should be 
initialized by filling it with random numbers between 1 and 
10."
*/

#include <iostream>
#include <random>
#include <chrono>
#include <iomanip>

// Global constant to setup the array size across main and other functions.
const int MAXROWS{ 10 };
const int MAXCOLS{ 10 };

//Function Prototype
void MatrixMultiplication(int arr[][MAXCOLS]);
void RandomizeArrayNumbers(int arr[][MAXCOLS]);

int main()
{
    int arrayNumbers[MAXROWS][MAXCOLS];

    RandomizeArrayNumbers(arrayNumbers); // Call the function to initialized the array with random numbers.
    MatrixMultiplication(arrayNumbers);  // Call the function to display the arrays element multiplies by 2.

    return 0;
}

/*
This function uses a nested-for loops to go thru each element to multiply by 2.
Then, it will display the result in a matrix format.
*/
void MatrixMultiplication(int arr[][MAXCOLS])
{
    std::cout << "Displaying the " << MAXROWS << " x " << MAXCOLS << " array format\n";
    std::cout << "with random numbers multiplied by 2...\n";
    for (int rows = 0; rows < MAXROWS; rows++)
    {
	   for (int cols = 0; cols < MAXCOLS; cols++)
	   {
		  std::cout << std::right << std::setw(5) << arr[rows][cols] * 2;
	   }

	   std::cout << std::endl;
    }
}

/*
This function will use the random and chrono libraries to provide random numbers
from 1 to 10. Then it will use a nested-for loops to initiliazed each element
with the random number generated.
*/
void RandomizeArrayNumbers(int arrrnd[][MAXCOLS])
{
    unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
    std::default_random_engine generator(seed);
    std::uniform_int_distribution<int> distributionToss(1, 10); // Set the numbers for int.

    std::cout << "Displaying the " << MAXROWS << " x " << MAXCOLS << " array format\n";
    std::cout << "with random numbers...\n";
    for (int rows = 0; rows < MAXROWS; rows++)
    {
	   for (int cols = 0; cols < MAXCOLS; cols++)
	   {
		  arrrnd[rows][cols] = distributionToss(generator);
		  std::cout << std::right << std::setw(5) << arrrnd[rows][cols];
	   }
	   std::cout << std::endl;
    }
}

Last edited on
Topic archived. No new replies allowed.