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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
// Examples of things to do with Multiple-Subscripted Arrays, referancing p.273
#include <iostream>
#include <cstdlib>
#include <array>
#include <iomanip>
using namespace std;
// display table function
void display_table(int array_name[][3], int array_size_rows) // void display_table(int array_name[], int array_size_rowsint array_size_columns)
{
cout << "\n" << endl;
cout << "Show full table: " << "\n" << endl;
int i, j, rows, columns;
for (i = 0; i < rows; i++)
{
for (j = 0; j < columns; j++)
{
cout << array_name[i][j] << " ";
}
cout << endl;
}
}
int main()
{
//create a 3x3 table
const int rows = 3;
const int columns = 3;
int a[rows][columns];// = { {} , {}, {} }; create a 3x3 table, initalize all data to 0, in all rows & columns (although don't have to if overwriting it
// enter numbers into the data
//int array_modification(int a, int 2)
int i, j;
int user_choice; // user inputs a row to display
int enter_data;
bool change = true;
// edit element varaibles
char edit_element, display_table;
int edit_row;
int edit_column;
int new_value;
cout << "Table is: " << rows << " x " << columns << endl;
// Populate data into Table
for (int i = 0; i < rows; i++)
{
//cout << "Enter data into Row " << i + 1;
for (int j = 0; j < columns; j++)
{
cout << "\nEnter data into Row " << i + 1 << ", Column " << j + 1 << " : ";
cin >> enter_data;
a[i][j] = enter_data; // place data into cell
}
}
// Display Table
//display_table(a, rows, columns)
display_table(a, rows);
/*
// Display Table
cout << "\n" << endl;
cout << "Show full table: " << "\n" << endl;
for (i = 0; i < rows; i++)
{
for (j = 0; j < columns; j++)
{
cout << a[i][j] << " ";
}
cout << endl;
}
*/
// Modify elements in table
while (change == true) // default set to true so this will code will run
{
cout << "\nDo you want to modify any elements in the table? (y/n): ";
cin >> edit_element;
if (edit_element == 'y')
{
// ask user to enter which element they want to change
cout << "\nEnter row #: ";
cin >> edit_row;
cout << "Enter column #: ";
cin >> edit_column;
cout << "Enter new value: ";
cin >> new_value;
a[edit_row - 1][edit_column - 1] = new_value;
}
else
{
break;
}
cout << "Display Table? (y/n): ";
cin >> display_table;
if (display_table == 'y')
{ // replace with call function which shows table
// Display Table
cout << "\n" << endl;
cout << "Show full table: " << "\n" << endl;
for (i = 0; i < rows; i++)
{
for (j = 0; j < columns; j++)
{
cout << a[i][j] << " ";
}
cout << endl;
}
}
else
{// empty, loop back to top
}
}
// Show a row, chosen by the user
cout << "\n" << endl;
cout << "Enter a row number to show (1-" << rows << "): ";
cin >> user_choice;
for (j = 0; j < columns; j++)
{
cout << a[user_choice-1][j] << " "; // where a row_choice of 1 will equal 0 in the computer
}
// Show a column, chosen by the user
cout << "\n" << endl;
cout << "Enter a column number to show (1-" << columns << "): ";
cin >> user_choice;
for (i = 0; i < rows; i++)
{
cout << a[i][user_choice-1] << endl; // where a row_choice of 1 will equal 0 in the computer.
} // hold the column number constant, while increasing row number
// Sum a column, chosen by the user
cout << "\n" << endl;
cout << "Enter a column to sum (1-" << columns << "): ";
cin >> user_choice;
int column_sum = 0; // reset to 0
for (i = 0; i < rows; i++)
{
column_sum = column_sum + a[i][user_choice-1]; // where a row_choice of 1 will equal 0 in the computer
}
cout << "\nSum of Column " << user_choice << ": " << column_sum;
// Sum a row, chosen by the user
cout << "\n" << endl;
cout << "Enter a row to sum (1-" << rows << "): ";
cin >> user_choice;
int row_sum = 0; // reset to 0
for (j = 0; j < columns; j++)
{
row_sum = row_sum + a[user_choice-1][j]; // where a row_choice of 1 will equal 0 in the computer
}
cout << "\nSum of Row " << user_choice << ": " << row_sum << endl;
cin.get();
cin.ignore();
return 0;
}
|