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 99 100 101 102 103 104 105
|
#include <iostream>
#include <cstdlib>
#include <vector>
#include <ctime>
#include <iomanip>
using namespace std;
void col_row_input( int &rows, int &columns )
{
cout << endl << "Create an NxM matrix." << endl << endl;
cout << "Enter number of rows: ";
cin >> rows;
cout << "Enter number of columns: ";
cin >> columns;
cout << endl;
}
void matrix( int rows, int columns, int **&array )
{
srand( time(0) );
array = new int *[rows];
for ( int i = 0; i < rows; ++i )
{
array[i] = new int [columns];
}
for ( int i = 0; i < rows; ++i )
{
for ( int j = 0; j < columns; ++j )
{
array[i][j] = rand() % 50 + 1;
cout << setw(5) << array[i][j];
}
cout << endl;
}
}
void location( int rows, int columns, int **&array )
{
cout << endl << "Choose the row and column of a number." << endl << endl;
cout << "Row: ";
cin >> rows;
cout << "Column: ";
cin >> columns;
cout << "The number at this location is: " << array[rows-1][columns-1] << endl << endl;
}
void horizontal( int rows, int columns, int **&array, int &greatest_product )
{
for ( int i = 0; i < rows; ++i )
{
for ( int j = 0; j < columns; ++j )
{
int num1 = array[i][j];
int num2 = array[i][j+1];
int num3 = array[i][j+2];
int num4 = array[i][j+3];
greatest_product = num1*num2*num3*num4;
cout << "The greatest product is "<< greatest_product << endl;
}
}
}
int main()
{
int rows = 0, r = 0;
int columns = 0, c = 0;
int **array;
col_row_input( rows, columns );
matrix( rows, columns, *&array );
location( rows, columns, *&array );
return 0;
}
|