I can't figure out how to get my functions to work. I have to use void functions but I'm lost. I have to bring data in. An array of numbers.

8 27 33 14
81 146 305 249
412 71 226 4
144 55 97 493
133 265 788 240
380 117 88 25

The program works great until I add the computeSums or computeAvgs functions. Please help. Why are my functions working? I'm so lost.

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cmath>




using namespace std;

void computeSums(int fishArray[][4], int rowSum[], int colSum[]);
void computeAvgs(int rowSum[], int colSum[], double rowAvg[], double colAvgs[]);


int main()
{

    ifstream FileIn;
    ofstream FileOut;

    FileIn.open("E:\\Datafile3.txt");
    FileOut.open("E:\\FISHDATA2015.TXT");

    int row, column;
    int fishArray[6][4];

    for (row = 0; row < 6; row++)
          for (column = 0; column < 4; column++)
               FileIn >> fishArray[row][column];

          cout << setw(21) << "Cropi" << setw(8) << "Bass" << setw(10) << "Catfish" << setw(8) << "Trout" 
              <<setw(8) << "Sums" << setw(10) << "Averages" << endl << endl;
          for (row = 0; row < 6; row++)
          {
              cout << "Keystone" << row + 1 << "   ";
              for (column = 0; column < 4; column++)
              {
                  cout << setw(8) << fishArray[row][column]; //if I put computeSums or computeAvgs here //the program goes crazy.
              }

              cout << endl << endl;



          }



}

void computeSums(int fishArray[][4], int rowSum[], int colSum[])
{
    int row = 0, column = 0;

    rowSum[row] = rowSum[row] + fishArray[row][column];
    colSum[column] = colSum[column] + fishArray[row][column];


}

void computeAvgs(int rowSum[], int colSum[], double rowAvg[], double colAvgs[])
{
    int row = 0, column = 0;
    rowAvg[row] = rowSum[row] / 6.0;
    colAvgs[column] = colSum[column] / 4.0;
}

A few things I notice right off the bat:

  • You're only using one index, 0, to do any calculations. You need to loop through the indexes over the whole array to get the sums and the averages.

  • Nowhere do you declare rowSum, colSum, rowAvg, or colAvg

  • Once they're declared you also need to initialize them otherwise they'll have a random value.

This article has been dead for over six months. Start a new discussion instead.