Ok this has been driving me crazy. I solved a problem on spoj called MIXTURES (http://www.spoj.com/problems/MIXTURES/). I don't know why i keep getting segmentation fault. There is also one catch in the problem that there is no explicit indicator for end of input. I think I have handled it correctly but correct me if I am wrong. Here is my code
#include<stdio.h>
#include<stdlib.h>
typedef struct temp
{
int modSum; //the modular sum of the cluster
int smoke; //the minimum smoke that a cluster can give.
}clusterInfo;
int fxDP(int *A,int len)
{
int i,j,k,smoke1,smoke2;
clusterInfo **dpArr=(clusterInfo **)malloc(sizeof(clusterInfo *)*(len-1));
for(i=0;i<len-1;i++)
dpArr[i]=(clusterInfo *)malloc(sizeof(clusterInfo)*(len-i-1)); //len- ( (i+2) -1)= len-i-1
//dpArr[i] gives info of all clusters of length i+2
//base case for clusterLength=2
for(i=0;i<len-1;i++)
{
dpArr[0][i].modSum=(A[i]+A[i+1])%100;
dpArr[0][i].smoke=A[i]*A[i+1];
}
//endBase Case
//induction
for(i=1;i<len-1;i++) //lengthOfCluster=i+2
{
for(j=0;j<len-i-1;j++) //len-i-1+i+2-1=len
{
smoke1=(dpArr[i-1][j].modSum*A[j+(i+2)-1]) + dpArr[i-1][j].smoke;
smoke2=(A[j]*dpArr[i-1][j+1].modSum) + dpArr[i-1][j+1].smoke;
dpArr[i][j].smoke=smoke1<smoke2 ? smoke1:smoke2 ;
dpArr[i][j].modSum=(dpArr[i-1][j].modSum+A[j+(i+2)-1])%100;
}
}
int result=dpArr[len-2][0].smoke;
free(dpArr);
return result;
}
int main()
{
int *A; int len,i;
while(1)
{
scanf("%d",&len);
if(feof(stdin)) break;
A=(int *)malloc(sizeof(int)*len);
for(i=0;i<len;i++)
scanf("%d",&A[i]);
printf("%d\n",fxDP(A,len));
}
return 0;
}
int result=dpArr[len-2][0].smoke;
What happens if len=1 ??
Related
This is my first time posting so bear with me! I have to make a quicksort algorithm as an assignment for university, using c. However, when I try running the program a pop-up window opens up and informs me that the exe has stopped working. So then I started debugging and I found that the problem appears when the program enters the recursion process, as I receive around those lines a SIGSEGV type error. Unfortunately I cant find the problem so that's why I'm posting here my code, hoping that you can help me. Any further advice about my code in general will be appreciated. Thanks!
#include <stdio.h>
#include <string.h>
#define SIZE 20
void quicksort(int *first, int *last);
main()
{
FILE *fp;
int *ptr, arr[SIZE], k;
ptr=arr;
fp=fopen("numbers.txt", "r");
printf("Initial array before sorting:\n");
for (k=0; k<SIZE; k++)
{
fscanf(fp, "%d", ptr);
printf("Element[%d]:\t%d\n", k+1, *ptr);
ptr++;
}
fclose(fp);
printf("Final array after sorting:\n");
quicksort(arr, arr+SIZE-1);
for (k=0; k<SIZE; k++)
{
printf("Element[%d]:\t%d\n", k+1, *(arr+k));
}
}
void quicksort(int *first, int *last)
{
int *item_left, *item_right, *i, *j, *pivot, temp, check_left=0, check_right=0, limiter=0;
pivot=last;
while (!check_left && !check_right && limiter<10)
{
for (i=first; i<last; i++)
{
if (*i>*pivot)
{
item_left=i;
check_left=1;
break;
}
}
for (j=last; j>first; j--)
{
if (*j<*pivot)
{
item_right=j;
check_right=1;
break;
}
}
if (check_left==1 && check_right==1)
{
temp=*item_left;
*item_left=*item_right;
*item_right=temp;
check_left=0;
check_right=0;
}
limiter++;
}
temp=*item_right;
*item_right=*pivot;
*pivot=temp;
if (last-first>1)//-------problem
{
quicksort(first, item_left-1);
quicksort(item_left, last);
}//----------problem
}
So I checked the code again and I found the problem. You see, I declared some local variables in the function I was calling. This was causing trouble in the computer's memory, as every time the function was called again, it was told to create the same variables which were already created and stored in the memory. So what I did is to make all those variables needed global. This way they are created only one time.
In the following code there is a calling convention error(possibly leading to an eternal loop), and i cannot detect it. I try to verify the code using 'Satabs'. What kind of model can bring the error to the surface. With the following model i get a segfault.
By changing the VLEN and TMAX you can play a bit.
Q1. What is the calling convention error?
Q2. What kind of model would be most appropriate to use for finding the error?
#include <stdio.h>
#if MODEL==1
#define VLEN 3
#define TMAX 4
int trans(int T,int*src,int*dst){
if (T < VLEN && T < TMAX && src[T] < 4){
dst[T]=src[T]+1;
return 1;
} else {
return 0;
}
}
#endif
struct next_state {
int next;
int src[VLEN];
};
typedef struct next_state *iterator_t;
void init(iterator_t iter,int *src){
for(int i=0;i<VLEN;i++){
iter->src[i]=src[i];
}
iter->next=0;
}
int next(iterator_t iter,int *dst){
#ifdef FIX_ARRAY
for(int i=0;i<VLEN;i++){
#else
for(int i=0;i<TMAX;i++){
#endif
dst[i]=iter->src[i];
}
int res=0;
while(!res&&iter->next<TMAX){
res=trans(iter->next,iter->src,dst);
iter->next++;
}
return res;
}
int find_depth(iterator_t iter,int *src){
int table[VLEN*TMAX];
int N=0;
init(iter,src);
for(int i=0;i<TMAX;i++){
if(next(iter,&(table[N*VLEN]))){
N++;
}
}
int depth=0;
for(int i=0; i<N;i++){
printf("Eimai stin for \n");
int tmp=find_depth(iter,&(table[i*VLEN]));
printf("tmp= %d\n",tmp);
if(tmp>=depth){
depth=tmp+1;
//assert(depth);
}
}
printf("\n\n");
return depth;
}
int main(int argc,char*argv[]){
int state[VLEN];
struct next_state ns;
for(int i=0;i<VLEN;i++){
state[i]=0;
}
int depth=find_depth(&ns,state);
printf("depth is %d\n",depth);
}
int depth=find_depth(&ns,state);
You are passing &ns, but taking arg in function as iterator_t iter, is this correct ?
void init(iterator_t iter,int *src){
for(int i=0;i<VLEN;i++){
iter->src[i]=src[i];
iter->src[i] is this expression fine?
I dont know 'Satabs' but the most promising candidate for an endless loop for me seems to be
while(!res&&iter->next<TMAX){
res=trans(iter->next,iter->src,dst);
iter->next++;
}
All other loops rather look like fix count.
This loop might be dangerous for itself even without the so called calling convention error, which doest jump to my eye yet.
Anyhow you should take a look not only to the call of the funtion trans but the whole call tree to it.
You could also try to paste your code there
http://gimpel-online.com//cgi-bin/genPage.py?srcFile=intro.txt&cgiScript=analyseCode.py&title=Introduction+and+Welcome&intro=Introducing+the+testing+facility&compilerOption=online32.lnt&in
Maybe you get a few more hints.
Just a guess:
Maybe 'Satabs' doesn't like undefined preprocessor conditions
like
#if MODEL==1
I was writing a C code for quick sort but something went wrong. After some debugging i finally found where my code was going wrong.
When i replaced
{
a[lp]+=a[ub];
a[ub]=a[lp]-a[ub];
a[lp]=a[lp]-a[ub];
}
with
{
tmp=a[lp];
a[lp]=a[ub];
a[ub]=tmp;
}
my code started working.
I am Curious to know why my initial implementation of swapping didn't work?
Can anyone help me?
#include<stdio.h>
#define swap(a,b) (a)=(a)+(b);b=(a)-(b);(a)=(a)-(b);
int a[]={7,1,5,2,3};
int partition(int lb,int ub)
{
int k,hp,lp;
k=a[ub];
lp=lb-1;
for(hp=lb;hp<ub;hp++)
{
if(a[hp]<k)
{
lp++;
int tmp=a[lp];
a[lp]=a[hp];
a[hp]=tmp;
}
}
lp++;
a[lp]+=a[ub];
a[ub]=a[lp]-a[ub];
a[lp]=a[lp]-a[ub];
return lp;
}
void quicksort(int lb,int ub)
{
if(lb<ub)
{
int pos=partition(lb,ub);
quicksort(lb,pos-1);
quicksort(pos+1,ub);
}
}
int main()
{
quicksort(0,4);
int i;
for(i=0;i<5;i++)printf("%d ",a[i]);
printf("\n");
return 0;
}
You need to consider what happens when lp == ub (ie. you are being asked to swap an element with itself).
Change it to this:
if (lp != ub) {
a[lp]+=a[ub];
a[ub]=a[lp]-a[ub];
a[lp]=a[lp]-a[ub];
}
Example: http://ideone.com/AS1Dgf
The task can be found here: http://www.talentbuddy.co/challenge/51846c184af0110af3822c32
And my programme regarding this task is the following:
#include <stdio.h>
#include<string.h>
void tokenize_query(char *query, char *punctuation) {
int i,j,ok=1,k,t;
char x[1000];
for(i=0;i<strlen(query);i++)
{
ok=1;
for(j=0;j<strlen(punctuation);j++)
{
if(query[i]==punctuation[j] || query[i]==' ')
ok=0;
}
if(ok!=0)
{
x[k]=query[i];
k++;
}
else {
for(t=0;t<k;t++)
{
printf("%c",x[t]);
}
k=0;
printf("\n");
}
}
}
k is uninitialised in the line
x[k]=query[i];
so you'll probably try to write beyond the end of the memory allocated for x.
The easiest fix is to initialise k when you declare it
int i,j,ok=1,k=0,t;
// ^^
i am tring to solve this http://www.spoj.com/problems/LEXISORT/ question
it working fine in visual studio compiler and IDEone also but when i running in SPOJ compiler it is getting SEGSIGV error
Here my code goes
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char *getString();
void lexisort(char **str,int num);
void countsort(char **str,int i,int num);
int main()
{
int num_test;
int num_strings;
char **str;
int i,j;
scanf("%d",&num_test);
for(i=0;i<num_test;i++)
{
scanf("%d",&num_strings);
str=(char **)malloc(sizeof(char *)*num_strings);
for(j=0;j<num_strings;j++)
{
str[j]=(char *)malloc(sizeof(char)*11);
scanf("%s",str[j]);
}
lexisort(str,num_strings);
for(j=0;j<num_strings;j++)
{
printf("%s\n",str[j]);
free(str[j]);
}
free(str);
}
return 0;
}
void lexisort(char **str,int num)
{
int i;
for(i=9;i>=0;i--)
{
countsort(str,i,num);
}
}
void countsort(char **str,int i,int num)
{
int buff[52]={0,0},k,x;
char **temp=(char **)malloc(sizeof(char *)*num);
for(k=0;k<52;k++)
{
buff[k]=0;
}
for(k=0;k<num;k++)
{
if(str[k][i]>='A' && str[k][i]<='Z')
{
buff[(str[k][i]-'A')]++;
}
else
{
buff[26+(str[k][i]-'a')]++;
}
}
for(k=1;k<52;k++)
{
buff[k]=buff[k]+buff[k-1];
}
for(k=num-1;k>=0;k--)
{
if(str[k][i]>='A' && str[k][i]<='Z')
{
x=buff[(str[k][i]-'A')];
temp[x-1]=str[k];
buff[(str[k][i]-'A')]--;
}
else
{
x=buff[26+(str[k][i]-'a')];
temp[x-1]=str[k];
buff[26+(str[k][i]-'a')]--;
}
}
for(k=0;k<num;k++)
{
str[k]=temp[k];
}
free(temp);
}
Generally speaking, these online judge programs give an example input (in this case, that input appears to work perfectly), but also use a set of harder hidden inputs.
In this case, what happens if the input string has a space in it? For example, an input of:
1
2
hello orld
whateverss
In this case, your scanf("%s",str[j]); will not properly read that input line. I'd suggest switching over to a getline style interface, rather than a scanf style interface.