-5

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;
}
10
  • 1
    Mo99[i] when i == 100 is access the array out of bounds, same for the other arrays of size 100. Commented Nov 4 at 19:59
  • But doesn't i = 100 never run, since the for loop uses i < length and length = 100? Commented Nov 4 at 20:01
  • for(int i = 0; i <= 100; i++) note the <= Commented Nov 4 at 20:02
  • OH! Wait, yeah no I think I get it. i = 100 in the main function, not the countForValue function. Thanks! Commented Nov 4 at 20:04
  • 3
    Welcome to Stack Overflow. Please read the help center, take the SO tour, and read How to Ask. Also please read about how to write the "perfect" question, especially its checklist. Also please learn how to create a minimal reproducible example. Commented Nov 4 at 20:23

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

In general, if you ever see a <= in a a loop exit condition you should stop and check the range. You're probably looking at a bug.
The two hardest things in computer programming are naming things, cache invalidation, and off-by-one errors...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.