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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
/* Create a 2d array that takes the size of 2 arrays and then adds them together.
Output the 2 arrays and then the sum of the 2.
input: rows and colums of array 1 and array 2.
output: array 1, array 2, sum of array 1 and 2 (array 3)
processing: value returning function takes values into two dimensional array.
void function to sum the two arrays.
void function to output the 3 arrays.
confirm sizes of array in main.
*/
#include <iostream>
using namespace std;
const int cols=15;
void fillArray (int r, int c, int a[][cols]);
int sumArrays (int a[][cols], int b[][cols], int d[][cols],int r, int c);
int main ()
{
int rows, columns, rows2, columns2;
int array1[15][15], array2[15][15], array3[15][15];
cout<<"Enter the number of rows and columns for the first array.";
cin >> rows;
cin >> columns;
cout<<"Enter the number of rows and columns for the second array.";
cin >> rows2;
cin >> columns2;
while ((rows * columns) != (rows2 * columns2))
{
cout << "The size of the two arrays must be the same!";
cout<<"Enter the number of rows and columns for the first array.";
cin >> rows;
cin >> columns;
cout<<"Enter the number of rows and columns for the second array.";
cin >> rows2;
cin >> columns2;
}
while (rows > 15 || columns > 15 || rows2 > 15 || columns2 > 15)
{
cout<< "The maximum number of rows or columns is 15.";
cout << "The size of the two arrays must be the same!";
cout<<"Enter the number of rows and columns for the first array.";
cin >> rows;
cin >> columns;
cout<<"Enter the number of rows and columns for the second array.";
cin >> rows2;
cin >> columns2;
}
fillArray (rows, columns, array1);
fillArray (rows2, columns2, array2);
sumArrays (array1,array2,array3, rows,columns);
output (array3,rows,columns);
return 0;
}
void fillArray (int r, int c, int a[][cols])
{
for (int x = 0; x<r; x++)
{
for ( int y = 0; y < c; y++)
{
a[x][y] = a[1][1];
}
cout<<endl;
}
}
int sumArrays (int a[][cols], int b[][cols], int d[][cols],int r, int c)
{
for (int i=0; i < r; i++)
{
for(int j=0; j<c; j++)
{
d[i][j]= a[i][j]+b[i][j];
}
}
return d[15][15];
}
void output (int t[][cols],int r, int c)
{
for (int x = 0; x<r; x++)
{
for ( int y = 0; y < c; y++)
{
cout<< t[x][y];
}
cout<<endl;
}
}
|