How to take in integers into a 3x5 array and double it?

I am new to C++ and I've written many programs! My favorite is one where it displays all prime numbers from 1 to x.

Anyways, I had a very stupid question. I'm trying to write a program that receives 15 total integers (3 x 5) into an array, doubles them, and displays them. I have no idea how to begin. Any pointers would be very helpful!

Thanks!
Qaisar
qqaisarr wrote:
I have no idea how to begin.


1. Declare an int array of dimensions 3 x 5.

2. Request and get input from the user. (I suggest a whole row at a time.)

3. Nested loops to double each element.

4. Nested loops to display the array.



If you want a more advanced version do it with a 1d array of 15 elements and break it into 3 rows when you want to print it.

For a still more condensed version use a valarray<int>; then doubling amounts to A = 2 * A; for the whole array A.
2. Request and get input from the user. (I suggest a whole row at a time.)


Okay, so first of all thanks for your response! I've written this so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
	int array[2][4], x;
	cout << "3 x 5 Integer Array Doubler" << endl;
	cout << "---------------------------" << endl << endl;
	cout << "Enter the first row of integers seperated by a space (5 total): ";
	for (x = 0; x <= 4; x++)
		cin >> array[0][x];
	cout << "Enter the second row of integers seperated by a space (5 total): ";
	for (x = 0; x <= 4; x++)
		cin >> array[1][x];
	cout << "Enter the third row of integers seperated by a space (5 total): ";
	for (x = 0; x <= 4; x++)
		cin >> array[2][x];
	system("pause");
    return 0;
}


I don't think this works but I don't know how to test it. Do you know if this will work because I don't know if the computer received it properly?
How to test it? Compile it and run it!

You haven't included headers.

You have declared an array of 2 by 4 elements, not 3 by 5.

Once array dimensions are correctly declared you should be OK, but it will make it substantially shorter (and more flexible) if you write an outer loop to run over first index = 0, 1, 2, rather than coding the same thing 3 times. Imagine if there were 10 rows instead!
Thank you for the help! I found my error and made it work really well! I posted the result and if anyone has any suggestions I'd really appreciate it because it is kind of sloppy.

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
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;

int main()
{
	int array[3][5], x, row, column;
	cout << "3 x 5 Integer Array Doubler" << endl;
	cout << "---------------------------" << endl << endl;
	cout << "Enter the first row of integers seperated by a space (5 total): ";
	for (x = 0; x <= 4; x++)
		cin >> array[0][x];
	cout << "Enter the second row of integers seperated by a space (5 total): ";
	for (x = 0; x <= 4; x++)
		cin >> array[1][x];
	cout << "Enter the third row of integers seperated by a space (5 total): ";
	for (x = 0; x <= 4; x++)
		cin >> array[2][x];
	for (row = 0; row <= 2; row++)
	{
		for (column = 0; column <= 4; column++)
		{
			array[row][column] = array[row][column] * 2;
		}
	}
	cout << endl << "Your array has been doubled relatively: " << endl;
	for (row = 0; row <= 2; row++)
	{
		cout << "* ";
		for (column = 0; column <= 4; column++)
		{
			cout << setw(5) << array[row][column];
		}
		cout << "    *" << endl;
	}
	cout << endl;
	system("pause");
    return 0;
}


The output:

3 x 5 Integer Array Doubler
---------------------------

Enter the first row of integers seperated by a space (5 total): 532 32 2 62 63
Enter the second row of integers seperated by a space (5 total): -325 235 21 56 7
Enter the third row of integers seperated by a space (5 total): 623 21 6 21 -656

Your array has been doubled relatively:
*   1064    64     4   124   126     *
*   -650   470    42   112    14     *
*   1246    42    12    42 -1312     *

Press any key to continue . . .
No major suggestions except ... keep it up!

It's nicely laid out.

(Maybe you could loop for input, though, rather than "first", "second", "third" ).
Last edited on
Topic archived. No new replies allowed.