Im trying to get this program working as it requires to calculate continued fraction inputed in a linked list. the programme doesn't show any error however it always crashes in the middle. Can someone help me?
The task is simply to store the programme on a linked list and then calculate it by taking the parameter from the link. It is a continued fraction.
#include <iostream>
#include <cmath>
using namespace std;
struct node
{
int data;
node *next;
} *h=nullptr, *t=nullptr;
float calculation (int co)
{
float a,c;
node *b,*f;
b->next = f;
while(co != 0)
{
f = t;
a = (float)f->data;
a = 1/a;
c = (float)b->data;
a = c + a;
t = b;
co--;
}
return a;
}
void storedata (int& c)
{
node *n = new node;
n->data = c;
n->next = nullptr;
cout<<n->data<<endl;
if(h==nullptr)
{
h=n;
t=n;
n=nullptr;
}
else
{
t->next=n;
t=n;
}
}
void formula (int a, int b, int co)
{
int c;
int z;
while (co!=0)
{
c = a/b;
storedata(c);
z = b*c;
z = a-z;
a = b;
b = z;
co--;
}
}
int main ()
{
int a,b,c,z,co,d;
float e;
a = 123;
b = 100;
co = 5;
formula (a,b,co);
e = calculation(co);
cout<<"cf1 = 123/100 ="<<e;
}
At least the following problems (some of which would be caught with warnings enabled, e.g.
-Wall
):Using uninitialized pointers, e.g.
node *b,*f; b->next = f;
. This causes undefined behavior.new
but nodelete
anywhere, therefore memory leaks will be present.