-1

I'm getting an error "segmentation fault(core dumped)" for my program here, but everything in the program seems to be working fine. I understand what the error signifies I just cant identify what in my code is causing it. Any ideas?

#include <cmath> // for use of math functions
#include <iomanip> // for output display
#include "cosc1437_tk_1371133.h"
using namespace std;

//Global Constansts
const int ROWS = 3;
const int COLS = 10;

//Prototypes: None (other than defined in toolkit)

// MAIN program
int main()
{

  // Hello Message
  displayMessage("Hello. This program will demonstrate the use of 2D Arrays and mathematical operations.");

  // Intialize and load arrays with flight information
  int dArr[ROWS][COLS];

  for (int i = 2; i < ROWS + 2; i++){
    for (int j = 1; j < COLS + 1; j++) {
      dArr[i][j] = pow(i, j);
      cout << setw(8);
      cout << dArr[i][j];
    }
    cout << "\n";
  }

  //Goodbye Message
  displayMessage("Goodbye!");
}
Jerry123
  • 67
  • 8

1 Answers1

-1
for (int i = 2; i < ROWS + 2; i++){
    for (int j = 1; j < COLS + 1; j++) {
          dArr[i][j] = pow(i, j);

You set the max value of ‘i’ and ‘j’ to exceed ROWS and COLS, which you used to set the array dimension with.

Since max position in the array is 1 less than the value used:

int a[9]; // indexes 0 through 8.

So when you attempt to access a position of ROWS or COLS you exceed the array’s size. This translates into a segfault.

Wing Tang Wong
  • 802
  • 4
  • 10