C++ code Runtime error on 2D array declaration - c++

I dont understand why is this code giving runtime error on input of any test case (size of string is n, and then string is input). Not even the word "CHECK" (in the solve() function) gets printed.. Please help!
The problem link is : problem
#include <bits/stdc++.h>
using namespace std;
int a[1000][26];
int mov(int l, int u, int x)
{
if(l==u)
{
if(a[l][x]-a[l-1][x]==1)
return 1;
else
return 0;
}
int m=(l+u)/2;
return max(mov(l,m,x+1)+a[u][x]-a[m][x],mov(m,u,x+1)+a[m][x]-a[l-1][x]);
}
void solve()
{
int i,k,j,n;
string str;
cin >> n >> str;
cout << "CHECK";// This is not getting printed
for(i=0;i<26;i++)
a[0][i]=0;
for(i=1;i<n+1;i++)
{
for(j=0;j<26;j++)
{
a[i][j]=a[i-1][j];
if(str[i-1]==j+'a')
a[i][j]++;
}
}
k=mov(1,n,0);
cout << n-k << "\n";
}
int main()
{
int t=1;
cin >> t;
while(t--)
solve();
}

Your very first input shows how many input "pairs" will be given. In your case, you have skipped this part and gone into gathering the input "pairs". This is why "CHECK" wont get printed. To resolve this, have a cin to capture the number of input pairs, then iterate through the inputs to get the pair.
void solve()
{
int i, k, j, n;
string str;
memset(a[0], 0, sizeof(int) * 26); // Use this instead.
cin >> n;
cout << "CHECK";
for (i = 1; i < n + 1; i++)
{
cin >> j >> str;
for (j = 0; j < 26; j++)
{
a[i][j] = a[i - 1][j];
if (str[i - 1] == j + 'a')
a[i][j]++;
}
}
k = mov(1, n, 0);
cout << k << "\n";
}
Also, try not to use using namespace std;.

Related

I want to sum all my vector indexes with a recursive function but starting backwards

I am stuck with this program so far, and I don't know what else to do.
I just need help with the recursive function so I can do the other modes too, which are multiplication and subtraction.
#include "std_lib_facilities.h"
//this contains all the libraries my university offers.
int sum(vector <int> test, int i){
if (i < 0){
return 0;
}
else {
return test[i] + sum(test, i - 1);
}
}
int main(){
int n;
int x;
int mode;
int result = 0;
cout << "give the size of the vector:\n";
cin >> n;
while (n < 0){
cout << "you can't give negative number!give again:\n";
cin >> n;
}
vector <int> myvector;
for (int i = 0; i <= n-1; i++){
cout << "give numbers to fill the vector:\n";
cin >> x;
myvector.push_back(x);
}
cout << "give 1 for sum 2 for multiplication 3 for substraction\n";
cin >> mode;
while (mode < 1 && mode > 3){
cout << "this mode doesn't exist.give again:\n";
cin >> mode;
}
if (mode == 1){
result = sum(myvector, n);
cout << "result is:" << result;
}
}
I want to get all the indexes starting from backwards to sum them all.
You are accessing outside the bounds of your array.
The simple fix to your issue is to pass n-1 to your sum function.
result=sum(myvector,n - 1);
However, there is no need for you to pass n at all, rather i would suggest you add a function that starts the recursion like this. (this is a rather common pattern for recursion)
int sum(const vector<int>& test, int i);
int sum(const vector<int>& test)
{
if(test.empty())
{
return 0;
}
if(test.size() == 1)
{
return test[0];
}
return sum(test, test.size() - 1);
};
int sum(const vector<int>& test, int i)
{
if(i < 0)
{
return 0;
}
return test[i] + sum(test, i - 1);
};
then call it like sum(myvector);

c++ assertion string iterator offset out of reange

I am getting Assertion Failed. It show it after cin. I have checked also the file whose path it shows, but no solution.
"string iterator + offset out of range" is the massage it shows. It happens after I use "cin >> a" at line 65. Can anyone help me? Thanks in advance.
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
int gcd(int a, int b){
cout << "gcd" << endl;
if (b == 0) return a;
else return gcd(b, a % b);
}
int gcd_n(vector<int>nums){
int g;
size_t n = nums.size();
g = nums.at(0);
cout << "son" << endl;
for (int i = 1; i < n; i++){
g = gcd(nums.at(i), g);
}
return g;
}
long long int multi_vect(vector<int> v){
long long int m = 1;
for (size_t i = 0; i < v.size(); i++)
m *= v.at(i);
return m;
}
int main() {
int a;
vector <vector<int>> all_vects;
cin >> a;
string input, sub;
getline(cin, input);
int n = input.find(' ');
vector<int> vect;
while(true){
int num;
sub = input.substr(0, n);
istringstream(sub) >> num;
vect.push_back(num);
input = input.substr(n+1, input.back()+1);
n = input.find(' ');
if (n == -1){
istringstream(input) >> num;
vect.push_back(num);
break;
}
}
all_vects.push_back(vect);
}
When I tested this code, I found that n=-1 needs to be judged in advance. Suppose that input. Assuming that the string input has no ' ', then n will become -1, And the sub = input.substr(0, n); is not true. So, the vector vect will be empty. That is why string iterator + offset out of range will appear.
The following are my changes to this code. And you could refer to it.
if (n == -1)
{
istringstream(input) >> num;
vect.push_back(num);
break;
}
else {
sub = input.substr(0, n);
istringstream(sub) >> num;
vect.push_back(num);
input = input.substr(n + 1, input.back() + 1);
n = input.find(' ');
}

Can someone help to find the reason for s runtime error in this code?

The code runs fine in my compiler but shows runtime error on codeforces. I found out that I should avoid unitialized variables but I think I've already covered that.
#include <iostream>
using namespace std;
int main()
{
int i, n, flag = 1, k = 1;
int hours[n];
int minutes[n];
cin >> n;
for (i = 0; i < n; i++)
{
cin >> hours[i];
cin >> minutes[i];
}
for (i = 0; i < n; i++)
{
if (hours[i] == hours[i + 1] && minutes[i] == minutes[i + 1])
{
flag++;
if (flag > k)
k = flag;
}
else
flag = 1;
}
cout << k;
return 0;
}
The problem lies here
int i, n, flag = 1, k = 1;
int hours[n];
int minutes[n];
You declare variables n, but you don't initialize it, and then you use it to specify an array size. Uninitialized variables of type int contain garbage data, and using it is undefined behavior, whatever happens is unspecified, and you can not rely on it.
If you must create an array of size specified by a user input, you need to allocate at runtime.
int *hours;
int *minutes;
cin >> n;
hours = new int[n];
minutes = new int[n];
// Rest of the code
// Remember to delete them
delete[] minutes;
delete[] hours;
Better yet, use a container provided by the standard library, a std::vector would be a perfect container to use here
#include <vector>
std::vector<int> hours;
std::vector<int> minutes;
cin >> n
hours.resize(n);
minutes.resize(n);
VLAs are not standard C++ but are allowed as an extension. The first problem in your code is that you use the variable n without initialization. This causes undefined behavior according to the standard.
You need to move the call to cin() to above the arrays that use n at declaration. The second problem in your code is that the expression inside the if statement attempts to access an element outside the bounds of your array. This also causes undefined behavior.
Here is a refined version of your code:
#include <iostream>
using namespace std;
int main()
{
int n;
int k = 1;
int flag = 1;
cout << "Enter a value for n:" << endl;
cin >> n;
int *hours = new int[n];
int *minutes = new int[n];
cout << "Enter values for hours:" << endl;
for (int i = 0; i < n; i++)
{
cout << i + 1 << ":";
cin >> hours[i];
}
cout << "Enter values for minutes:" << endl;
for (int i = 0; i < n; i++)
{
cout << i + 1 << ":";
cin >> minutes[i];
}
// note here: 'i' goes to (n - 1) instead of (n) so as not to be out of range
for (int i = 0; i < n - 1; i++)
{
if ((hours[i] == hours[i + 1]) && (minutes[i] == minutes[i + 1]))
{
flag++;
if (flag > k)
{
k = flag;
}
}
else
{
flag = 1;
}
}
cout << "The value of k is: " << k << endl;
delete hours;
delete minutes;
return 0;
}

C++ line.exe has stopped working

I make some program in C++ which in matrix finds longest horizontal line of 0s.
In first line I input n and m (rows and columns in matrix array a), and later array. Main problem is when I insert first line (all correct with it) the program stops with error -1073741510, line.exe has stopped working.
#include <iostream>
using namespace std;
int main()
{
int n, m;
cin >> n, m;
int a[n][m];
int i,j,k;
for (i=0;i<n;i=i+1){
for(j=0;j<m;j=j+1){
int temp;
cin >> temp;
a[i][j] = temp;
}
}
int max;
for (i=0;i<n;i++){
for(j=0;j<m;j++){
if(a[i][j]==0){
for(k=j+1;k<m;k++){
if(a[i][k]==0){
max++;
}else{break;}
}
}
}
}
cout << max;
return 0;
}
Sorry for big amount of for loops, I don't know better way of solving problem.
To read both numbers n and m use:
cin >> n >> m;
Otherwise, as you have cin >> n, m; it is equivalent with
cin >> n;
m; // This has no effect
For a solution to the correct operation of your algorithm, try this:
int maxZeros = 0;
int lineContainingMaxZeros = 0;
for (i = 0; i < n; i++) {
int countZero = 0;
for (j = 0; j < m; j++) {
if (a[i][j] == 0)
countZero++;
}
if (countZero > maxZeros) {
maxZeros = countZero;
lineContainingMaxZeros = i;
}
}
cout << "Line: " << lineContainingMaxZeros << " containing " << maxZeros << " zeros";

Heap corruption detected due to array usage

Sorry, I'm a brand new newbie to C++ and programming and I'm getting a heap corruption error. I think Im writing in unallocated memory but I can't seem to find where the error is... the program is soppuse to take user input values and rearrange them so that they would ascend. I'm also learning templates too.
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
template <typename T>
void sort(T arrayz[], int size, char ch)
{
T temporary;
int k, j;
if (ch = 'a')
{
for (k = 0; k < size; k++)
{
for (j = 0; j < size; j++)
{
temporary = arrayz[j];
arrayz[j] = arrayz[j + 1];
arrayz[j + 1] = temporary;
}
}
}
}
int main()
{
int choices, range, i;
int x;
char ch;
cout << ("Enter the amount of numbers you want =>");
cin >> x;
int *numbers = new int[x];
if (!numbers)
{
cout << "Memory Allocation error!";
cin.get();
exit(1);
}
for (int i = 0; i<x; i++)
{
cout << "Option number" << i + 1 << " =>";
cin >> numbers[i];
}
cout << "Do you want ascending or descending values (a/d) =>" ;
cin >> ch;
if (ch = 'a')
{
sort(numbers, x, ch);
}
else if (ch = 'd')
{
sort(numbers, x, ch);
}
delete[] numbers;
fflush(stdin);
cin.get();
return 0;
}
In your sort function, you are accessing elements at index j + 1. However, this is out of bounds. The valid indexes for your arrayz array are 0 through size-1. When j is size-1, j+1 is size, which accesses past the end of the array.