Home c++ Error: exe triggered a breakpoint. C++

Error: exe triggered a breakpoint. C++

Author

Date

Category

The code is a task from practice. The task is to write and apply a linear search on an array of 6 mil. up to 8 mil. in steps of 1 mil. And everything seems to work, but an error appears and I can’t figure out why …

Here’s the code:

# include & lt; fstream & gt;
#include & lt; string & gt;
#include & lt; time.h & gt;
#include & lt; math.h & gt;
#include & lt; stdio.h & gt;
using namespace std;
int search (int key, int arr [], int numb) {
  int c = 0;
  double time = clock (); // Time at the start of the search
  for (int i = 0; i & lt; = numb-1; i ++) {
    C++;
    if (arr [i] == key) {
      cout & lt; & lt; "Number of comparisons:" & lt; & lt; c & lt; & lt; "\ nSearch time:" & lt; & lt; (clock () - time) / CLOCKS_PER_SEC & lt; & lt; "seconds. \ n";
      return i;
    }
  }
  cout & lt; & lt; "Number of Comparisons:" & lt; & lt; c & lt; & lt; "\ nSearch time:" & lt; & lt; (clock () - time) / CLOCKS_PER_SEC & lt; & lt; "seconds. \ n";
  return -1;
}
void LineSerch (int key, int arr [], int numb) {
  int f;
  f = search (key, arr, numb);
  if (f & gt; -1) {
    cout & lt; & lt; "Number is in item numbered:" & lt; & lt; f + 1 & lt; & lt; endl;
  }
  if (f == -1) {
    cout & lt; & lt; "There is no such number. \ N";
  }
}
void write (int col, string text) {
  ofstream fout;
  fout.open ("file.txt");
  for (int i = 1; i & lt; = col; i ++) {
    fout & lt; & lt; text;
  }
  fout.close ();
}
int main () {
  setlocale (LC_ALL, "Russian");
  int number, key, t = 0;
  int arr [10];
  cout & lt; & lt; "Enter a test array of 10 elements \ n";
  for (int i = 0; i & lt; = 9; i ++) {
    cin & gt; & gt; arr [i];
  }
  cout & lt; & lt; "Enter the number to search for: \ n";
  LineSerch (2, arr, 10);
  for (int i = 0; i & lt; = 9; i ++) {
    cout & lt; & lt; arr [i] & lt; & lt; "";
  }
  string text = "5";
  write (70, text);
  for (int n = 6000000; n & lt; = 8000000; n = n + 1000000) {
    int * b = new int [n];
    t ++;
    cout & lt; & lt; "\ n ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \ n "& lt; & lt; "n" & lt; & lt; t & lt; & lt; "=" & lt; & lt; n & lt; & lt; "\ n";
    for (int i = 0; i & lt; n; i ++) b [i] = rand ();
    cout & lt; & lt; "Enter the number to search for:";
    cin & gt; & gt; key;
    for (int i = 1; i & lt; = n; i ++) {
      if (i == n / 2 - 1) {b [i] = 2; }
      else {b [i] = 1; }
    }
    LineSerch (key, b, n);
    cout & lt; & lt; "\ n ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \ n ";
    delete [] b;
  }
  return 0;
}

Answer 1

At least –

int * b = new int [n];
for (int i = 1; i & lt; = n; i ++) {
  if (i == n / 2 - 1) {b [i] = 2; }
    else {b [i] = 1; }

Total – outside the array (i in b [i] cannot equal n ).

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions