Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Keep getting (*** stack smashing detected ***: terminated) with a two dimensional array.

+3 votes
asked Mar 16 by Joshua Elias (270 points)
This program works well for a little bit, but when I ever I try to print a number from the second column in the array student_1, it will say {*** stack smashing detected ***: terminated}> I don't know how to print out any numbers in the second column or at least exchange its value with another integer to print-out without getting this error.

/*
   Programmer:     Joshua Elias
   Program Name: Star Pattern Displayer
   Date:                  3/15/2022
   Version:             14.0. 23918.0.?
   Description: This program takes the user input for three tests for two students and
   displays the test scores and also displays the average of the test scores.
*/
#include <iostream>
#include <cmath>

using namespace std;

int average(int student_1[2][3])
//function that will be used to display the test scores and average.
{
    int sum1 = 0;
    int sum2 = 0;
    //used to help find the average more easily.
    cout<<"       Student 1 Student 2 " << endl;
    for(int i=0;i<3;++i)
    //function will display every test score for both students 1 and 2.
    {
        cout << "Test " << i + 1 << "        " << student_1[i][0];
        //display test and its number.
        cout << "        " << student_1[i][1] << endl;
        //displays the test scores
        //adds up the sum of every test for both students 1 and 2.
        sum1 = sum1 + student_1[i][0];
        sum2 = sum2+student_1[i][1];
    }
    cout << "Average Test Score of Student 1: " << sum1 / 3 << endl;
    //calculates, then displays the test average.
  return 0;
  //ends the function.
}

int main()
{
  int student_1[2][3]={{0,1,2},{3,4,5}};
  //arrays made basic so that the user can input into them.
  cout << "This program will show you the average test score of two students" << endl;
  cout << "Please input the the test scores for student 1 Test 1: ";
  cin >> student_1[0][0];
  cout << "Test 2: ";
  cin >> student_1[1][0];
  cout << "Test 3: ";
  cin >> student_1[2][0];
    cout << "Please input the the test scores for student 2 Test 1: ";
  cin >> student_1[0][1];
  cout << "Test 2: ";
  cin >> student_1[1][1];
  cout << "Test 3: ";
  cin >> student_1[2][1];
  //gets the user's input for every test score for both student 1 and 2.
  cout << endl;
  average(student_1);
  cout << "Average Test Score of Student 2: " << endl;
  cout << (student_1[0][1]+student_1[1][1]+student_1[2][1])/3;
  //calls the function that will display the test score and average.
    return 0;
}

1 Answer

0 votes
answered Mar 16 by Peter Minarik (63,140 points)

The problem is that you index your array incorrectly.

You defined student_1 to be a 2 x 3 two-dimensional array. But later, when you try to index it as it would be a 3 x 2 array (e.g. student_1[2][1])

int student_1[2][3]={{0,1,2},{3,4,5}};
// ...
cout << (student_1[0][1]+student_1[1][1]+student_1[2][1])/3;

The simplest way to fix this is to define your student_1 array as a 3 x 2 matrix instead:

int student_1[2][3];

Please note that it is totally unnecessary to initialize your student_1 with values as you'll read them from the user anyway.

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...