Reputation: 29
OK I have this program and Im having some trouble with it. In the addRows and subtractRows functions, the program freezes and also the sums are not correct. Can any take a look at this and point out my mistakes? thanks.
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
const int ROW1MAX = 100;
const int COL1MAX = 100;
const int ROW2MAX = 100;
const int COL2MAX = 100;
void inputInfo(double[][COL1MAX], int , int );
void addRows(double AR[][COL2MAX], int ROW1, int RW2);
void subtractRows(double array[][COL2MAX], int RW1, int RW2);
void addColumns(double ar[][COL2MAX], int COL1, int COL2);
void subtractColumns(double RAY[][COL2MAX], int CL1, int CL2);
void printAry(int ary[ROW1MAX][COL1MAX]);
//void copyAry(double RR[][COL2MAX], int RZ, int CZ);
void rowLargestNumber(double RY[100][100], int RS, int COS);
void columnLargestNumber(double XC[100][100], int X, int Y);
void rowSmallestNumber(double BK[100][100], int RB, int CB);
void columnSmallestNumber(double QU[100][100], int ROWB, int COLB);
void replace(double VW[100][100], int D, int H);
void printAry(double GH[100][100], int A, int B);
int rows;
int colm;
double ary1[ROW1MAX][COL1MAX];
double ary2[ROW2MAX][COL2MAX];
ofstream jfOutputFile;
int main()
{
jfOutputFile.open("2D_Array_Stuff");
inputInfo(ary1, ROW1MAX, COL1MAX);
addRows(ary1, ROW1MAX, ROW1MAX);
subtractRows(ary1, ROW1MAX, ROW1MAX);
addColumns(ary1, COL1MAX, COL1MAX);
subtractColumns(ary1, COL1MAX, COL1MAX);
//copyAry(ary1, ROW1MAX, COL1MAX);
rowLargestNumber(ary2, ROW2MAX, COL2MAX);
columnLargestNumber(ary2, ROW2MAX, COL2MAX);
rowSmallestNumber(ary2, ROW2MAX, COL2MAX);
columnSmallestNumber(ary2, ROW2MAX, COL2MAX);
replace(ary2, ROW2MAX, COL2MAX);
printAry(ary2, ROW2MAX, COL2MAX);
jfOutputFile.close();
return 0;
}
void inputInfo(double inputInfo[][COL2MAX], int ROW1MAX, int COL1MAX)
{
cout << "How many rows are in your first array?: ";
cin >> ROW1MAX;
cout << "How many columns are in your first array?: ";
cin >> COL1MAX;
cout << "\nEnter the information for each row and column:\n";
for (rows = 0; rows < ROW1MAX; rows++)
{
for (colm = 0; colm < COL1MAX; colm++)
{
cout << "Row " << (rows + 1);
cout << ", Column " << (colm + 1)<< " : ";
cin >> inputInfo[rows][colm];
}
}
cout << "\nThe first array is: \n";
cout<<endl;
printAry(inputInfo, ROW1MAX, COL1MAX);
addRows(inputInfo, ROW1MAX, ROW1MAX);
subtractRows(inputInfo, ROW1MAX, ROW1MAX);
addColumns(inputInfo, COL1MAX, COL1MAX);
subtractColumns(inputInfo, COL1MAX, COL1MAX);
}
void addRows(double addRows[][COL2MAX], int row1, int row2)
{
double rowSum1 = 0;
double rowSum2 = 0;
for(colm = 0; colm < COL1MAX; colm++)
{
rowSum1 = rowSum1 + addRows[row1][colm];
cout<<rowSum1<<endl;
}
for(colm = 0; colm < COL1MAX; colm++)
{
rowSum2 = rowSum2 + addRows[row2+4][colm];
}
cout << "\nthe sum of two rows is: " << rowSum1 + rowSum2 << ".\n";
for (int r = 0; r < ROW1MAX; r++)
{
rowSum1 = 0;
for (int c = 0; c < colm; c++)
rowSum1 += addRows[r][c];
}
}
void subtractRows(double subRows[][COL2MAX], int rows1, int rows2)
{
int rowDif1 = 0;
int rowDif2 = 0;
for(colm = 0; colm < COL1MAX; colm++)
{
rowDif1 = rowDif1 - subRows[rows1][colm];
}
for(colm = 0; colm < COL1MAX; colm++)
{
rowDif2 = rowDif2 - subRows[rows2][colm];
}
int subRowSum = rowDif2 - rowDif1;
cout << "\nThe difference between two rows is: " << subRowSum << ".\n";
}
void addColumns(double addCols[][COL2MAX], int colm1, int colm2)
{
double colSum1;
double colSum2;
colSum1 = 0;colSum2=0;
for(rows = 0; rows < ROW1MAX; rows++)
{
colSum1 = colSum1 + addCols[rows][colm1];
}
for(rows = 0; rows < ROW1MAX; colm++)
{
colSum2 = colSum2 + addCols[rows][colm2];
}
cout << "\nThe sum of 2 columns is: " << colSum1 + colSum2 << ".\n";
}
void subtractColumns(double subCols[][COL2MAX], int colm1, int colm2)
{
double colDif1;
double colDif2;
colDif1 = 0;
colDif2 = 0;
for(rows = 0; rows < ROW1MAX; rows++)
{
colDif1 = colDif1 - subCols[rows][colm1];
}
for(rows = 0; rows < ROW1MAX; rows++)
{
colDif2 = colDif2 - subCols[rows][colm2];
}
cout << "\nThe difference between 2 columns is: " << colDif2 - colDif1 << ".\n";
}
void copyAry(double copyAry[100][100], int RA, int CB)
{
for (rows = 0; rows < RA; rows++)
{
for (colm = 0; colm < CB; colm++)
{
copyAry[RA][CB] = ary2[ROW2MAX][COL2MAX];
}
}
cout << "\nThe copied array is:\n" << ary2[ROW2MAX][COL2MAX] << ".\n";
}
void rowLargestNumber(double rowLgstNum[100][100], int RS, int COS)
{
double highest;
for (int rw = 0; rw < RS; rw++)
{
highest = rowLgstNum[rw][0];
for (int clm = 0; clm < COS; clm++)
{
if ( rowLgstNum[rw][clm] > highest)
{
highest = rowLgstNum[rw][clm];
}
}
}
cout << "The highest number in Row " << ROW2MAX << "is " << highest << ".\n";
}
void columnLargestNumber(double colLgstNum[100][100], int X, int Y)
{
double HIGH;
for (int CM = 1; CM < Y; CM++)
{
HIGH = colLgstNum[0][CM];
for (int RW = 0; RW < X; RW++)
{
if ( colLgstNum[RW][CM] > HIGH)
{
HIGH = colLgstNum[RW][CM];
}
}
}
cout << "The highest number in Column " << COL2MAX << "is " << HIGH << ".\n";
}
void rowSmallestNumber(double rowSmstNum[100][100], int RB, int CB)
{
double lowest;
for (int rx = 1; rx < RB; rx++)
{
lowest = rowSmstNum[rx][0];
for (int cx = 0; cx < CB; cx++)
{
if ( rowSmstNum[rx][cx] < lowest)
{
lowest = rowSmstNum[rx][cx];
}
}
}
cout << "The lowest number in Row " << ROW2MAX << "is " << lowest << ".\n";
}
void columnSmallestNumber(double colSmstNum[100][100], int ROWB, int COLB)
{
double LOW;
for (int CN = 0; CN < COLB; CN++)
{
LOW = colSmstNum[0][CN];
for (int rH = 0; rH < ROWB; rH++)
{
if ( colSmstNum[rH][0] < LOW)
{
LOW = colSmstNum[rH][CN];
}
}
cout << "The lowest number in Column " << COL2MAX << "is " << LOW << ".\n";
}
}
void replace(double replace[100][100], int D, int H)
{
for (int f = 0; f < D; f++)
{
for (int c = 0; c < H; c++)
{
replace[1][1] = 100;
}
}
}
void printAry(double prtAry[ROW1MAX][COL1MAX], int A, int B)
{
for (int a = 0; a < A; a++)
{
for (int b = 0; b < B; b++)
{
cout << prtAry[a][b] << " ";
if(b==B)
{
cout<<endl;
}
}
}
cout << "\n";
}
Upvotes: 0
Views: 90
Reputation: 693
There are a few issues here. First, never use the same name for a variable as your method / function. It's confusing when we see 'addRows' as both a variable and a function.
Specifically to your question about why addRows fails, you're declaring the array as ROW1MAX in size but trying to access row ROW1MAX. In the declaration (where you create the array) you specify how many rows. When you actually access the array it's a 0 based index. So an array with 5 rows will have rows 0, 1, 2, 3, and 4. There is no 5.
Upvotes: 1