So i finished my code, for a 2 dimensional array homework, and when i run the program i keep getting an error, " Segmentation fault(core dumped)". I am not sure what it means, can anyone help me out so i can run the program without errors?
Here is the code:
------------------------
// Program Chart2 manipulates a two-dimensional array
// variable.
#include <iostream>
#include <fstream>
using namespace std;
const int ROW_MAX = 8;
const int COL_MAX = 10;
typedef int ItemType;
typedef ItemType ChartType[ROW_MAX][COL_MAX];
void getChart(ifstream&, ChartType, int&, int&);
// Reads values and stores them in the chart.
void PrintChart(ofstream&, const ChartType, int, int);
// Writes values in the chart to a file.
int largest (ChartType, int, int);
// Returns largest value
int smallest (ChartType, int, int);
// Returns smallest value
int sumOf1dArray(int[], int);
// Returns sum of values of given 1d array
int main ()
{
ChartType chart;
int rowsUsed;
int colsUsed;
ifstream dataIn;
ofstream dataOut;
cout << "The largest value is: " << largest(chart, rowsUsed, colsUsed) << endl;
cout << "The smallest value is: " << smallest(chart, rowsUsed, colsUsed) << endl;
for (int i =0; i < rowsUsed; i++)
{
cout << "Sum of values of row: " << i+1 << sumOf1dArray(chart[i], colsUsed) << endl;
}
void getChart(ifstream& data, ChartType chart,
int& rowsUsed, int& colsUsed)
// Pre: rowsUsed and colsUsed are on the first line of
// file data; values are one row per line
// beginning with the second line.
// Post: Values have been read and stored in the chart.
{
ItemType item;
data >> rowsUsed >> colsUsed;
for (int row = 0; row < rowsUsed; row++)
for (int col = 0; col < colsUsed; col++)
// FILL IN Code to read and store the next value.
{
data >> item;
chart[row][col] = item;
}
}
void PrintChart(ofstream& data, const ChartType chart,
int rowsUsed, int colsUsed)
// Pre: The chart contains valid data.
// Post: Values in the chart have been sent to a file by row,
// one row per line.
{ // FILL IN Code to print chart by row.
for (int row=0; row < rowsUsed; row++)
{
for (int col=0; col < colsUsed; col++)
{
data << chart[row][col] << " ";
}
data << endl;
}
}
int largest(ChartType chart, int rowsUsed, int colsUsed)
{
int large = chart [0][0];
for (int row=0; row < rowsUsed; row++)
{
for (int col=0; col < colsUsed; col++)
{
if (large < chart [row][col])
large = chart [row][col];
}
}
return large;
}
int smallest(ChartType chart, int rowsUsed, int colsUsed)
{
int small = chart [0][0];
for (int row=0; row < rowsUsed; row++)
{
for (int col=0; col < colsUsed; col++)
{
if (small > chart [row][col])
small = chart [row][col];
}
}
return small;
}
Are you sure it is successfully accessing the input file "twod.dat"?
There don't appear to be any checks on opening the file or when reading any of the data.