(C++) fstream win32 exception occured in title.exe [6284] - c++

So I made the following algorithm to solve a problem, but I don't understand why it crashes when I use fstream instead of iostream. When I use lower numbers, for example (input e.g.: 1 4 5 ) it works perfectly with fstream, but if I use some larger input numbers (e.g: 3 987654300 210 ),it says "Process returned -1073741819 (0xC0000005)". If I use iostream, it works great even with 9 digit inputs.
Yes, I have the parola.in and parola.out in the same folder with the .cpp/.exe and the .in has the input numbers and everything.
Ignore the algorithm and the comments, and just pay attention at the use of "fout" and "fin" please.
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
ifstream fin("parola.in");
ofstream fout("parola.out");
int a,b,k; //VARIABILE ORIGINALE
int ord=1; //NUMARUL DE ORDINE AL VECTORILOR
int cifa=0,cifb=0; //NUMARUL DE CIFRE AL NUMERELOR A SI B
int nrA[18],nrB[10]; //VECTORII CE CONTIN CIFRELE NUMERELOR A SI B
int temp[9]; //VECTORUL CE CONTINE CIFRELE NR. A IN ORDINE INVERSA
fin>>k>>a>>b;
while (a>0)
{
temp[ord]=a%10;
a/=10;
ord++;
cifa++;
}
ord=1; //RESET NR ORDINE
while (b>0)
{
nrB[ord]=b%10;
b/=10;
ord++;
cifb++;
}
for (int topkek=1; topkek<=cifa; topkek++)
{
nrA[topkek]=temp[cifa+1-topkek]; //ORDONAREA CIFRELOR LUI A CRESCATOR, IN VECTORUL nrA
}
for (int zomtan=1; zomtan<=k; zomtan++)
{
fout<<nrA[zomtan];
nrA[zomtan]=-1;
}
fout<<nrB[1];
nrB[1]=-1;
for (int zomtan2=cifa+1; zomtan2<=18; zomtan2++)
{
nrA[zomtan2]=-1;
}
for (int zomtan3=cifb+1; zomtan3<=10; zomtan3++)
{
nrB[zomtan3]=-1;
}
for (ord=1; ord<=9; ord++)
{
if (nrA[ord+k]!=-1)
{
fout<<nrA[ord+k];
}
if (nrB[ord+1]!=-1)
{
fout<<nrB[ord+1];
}
}
return 0;
}

The problem can occurs beause, as I suppose, you use 1 based arrays instead of propper 0 based
for (int zomtan2=cifa+1; zomtan2<=18; zomtan2++)
{
nrA[zomtan2]=-1;
}
the problem will always occurs because you have wrote zomtan2<=18
and your array declaration is int nrA[18] wich I have said previously always casues problems.
These errors are everywhere in your code, so i suggest you to rewrite the program

Related

From Lower Cases to Upper Cases Quote Console App

I have a console app which takes an input. The user inputs a text such as "It's better to be a '<'good man'>' rather than a '<succesful man'>" , and transforms it like so : "It's better to be a '<'GOOD MAN'>' rather than a '<SUCCESFUL MAN'>".
I did the following script but beeing C++ newbie it doesn t quite work and I get the following output:"It's better to be a '<good man'>' rather than a '<SUCCESFUL MAN'>"
Note:the program can have multiple "quotes".
This is my attept :
#include <iostream>
#include <cstring>
using namespace std;
char a[]="It's better to be a <good man> rather than a <succesful man>",*p;
int x,y,bec1,bec2;
int main()
{
for(int i=0;i<strlen(a);i++){
if(a[i]=='<'){
x=i;
bec1=1;
}
if(a[i]=='>'){
y=i;
bec2=1;
}
if(bec1==1 && bec2==1)
for(int j=x+1;j<y;j++){
if(a[j]!=' ')
a[j]-=32;
if(j==y){
bec1=0;
bec2=0;
}
}
}
cout<<a;
return 0;
}

Understanding the dp on tree

I was recently solving a problem from Codeforces. After giving it a lot of tries I was not able to get how in tree dp the matrix calculation works in the editorial solution. The following is the code where I have added comments to the parts I don't understand in it.
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int f[2][10010][110];//0 max 1 min
char s[10010];
int tr[10010][2],size,n,fa[10010],p,m,minn,pre;
void dfs(int x)
{
//cout<<x<<" "<<f[0][x][0]<<endl;
if (!tr[x][0]) return;
int l=tr[x][0],r=tr[x][1];
dfs(l),dfs(r);
/*The part which gets complicated need help why and how this calculation works*/
for (int i=0;i<=minn;i++)
for (int j=0;i+j<=minn;j++)
{
f[0][x][i+j+(p<m)]=max(f[0][x][i+j+(p<m)],f[0][l][i]+f[0][r][j]);
f[0][x][i+j+(p>=m)]=max(f[0][x][i+j+(p>=m)],f[0][l][i]-f[1][r][j]);
f[1][x][i+j+(p<m)]=min(f[1][x][i+j+(p<m)],f[1][l][i]+f[1][r][j]);
f[1][x][i+j+(p>=m)]=min(f[1][x][i+j+(p>=m)],f[1][l][i]-f[0][r][j]);
}
}
int main()
{
scanf("%s",s+1);
scanf("%d%d",&p,&m);
memset(f[0],-63,sizeof(f[0]));
memset(f[1],63,sizeof(f[1]));
/* Why we have used min of the two and how does it handle both condition */
minn=min(p,m);
n=strlen(s+1);
size=1;pre=size;
for (int i=1;i<=n;i++)
{
if (s[i]=='('||s[i]=='?')
{
tr[pre][tr[pre][0]?1:0]=++size;
fa[size]=pre;
pre=size;
}
else if (s[i]==')') pre=fa[pre];
else f[0][size][0]=f[1][size][0]=s[i]-'0',pre=fa[pre];
}
dfs(1);
printf("%d",f[0][1][minn]);
}
The part where I get lost is this
f[0][x][i+j+(p<m)]=max(f[0][x][i+j+(p<m)],f[0][l][i]+f[0][r][j]);
f[0][x][i+j+(p>=m)]=max(f[0][x][i+j+(p>=m)],f[0][l][i]-f[1][r][j]);
f[1][x][i+j+(p<m)]=min(f[1][x][i+j+(p<m)],f[1][l][i]+f[1][r][j]);
f[1][x][i+j+(p>=m)]=min(f[1][x][i+j+(p>=m)],f[1][l][i]-f[0][r][j]);
I always struggle with such types of problems. Can someone give the link to approach such problems.
Which part of the lines don't you understood? I take one line
f[0][x][i+j+(p<m)]=max(f[0][x][i+j+(p<m)],f[0][l][i]+f[0][r][j]);
and rewrite it
const int index_max = 0;
int y = i+j + (p<m? 1: 0); // in your code p<m is cast to int, true=1, false=0
int old_max = f[index_max][x][y];
int next_value = f[index_max][l][i] + f[index_max][r][j]:
f[index_max][x][y] = max(old_max, next_value);
You are looking for the maximum of the next_values of your double-loop. As l, r are fixed the next_values are sums of values in two rows.
Similar for the other 3 lines.

Task similar to happy number

I am solving a c++ algorithmic problem and I don't know why I don't get a right answer. The problem consists in : there is given a row of n numbers. The you need to take each number and add the square of its digits till you get the sum 4 or 1. Then you need to print how many times you have added that number. His is an example: you have a row of 2 numbers: 89 and 68. You start with 89:
64 + 81 = 145;(1) 1 + 16 + 25 = 42;(2) 16 + 4 = 20;(3) 4 + 0 = 4;(4)
and we stop because it is 4. We need to print 4 because we added 4 times again and again till we got 4 or 1.The same with 68. My code is in c++ but it uses for files c.
#include<stdio.h>
#include<math.h>
FILE *f,*g;
int n,i,a[500],sqr,nr;
int main()
{
f = fopen("unupatru.in","r");
fscanf(f,"%d\n",&n);
for(i=1;i<=n;i++)fscanf(f,"%d",&a[i]);
fclose(f);
for(i=1;i<=n;i++)
{
nr=0;
sqr=0;
while(sqr!=4 || sqr !=1)
{
while(a[i])
{
sqr+=pow(a[i]%10,2);
a[i]/=10;
}
a[i]=sqr;
nr++;
}
g=fopen("unupatru.out", "w");
fprintf(g,"%d\n",nr);
fclose(g);
}
return 0;
}
Please help me
The problem with my code is that when I click on run and compile it doesn't stop running please help
Not sure is enough but...
You should put sqr = 0 also inside the external while and (as pointed by Igor Tandetnik) modify the test using && instead ||
Or better: you shuold use do/while (with sqr=0 only inside)
do
{
sqr=0;
while(a[i])
{
sqr+=pow(a[i]%10,2);
a[i]/=10;
}
a[i]=sqr;
nr++;
}
while(sqr!=4 && sqr !=1)
#include<iostream>
#include<cmath>
#include<fstream>
using namespace std;
FILE *f,*g;
int n,i,a[500],sqr,nr,b[500];
int main()
{
f = fopen("unupatru.in","r");
fscanf(f,"%d\n",&n);
for(i=1;i<=n;i++)fscanf(f,"%d",&a[i]);
fclose(f);
for(i=1;i<=n;i++)
{
nr=0;
sqr=0;
while(sqr!=4 && sqr !=1)
{
sqr=0;
while(a[i])
{
sqr+=pow(a[i]%10,2);
a[i]/=10;
}
a[i]=sqr;
nr++;
b[i]=nr;
}
}
g=fopen("unupatru.out", "w");
for(i=1;i<=n;i++)
fprintf(g,"%d ",b[i]);
fclose(g);
return 0;
}

getting TLE from mouse maze in c++

I keep getting TLE in this question.I have tried to use scanf printf instead of cin cout but it didnt work.Then,I tried another question which has same
description,the only different is input N which means test case change from 1000 to 10^6.However, I got all AC there.I just cant figure out why.
following is the question
Description
Write a program that simulates a mouse in a maze. The program must count the steps taken by the mouse from the starting point to the final point.
The maze type is shown in following figure:
S$###
$$#$$
$$$##
##$$F
it consists of S (starting point), #(walls), $(road) and F (final point).
In above case, it needs 7 steps from S to F as following figure,
S$###
$$#$$
$$$##
##$$F
and the mouse can move in the four directions: up, down, left, right. There may be more than one way to reach final point, the program only need to print the least steps.
If there is no way from S to F, then print -1.
Input
The first line has an integer N(1<=N<=1000), which means the number of test cases.
For each case, the first line has two integers. The first and second integers R and C (3<=R, C<=500) represent the numbers of rows and columns of the maze, respectively. The total number of elements in the maze is thus R x C.
The following R lines, each containing C characters, specify the elements of the maze.
Output
Print out the least steps for each case, and there is a new line character at the end of each line.
Sample Input
3
4 5
S$###
$$#$$
$$$##
##$$F
4 5
S$$##
#$$$#
#$#$#
#$$$F
4 5
##S$#
$##$$
$$$##
#F###
Sample Output
7
7
-1
following is my code
#include <iostream>
#include <climits>
using namespace std;
int least_step;
void maze(char a[501][501],int,int,int,int,int);
#define wall '#'
int main(){
int ncase;
cin>>ncase;
char a[501][501];
while(ncase--){
int num1,num2;
cin>>num1>>num2;
least_step = INT_MAX;
int si,sj;
for(int i=0;i<num1;i++){
for(int j=0;j<num2;j++){
cin >> a[i][j];
if(a[i][j]=='S'){
si = i;
sj = j;
}
}
}
maze(a,si,sj,num1,num2,0);
if(least_step==INT_MAX){
least_step = -1;
}
cout<<least_step<<endl;
}
return 0;
}
void maze(char a[501][501],int i,int j,int num1,int num2,int step){
if(a[i][j] == 'F'){
if(step<least_step){
least_step = step;
}
return;
}
a[i][j] = wall;
if(i+1<num1&&a[i+1][j] != wall){
maze(a,i+1,j,num1,num2,step+1);
}
if(j+1<num2&&a[i][j+1] != wall){
maze(a,i,j+1,num1,num2,step+1);
}
if(i-1>=0&&a[i-1][j] != wall){
maze(a,i-1,j,num1,num2,step+1);
}
if(j-1>=0&&a[i][j-1] != wall){
maze(a,i,j-1,num1,num2,step+1);
}
a[i][j] = '$';
return;
}

Error when running code

I have tried the following code but I get an error when running it. I have used Debugger but I can't understand the errors in the call stack.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int a[10][2],i,j, b[10],max, min;
ifstream f("numere.txt");
for(i=1;i<=10;i++)
{
for(j=1;j<=2;j++)
{
f>>a[i][j];
b[i]=0;
}
}
for(i=1;i<=10;i++)
{
for(j=1;j<=2;j++)
{
b[i]=b[i]+a[i][j];
}
}
max=b[1];
min=b[1];
for(i=1;i<=5;i++)
{
if(max<=b[i]) max=b[i];
if(min>=b[i]) min=b[i];
}
cout<<"Cea mai mare suma este:"<< max<<endl;
cout<<"Cea mai mica suma este:"<< min<<endl;
f.close();
return 0;
}
Please, help me. I am a beginner and I have never worked with files before.
You have at least one error: array index out of bounds:
for(i= 0;i<10;i++)
{ //^^^
for(j=0;j< 2;j++)
{ //^^^
f>>a[i][j];
b[i]=0; //Why you put b[i] here??
}
}
Since you declare a[10][2] and array indices start from 0, not 1 in C++. You will access memory that does not belong to a (and b).
The first index in C++ array is 0, not 1.
Try to go from 0 to 9 in your loops, instead of from 1 to 10.
You can get more information on C++ arrays here.
The first error that stands out here is you are starting your array accesses at 1 in C++ arrays indexes start at 0, this also means that you are accessing out of bounds in your for loops as well, for example this:
for(i=1;i<=10;i++)
^^^ ^^
should be:
for(i=0;i<10;i++)
accessing memory outside of what is allocated is undefined behavior and can result in anything even code that appears to work.