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!
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?
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.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cstdlib>
usingnamespace 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 . . .