Results 1 to 7 of 7
  1. #1
    yodaliketaco's Avatar
    Join Date
    Jan 2010
    Gender
    male
    Location
    winsock.dll
    Posts
    645
    Reputation
    45
    Thanks
    515
    My Mood
    Tired

    [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.
    Last edited by Hell_Demon; 06-14-2010 at 05:53 PM. Reason: Added original, non dynamic code | Added code tags

  2. #2
    Void's Avatar
    Join Date
    Sep 2009
    Gender
    male
    Location
    Inline.
    Posts
    3,198
    Reputation
    205
    Thanks
    1,445
    My Mood
    Mellow
    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..

  3. #3
    mwb1234's Avatar
    Join Date
    May 2009
    Gender
    male
    Posts
    460
    Reputation
    7
    Thanks
    65
    May I ask which compiler you are using sir?

  4. #4
    yodaliketaco's Avatar
    Join Date
    Jan 2010
    Gender
    male
    Location
    winsock.dll
    Posts
    645
    Reputation
    45
    Thanks
    515
    My Mood
    Tired
    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.
    Last edited by yodaliketaco; 06-14-2010 at 01:36 PM.

  5. #5
    serpentine's Avatar
    Join Date
    Aug 2008
    Gender
    male
    Location
    In your moms bed
    Posts
    1,623
    Reputation
    22
    Thanks
    176
    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. =/
    [IMG]https://i306.photobucke*****m/albums/nn265/chugsweet23/Reverb.png[/IMG]


    Bans: 1337

    .:Gifts:.

    Johhny 3 Tears: [x] [x]

  6. The Following User Says Thank You to serpentine For This Useful Post:

    mwb1234 (06-14-2010)

  7. #6
    nwilliams's Avatar
    Join Date
    Jun 2010
    Gender
    female
    Posts
    3
    Reputation
    10
    Thanks
    0
    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.

  8. #7
    why06's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    IBM
    Posts
    4,304
    Reputation
    170
    Thanks
    2,203
    My Mood
    Flirty
    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.

    "Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."
    - Dwight D. Eisenhower

Similar Threads

  1. Help. Exp with computers, mods not working
    By gatorzfan12 in forum Combat Arms Help
    Replies: 3
    Last Post: 07-03-2010, 04:40 AM
  2. Replies: 28
    Last Post: 08-16-2009, 01:40 AM
  3. [Help] How do you get DDD555 aimbot to work with Xfire chams?
    By HAPPYxHACKING in forum Combat Arms Hacks & Cheats
    Replies: 1
    Last Post: 07-22-2009, 06:53 PM
  4. Bypass 2.5 Doesn't Work with My Vista! HELP!!!
    By saefine in forum Combat Arms Hacks & Cheats
    Replies: 10
    Last Post: 02-13-2009, 07:58 AM
  5. Replies: 2
    Last Post: 03-01-2007, 07:10 PM

Tags for this Thread