C++ .exe "has stopped working" after calling function twice - c++

So I have a program in C++ that calls a function three times, I'm making this project using Code::Blocks and when I run the release version of the .exe it tells me that the program has stopped working and that I have to close the program. I tried calling the function just once and it works, no matter which call I leave. So it looks like it crashes only when it's called twice, regardless of which variables I pass. Any idea of why this happens?
Here's the function I call:
int interval(int h, int i, std::vector< std::vector<int> >& Array, int arr[], int interv)
{
int k = i + 1;
int P[20];
int number;
bool a;
int b;
int N;
int maxres;
int result[90];
for (h = 0; h < 20; h++)
{
for (N = 1; N <= 90; N += 1)
{
result[N] = 0;
for (i = 1; i <= (k - interv); i++)
{
number = Array[i - 1][h] - N;
if (number <= 0)
number += 90;
for (b = 0; b < 20; b++)
{
a = false;
if ((number == Array[i][b]) || ( (interv == 1) && (number == Array[i + 1][b]) ) || ( (interv == 2) && (number == Array[i + 2][b]) ) )
{
a = true;
result[N]++;
break;
}
}
if (!a)
break;
}
if (N == 1)
maxres = N;
else
{
if (result[N] >= result[maxres])
maxres = N;
}
}
P[h] = maxres;
}
for (h = 0; h < 20; h++)
{
arr[h] = Array[0][h] + P[h];
if ( arr[h] > 90 )
arr[h] -= 90;
}
return 0;
}
And here are the calls:
int c[20];
int e[20];
int d[20];
interval(h, i, Array, c, 0);
interval(h, i, Array, e, 1);
interval(h, i, Array, d, 2);

One problem is out-of-bounds accesses to result:
int result[90];
for (h = 0; h < 20; h++)
{
for (N = 1; N <= 90; N += 1) <--- Here!
{
result[N] = 0;
The allowed values for N are 0 to 89 (like in for (N = 0; N != 90; ++N))
There might be a similar problem here
for (i = 1; i <= (k - interv); i++)
but I can't tell for sure.

Related

Rabin-Karp algorithm in c++

I am trying to understand the implementation of the Rabin-Karp algorithm. d is the number of characters in the input alphabet, but if I replace 0 or any other value instead of 20, it won't affect anything. Why is this happening like this ?
// Rabin-Karp algorithm in C++
#include <string.h>
#include <iostream>
using namespace std;
#define d 20
void rabinKarp(char pattern[], char text[], int q) {
int m = strlen(pattern);
int n = strlen(text);
int i, j;
int p = 0;
int t = 0;
int h = 1;
for (i = 0; i < m - 1; i++)
h = (h * d) % q;
// Calculate hash value for pattern and text
for (i = 0; i < m; i++) {
p = (d * p + pattern[i]) % q;
t = (d * t + text[i]) % q;
}
// Find the match
for (i = 0; i <= n - m; i++) {
if (p == t) {
for (j = 0; j < m; j++) {
if (text[i + j] != pattern[j])
break;
}
if (j == m)
cout << "Pattern is found at position: " << i + 1 << endl;
}
if (i < n - m) {
t = (d * (t - text[i] * h) + text[i + m]) % q;
if (t < 0)
t = (t + q);
}
}
}
int main() {
// char text[] = "ABCCDXAEFGX";
char text[] = "QWERTYUIOPASDFGHJKLXQWERTYUIOPASDFGHJKLX";
char pattern[] = "KLXQW";
int q = 13;
rabinKarp(pattern, text, q);
}
I believe the short answer is that the lower d is the more hash collisions you will have, but you go about verifying the match anyway so it does not affect anything.
A bit more verbose:
First let me modify your code to be have more expressive variables:
// Rabin-Karp algorithm in C++
#include <string.h>
#include <iostream>
using namespace std;
#define HASH_BASE 0
void rabinKarp(char pattern[], char text[], int inputBase) {
int patternLen = strlen(pattern);
int textLen = strlen(text);
int i, j; //predefined iterators
int patternHash = 0;
int textHash = 0;
int patternLenOut = 1;
for (i = 0; i < patternLen - 1; i++)
patternLenOut = (patternLenOut * HASH_BASE) % inputBase; // hash of pattern len
// Calculate hash value for pattern and text
for (i = 0; i < patternLen; i++) {
patternHash = (HASH_BASE * patternHash + pattern[i]) % inputBase;
textHash = (HASH_BASE * textHash + text[i]) % inputBase;
}
// Find the match
for (i = 0; i <= textLen - patternLen; i++) {
if (patternHash == textHash) {
for (j = 0; j < patternLen; j++) {
if (text[i + j] != pattern[j])
break;
}
if (j == patternLen)
cout << "Pattern is found at position: " << i + 1 << endl;
}
if (i < textLen - patternLen) {
textHash = (HASH_BASE * (textHash - text[i] * patternLenOut) + text[i + patternLen]) % inputBase;
if (textHash < 0)
textHash = (textHash + inputBase);
}
}
}
int main() {
// char text[] = "ABCCDXAEFGX";
char text[] = "QWEEERTYUIOPASDFGHJKLXQWERTYUIOPASDFGHJKLX";
char pattern[] = "EE";
int q = 13;
rabinKarp(pattern, text, q);
}
The easiest way to attack it is to set HASH_BASE (previously d) to zero and see where we can simplify. The rabinKarp function can then be reduced to:
void rabinKarp(char pattern[], char text[], int inputBase) {
int patternLen = strlen(pattern);
int textLen = strlen(text);
int i, j; //predefined iterators
int patternHash = 0;
int textHash = 0;
int patternLenOut = 0;
// Calculate hash value for pattern and text
for (i = 0; i < patternLen; i++) {
patternHash = (pattern[i]) % inputBase;
textHash = (text[i]) % inputBase;
}
// Find the match
for (i = 0; i <= textLen - patternLen; i++) {
if (patternHash == textHash) {
for (j = 0; j < patternLen; j++) {
if (text[i + j] != pattern[j])
break;
}
if (j == patternLen)
cout << "Pattern is found at position: " << i + 1 << endl;
}
if (i < textLen - patternLen) {
textHash = (text[i + patternLen]) % inputBase;
if (textHash < 0)
textHash = (textHash + inputBase);
}
}
}
now you'll notice that all the hashes becomes is the sum of the letters mod some number (in your case 13, in my case 2). This is a bad hash, meaning many things will sum to the same number. However, in this portion of the code:
if (patternHash == textHash) {
for (j = 0; j < patternLen; j++) {
if (text[i + j] != pattern[j])
break;
}
if (j == patternLen)
cout << "Pattern is found at position: " << i + 1 << endl;
}
you explicitly check the match, letter by letter, if the hashes match. The worse your hash function is, the more often you will have false positives (which will mean a longer runtime for your function). There are more details, but I believe that directly answers your question. What might be interesting is to record false positives and see how the false positive rate increases as d and q decrease.

Both are using Expand Around Center Algorithm for finding longest palindromic substring and why is one faster a lot?

Here is the faster solution A that only perform 6ms on leetcode and it's almost as fast as Manacher Algorithm(6ms on leetcode).
class Solution {
public:
string longestPalindrome(string s) {
if (s.empty()) return "";
if (s.size() == 1) return s;
int min_start = 0, max_len = 1;
for (int i = 0; i < s.size();) {
if (s.size() - i <= max_len / 2) break;
int j = i, k = i;
while (k < s.size()-1 && s[k+1] == s[k]) ++k; // Skip duplicate characters.
i = k+1;
while (k < s.size()-1 && j > 0 && s[k + 1] == s[j - 1]) { ++k; --j; } // Expand.
int new_len = k - j + 1;
if (new_len > max_len) { min_start = j; max_len = new_len; }
}
return s.substr(min_start, max_len);
}
};
And I don't understand why the blow solution B is slow to much compared with solution A, which costs 16ms runtime.
class Solution {
public:
int expandAroundCenter(string s, int left,int right) {
int L = left, R = right;
while(L >=0 && R < s.length() && s[L] == s[R]) {
L--;
R++;
}
return R - L - 1;
}
string longestPalindrome(string s) {
int start = 0, end = 0;
if (s.empty()) return "";
if (s.size() == 1) return s;
for(size_t i = 0; i < s.length(); i++) {
int len1 = expandAroundCenter(s, i, i);
int len2 = expandAroundCenter(s, i, i + 1);
int len = std::max(len1, len2);
if(len > end - start + 1) {
start = i - (len - 1) / 2;
end = i + len / 2;
}
if(start + len > s.length()) break;
}
return s.substr(start, end - start + 1);
}
};
Since they are both Expand Around Center Algorithm and get O(N^2) time complexity and O(1) space complexity(Manacher's Algorithm gets O(n) time complexity), I can only speculate the coding method is the most important factor. Well, if it's the case and I really want to know why.
With the optimazation, solution B is still slower 50% than A.
class Solution {
public:
int expandAroundCenter(const string& s, int left,int right) {
int L = left, R = right;
while(L >=0 && R < s.length() && s[L] == s[R]) {
L--;
R++;
}
return R - L - 1;
}
string longestPalindrome(const string& s) {
int max_len = 0;
int min_start = 0;
if (s.empty()) return "";
if (s.size() == 1) return s;
for(size_t i = 0; i < s.length(); i++) {
if (s.size() - i <= max_len / 2)
break;
int j = i, k = i;
while (k < s.size()-1 && s[k+1] == s[k]) ++k;
int len = expandAroundCenter(s, j, k);
if(len > max_len) {
max_len = len;
min_start = j - (len - (k - j + 1)) / 2;
}
}
return s.substr(min_start, max_len);
}
};
Algorithm A skips pumped character sequences first which most likely make up for a significant portion of the benchmark. Algorithm B falls for that trap and iterates over the pumped sequence twice, once for each call of the predicate.
Take the benchmark suite, and replace all single character repetitions by a [Pad][Character][Pad], then this optimization breaks.

C ignoring incrementation

I tried this but my compiler(Visual Studio 2013) keeps messing up things.
I have a 9 by 9 matrix indexed from 1. It is 0 at the beginig. And starting from element 1:1 I start incrementing the value in the matrix or incrementing x,y, basically moving to the next matrix element.
However, the program ignores my incrementation and the fact that x,y are initially set to 1.
Also it ignores a function call.
Code is commented below.
I am sure this is the source I am compiling!
Restarted laptop and Visual Studio but still doesn't work.
Opened new project, same thing.
Thanks in advance.
#include<stdio.h>
#include<stdlib.h>
unsigned int Matrix[10][10], x, y;
// Ignore this..
int checkLine()
{
unsigned int i, j, k;
for (k = 1; k <= 9; k++){
if (Matrix[k][1] == 0) break;
for (i = 1; i <= 9; i++)
for (j = 1; j <= 9; j++)
if (Matrix[k][i] == Matrix[k][j] && i!=j)
return 0;
}
return 1;
}
//Ignore this..
int checkColumn()
{
unsigned int i, j, k;
for (k = 1; k <= 9; k++){
if (Matrix[1][k] == 0) break;
for (i = 1; i <= 9; i++)
for (j = 1; j <= 9; j++)
if (Matrix[i][k] == Matrix[j][k] && i!=j)
return 0;
}
return 1;
}
//Ignore this..
int checkSquare()
{
unsigned int i, j, k,l,m,n;
for (m = 1; m <= 7; m = m + 3)
for (n = 1; n <= 7; n = n + 3)
for (k = m; k <= m + 2; k++)
for (l = n; l <= n + 2; l++)
for (i = m; i <= m + 2; i++)
for (j = n; j <= n + 2; j++)
if (Matrix[k][l] == Matrix[i][j] && !(k==i && l==j))
return 0;
return 1;
}
void increment()
{
if (y == 9)
{
x++;
y = 1;
}
else y++;
}
void decrement()
{
if (y == 1)
{
x--;
y = 9;
}
else
y--;
}
void print_Matrix(){
unsigned int i, j;
for (i = 1; i <= 9; i++){
for (j = 1; j <= 9; j++)
printf("%u ", Matrix[i][j]);
printf("\n");
}
}
//
// MAIN. PROBLEM BELOW
//**
void main()
{
unsigned int i, j;
for (i = 1; i <= 9;i++)
for (j = 1; j <= 9; j++)
Matrix[i][j] = 0;
print_Matrix(); // Function call is ignored here. Don't know why.***
x = 1;
y = 1;
// X and Y are OBVIOUSLY 1***
while (x < 10) //Condition OBVIOUSLY true***
{
printf("%u, %u", x, y); //Keeps printing 0,3 and eventually 0,2***
printf("\n");
Matrix[x][y]++; //Incrementation...***
print_Matrix(); // Always prints a blank Matrix consisting of only 0's***
if (checkLine() && checkColumn() && checkSquare())
{
increment();
}
if (Matrix[x][y] == 10){
Matrix[x][y] = 0;
decrement();
}
}
print_Matrix();
}
You feel that the increment is ignored because the checkSquare function is buggy. It never returned 1 and hence the increment function was never called.
What happens is that you're incrementing the position marked by x and y, that's position [1][1]. Until it reaches 10 nothing interesting happens, you can actually see the top left corner of the matrix increasign to 10, but then the condition to decrement becomes true and you decrement.
See for yourself (prints),
while (x < 10) //Condition OBVIOUSLY true***
{
printf("%u, %u", x, y); //Keeps printing 0,3 and eventually 0,2***
printf("\n");
Matrix[x][y]++; //Incrementation...***
print_Matrix(); // Always prints a blank Matrix consisting of only 0's***
if (checkLine() && checkColumn() && checkSquare())
{
increment();
}
if (Matrix[x][y] == 10){
Matrix[x][y] = 0;
decrement();
}
printf( "Enter to continue\n" );
getchar();
}
It turns Y = 9, X = 0 and the [1][1] position becomes 0, so you see only zeros because you're not printing the zero indexes.
This process repeats until Y = 1 and X = 0, you increase the position until 10 so that the decrement works again.
When Y = 1, X = 0 and position [0][1] is 10, the decrement call will do x--. Since X is an unsigned int, it will underflow and become 4.2 billion something, which is greater than 10 and the loop ends.
What are you trying to achieve here?
Edit: Something even more amazing happens when you make x and y ints instead of unsigned ints.
Instead of x underflowing it will become -1. Matrix[-1][9]++ strangely increased x by 1 when I ran the code, so x went back to 0. Which means the program loops forever at this point.
The increment function was never called.
It shows matrix and increment when tested online, here are results
1) for C compiler
http://ideone.com/KRLO8w
#include<stdio.h>
#include<stdlib.h>
unsigned int Matrix[10][10], x, y;
// Ignore this..
int checkLine()
{
unsigned int i, j, k;
for (k = 1; k <= 9; k++){
if (Matrix[k][1] == 0) break;
for (i = 1; i <= 9; i++)
for (j = 1; j <= 9; j++)
if (Matrix[k][i] == Matrix[k][j] && i!=j)
return 0;
}
return 1;
}
//Ignore this..
int checkColumn()
{
unsigned int i, j, k;
for (k = 1; k <= 9; k++){
if (Matrix[1][k] == 0) break;
for (i = 1; i <= 9; i++)
for (j = 1; j <= 9; j++)
if (Matrix[i][k] == Matrix[j][k] && i!=j)
return 0;
}
return 1;
}
//Ignore this..
int checkSquare()
{
unsigned int i, j, k,l,m,n;
for (m = 1; m <= 7; m = m + 3)
for (n = 1; n <= 7; n = n + 3)
for (k = m; k <= m + 2; k++)
for (l = n; l <= n + 2; l++)
for (i = m; i <= m + 2; i++)
for (j = n; j <= n + 2; j++)
if (Matrix[k][l] == Matrix[i][j] && !(k==i && l==j))
return 0;
return 1;
}
void increment()
{
if (y == 9)
{
x++;
y = 1;
}
else y++;
}
void decrement()
{
if (y == 1)
{
x--;
y = 9;
}
else
y--;
}
void print_Matrix(){
unsigned int i, j;
for (i = 1; i <= 9; i++){
for (j = 1; j <= 9; j++)
printf("%u ", Matrix[i][j]);
printf("\n");
}
}
//
// MAIN. PROBLEM BELOW
//**
void main()
{
unsigned int i, j;
for (i = 1; i <= 9;i++)
for (j = 1; j <= 9; j++)
Matrix[i][j] = 0;
print_Matrix(); // Function call is ignored here. Don't know why.***
x = 1;
y = 1;
// X and Y are OBVIOUSLY 1***
while (x < 10) //Condition OBVIOUSLY true***
{
printf("%u, %u", x, y); //Keeps printing 0,3 and eventually 0,2***
printf("\n");
Matrix[x][y]++; //Incrementation...***
print_Matrix(); // Always prints a blank Matrix consisting of only 0's***
if (checkLine() && checkColumn() && checkSquare())
{
increment();
}
if (Matrix[x][y] == 10){
Matrix[x][y] = 0;
decrement();
}
}
print_Matrix();
}
2) for c++ compiler C++ 4.9.2 (changed return type of main to int)
http://ideone.com/Ey5nG1
In your image starting value of 0, 3 is due to buffer limit of command prompt. As the program never ends, so it terminates abruptly and latest few bytes are stored in buffer and is only shown that much. To see complete output redirect it to a file and open it.
Your output is a bit confusing since the output of the line
printf("%u, %u", x, y);
runs into the output of
print_Matrix();
By adding a newline to the output of the first line, i.e. by using
printf("%u, %u\n", x, y);
you will notice that at some point x gets decremented to 0 and never gets incremented again. Since you never print Matrix[0][y], you never see the non-zero values.
In addition to the change to above printf, if you change print_Matrix to:
void print_Matrix(){
unsigned int i, j;
for (i = 0; i <= 9; i++){
// ^^^ Use 0 instead of 1
for (j = 0; j <= 9; j++)
// ^^^ Use 0 instead of 1
printf("%u ", Matrix[i][j]);
printf("\n");
}
}
you will see the non-zero values.
See working code at http://ideone.com/HlQ4xp.

Optimizing a program C++

I have to create a program, which counts bursted baloons, like from ZUMA. If I have a line with 3 or more baloons with the same color this sequence will burst. So in input, I have number of ballons (3 <= N <= 10^5) , and line with numbers (line with baloons color (1 <= сi <= 100) ), with 1 sequence for sure. I have to output number of bursted baloons. I have a programm, but it is working longer than 4000msv sometimes. How can I make it working faster?
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int Fmax(int n, const string& f){
int max;
vector<int> k(n);
int i, j, p = 0;
for (i = 0; i <= n; i++)
{
k[i] = 0;
}
for (i = 0; i <= n; i++)
{
for (j = i; j <= n; j++)
{
if (f[i] == f[j])
{
k[p]++;
}
else break;
}
p++;
}
max = k[0];
for (i = 0; i <= p; i++){ if (max <= k[i]){ max = k[i]; } }
return max;
}
string pog(int n){
int d;
string doa;
for (int i = 1; i <= n; i++){
cin >> d;
doa += (char)d;
}
return doa;
}
void main(){
int i, sc = 1, bf = 1;
string f;
int len;
cin >> i;
f = pog(i);
len = i;
while (Fmax(f.length(), f) >= 3){
for (int c = 1; c <= f.length(); c++){
if (f[c] == f[c - 1]){
if (sc == 1){ bf = c - 1; }
sc++;
}
else{
if (sc >= 3){ f.erase(bf, sc); sc = 1; break; }
sc = 1;
}
}
}
cout << len - f.length() << endl;
}
Any help is warmly welcome.
You are leaking memory. Use vectors to avoid that.
Why do you need to create array? Why not use the string directly?
Pass strings which aren't modified by const reference to avoid copies.
Use constant variables for the lengths:
const unsigned int f_length = f.length();
while (Fmax(f_length, f) >= 3){
for (int c = 1; c <= f_length ; c++){
This helps the compiler reduce the number of calls to the length method.

equidivision partition using breadth first search

i was trying to solve a breadth first search problem on spoj.the problem statement is as follows:
An equidivision of an n × n square array of cells is a partition of the n^2 cells in the array in exactly n sets, each one with n contiguous cells. Two cells are contiguous when they have a common side.
the problem link is:http://www.spoj.com/problems/EQDIV/
my code:http://ideone.com/OjluJG
#include<cstdio>
#include<cstring>
#include<iostream>
#include<sstream>
#include<queue>
using namespace std;
#define pii pair< int, int>
int n, m, grid[105][105];
char inp[101 * 101];
bool inRange(int i, int j)
{
m = n;
return (i >= 0 && i < n && j >= 0 && j < m);
}
int main()
{
int i, j, comp;
scanf("%d", &n);
while (n != 0)
{
if (n == 1)
{
printf("good\n");
scanf("%d", &n);
continue;
}
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
grid[i][j] = 0;
}
}
int a, b, start1[n], start2[n], print = 0, c, d;
queue< pii > Q;
pii p;
getchar();
for (i = 1; i <= n - 1; i++)
{
gets(inp);
stringstream ss(inp);
for (j = 1; j <= n; j++)
{
ss >> a >> b;
if (j == 1)
{
start1[i - 1] = a;
start2[i - 1] = b;
//printf("%d %d\n",start1[i-1],start2[i-1]);
}
grid[a - 1][b - 1] = i;
}
}
for (int x = 0; x < n; x++)
{
int count = 1;
if (x == n - 1)
{
comp = 0;
p.first = c;
p.second = d;
Q.push(p);
}
else
{
comp = x + 1;
p.first = start1[x] - 1;
p.second = start2[x] - 1;
Q.push(p);
}
while (!Q.empty())
{
p = Q.front();
i = p.first;
j = p.second;
Q.pop();
grid[i][j] = -1;
if (x != n - 1)
{
if (inRange(i + 1, j) && grid[i + 1][j] == 0)
{
c = i + 1;
d = j;
}
if (inRange(i - 1, j) && grid[i - 1][j] == 0)
{
c = i - 1;
d = j;
}
if (inRange(i, j + 1) && grid[i][j + 1] == 0)
{
c = i;
d = j + 1;
}
if (inRange(i, j - 1) && grid[i][j - 1] == x + 1)
{
c = i;
d = j - 1;
}
}
if (inRange(i + 1, j) && grid[i + 1][j] == comp)
{
p.first = i + 1;
p.second = j;
Q.push(p);
count += 1;
}
if (inRange(i - 1, j) && grid[i - 1][j] == comp)
{
p.first = i - 1;
p.second = j;
Q.push(p);
count += 1;
}
if (inRange(i, j + 1) && grid[i][j + 1] == comp)
{
p.first = i;
p.second = j + 1;
Q.push(p);
count += 1;
}
if (inRange(i, j - 1) && grid[i][j - 1] == comp)
{
p.first = i;
p.second = j - 1;
Q.push(p);
count += 1;
}
}
if (count != n)
{
print = 1;
printf("wrong\n");
break;
}
}
if (print == 0)
printf("good\n");
//printf("%d %d\n",c,d);
scanf("%d", &n);
}
}
i am getting wrong answer ,don't know why?
can someone provide the testcase for which it is wrong?