[Help] Working with dynamic memory

Posts 1–7 of 7 · Page 1 of 1
[Help] Working with dynamic memory
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.
I don't think you're able to initialize variables that way, when you used the new operator.

[php]
p = new int one[d];
[/php]

It should be.

[php]
p = new int[d];
[/php]

now 'p' has 'd' elements. Which in your case is 0, may I ask why?

Sorry, I'm not understanding what you're trying to do here..
May I ask which compiler you are using sir?
I am using visual studio 2008 express edition for C++ compiling. The point of the program is to list the numbers in the fibonnaci sequence without having errors when the numbers become too large to be stored in normal variables (like integer or long double).

The fibonnaci sequence is 0,1,1,2,3,5,8,13,21,34, etc... Each number is the previous two numbers added together, and the first two numbers are 0 and 1.

If I use p = int[d] for initialization, how can I reference the array, as it has no name? I meant to use "one" to reference the array within the main of the code.

p is meant to be a pointer that holds the address of the beginning of the code for the int array one, so that I can modify the program to delete the array at a later time for modification of size by referencing it with the pointer located within variable p.
Code:
int a;
int b;
int c;

int main()
{
    a = 0;
    b = 1;

    while (true)
    {
        c = a + b;
        printf(a, b, c);
        c = a += b;
        b += a;
        printf(a, b, c);
     }
}
That should do it. =/
Which memory would be the best to work with? Right now I am using C++ which is also great but I want to modify my work more with sufficient and in an effective manner.
This is actally a really difficult problem. once u go past the largest data type there's huge issues w/ displaying, adding, and storing numbers that large. mathemeticians should be the only ones who have to encounter problems like this. Really the only way to solve this problem that I can think of would be at the assembly level, but perhaps there is some nice library out there u can use, to use really large numbers. But yeh what ur doing gives professional programmers a headache.
Posts 1–7 of 7 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

Need help?