/* Filename: square.cpp Author: Br. David Carlson Date: January 18, 1998 Revised: June 25, 2000; November 21, 2001; Februrary 4, 2002 This program prompts the user to enter an integer from 2 to 11 as the size of a square array. Next, the user is prompted to enter integer values for this array. All values are to be between 1 and 999. In each row the minimum number is found and is then subtracted from all values in this row. The adjusted array is then printed. Tested with: Microsoft Visual C++ 6.0 g++ under Linux */ #include #include using namespace std; const int MinSize = 2; const int MaxSize = 11; const int MinValue = 1; const int MaxValue = 999; const int Internal = 0; const int External = 1; typedef int ArrayType[MaxSize][MaxSize]; // Function prototypes: int GetSize(void); void FillSquare(ArrayType Square, int Size); void AdjustSquare(ArrayType Square, int Size); void PrintSquare(ArrayType Square, int Size); int GetMin(ArrayType Square, int Row, int Size); void PrintLine(int Size, int LineType); int main(void) { int Size; ArrayType Square; char Reply; Size = GetSize(); FillSquare(Square, Size); AdjustSquare(Square, Size); PrintSquare(Square, Size); cout << "Enter g to go on: "; cin >> Reply; return 0; } /* Given: Nothing. Task: To prompt the user for an integer from MinSize to MaxSize and to insist on getting only that. Return: The user's number in the function name. */ int GetSize(void) { int Num; cout << "Enter the size for the square array, using an integer from " << MinSize << " to " << MaxSize << ": "; cin >> Num; while ((Num < MinSize) || (Num > MaxSize)) { cout << "Re-enter. Use an integer from " << MinSize << " to " << MaxSize << ": "; cin >> Num; } return Num; } /* Given: Size The desired size of the square array Square. Task: To prompt the user to enter items into array Square from row 0 to row Size - 1 and from column 0 to column Size - 1. All items entered are forced to fit the range MinValue to MaxValue. Return: Square The array of numbers. */ void FillSquare(ArrayType Square, int Size) { int Value, Row, Col; for (Row = 0; Row < Size; Row++) for (Col = 0; Col < Size; Col++) { cout << "Enter the value for row " << Row << " and column " << Col << ": "; cin >> Value; while ((Value < MinValue) || (Value > MaxValue)) { cout << "Re-enter. Use an integer from " << MinValue << " to " << MaxValue << ": "; cin >> Value; } Square[Row][Col] = Value; } } /* Given: Square Square array of numbers. Size The number of items in each row (and column) of Square. Task: To find the minimum item in each row and to subtract it from each item in that row. Return: Square The adjusted array. */ void AdjustSquare(ArrayType Square, int Size) { int Row, Col, RowMin; for (Row = 0; Row < Size; Row++) { RowMin = GetMin(Square, Row, Size); for (Col = 0; Col < Size; Col++) Square[Row][Col] = Square[Row][Col] - RowMin; } } /* Given: Square The square array of numbers. Row The index of one of the rows of numbers. Size The number of items in each row (or column) of Square. Task: To find the minimum item in row Row of this array Square. Return: This minimum in the function name. */ int GetMin(ArrayType Square, int Row, int Size) { int Min, Col; Min = Square[Row][0]; for (Col = 1; Col < Size; Col++) if (Square[Row][Col] < Min) Min = Square[Row][Col]; return Min; } /* Given: Square The 2-dimensional array of numbers. Size Number of items in a row (or col) of the square. Task: To print Square in a nicely formatted manner. */ void PrintSquare(ArrayType Square, int Size) { int Row, Col; PrintLine(Size, External); for (Row = 0; Row < Size; Row++) { cout << setw(1) << '|'; for (Col = 0; Col < Size; Col++) cout << setw(3) << Square[Row][Col] << setw(1) << '|'; cout << endl; if (Row != Size - 1) PrintLine(Size, Internal); } PrintLine(Size, External); } /* Given: Size The number of rows (or cols) in the array. LineType Either the const Internal or External. Task: This is a helping routine for PrintSquare. It's job is to print a horizontal line of the correct length based on Size. It begins and ends the line with the correct character for an internal or external line, based on LineType. Return: Nothing. */ void PrintLine(int Size, int LineType) { int Num; char StartChar; if (LineType == Internal) StartChar = '|'; else StartChar = ' '; cout << setw(1) << StartChar; for (Num = 0; Num < 4 * Size - 1; Num++) cout << setw(1) << '-'; cout << setw(1) << StartChar << endl; }