In order to better understand dynamic memory allocation, I have tried to make a program that lists the digits of the fibonnaci sequence in order, using arrays to make infinitely long numbers possible. I am coming up with 31 errors compiling it. Is there anything fundamentally wrong, or just typos? Please help me understand this topic for C++.
P.S. -Will it work so that I can add sequence to an array without predetermining the amount of integers needed and without deleting and recreating the array each time? If not, please give me some suggestion on how to get around the issue of copying all the data from each array into a new one every time it gets too small and needs to be deleted.
-Thanks
Yodaliketaco
(2ND PROGRAM IN C++, AFTER MY CROSSHAIR)
Code:
#include <iostream>
#include <new>
#include <dos.h>
#include <windows.h>
using namespace std;
int d = 0;
int m;
int n = 0;
int * p;
int * pp;
int * ppp;
p= new int one[d];
pp= new int two[d];
ppp= new int three[d];
one [0] = 0;
two [0] = 1;
void printmemmm (int num[], int g)
{
int s;
for (s=0; s<g; s++)
cout << num[s];
}
void sequence (&int mem[], &int memm[], &int memmm[])
{
adding:
if (mem[n]+memm[n])<10 & n<d)
{
memmm[n] = (mem[n]+memm[n]);
if (n<d)
n++;
}
int c;
c = (mem[n]+memm[n])%10;
memmm[n+1] = c;
n++;
if (n<d)
goto adding;
else
{
m = 0;
if (m<d)
{
mem[m] = memm[m];
memm[m] = memmm[m];
m++;
}
else
{
d++;
}
}
}
int main ()
{
if (1 == 1)
{
sequence (one, two, three);
printmemmm (three, d);
Sleep(10);
}
return 0;
}
My original code using long double integers that functioned correctly was as follows. I am trying to make a version of this code that will not encounter an error after 1.30699e+308, which is the most long doubles can hold in this instance. I am trying to do this using a dynamic array, but I have trouble getting the initialization and modification of the array to work correctly.
Code:
#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <math.h>
using namespace std;
int main ()
{
int f;
f = 1;
do
{
int y;
int t;
cout << "How much delay would you like in between numbers (miliseconds)" << endl << "Type any number of miliseconds then press enter ";
cin >> t;
y = 1;
if(y == 1)
{
long double a;
a = 0;
long double b;
b = 1;
long double c;
c = a + b;
cout << a << ", ";
Sleep(t);
cout << b << ", ";
Sleep(t);
while (c < 1.30699e+308)
{
cout << c << ", ";
a = b;
b = c;
c = a + b;
Sleep(t);
}
cout << endl << "You have reached the limit of compution using standard variables" << endl;
cout << endl << "If you would like to start again press 1 if not press anything else (then enter)";
cin >> f;
}
}
while (f == 1);
return 0;
}
Hell_Demon: added code tags.