#include <iostream>
#include <iomanip>
using namespace std;
const int SIZE = 5;
/*
* This program implements 3 functions and then uses them in the program
* 1) A function that takes as a parameter an array of floating-point values
* and lets the user set all the values in the array.
* 2) A function that takes as parameters a one-dimensional array of floating-
* point values, a two-dimensional array of floating-point values, a
* character representing either row or column ('r' for row and 'c' for
* column), and an integer representing the row or column position.
* It then copies the values in the one-dimensional array into the
* specified row or column of the two-dimensional array.
* 3) A function that takes as a parameter a two-dimensional array of
* floating-point values and prints them out nicely.
*
*/
// INSERT FUNCTION PROTOTYPES HERE
// Allows the user to enter SIZE values in the 1 dimensional array
void setValues(double values[]);
// replaces the pos column/row of matrix "table" with the vector "values"
void replace(const double values[], double table[][SIZE], char c, int pos);
// Prints out the 2-dimensional array nicely in columns
void printTable(const double table[][SIZE]);
void main()
{
double values[SIZE] = {0};
double table[SIZE][SIZE] = {{0}};
char choice, row_col;
int position;
cout << "Row/Column Copy program - by Demetris Zeinalipour - cs10 Lab10";
cout << endl << endl;
do
{
// CALL FUNCTION TO SET VALUES IN ONE-DIMENSIONAL ARRAY
setValues(values);
do
{
cout << "Copy values to specified row (r) or column(c)? ";
cin >> row_col;
} while(row_col != 'r' && row_col != 'c');
cout << "Enter row/column position (0-" << (SIZE-1) <<"): ";
cin >> position;
while(cin.fail() || position < 0 || position >= SIZE)
{
cin.clear();
cin.ignore(80, '\n');
cout << "Enter row/column position (0-" << (SIZE-1) <<"): ";
cin >> position;
}
// CALL FUNCTION TO COPY VALUES FROM ONE-DIMENSIONAL
// ARRAY TO TWO-DIMENSIONAL ARRAY IN GIVEN LOCATION
replace(values, table, row_col, position);
// CALL FUNCTION TO PRINT TWO-DIMENSIONAL ARRAY NICELY
printTable(table);
cout << "Set more values: (y/n): ";
cin >> choice;
cout << endl << endl;
} while(choice == 'y');
}
/* Allows the user to enter SIZE values in the 1 dimensional array
* @return : void
*/
void setValues(double values[])
{
int i;
cout << "Enter floating-point value for 1-dim table\n";
for (i=0; i<SIZE; i++)
{
cout << "values[" << i << "] : ";
cin >> values[i];
if (cin.fail())
{
// if error notify him and step back
cout << "Error! - Only float-point values are allowed!\n";
cin.clear();
cin.ignore(80, '\n');
i--;
}
}
cout << endl;
}
/* Replaces a given row or column of table by the numbers in values.
* pos is the selected column/row and char the choice (colum/row)
* @return : void
*/
void replace(const double values[], double table[][SIZE], char c, int pos)
{
int i;
switch (c)
{
case 'r': for (i=0; i<SIZE; i++)
{
table[pos][i] = values[i];
}
break;
case 'c': for (i=0; i<SIZE; i++)
{
table[i][pos] = values[i];
}
break;
}
}
/* Prints out the 2-dimensional array nicely in columns
* @return : void
*/
void printTable(const double table[][SIZE])
{
//
cout << setprecision(2) << setiosflags(ios::showpoint | ios::fixed);
for (int i=0; i<SIZE; i++)
{
for (int j=0; j<SIZE; j++)
{
cout << setw(8) << table[i][j];
}
cout << endl;
}
}