Error during submitting code on Leet code. (Roman to Integer) - c

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?

Related

Tried Knap snap problem but its not working

#include<stdio.h>
int max(int a,int b)
{
if(a>b)
return a;
return b;
}
void knacksnap(int n,int a[][n+1],int* val,int* weight,int maxweight)
{
if(n==0 || maxweight==0)
{
a[maxweight][n]=0;
return;
}
if(a[maxweight][n]!=-1)
return;
if(weight[n-1]>maxweight)
{
knacksnap(n-1,a,val,weight,maxweight);
a[maxweight][n]=a[maxweight][n-1];
}
else
{
knacksnap(n-1,a,val,weight,maxweight-weight[n-1]);
knacksnap(n-1,a,val,weight,maxweight);
a[maxweight][n]=max(val[n-1]+a[maxweight-weight[n-1]][n-1],a[maxweight][n-1]);
}
}
int main()
{
int n;
scanf("%d",&n);
int val[n],weight[n];
int i;
for(i=0;i<n;i++)
scanf("%d",&val[i]);
for(i=0;i<n;i++)
scanf("%d",&weight[i]);
int maxweight;
scanf("%d",&maxweight);
int a[maxweight+1][n+1];
int j;
for(i=0;i<maxweight+1;i++)
{
for(j=0;j<n+1;j++)
{
a[i][j]=-1;
}
}
knacksnap(n,a,val,weight,maxweight);
printf("\n");
printf("%d",a[maxweight][n]);
}
It not taking updated values by the knacksnap function. for example the inner knacksnap funtions are not generating correct values. eventhough their values are updated its not taking them. can someone please help

Why does the program shows memory limit exceeded in test 6 and runtime error in test 8,9,10 when i use local array variable?

Its a easy sorting problem.The problem link is https://www.hackerearth.com/practice/algorithms/sorting/quick-sort/practice-problems/algorithm/kings-race-8/
When i use global array variable the program get accepted.But when i use local array variable memory limit exceeded in case 6 and runtime error in cases 8,9,10.Why this is happened?
My code with local array variable:
#include<stdio.h>
void Quick_Sort(int a[][2],int Start,int End)
{
if(Start<End)
{
int Piv_pos=Partition(a,Start,End);
Quick_Sort(a,Start,Piv_pos-1);
Quick_Sort(a,Piv_pos+1,End);
}
}
int Partition(int a[][2],int Start,int End)
{
int i=Start+1,j,temp;
int Pivot=a[Start][0];
for(j=Start+1;j<=End;j++)
{
if(a[j][0]<Pivot)
{
temp=a[j][0];
a[j][0]=a[i][0];
a[i][0]=temp;
temp=a[j][1];
a[j][1]=a[i][1];
a[i][1]=temp;
i++;
}
}
temp=a[Start][0];
a[Start][0]=a[i-1][0];
a[i-1][0]=temp;
temp=a[Start][1];
a[Start][1]=a[i-1][1];
a[i-1][1]=temp;
return i-1;
}
int min(int a,int b)
{
if(a<b)
return a;
else
return b;
}
int main()
{
int T,i,j,N,K,prince_ind;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&N,&K);
int a[N][2],h[K];
for(i=0;i<N;i++)
{
scanf("%d",&a[i][0]);
a[i][1]=i;
}
Quick_Sort(a,0,N-1);
/* for(i=0;i<N;i++)
{
printf("%d ",a[i][0]);
}*/
for(i=0;i<K;i++)
{
scanf("%d",&h[i]);
}
for(i=0,j=0;i<K&&j<N;i++)
{
prince_ind=a[j][1];
while(a[j][0]<h[i]&&j<N)
{
prince_ind=min(prince_ind,a[j][1]);
j++;
}
}
while(j<N)
{
prince_ind=min(prince_ind,a[j][1]);
j++;
}
printf("%d\n",prince_ind);
}
return 0;
}
My code with global array variable:
#include<stdio.h>
int a[1000000][2],h[1000000];
void Quick_Sort(int Start,int End)
{
if(Start<End)
{
int Piv_pos=Partition(Start,End);
Quick_Sort(Start,Piv_pos-1);
Quick_Sort(Piv_pos+1,End);
}
}
int Partition(int Start,int End)
{
int i=Start+1,j,temp;
int Pivot=a[Start][0];
for(j=Start+1;j<=End;j++)
{
if(a[j][0]<Pivot)
{
temp=a[j][0];
a[j][0]=a[i][0];
a[i][0]=temp;
temp=a[j][1];
a[j][1]=a[i][1];
a[i][1]=temp;
i++;
}
}
temp=a[Start][0];
a[Start][0]=a[i-1][0];
a[i-1][0]=temp;
temp=a[Start][1];
a[Start][1]=a[i-1][1];
a[i-1][1]=temp;
return i-1;
}
int min(int a,int b)
{
if(a<b)
return a;
else
return b;
}
int main()
{
int T,i,j,N,K,prince_ind;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&N,&K);
//int a[N][2],h[K];
for(i=0;i<N;i++)
{
scanf("%d",&a[i][0]);
a[i][1]=i;
}
Quick_Sort(0,N-1);
/* for(i=0;i<N;i++)
{
printf("%d ",a[i][0]);
}*/
for(i=0;i<K;i++)
{
scanf("%d",&h[i]);
}
for(i=0,j=0;i<K&&j<N;i++)
{
prince_ind=a[j][1];
while(a[j][0]<h[i]&&j<N)
{
prince_ind=min(prince_ind,a[j][1]);
j++;
}
}
while(j<N)
{
prince_ind=min(prince_ind,a[j][1]);
j++;
}
printf("%d\n",prince_ind);
}
return 0;
}

Why does my function go into infinite loop sometimes?

I have the following code. It is running fine. But sometimes the del and ins functions are going into infinite loop but sometimes working fine. The readt function is working fine, still I have included it for your reference. What is the problem with my del and ins? Is there any memory leak?
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
#include<math.h>
#include<unistd.h>
struct node
{ int info;
struct node *link;
};
typedef struct node m;
m *search(int,m*);
m *del(int,m*);
m *ins(int,int,m*);
int posof(int,m*);
int readt(m *t_c,char path[])
{ int t,szt=0;
FILE *tfile;
char ch;
char filename[]="/temp.txt";
strcat(path,filename);
tfile=fopen(path,"r");
if(tfile==NULL)
exit(0);
fseek(tfile, 0, SEEK_END);
szt = ftell(tfile);
fseek(tfile,0,SEEK_SET);
if(szt!=0)
{ while(1)
{ fscanf(tfile,"%d%c",&t,&ch);
t_c->info=t;
t_c->link=(m*)malloc(sizeof(m));
t_c=t_c->link;
if(ch==';')
break;
}
}
t_c->link=NULL;
//free(t_c);
fclose(tfile);
return 0;
}
m *search(int Noftarget,m *t_c)
{ int i,p1,p2;
srand(time(NULL));
for(i=0;i<100;i++)
{ p1=(1+rand()%(Noftarget));
p2=(1+rand()%(Noftarget));
t_c=del(p1,t_c);
t_c=ins(p1,p2,t_c);
break;
}
return t_c;
}
m *del(int target,m *t_h)
{ m *t_c;
int j=1,i;
t_c=t_h;
i=posof(target,t_h);
if(i==1)
{ t_c=t_c->link;
t_h=t_c;
}
else
{ while(j<i-1)
{ t_c=t_c->link;
j++;
}
t_c->link=t_c->link->link;
}
return t_h;
}
m *ins(int target,int position,m *t_h)
{ int j=0;
m *swaptarget,*t_c;
t_c=t_h;
swaptarget=(m*)malloc(sizeof(m));
swaptarget->info=target;
if(position==1)
{ swaptarget->link=t_c;
t_h=swaptarget;
}
else
{ while(j<position-2)
{ t_c=t_c->link;
j++;
}
swaptarget->link=t_c->link;
t_c->link=swaptarget;
}
free(swaptarget);
return t_h;
}
int posof(int p1,m *t_c)
{ int i=1,a=0;
while(t_c->link!=NULL)
{ if(p1==t_c->info)
{ a=i;
break;
}
t_c=t_c->link;
i++;
}
return a;
}
int main()
{ int Noftarget=8,j,r=1,count=0,noi,szd_n=0,i=0,sz;
char cwd[200];
m *t_h;
getcwd(cwd, sizeof(cwd));
t_h=(m*)malloc(sizeof(m));
readt(t_h,cwd);
t_h=search(Noftarget,t_h);
free(t_h);
return 0;
}
and the content of temp file is:
1,2,3,4,5,6,7,8;
The program contains memory leaks. Memory is allocating iteratively inside the while loop but only one pointer is removing at the end. Need to remove all allocations. And there is no need to free any pointer at the ins function rather del function needs the free operation of deleted pointer. The modified code is here:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
#include<math.h>
#include<unistd.h>
struct node
{ int info;
struct node *link;
};
typedef struct node m;
m *search(int,m*);
m *del(int,m*);
m *ins(int,int,m*);
int posof(int,m*);
int readt(m *t_c,char path[])
{ int t,szt=0;
FILE *tfile;
char ch;
char filename[]="/temp.txt";
strcat(path,filename);
tfile=fopen(path,"r");
if(tfile==NULL)
exit(0);
fseek(tfile, 0, SEEK_END);
szt = ftell(tfile);
fseek(tfile,0,SEEK_SET);
if(szt!=0)
{ while(1)
{ fscanf(tfile,"%d%c",&t,&ch);
t_c->info=t;
t_c->link=(m*)malloc(sizeof(m));
//printf("%d ",t_c->info);
t_c=t_c->link;
if(ch==';')
break;
}
}
t_c->link=NULL;
//free(t_c);
fclose(tfile);
return 0;
}
m *search(int Noftarget,m *t_c)
{ int i,p1,p2;
srand(time(NULL));
for(i=0;i<100;i++)
{ p1=(1+rand()%(Noftarget));
p2=(1+rand()%(Noftarget));
t_c=del(p1,t_c);
t_c=ins(p1,p2,t_c);
break;
}
return t_c;
}
m *del(int target,m *t_h)
{ m *t_c;
int j=1,i;
t_c=t_h;
i=posof(target,t_h);
if(i==1)
{ free(t_c);
t_c=t_c->link;
t_h=t_c;
}
else
{ while(j<i-1)
{ t_c=t_c->link;
j++;
}
free(t_c->link);
t_c->link=t_c->link->link;
}
return t_h;
}
m *ins(int target,int position,m *t_h)
{ int j=0;
m *swaptarget,*t_c;
t_c=t_h;
swaptarget=(m*)malloc(sizeof(m));
swaptarget->info=target;
if(position==1)
{ swaptarget->link=t_c;
t_h=swaptarget;
}
else
{ while(j<position-2)
{ t_c=t_c->link;
j++;
}
swaptarget->link=t_c->link;
t_c->link=swaptarget;
}
return t_h;
}
int posof(int p1,m *t_c)
{ int i=1,a=0;
while(t_c->link!=NULL)
{ if(p1==t_c->info)
{ a=i;
break;
}
t_c=t_c->link;
i++;
}
return a;
}
int main()
{ int Noftarget=7,j,r=1,count=0,noi,szd_n=0,i=0,sz;
char cwd[200];
m *t_h;
getcwd(cwd, sizeof(cwd));
t_h=(m*)malloc(sizeof(m));
readt(t_h,cwd);
print_tsp(t_h);
t_h=search(Noftarget,t_h);
print_tsp(t_h);
while(t_h!=NULL)
{ free(t_h);
t_h=t_h->link;
}
return 0;
}
It is checked by valgrind and does not have any memory leak.

Strassen multiplication - c program

We had to implement strassen's multiplication in a practical session , the code I wrote is given below , as you can see I have used a lot of intermediate matrices .I wanted to know how to return a 2d array from a function so that the code will look cleaner and more understandable , also it will give me some insights on pointers(a weak area for me )i.e say is use a double pointer as return type of sub function (int **sub(args list))
and since my strassen function has prototype strassen(int , int [],int[]..)
When one argument of strassen function is a result of sub function , I get an error saying int (*)[] expected but returning int **
To resolve this I typecasted the result of sub function with int (*)[] but it does not work as expected
Help please ? Thanks !
#include<stdio.h>
#include<stdlib.h>
void add(int n,int a[n][n],int b[n][n],int result[][n])
{
printf("---add---\n");
int i,j;
//int **result = (int **)malloc(n*sizeof(int *));
/*for(i=0;i<n;i++)
result[i] = (int *)malloc(n*sizeof(int));*/
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
result[i][j] = a[i][j] + b[i][j];
printf("%d\t",result[i][j]);
}
printf("\n");
}
//return result;
}
void sub(int n,int a[n][n],int b[n][n],int result[][n])
{
printf("---sub---\n");
int i,j;
/*int **result = (int **)malloc(n*sizeof(int *));
for(i=0;i<n;i++)
result[i] = (int *)malloc(n*sizeof(int));*/
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
result[i][j] = a[i][j] - b[i][j];
printf("%d\t",result[i][j]);
}
}
}
void divide(int n,int a[n][n],int c[n/2][n/2],int i,int j)
{
int i1,i2,j1,j2;
for(i1=0,i2=i;i1<n/2;i1++,i2++)
{
for(j1=0,j2=j;j1<n/2;j1++,j2++)
{
c[i1][j1] = a[i2][j2];
}
}
}
void join(int n,int a[][n],int c[][n/2],int i,int j)
{
printf("join\n");
int i1,i2,j1,j2;
for(i1=0,i2=i;i1<(n/2);i1++,i2++)
{
for(j1=0,j2=j;j1<(n/2);j1++,j2++)
{
a[i2][j2] = c[i1][j1];
printf("c[%d][%d] %d\n",i1,j1,c[i1][j1]);
}
}
}
void multiply(int n,int a[][n],int b[][n],int result[][n])
{
int i,j;
if(n==2)
{
//partial products
printf("base case\n");
int p1 = (a[0][0]+a[1][1])*(b[0][0]+b[1][1]);
int p2 = (a[1][0]+a[1][1])*b[0][0];
int p3 = a[0][0]*(b[0][1]-b[1][1]);
int p4 = a[1][1]*(b[1][0]-b[0][0]);
int p5 = (a[0][0]+a[0][1])*b[1][1];
int p6 = (a[1][0]-a[0][0])*(b[0][0]+b[0][1]);
int p7 = (a[0][1]-a[1][1])*(b[1][0]+b[1][1]);
int c11 = p1 + p4 - p5 + p7;
int c12 = p3 + p5;
int c21 = p2 + p4;
int c22 = p1 + p3 - p2 + p6;
result[0][0] = c11;
result[0][1] = c12;
result[1][0] = c21;
result[1][1] = c22;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%d\t",result[i][j]);
}
printf("\n");
}
}
else
{
int a11[n/2][n/2];
int a12[n/2][n/2];
int a21[n/2][n/2];
int a22[n/2][n/2];
int b11[n/2][n/2];
int b12[n/2][n/2];
int b21[n/2][n/2];
int b22[n/2][n/2];
//divide matrices A & B into four parts
divide(n,a,a11,0,0);
divide(n,a,a12,0,n/2);
divide(n,a,a21,n/2,0);
divide(n,a,a22,n/2,n/2);
divide(n,b,b11,0,0);
divide(n,b,b12,0,n/2);
divide(n,b,b21,n/2,0);
divide(n,b,b22,n/2,n/2);
//partial products
int p1[n/2][n/2],p2[n/2][n/2],p3[n/2][n/2],p4[n/2][n/2],p5[n/2][n/2],p6[n/2][n/2],p7[n/2][n/2];
int c11[n/2][n/2],c12[n/2][n/2],c21[n/2][n/2],c22[n/2][n/2];
int i1[n/2][n/2],i2[n/2][n/2];
add(n/2,a11,a22,i1);
add(n/2,b11,b22,i2);
multiply(n/2,i1,i2,p1);
int i3[n/2][n/2];
add(n/2,a21,a22,i3);
multiply(n/2,i3,b11,p2);
int i4[n/2][n/2];
sub(n/2,b12,b22,i4);
multiply(n/2,a11,i4,p3);
int i5[n/2][n/2];
sub(n/2,b21,b11,i5);
multiply(n/2,a22,i5,p4);
int i6[n/2][n/2];
add(n/2,a11,a12,i6);
multiply(n/2,i6,b22,p5);
int i7[n/2][n/2];
int i8[n/2][n/2];
sub(n/2,a21,a11,i7);
add(n/2,b11,b12,i8);
multiply(n/2,i7,i8,p6);
int i9[n/2][n/2];
int i10[n/2][n/2];
sub(n/2,a12,a22,i9);
add(n/2,b21,b22,i10);
multiply(n/2,i9,i10,p7);
//for c11
int r1[n/2][n/2];
int r2[n/2][n/2];
add(n/2,p1,p4,r1); //sub operation
sub(n/2,r1,p5,r2); //sub operation
add(n/2,r2,p7,c11); //main operation
//for c12
add(n/2,p3,p5,c12);
//for c21
add(n/2,p2,p4,c21);
//for c22
int r3[n/2][n/2];
int r4[n/2][n/2];
add(n/2,p1,p3,r3); //sub operation
sub(n/2,r3,p2,r4); //sub operation
add(n/2,r4,p6,c22); //main operation
join(n,result,c11,0,0);
join(n,result,c12,0,n/2);
join(n,result,c21,n/2,0);
join(n,result,c22,n/2,n/2);
printf("---c11---\n");
for(i=0;i<n/2;i++)
{
for(j=0;j<n/2;j++)
{
printf("%d\t",c11[i][j]);
}
printf("\n");
}
printf("---c12---\n");
for(i=0;i<n/2;i++)
{
for(j=0;j<n/2;j++)
{
printf("%d\t",c12[i][j]);
}
printf("\n");
}
printf("---c21---\n");
for(i=0;i<n/2;i++)
{
for(j=0;j<n/2;j++)
{
printf("%d\t",c21[i][j]);
}
printf("\n");
}
printf("---c22---\n");
for(i=0;i<n/2;i++)
{
for(j=0;j<n/2;j++)
{
printf("%d\t",c22[i][j]);
}
printf("\n");
}
/*for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",result[i][j]);
}
printf("\n");
}*/
}
}
int main()
{
int n;
printf("Enter the order of the matrices(power of 2)\n");
scanf("%d",&n);
int i,j;
int a[n][n],b[n][n];
printf("Enter first matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter second matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("First matrix is \n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("Second matrix is \n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
int r[n][n];
multiply(n,a,b,r);
printf("---RESULT OF MULTIPLICATION---\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",r[i][j]);
}
printf("\n");
}
return 0;
}

What is causing my blackjack program to crash before doing anything?

When I run my program, it will print "Welcome to Blackjack! Rules: " and then an error window will come up saying "Blackjack.exe has stopped working and I have to close the program. In the compiler window it says "Process terminated". How do I stop this from happening?
Is one of the loops not working correctly?
My code:
#include <stdio.h>
#include <stdlib.h>
void Initializedeck(int[]);
void Shuffledeck(int[]);
void displayscore(int, int);
int hdrawcards(int[], int[]);
char getsuit(int);
int cdrawcards(int[], int[]);
int main()
{
int deck[52];
char cont;
int hcherries=10;
int ccherries=10;
int hcards[10];
int ccards[10];
int htotal;
int ctotal;
printf("Welcome to Blackjack!\n");
printf("Rules: \n ");
Initializedeck(deck);
cont='y';
while(cont=='y')
{
Shuffledeck(deck);
system("cls");
displayscore(hcherries, ccherries);
htotal=hdrawcards(hcards, deck);
ctotal=cdrawcards(ccards, deck);
if(htotal>ctotal)
{
printf("You Win!");
}
else
{
printf("Computer Wins. :( ");
}
printf("Do you want to continue?(y/n)");
scanf("%c", &cont);
}
return 0;
}
void Initializedeck(int deck[])
{
int i=0;
while(i<52)
{
deck[i]=i;
i++;
}
}
void Shuffledeck(int deck[])
{
int hold;
int max=51;
int random;
while(max>=0)
{
random=rand()%(max)+1;
hold=deck[max];
deck[max]=random;
random=hold;
max--;
}
}
void displayscore(int hcherries, int ccherries)
{
printf("Human: %i(cherries) Computer: %i(cherries)", hcherries, ccherries);
}
int hdrawcards(int hcards[], int deck[])
{
char answer;
int i=0;
int score=0;
int total=0;
char rank;
int worth;
char suit;
int card;
printf("Do you want to draw a card? (y/n)");
scanf("%c", &answer);
while(answer=='y' && total<21)
{
score=score+deck[i];
hcards[i]=deck[i];
card=hcards[i];
worth=hcards[i]%13;
if(worth==10)
{
rank='T';
}
else if (worth==11)
{
rank='J';
}
else if (worth==12)
{
rank='Q';
}
else if (worth==13)
{
rank='K';
}
else if(worth==1)
{
rank='A';
}
else
{
rank=worth;
}
if(worth>10)
{
worth=10;
}
total=total+worth;
suit=getsuit(card);
printf("%c%c\n", rank, suit);
printf("Total: %i", total);
i++;
scanf("%c", &answer);
}
return total;
}
int cdrawcards(int ccards[], int deck[])
{
int i=26;
int total;
int score;
int worth;
char rank;
char suit;
int card;
while(total<18)
{
score=score+deck[i];
ccards[i]=deck[i];
card=ccards[i];
worth=ccards[i]%13;
if(worth==10)
{
rank='T';
}
else if (worth==11)
{
rank='J';
}
else if (worth==12)
{
rank='Q';
}
else if (worth==13)
{
rank='K';
}
else if(worth==1)
{
rank='A';
}
else
{
rank=worth;
}
if(worth>10)
{
worth=10;
}
total=total+worth;
suit=getsuit(card);
printf(" %c%c\n", rank, suit);
printf(" Total: %i", total);
i++;
}
return total;
}
char getsuit(int card)
{
char suit;
if(card<13)
{
suit='S';
}
else if(card>=13 && card<26)
{
suit='H';
}
else if(card>=26 && card<39)
{
suit='D';
}
else if(card>=39 && card<52)
{
suit='C';
}
return suit;
}
You have an array overflow in cdrawcards():
int ccards[10];
...
ctotal = cdrawcards(ccards, deck);
...
int cdrawcards(int ccards[], int deck[])
{
int i = 26;
...
ccards[i] = deck[i]; //Overflow, ccards[] only has 10 elements
Either change the initial value of i or make the array larger int ccards[52]. I would also add an explicit check in cdrawcards() and hdrawcards() to ensure you don't overflow the arrays, like:
while (total < 18 && i < 10)
For example, in hdrawcards() you can overflow the array if you get a bunch of low value cards in a row (ex: 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3(overflow), 3, ...).

Resources