Multiplying each element of an 5x5 2D array by 2

Hello, i am having a hard time writing this question out into code.

Multiply each element of a 5x5 2-dimensional array by 2 and display it.

The code i currently have, has the array already displayed and initialized, but the second part, which is the multiplication part is what i am having problems with.

Aswell, i need to pass the array through the first function, then two the second function to be multiplied, then back to the first function.

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
#include <iostream>
#include <iomanip>
using namespace std;
//Function Declarations
void display(int n[5][5]);
int multiply(int arr[5][5], int b = 2)
{
    int calc;
    calc = arr[5][5] * b;
    return (calc);
    
};


int main()
{
	int myArray[5][5] = 
	{
		{1,2,3,4,5},
		{10,20,30,40,50},
		{15,25,35,45,55},
		{2,4,6,8,10},
		{3,3,3,3,3,}
	};
	display(myArray);
	multiply(myArray);

}

void display(int n[5][5])
{
   cout << "Displaying Values: " << endl;
   for(int i = 0; i < 5; i++)
    {
       for(int j = 0; j < 5; j++)
        {
            cout << setw(5) << right << n[i][j] << " ";        
            
        }
        cout << endl;
    }
} 

void multiply(int arr[5][5])
{
    cout << "Displaying New Values: " << endl;
    for(int i = 0; i < 5; i++)
     {
        for(int j = 0; j < 5; j++)
         {
            cout << setw(5) << right << arr[i][j] << " ";
            
         }
         cout << endl;
    }
}
Last edited on
To multiply use an expression like arr[i][j] *= 2;
It would make sense if multiply only multiplies the values.
You have a function to display the array already.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{
	int myArray[5][5] = 
	{
		{1,2,3,4,5},
		{10,20,30,40,50},
		{15,25,35,45,55},
		{2,4,6,8,10},
		{3,3,3,3,3,}
	};
        cout << "Original values...\n";
	display(myArray);
        cout << "Multiplied values...\n";
	multiply(myArray);
       display(myArray);
}
Hey Thomas, thanks for the reply. I do have the function which displays the array already, but i was instructed to make a second function that multiplies each element from the array by 2, and then display that second function, which would be essentially a new array output.

Where would i put the arr[i][j] *=2?

Thanks for helping.
You need to put it in the multiply function before you output the values on line 50.
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
#include <iostream>
#include <iomanip>

using namespace std;
//Function Declarations

void display(int n[5][5]);
void multiply(int arr[5][5]);

int main()
{
	int myArray[5][5] = 
	{
		{1,2,3,4,5},
		{10,20,30,40,50},
		{15,25,35,45,55},
		{2,4,6,8,10},
		{3,3,3,3,3,}
	};
	display(myArray);
	multiply(myArray);

}

void display(int n[5][5])
{
   cout << "Displaying Values: " << endl;
   for(int i = 0; i < 5; i++)
    {
       for(int j = 0; j < 5; j++)
        {
            cout << setw(5) << right << n[i][j] << " ";        
            
        }
        cout << endl;
    }
} 

void multiply(int arr[5][5])
{
    cout << "Displaying New Values: " << endl;
    for(int i = 0; i < 5; i++)
     {
        for(int j = 0; j < 5; j++)
         {
             arr[i][j]  *= 2;
            cout << setw(5) << right << arr[i][j] << " ";
            
         }
         cout << endl;
    }
}

OUTPUT
Displaying Values: 
    1     2     3     4     5 
   10    20    30    40    50 
   15    25    35    45    55 
    2     4     6     8    10 
    3     3     3     3     3 
Displaying New Values: 
    2     4     6     8    10 
   20    40    60    80   100 
   30    50    70    90   110 
    4     8    12    16    20 
    6     6     6     6     6 
 

You need only 1 multiply function
i need to pass the array through the first function, then two the second function to be multiplied, then back to the first function.
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
#include <iostream>
#include <iomanip>

void display(int n[5][5]);
void multiply(int arr[5][5]);

int main()
{
	int myArray[5][5] = 
	{
		{1,2,3,4,5},
		{10,20,30,40,50},
		{15,25,35,45,55},
		{2,4,6,8,10},
		{3,3,3,3,3}
	};
	std::cout << "Displaying Values:\n";
	display(myArray); // pass the array through the first function
	multiply(myArray); // to the second function to be multiplied
	std::cout << "Displaying New Values:\n";
	display(myArray); // then back to the first function
}

void display(int n[5][5])
{
   for(int i = 0; i < 5; i++)
    {
       for(int j = 0; j < 5; j++)
        {
            std::cout << std::setw(5) << std::right << n[i][j] << " ";
        }
        std::cout << '\n';
    }
} 

void multiply(int arr[5][5])
{
    for(int i = 0; i < 5; i++)
     {
        for(int j = 0; j < 5; j++)
         {
             arr[i][j]  *= 2;
         }then back to the first function
    }
}
Last edited on
Had to google a bit on this, lol. I really prefer function signature with int** as opposed to int[][] , but yeah, you lose some dimension data. The downside is that you need to do a little work to create a pointer to your previously declared int[][] array. I'm not sure if there's a more natural way to declare an int** array without mallocs.

Anyways, with row and column information throughout, you could do it 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
#include <iostream>
#include <iomanip>

void display(int** arr, int rows, int cols)
{
  for (int i=0; i<rows; i++)
  {
    for(int j=0; j<cols; j++)
    {
      std::cout << std::setw(5) << std::right << arr[i][j] << " ";
    }
    std::cout << std::endl;
  }
  std::cout << std::endl;
} 

// The array will be forever changed
void multiply(int** arr, int rows, int cols)
{
  for(int i=0; i<rows; i++)
  {
    for(int j=0; j<cols; j++)
    {
      arr[i][j] *= 2;
    }
  }
}

int main()
{
  int rows=3;
  int cols=6;
  
  int myArray[rows][cols] = 
  {
    {1,2,3,4,5,6},
    {10,20,30,40,50,60},
    {100,200,300,400,500,600}
  };
  
  // Creating pointer access for compatibility between int[][] and int**
  int* a[rows];
  for (int i=0; i<rows; ++i)
    a[i]=myArray[i];
  
  std::cout << "Displayed:\n";
  display(a, rows, cols);
  
  std::cout << "Multiplied + Displayed:\n";
  multiply(a, rows, cols);
  display(a, rows, cols);
  
  std::cout << "Multiplied + Displayed:\n";
  multiply(a, rows, cols);
  display(a, rows, cols);
  
  return 0;
}
No, I'm well aware this isn't directly answering the OP's question, so please don't bite my head off. But it seemed a different way of doing the problem.

I had hoped that if I used a valarray< valarray<int> > instead of a vector< vector<int> > for a 2-d array then I might avoid having to overload the * operator. However, that didn't seem to work, so I stuck with vectors instead.

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
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;

using matrix = vector< vector<int> >;

template <typename T, typename N> vector<T> operator *( N number, vector<T> V )
{
   vector<T> result = V;
   for ( auto &e : result ) e = number * e;
   return result;
}

void display( matrix n );



int main()
{
   matrix myArray =
   {
      {1,2,3,4,5},
      {10,20,30,40,50},
      {15,25,35,45,55},
      {2,4,6,8,10},
      {3,3,3,3,3,}
   };
   display( myArray );
   display( 2 * myArray );
}


void display( matrix n )
{
   int rows = n.size();
   int cols = n[0].size();

   cout << "Displaying Values:\n";

   for ( int i = 0; i < rows; i++ )
   {
      for ( int j = 0; j < cols; j++ ) cout << setw(5) << right << n[i][j] << " ";        
      cout << endl;
   }
}

Displaying Values:
    1     2     3     4     5 
   10    20    30    40    50 
   15    25    35    45    55 
    2     4     6     8    10 
    3     3     3     3     3 
Displaying Values:
    2     4     6     8    10 
   20    40    60    80   100 
   30    50    70    90   110 
    4     8    12    16    20 
    6     6     6     6     6 


I would really love to be able to write
2 * myArray
for any shape and dimension of array, without having to overload a * operator. You can do it in other languages.
Last edited on
this won't work on 2-d pointer allocations, but 2d explicit arrays like [5][5] are one block of memory allowing a collapse to do it in a single loop.

int * ip = &(array[0][0]);
for(i = 0; i < 25; i++)
ip[i] <<=1;

this code is provided for amusement only, its not really the best way to do things for a number of reasons.
Last edited on
Topic archived. No new replies allowed.