Question Link : https://leetcode.com/problems/roman-to-integer/
My code is running fine on my computer but during my attempts to upload it to leet code it shows the following:
Line 20: Char 18: runtime error: index 10 out of bounds for type 'int [10]' [solution.c]
int romanToInt(char * s){
char a[7]={'M','D','C','L','X','V','I'};
int b[7]={1000,500,100,50,10,5,1};
int x;
x=strlen(s);
int c[x];
for(int i=0;i<x;++i)
{
for(int j=0;j<7;++j)
{
if(s[i]==a[j])
{
c[i]=b[j];
break;
}
}
}
int sum=0;
for(int i=0;i<x;++i)
{
if(c[i]<c[i+1]&&i!=x-1)
{
sum-=c[i];
}
else
{
sum+=c[i];
}
}
//printf("%d",sum);
return sum;
}
Running the same code on my computer using it as a custom function works just fine
#include <stdio.h>
#include<string.h>
int romanToInt(char * s);
void main()
{
char a[100];
char* p;
gets(a);
p=a;
int sum= romanToInt(p);
printf("%d",sum);
}
int romanToInt(char * s){
char a[7]={'M','D','C','L','X','V','I'};
int b[7]={1000,500,100,50,10,5,1};
int x;
x=strlen(s);
int c[x];
for(int i=0;i<x;++i)
{
for(int j=0;j<7;++j)
{
if(s[i]==a[j])
{
c[i]=b[j];
break;
}
}
}
int sum=0;
for(int i=0;i<x;++i)
{
if(c[i]<c[i+1]&&i!=x-1)
{
sum-=c[i];
}
else
{
sum+=c[i];
}
}
//printf("%d",sum);
return sum;
}
Why isn't leet code accepting this result?
This is a plane game function I wrote. I use a two-dimensional array to represent the game variables but The running result is abnormal, and * will jump suddenly.
And there will be two * , at the same time, and the plane also will stop
There should be no two * in the process of traversing the two-dimensional array. I tried to modify the position of * but I still couldn't.It's OK to run part of the code alone, but when you use the key operation, the program makes an error, but I don't know what's wrong
#include<stdio.h>
#include <windows.h>
#include<stdlib.h>
#include<conio.h>
#define enemynum 3
int element [20][30];
int position_x,position_y;
int enemy_x[enemynum],enemy_y[enemynum];
int score;
void gotoxy(int x, int y)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle, pos);
}
void HideCursor()
{
CONSOLE_CURSOR_INFO cursor_info = {1,0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void startup()
{ element[20][30]={0};
position_x=10;position_y=15;
element[position_x][position_y]=1;
for(int k=0;k<enemynum;k++)
{
enemy_x[k]=rand()%3;enemy_y[k]=rand()%20;
element[enemy_x[k]][enemy_y[k]]=3;
}
HideCursor();
}
This is an encapsulated pointer callback function. I don't think we need to consider the above functions
void show()
{ int i,j;
gotoxy(0,0);
for(i=0;i<20;i++)
{
for(j=0;j<30;j++)
{ if(element[i][j]==1)
{
printf("*");
}else if(element[i][j]==3)
{
printf("#");
}
else
printf(" ");
}
printf("\n");
}
}
void updatewhithout()
{
int i,j;
for(i=0;i<20;i++)
{
for(j=0;j<30;j++)
{
if(element[i][j]==2)
{
element[i][j]=0;
if(i>0)
element[i-1][j]=2;
}
}
} static int flag;
if(flag<20)
flag++;
if(flag==20)
{ for(int k=0;k<enemynum;k++)
{
if(enemy_x[k]==20)
{
enemy_x[k]=rand()%3;
enemy_y[k]=rand()%20;
}
element[enemy_x[k]][enemy_y[k]]=0;
enemy_x[k]++;
element[enemy_x[k]][enemy_y[k]]=3;
flag=0;
}
}
}
void updatewhith()
{ char ch;
if( kbhit())
ch=getch();
if(ch=='a')
{ element[position_x][position_y]=0;
position_y--;
element[position_x][position_y]=1;
}
if(ch=='d')
{ element[position_x][position_y]=0;
position_y++;
element[position_x][position_y]=1;
}
}
int main()
{startup();
while(1)
{show();
updatewhithout();
updatewhith();
}
}
void particle_initialize()
{
int i;
for (i = 0; i<particlenumber; i++)
{
Pu[i]=0;
}
return; }
void forceon_particle()
{
for (i = 0; i<particlenumber; i++)
{
FDx[i]=(U[i]-Pu[i])/taup;
FDy[i]=(V[i]-Pv[i])/taup;
}
return; }
void particle_motion()
{
int i;
for (i = 0; i<particlenumber; i++)
{
Pu[i]=Pu[i]+FDx[i]*tp;
Px[i]=Px[i]+Pu[i]*tp;}
return;
}
Hi Everyone, I'm unable to understand why I'm getting segmentation error in the function particle_motion.
The error comes only when I try assigning Px[i]=Px[i]+Pu[i]*tp; Py[i]=Py[i]+Pv[i]*tp;. Kindly help.
/*Developing flow in a Two Dimensional channel*/
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#define NX 101
#define NY 101
#define Q 9
#define T 300001
#define u_in 0.01
#define omega 1
#define kvisc 1/6
#define W0 4.0/9.0
#define W1 1.0/9.0
#define W2 1.0/36.0
#define rho0 1.0
#define particlenumber 10000
#define dp 0.01
#define rhop 2
#define tp 1/omega
#define pi 3.14
int cx [Q]={0,1,0,-1,0,1,-1,-1,1};
int cy [Q]={0,0,1,0,-1,1,1,-1,-1};
double t [Q]={W0,W1,W1,W1,W1,W2,W2,W2,W2};
int next_x [NX][Q];
int next_y [NY][Q];
double f1 [NX][NY][Q];
double f2 [NX][NY][Q];
double ux [NX][NY];
double uy [NX][NY];
double uxeq [NX][NY];//eq velocity for effect of particle
double uyeq [NX][NY];
double ustar [NX][NY];//forcing term added to ueq
double vstar [NX][NY];
double rho [NX][NY];
double Px [particlenumber];//location of particles
double Py [particlenumber];
double Pu [particlenumber];//velocity of particles
double Pv [particlenumber];
double FDx [particlenumber];// drag force
double FDy [particlenumber];
double U [particlenumber];//velocity of particle
double V [particlenumber];
double FPx [particlenumber];//force on fluid due to particle
double FPy [particlenumber];
double uforcing [particlenumber];
double rhoatpar [particlenumber];
double vforcing [particlenumber];
void tables();
void initialise();
void cal_obs();
void streaming();
void collision();
void density_data();
void particle_initialize();
void forceon_particle();
void intrapolation_velocity();
void particle_motion();
void forceon_fluid();
void intrapolation_density();
void reverse_intrapolation();
int main()
{
int count=0, k;
tables();
initialise();
particle_initialize();
for(k=0;k<T;k++)
{
//printf("k=%d\t",k);
cal_obs();
collision();
streaming();
if (k % 10000 ==0)
{
density_data(count, k);
}
intrapolation_velocity();
forceon_particle();
intrapolation_density();
forceon_fluid();
reverse_intrapolation();
particle_motion();
}
return 0;
}
void tables()
{
int x,y,i;
for(x=0; x<NX; x++)
{
for (y=0; y<NY; y++)
{
for (i=0; i<Q; i++)
{
next_x[x][i] = x + cx[i];
if (next_x[x][i]>NX-1)
next_x[x][i] = 0;
if (next_x[x][i]<0)
next_x[x][i] = NX-1;
next_y[y][i] = y + cy[i];
if (next_y[y][i]>NY-1)
next_y[y][i] = 0;
if (next_y[y][i]<0)
next_y[y][i] = NY-1;
}
}
}
return;
}
void initialise()
{
int x,y,i;
for(x=0; x<NX; x++)
{
for (y=0; y<NY; y++)
{
ustar[x][y]=0;//initialize forcing term zero
vstar[x][y]=0;
for (i=0; i<Q; i++)
{
f1[x][y][i]=rho0*t[i];
}
}
}
return;
}
void cal_obs()
{
int x,y,i;
double dense;
for(x=0; x<NX; x++)
{
for (y=0; y<NY; y++)
{
dense=ux[x][y]=uy[x][y]=0.0;
for (i=0; i<Q; i++)
{
dense += f1[x][y][i];
ux[x][y] += f1[x][y][i]*cx[i];
uy[x][y] += f1[x][y][i]*cy[i];
}
if(dense != 0.0)
{
rho[x][y] = dense;
ux[x][y] = ux[x][y]/rho[x][y];
uy[x][y] = uy[x][y]/rho[x][y];
}
else
{
rho[x][y] = 0.0;
ux[x][y] = 0.0;
uy[x][y] = 0.0;
}
}
}
for(x=0; x<NX; x++)
{
ux[x][0]=u_in;
ux[x][NY-1]=-u_in;
}
return;
}
void collision()
{
int x,y,i;
double udotc,u2,feq;
for(x=0; x<NX; x++)
{
for (y=0; y<NY; y++)
{
for (i=0; i<Q; i++)
{
uxeq[x][y]=ustar[x][y]+ux[x][y];
uyeq[x][y]=vstar[x][y]+uy[x][y];
udotc = uxeq[x][y]*cx[i]+uyeq[x][y]*cy[i];
u2=uxeq[x][y]*uxeq[x][y] + uyeq[x][y]*uyeq[x][y];
feq = t[i]*rho[x][y]*(1+3*udotc+4.5*udotc*udotc-1.5*u2);
f1[x][y][i]=f1[x][y][i]*(1-omega)+feq*omega;
}
}
}
return;
}
void streaming()
{
int x,y,i,newx,newy;
double rho_w;
for(x=0; x<NX; x++)
{
for (y=0; y<NY; y++)
{
for (i=0; i<Q; i++)
{
newx=next_x [x][i];
newy=next_y[y][i];
f2[newx][newy][i] = f1[x][y][i];
}
}
}
for(x=0; x<NX; x++)
{
for (y=0; y<NY; y++)
{
for (i=0; i<Q; i++)
{
f1[x][y][i]=f2[x][y][i];
}
}
}
return;
}
void density_data(int data_counter, int id)
{
int x, y;
FILE *f3;
char phase [100];
/* Data for Tecplot*/
sprintf ( phase,"phaseVelocity2D_%d.csv",id);
f3 = fopen(phase, "w");
fprintf(f3," X,Y,Rho,U,V\n");
for (y =0; y < NY; y++)
{ for (x = 0; x < NX; x++)
fprintf(f3,"%d%c%d%c%lf%c%lf%c%lf\n",x,',',y,',',rho[x][y],',',ux[x][y],',',uy[x][y]);
}
fflush(f3);
fclose(f3);
int i;
FILE *f4;
char particle [100];
sprintf ( particle,"particle_%d.csv",id);
f4 = fopen(particle, "w");
fprintf(f4," Id,X,Y\n");
for (i =0; i < particlenumber; i++)
{
fprintf(f4,"%d%c%lf%c%lf\n",i,',',Px[i],',',Py[i]);
}
fflush(f4);
fclose(f4);
return;
}
void particle_initialize()
{
int i;
srand(time(0));
float randomx;
for (i = 0; i<particlenumber; i++)
{
randomx=(float)rand()/RAND_MAX;
Px[i]=randomx*NX;
Pu[i]=0;
}
float randomy;
for (i = 0; i<particlenumber; i++)
{
randomy=(float)rand()/RAND_MAX;
Py[i]=randomy*NY;
Pv[i]=0;
}
return;
}
void forceon_particle()
{
int i;
double taup;
taup=(rhop*dp*dp)/(18*kvisc*rho0);
for (i = 0; i<particlenumber; i++)
{
FDx[i]=(U[i]-Pu[i])/taup;
FDy[i]=(V[i]-Pv[i])/taup;
}
return;
}
void intrapolation_velocity()
{
int i;
int node1x, node1y, node2x, node2y,node3x, node3y,node4x, node4y;
double w1,w2,w3,w4;
for (i = 0; i<particlenumber; i++)
{
node1x=trunc(Px[i]);
node1y=trunc(Py[i]);
node2x=node1x;
node2y=node1y+1;
node3x=node1x+1;
node3y=node1y+1;
node4x=node1x+1;
node4y=node1y;
w1=(Px[i]-node1x)*(Px[i]-node1x)+(Py[i]-node1y)*(Py[i]-node1y);
w1=sqrt(w1);
w2=(Px[i]-node2x)*(Px[i]-node2x)+(Py[i]-node2y)*(Py[i]-node2y);
w2=sqrt(w2);
w3=(Px[i]-node3x)*(Px[i]-node3x)+(Py[i]-node3y)*(Py[i]-node3y);
w3=sqrt(w3);
w4=(Px[i]-node4x)*(Px[i]-node4x)+(Py[i]-node4y)*(Py[i]-node4y);
w4=sqrt(w4);
U[i]=w1*ux[node1x][node1y]+w2*ux[node2x][node2y]+w3*ux[node3x][node3y]+w4*ux[node4x][node4y];
U[i]=U[i]/(w1+w2+w3+w4);
V[i]=w1*uy[node1x][node1y]+w2*uy[node2x][node2y]+w3*uy[node3x][node3y]+w4*uy[node4x][node4y];
V[i]=V[i]/(w1+w2+w3+w4);
}
return;
}
void particle_motion()
{
int i;
double A,B;
for (i = 0; i<particlenumber; i++)
{
Pu[i]=Pu[i]+FDx[i]*tp;//Pu[i] added so that it adds previous velocity
Pv[i]=Pv[i]+FDy[i]*tp;
}
for (i = 0; i<particlenumber; i++)
{
Px[i]=Px[i]+Pu[i]*tp;
Py[i]=Py[i]+Pv[i]*tp;
//if particle goes out of bound we r introducing periodic BC
if(Px[i]<0||Px[i]>(NX-1))
{
if(Px[i]<0)
{Px[i]=Px[i]+NX-1;}
else if(Px[i]>(NX-1))
{Px[i]=Px[i]-NX+1;}
}
if(Py[i]<0||Py[i]>(NY-1))
{
if(Py[i]<0)
{Py[i]=Py[i]+NY-1;}
else if(Py[i]>(NY-1))
{Py[i]=Py[i]-NY+1;}
}
}
return;
}
void forceon_fluid()
{
int i;
double taup;
long double mp;
mp=(1/6)*(pi)*rhop*dp*dp*dp;
taup=(rhop*dp*dp)/(18*kvisc*rho0);
for (i = 0; i<particlenumber; i++)
{
FPx[i]=mp*(U[i]-Pu[i]);
FPx[i]=FPx[i]/taup;
FPy[i]=mp*(V[i]-Pv[i]);
FPy[i]=FPy[i]/taup;
uforcing[i]=(FPx[i])/(rhoatpar[i]*omega);
vforcing[i]=(FPy[i])/(rhoatpar[i]*omega);
}
return;
}
void intrapolation_density()
{
int i;
int node1x, node1y, node2x, node2y,node3x, node3y,node4x, node4y;
double w1,w2,w3,w4;
for (i = 0; i<particlenumber; i++)
{
node1x=trunc(Px[i]);
node1y=trunc(Py[i]);
node2x=node1x;
node2y=node1y+1;
node3x=node1x+1;
node3y=node1y+1;
node4x=node1x+1;
node4y=node1y;
w1=(Px[i]-node1x)*(Px[i]-node1x)+(Py[i]-node1y)*(Py[i]-node1y);
w1=sqrt(w1);
w2=(Px[i]-node2x)*(Px[i]-node2x)+(Py[i]-node2y)*(Py[i]-node2y);
w2=sqrt(w2);
w3=(Px[i]-node3x)*(Px[i]-node3x)+(Py[i]-node3y)*(Py[i]-node3y);
w3=sqrt(w3);
w4=(Px[i]-node4x)*(Px[i]-node4x)+(Py[i]-node4y)*(Py[i]-node4y);
w4=sqrt(w4);
rhoatpar[i]=w1*rho[node1x][node1y]+w2*rho[node2x][node2y]+w3*rho[node3x][node3y]+w4*rho[node4x][node4y];
rhoatpar[i]=rhoatpar[i]/(w1+w2+w3+w4);
}
return;
}
void reverse_intrapolation()
{
int i;
int node1x, node1y, node2x, node2y,node3x, node3y,node4x, node4y;
double w1,w2,w3,w4;
int x,y;
for(x=0; x<NX; x++)
{
for (y=0; y<NY; y++)
{
ustar[x][y]=0;//initialize forcing term zero
vstar[x][y]=0;
}
}
for (i = 0; i<particlenumber; i++)
{
node1x=trunc(Px[i]);
node1y=trunc(Py[i]);
node2x=node1x;
node2y=node1y+1;
node3x=node1x+1;
node3y=node1y+1;
node4x=node1x+1;
node4y=node1y;
w1=(Px[i]-node1x)*(Px[i]-node1x)+(Py[i]-node1y)*(Py[i]-node1y);
w1=sqrt(w1);
w2=(Px[i]-node2x)*(Px[i]-node2x)+(Py[i]-node2y)*(Py[i]-node2y);
w2=sqrt(w2);
w3=(Px[i]-node3x)*(Px[i]-node3x)+(Py[i]-node3y)*(Py[i]-node3y);
w3=sqrt(w3);
w4=(Px[i]-node4x)*(Px[i]-node4x)+(Py[i]-node4y)*(Py[i]-node4y);
w4=sqrt(w4);
ustar[node1x][node1y]=ustar[node1x][node1y]+(w1*uforcing[i])/(w1+w2+w3+w4);
vstar[node1x][node1y]=vstar[node1x][node1y]+(w1*vforcing[i])/(w1+w2+w3+w4);
ustar[node2x][node2y]=ustar[node2x][node2y]+(w1*uforcing[i])/(w1+w2+w3+w4);
vstar[node2x][node2y]=vstar[node2x][node2y]+(w1*vforcing[i])/(w1+w2+w3+w4);
ustar[node3x][node3y]=ustar[node3x][node3y]+(w1*uforcing[i])/(w1+w2+w3+w4);
vstar[node3x][node3y]=vstar[node3x][node3y]+(w1*vforcing[i])/(w1+w2+w3+w4);
ustar[node4x][node4y]=ustar[node4x][node4y]+(w1*uforcing[i])/(w1+w2+w3+w4);
vstar[node4x][node4y]=vstar[node4x][node4y]+(w1*vforcing[i])/(w1+w2+w3+w4);
}
return;
}
I have added the whole code as the error may be due to other functions which might also access the arrays.
The issue is at this statement.
V[i] = w1* uy[node1x][node1y] + w2* uy[node2x][node2y] + w3 * uy[node3x][node3y] + w4*uy[node4x][node4y];
Here,the arrays are uy[101][101], ux[101][101]. Look at the values of node1x and node1y:
node1x=trunc(Px[i]);
node1y=trunc(Py[i]);
See the values of Px[i] and Py[i] :
Px[i]=randomx*NX; // NX=101. randomx ranges from 0 to 1. randomx=(float)rand()/RAND_MAX
Py[i]=randomy*NY; // NY=101. randomy ranges from 0 to 1. randomy=(float)rand()/RAND_MAX
I think at some point of time randomx = 1, then Px[i] = 101, then node1x = 101 then you are accessing uy[101][node1y]. This causes segmentation fault because the array is uy[101][101] , and you cannot access uy[101][node1y]element.
I'm using the fftw lib to compute a 2d r2c fit, but this function gives just n* (n/2+1) elements and I need all the values is there a way to obtain all the n*n elements? Thanks a lot. This is my code :
#include <stdio.h>
#include<Windows.h>
#include <fftw3.h>
#include<time.h>
#include<math.h>
void fourierForward(float *in,fftwf_complex *Out,int N)
{
int i,err;
fftwf_plan plan,planInv;
/*on calcule le plan d'exécution*/
plan=fftwf_plan_dft_r2c_2d ( N, N, in, Out, FFTW_ESTIMATE );
//planInv=fftwf_plan_dft_c2r_2d ( N, N, Out,out, FFTW_ESTIMATE );
fftwf_execute(plan);
//fftwf_execute(planInv);
fftwf_destroy_plan(plan);
//fftwf_destroy_plan(planInv);
//fftwf_free(Out);
}
int main()
{
const int n=500;
int i,j,k,l;
fftwf_complex *Out= (fftwf_complex*) fftwf_malloc((n/2+1)*n*sizeof(fftwf_complex));
FILE *f;
float fft[500][500];
float *in=(float*) fftwf_malloc(sizeof(float)*n*n);
FILE *pf=fopen("im.txt","r");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
fscanf(pf,"%f ",&in[i*n+j]);
}
}
for (i = 0; i < n*(n/2+1); i++)
{
Out[i][0]=0.0f;
Out[i][1]=0.0f;
}
fclose(pf);
fourierForward(in,Out,n);
f=fopen("RealPart.txt","w");
for(i=0;i<n;i++)
{
for(j=0;j<n/2+1;j++)
{
fft[i][j]=Out[i*(n/2+1)+j][0];
}
}
for(i=1;i<=(n-1)/2;i++)
{
fft[0][n-i]=fft[0][i];
}
for(i=1;i<n/2+1;i++)
{
for(j=1;j<n;j++)
{
fft[n-j][n-i]=fft[j][i];
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
fprintf(f,"%f ",fft[i][j]);
}
fprintf(f,"\n");
}
system("pause");
fftwf_free(Out);
fclose(f);
return 0;
}
Best regards.
I'm trying to write sliding puzzle where I'm using 3x3 matrix as board. In main function user inputs the number of tile which swaps positions with 0. Everything works except when I enter number that is located in a position above zero it does not swap positions with it. How do I correct that?
Here's the code:
#include <stdio.h>
#include <stdlib.h>
#define empty_space 0
int m,n,i,j,z,y;
void print_matrix(int matrix[3][3])
{
for ( m=0; m<3; m++){
for (n=0; n<3; n++)
{
printf("%d\t",matrix[m][n]);
} printf("\n");
}
printf("\n");
}
void swap(int *i, int *j) {
int t = *i;
*i = *j;
*j = t;
}
void slide(int a[3][3] , int t)
{
for ( i=0; i<3; i++){
for ( j=0; j<3; j++)
{
if (a[i][j]==t)
{
if (a[i+1][j]==empty_space && i+1<=2)
{
swap(&a[i+1][j],&a[i][j]);break;
}
if (a[i-1][j]==empty_space && i-1>=0)
{
swap(&a[i][j],&a[i-1][j]);break;
}
if (a[i][j+1]==empty_space && j+1<=2)
{
swap(&a[i][j],&a[i][j+1]);break;
}
if (a[i][j-1]==empty_space && j-1>=0)
{
swap(&a[i][j],&a[i][j-1]);break;
}
}
}
}
}
int goal_test (int a[3][3],int b[3][3])
{
int flag=0;
for ( z=0; z<3; z++){
for ( y=0; y<3; y++){
if(a[z][y]==b[z][y])
flag++;
}
}
if (flag==9)
return 1;
else return 0;
}
int main()
{
static int mat[3][3]={{1,2,3},{6,0,4},{7,5,8}};
int goal[3][3]={{1,0,3},{6,5,4},{7,8,2}};
print_matrix(mat);
int x;
while(goal_test(mat,goal)==0)
{
printf("enter tile to slide:\t");
scanf("%d",&x);
slide(mat, x);
print_matrix(mat);
}
return 0;
}
Here's what happens:
Any number can swap its position as long as it's not located above zero