c++arrayssegmentation-fault

Why is this C++ program giving a "Segmentation Error" on line 84?


I'm doing an assignment for an engineering course I'm taking, and I'm testing my code as I go along. For some reason, it gives an error when I compare an array element to a value passed into my countForValue function.

I am just learning C++ and didn't know what this meant at all until I googled it, and I still have no idea what could be putting either of these values out of the function's reach. At first, I thought it could have been a case of "I used a variable name I shouldn't have and it got confused", but that doesn't seem to be it at all (no change after I changed them).

Below is the entirety of the code I have written so far. The error occurs on the evaluation of:

if(myArray[i] == myValue) 

inside the countForValue function.


//Name: 
//Date: 11/4/2025
//Description:


#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <iomanip>

using namespace std;


double rand01();
int countForValue(int myArray[], int length, int myValue);
void decayOnce(int array[], int length);
void displayBar(int time, int amount);

int main()
{
  //declaraibles the variables
  int numAtoms = 100;
  srand(time(0));
  int atoms[numAtoms] = {0};
  int Mo99[100] = {0}, Tc99m[100] = {0}, Tc99[100] = {0};

  //open file in fout and check for failure
  ofstream fout("atoms.txt");
  if(fout.fail()){
    cerr << "File failed to open.";
    return -1;
  }
  
  
  //repeat 101x (this way we get values for t = 0 and 100)
  for(int i = 0; i <= 100; i++){
    //count for each array, store in its given index
    Mo99[i] = countForValue(atoms, numAtoms, 0);
    Tc99m[i] = countForValue(atoms, numAtoms, 1);
    Tc99[i] = countForValue(atoms, numAtoms, 2);

    //then decay
    decayOnce(atoms, numAtoms);
  }
  
  //document results in chart
  fout << "time  Mo99 Tc99m  Tc99" << endl;
  //repeat 101x (for same reason as before)
  for(int i = 0; i <= 100; i++){
    //print a row on the chart
    fout << setw(4) << i << setw(6) << Mo99[i] << setw(6) << Tc99m[i] << setw(6) << Tc99[i] << endl;
  }


  fout << setw(4) << "0" << setw(6) << "10000";


  return 0;
}



//Name: rand01
//Description: returns a double between 0 and 1.
double rand01()
{
  double maxrand = 100000000;
  //take random value, % it within 100 mil, then divide in double division by 10 mil.
  double result = (rand() % 100000000) / maxrand;
  return result;
}



//Name: countForValue
//Description: takes an array, array length and a value, then returns the amount of values in that array == that value.
int countForValue(int myArray[], int length, int myValue)
{
  int result = 0;
  //for each array item, if it's the value, increase result by 1
  for(int i = 0; i < length; i++){
    if(myArray[i] == myValue){
      result ++;
    }
  }
  return result;
}



//Name: decayOnce
//Description: takes an array and iterates it one step in radioactive decay.
void decayOnce(int array[], int length)
{
  //repeat 60x
  for(int i = 0; i < 60; i++){

    //repeat for each item in array
    for(int j = 0; j < length; j++){

      //if Mo99 and roll decay, set to Tc99m
      if(array[j] == 0 && rand01() <= 0.000175){
        array[j]++;

      //otherwise, if Tc99m and roll decay, set to Tc99
      }else if(array[j] == 1 && rand01() <= 0.0019254){
        array[j]++;
      }
    }
  }

  return;
}



//Name: displayBar
//Description: Takes a time and amount value, and prints a bar of a bar graph with that time as its header and that amount of asterisks.
void displayBar(int time, int amount)
{
  //print time and the little bar
  cout << setw(4) << time << " |";
  //print amount # of *s
  for(int i = 0; i < amount; i++){
    cout << "*";
  }
  cout << endl;

  return;
}

Solution

  • You wrongly delimited the memory segment I see. Look, you gave to Mo99 an array of size 100 but the loop goes from 0 to 100, it's 101 spaces of memory, because you wrote i=0 to i<=100. This includes i=100.