how can this program give a floating point error? - runtime

This is a "sort of" logical question that i tried to solve using c.
I basically input all the divisors of a number in an array and the added the digits to a single digit number and stored in a variable 'best'.
But the output that i get is "Floating point exception (core dumped)"
Thought it would be a problem with the one of the loops, but i cant find any.
could i please know what is the meaning of this error and whats causing it ?
#include <stdio.h>
void main()
{
int n,a[50],b[50],k=0,l1=0,l2=0,temp,big,bigi,best=0,i,l3;
printf("Enter an integer (less than 10^5):- ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(n%i==0)
{
a[k]=i;
k++;
}
}
for(i=0;i<k;i++)
{
b[i]=a[i];
}
for(i=0;i<k;i++)
{
if(a[i]<10)
{
continue;
}
else
{
l3=a[i];
while(l3>0)
{
temp=l3%10;
l1=l1+temp;
l3=l3/10;
}
if(l1>=10)
{
while(l1>0)
{
temp=l1%10;
l2=l2+temp;
l1=l1/10;
}
}
a[i]=l2;
}
}
big=a[0];
for(i=0;i<k;i++)
{
if(a[i]>big)
{
big=a[i];
bigi=i;
}
}
for(i=0;i<k;i++)
{
while(i!=bigi)
{
if(a[bigi]==a[i])
{
if(b[bigi]>b[i])
{
best=a[i];
}
else
{
best=a[bigi];
}
}
}
if(best=0)
{
best=a[bigi];
}
}
printf("The best number is :- %d",best);
}

Oh gosh Steve, your code formatting!
I don't have an answer, but I'm willing to bet it has something to do with the array indices. You are creating an array with 50 elements.
Then you are accessing it with an index of k that is dependent on your input! How can you guarantee that k will not be greater than 49?
Put some debug printfs in there. Everywhere. Monitor your variable k. Look at what your loops are doing, and where you are getting stuck.
And for my own sanity, and that of anyone else reading this, here:
#include <stdio.h>
void main()
{
int n,a[50],b[50],k=0,l1=0,l2=0,temp,big,bigi,best=0,i,l3;
printf("Enter an integer (less than 10^5):- ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(n%i==0)
{
a[k]=i;
k++;
}
}
for(i=0;i<k;i++)
{
b[i]=a[i];
}
for(i=0;i<k;i++)
{
if(a[i]<10)
{
continue;
}
else
{
l3=a[i];
while(l3>0)
{
temp=l3%10;
l1=l1+temp;
l3=l3/10;
}
if(l1>=10)
{
while(l1>0)
{
temp=l1%10;
l2=l2+temp;
l1=l1/10;
}
}
a[i]=l2;
}
}
big=a[0];
for(i=0;i<k;i++)
{
if(a[i]>big)
{
big=a[i];
bigi=i;
}
}
for(i=0;i<k;i++)
{
while(i!=bigi)
{
if(a[bigi]==a[i])
{
if(b[bigi]>b[i])
{
best=a[i];
}
else
{
best=a[bigi];
}
}
}
if(best=0)
{
best=a[bigi];
}
}
printf("The best number is :- %d",best);
}

Related

Segmentation Fault in Control Flow Analysis

I'm Getting a Segmentation fault here
if((cur->block)!=(cur->next)->block)
I Tried to fix Some on my own but didn't got it correct.
this Program is used to implement Control Flow Analysis it was having some more Segmentation faults which were identified and fixed ,but now. I don't have any alternative to try and edit this code.
struct Listnode
{
char data[50];
int leader,block,u_goto,c_goto;
struct Listnode *next;
char label[10],target[10];
}*temp,*cur,*first=NULL,*last=NULL,*cur1;
FILE *fpr;
void createnode(char code[50])
{
temp=(struct Listnode*) malloc(sizeof(struct Listnode));
strcpy(temp->data,code);
strcpy(temp->label,"\0");
strcpy(temp->target,"\0");
temp->leader=0;
temp->block=0;
temp->u_goto=0;
temp->c_goto=0;
temp->next=NULL;
if(first==NULL)
{
first=temp;
last=temp;
}
else
{
last->next=temp;
last=temp;
}
}
void main()
{
char codeline[50];
char c,dup[50],target[10];
char *substring,*token;
int i=0,j=0,block,block1;
fpr= fopen("P10-CFA.txt","r");
while((c=getc(fpr))!=EOF)
{
if(c!='\n')
{
codeline[i]=c;
i++;
}
else
{
codeline[i]='\0';
createnode(codeline);
i=0;
}
}
//create last node
codeline[i]='\0';
createnode(codeline);
fclose(fpr);
// find out leaders,conditional stmts
cur=first;
cur->leader=1;
while(cur!=NULL)
{
substring=strstr((cur->data),"if");
if(substring==NULL)
{
if((strstr((cur->data),"goto"))!=NULL)
{
cur->u_goto=1;
(cur->next)->leader=1;
}
}
else
{
cur->c_goto=1;
(cur->next)->leader=1;
}
substring=strstr((cur->data),":");
if(substring!=NULL)
{
cur->leader=1;
}
substring=strstr((cur->data),"call");
if(substring!=NULL)
{
cur->leader=1;
}
if(strstr(cur->data,"return")!=NULL)
{
cur->leader=1;
(cur->next)->leader=1;
}
cur=cur->next;
//to find labels and targets
cur=first;
while(cur!=NULL)
{
if((cur->u_goto==1)||(cur->c_goto==1))
{
substring=strstr(cur->data,":");
if(substring!=NULL)
{
token=strstr(substring,"L" );
if(token!=NULL)
strcpy(cur->target,token);
else
{
substring=strstr(cur->data,"L");
if(substring!=NULL)
strcpy(cur->target,substring);
}
}
if(strstr(cur->data,":")!=NULL)
{
strcpy(dup,cur->data);
token=strtok(dup,":");
//printf("\ntoken:%s",token);
if(token!=NULL)
strcpy(cur->label,token);
}
cur=cur->next;
//to identify blocks
cur=first;
while(cur!= NULL)
{
cur=cur->next;
if((cur->leader)==1)
{
j++;
cur->block=j;
}
else
cur->block=j;
}
printf("\n\n.....Basic Blocks \n");
cur=first;
j=0;
printf("\nBlock %d:",j);
while(cur!=NULL)
{
if ((cur->block)==j)
{
printf("%s",cur->data);
printf("\n\t");
cur=cur->next;
}
else
j++;
printf("\nBlock %d:",j);
}
}
//to output the control flow from each block
printf ("\t\t.......Control Flow.........\n\n");
cur=first;
i=0;
while(cur!=NULL)
{
if((cur->block)!=(cur->next)->block)
{
block=cur->block;
if(cur->u_goto==1)
{
strcpy(target,cur->target);
cur1=first;
while(cur1!=NULL)
{
if(strcmp(cur1->label,target)==0)
{
block1=cur1->block;
printf("\t\tBlock%d---->Block%dln",block,block1);
}
cur1=cur1->next;
}
}
else if(cur->c_goto==1)
{
strcpy(target,cur->target);
cur1=first;
while(cur1!=NULL)
{
if(strcmp(cur1->label,target)==0)
{
block1=cur1->block;
printf("lt\1Block%d---TRUE--->Block%d---FALSE--->Block%d\n",block,block1,(block+1));
}
cur1=cur1->next;
}
}
else if(strstr(cur->data,"return")==NULL)
{
printf("\t\tBlock%d--->Block%d\n",block,(block+1));
}
else
printf("lt\tBlock%d--->NULL\n",block);
}
cur=cur->next;
}
cur=last;
block= cur->block;
printf("\t\tBlock%d--->NULL",block);
}
}
}

deriving time-complexity of nqueens problem

Can anyone prove/derive the time complexity of my nqueens solution approach?
I am going through each and every position on the grid and if it is possible to place a queen there, then, I am calculating solution by first placing the queen and then unplacing the queen, else I move on.
Code:
bool notinrow(int row,int col,vector<string> tra)
{
for(int i=0;i<tra.size();i++)
{
if(tra[row][i]=='Q' & i!=col)
return false;
}
return true;
}
bool notincol(int row,int col,vector<string> tra)
{
for(int j=0;j<tra.size();j++)
{
if(tra[j][col]=='Q' & j!=row)
return false;
}
return true;
}
bool notindiag1(int r,int c, vector<string> tra)
{
int i=r-1;
int j=c-1;
while(i>=0 & j>=0)
{
if(tra[i][j]=='Q')
return false;
i--;j--;
}
return true;
}
bool notindiag2(int r, int c,vector<string> tra)
{ int i=r-1;
int j=c+1;
while(i>=0 & j<totqueens)
{
if(tra[i][j]=='Q')
return false;
i--;j++;
}
return true;
}
void nqueens(int number,vector<string> tra,int currqueens)
{
if(currqueens==totqueens)
{
bhej.push_back(tra);
return ;
}
if(number==totiter)
return;
int x=number/totqueens;
int y=number%totqueens;
if(ispossible(x,y,tra))
{
tra[x][y]='Q';
nqueens(number+1,tra,currqueens+1);
tra[x][y]='.';
nqueens(number+1,tra,currqueens) ;
}
else
nqueens(number+1,tra,currqueens);
}```

sorting via Merge Sort algorithm

This is the merge sort program. I traced this program and found that after sorting the first four elements of array, the recursive call to sort function with left array with elements 4,6 again gets executed which increments 'k' to upper bound and throws ArrayIndexOutofBound index. I am not able to figure out the complete flow. please help.
class MergeSort
{
static int k=0;
static int a[]={2,1,4,6,8,5,3,9};
public static void main(String args[])
{
int i;
System.out.println("Before");
for(i=0;i<a.length;i++)
{
System.out.print(a[i]+" ");
}
System.out.println("\n");
sort(a);
System.out.println("\nAfter");
for(i=0;i<a.length;i++)
{
System.out.print(a[i]+" ");
}
System.out.println("\n");
}
public static void sort(int b[])
{
int n=b.length;
if(n<2)
return;
int mid=n/2;
int left[]=new int[mid];
int right[]=new int[n-mid];
int i;
System.out.print("left ");
for(i=0;i<mid;i++)
{
left[i]=b[i];
System.out.print(left[i]+" ");
}
System.out.println();
System.out.print("right ");
for(i=mid;i<n;i++)
{
right[i-mid]=b[i];
System.out.print(right[i-mid]+" ");
}
System.out.println("\n");
sort(left);
sort(right);
merge(left,right);
}
public static void merge(int l[], int r[])
{
int i=0;
int j=0;
while(i<l.length && j<r.length)
{
if(l[i] < r[j]){
a[k]=l[i];
System.out.println("a["+k+"]="+a[k]);
i++;
}
else
{
a[k]=r[j];
System.out.println("a["+k+"]="+a[k]);
j++;
}
k++;
}
while(i<l.length)
{
a[k]=l[i];
System.out.println("a["+k+"]="+a[k]);
i++;
k++;
}
while(j<r.length)
{
a[k]=r[j];
System.out.println("a["+k+"]="+a[k]);
j++;
k++;
}
}
}

Stack is Full Logical Error?

I am Learning Stack Data Structure and Unfortunately in my first code I am getting Stack is Full Run time error due to my less knowledge of Stack I am unable to Trace error in my written code please have a look.
Thanks
#include
using namespace std;
struct Stack
{
int data[15];
int top;
};
void init(Stack &s)
{
s.top=-1;
}
bool isEmpty(Stack s)
{
if(s.top == -1)
return true;
else
return false;
}
bool isFull(Stack s)
{
if(s.top > 14)
{
return true;
}
else
{
return false ;
}
}
void push ( Stack &s,int value)
{
if(isFull(s) == true)
{
cout<<"Oooops Stack is Full :(\n";
}
else
{
s.top++;
s.data[s.top]=value;
}
}
int pop (Stack &s)
{
int removedValue = s.data[s.top];
s.data[s.top]=0;
if(isEmpty(s)== true)
{
cout<<"Ohhh Stack is Empty\n";
}
else
{
s.top--;
return removedValue;
}
}
int top(Stack s)
{
return s.top;
}
int main()
{
Stack items;
cout << "Hello Welcome to Stack!" << endl;
push(items,1);
cout<<pop(items)<<endl;
return 0;
}
ok folks here is the error..
i declared a new stack named Stack in main but didnot initialize it using
void init(Stack &s)
{
s.top=-1;
}
so here is the answer by initializing stack in main()
Stack items;
init(items);

Hierholzer algorithm to find eulerian path [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have implemented hierholzer algorithm to find eulerian path in a graph using two stacks. Below is my implementation. There is some runtime error, will be glad if somebody could help
#include<bits/stdc++.h>
using namespace std;
stack<int> result;
stack<int> temp;
class graph
{
int v;
list<int> *adj;
public:
graph(int v)
{
this->v=v;
adj=new list<int> [v];
}
~graph()
{
delete []adj;
}
void add_edge(int u,int v)
{
adj[u].push_back(v);
adj[v].push_back(u);
}
void remove_edge(int u, int v);
int start_vertex();
void print_euler_path(int u);
bool allvisited();
};
int graph::start_vertex()
{
int u=0;
for(int u=0;u<v;u++)
{
if(adj[u].size() & 1)
break;
}
return u;
}
bool graph::allvisited()
{
for(int i=0;i<v;i++)
{
if(adj[i].size()>0)
{
list<int>::iterator it;
for(it=adj[i].begin();it!=adj[i].end();it++)
{
if(*it!=-1)
return false;
}
}
}
return true;
}
void graph::remove_edge(int u,int v)
{
list<int>::iterator i;
i=find(adj[u].begin(),adj[u].end(),v);
*i=-1;
i=find(adj[v].begin(),adj[v].end(),u);
*i=-1;
}
void graph::print_euler_path(int u)
{
temp.push(u);
list<int>::iterator i;
int flag=0;
if(allvisited())
return;
for(i=adj[u].begin();i!=adj[u].end();i++)
{
if(*i!=-1)
{
cout<<"S";
remove_edge(u,*i);
print_euler_path(*i);
}
}
if(!temp.empty())
{
int k=temp.top();
temp.pop();
result.push(k);
if(!temp.empty())
print_euler_path(temp.top());
}
}
int main()
{
graph g(6);
g.add_edge(0,1);
g.add_edge(1,2);
g.add_edge(2,3);
g.add_edge(3,0);
g.add_edge(5,1);
g.add_edge(5,2);
g.add_edge(4,1);
g.add_edge(4,2);
int u=g.start_vertex();
g.print_euler_path(u);
while(!result.empty())
{
cout<<result.top()<<" ";
result.pop();
}
return 0;
}
For exact logic you can refer http://iampandiyan.blogspot.in/2013/10/c-program-to-find-euler-path-or-euler.html
I don't think that these lines do what you want:
remove_edge(u,*i);
print_euler_path(*i);

Resources