Thread: Newblet Question(s)

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    8

    Smile Newblet Question(s)

    So I'm trying to figure out how to dynamically create an array.
    I heard I could do this by using a pointer (dereferencing operator? is that a pointer, are there any other types of pointers?)
    Like so?
    Code:
    while (ch)
    {
    ii++;
    
    numbers[p] = ch;
    *p = ii;
    }
    I'm trying to segment out an input into an array so I can do stuff with it. Would that code redimension the array indefinately while input was not empty?

    Sorry, I'm still getting used to manipulation of data in c++.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Perhaps a vector would better suit you? It's like an array, but it grows without you handling the memory allocation.
    http://www.parashift.com/c++-faq-lit....html#faq-34.1
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    8
    Um, I'm not looking for a hetrogenous container.

    For example... I'm hoping to expand this into an arbitrary precision calculator. So I'm learning string manipulation functions and how to feed it into an array.

    For example I entered this:
    1111111111111 * 1111111111111

    The answer is of course: 1234567901234320987654321

    But, that would overload most of the primitive data types.

    So I'm taking this in small steps.
    First I figure out how to segment out input into an array.
    So lets say I cined a random number:
    123456789
    and wanted to check if it was divisible by nine using a method I used in grade school (sum all the numbers and see if that is a multiple of 9)
    So I need to pull out each index of that int variable
    and feed it into an array, sum the contents of that array, divide by nine and then check to see if that is a whole number thus giving me a program which will evaluate a function which I learned in third grade.


    Some might ask why I don't just use a modulus. Its because I can't stand the %. It looks like its staring at me man... really creeps me out.

    I'll read up a bit more on the container classes.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Using char arrays is certainly a common (only?) way to deal with huge numbers. In C++ to allocate your own memory on the fly you use the new or new[] operator and release the memory with the delete or delete[] operator. In C you would use the malloc and free keywords.

    However, why not use std::string to store the representation of a huge number and spare yourself the hassle of handling your own memory allocation?
    You're only born perfect.

  5. #5
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    you can still use a vector<int> or maybe better a vector<long long>, whenever you overflow you just push_back the overflow on the vector and not have to worry about dynamic allocation and deletion (actually, I'd recommend deque over vector, especially if your numbers will grow pretty big as hit handles reallocation a lot better)

    Using string as suggested is an easy method especially for beginners, but it will be much slower because of all the conversion back and forth.

    If you'd like, i'd be more than willing to show you some of my large integer code.

    Disclaimer. I oversimplified the overflow part above, there's actually a lot of detail in that you actually don't want your variable to overflow.
    Last edited by Darryl; 05-23-2006 at 11:07 AM.

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by KyussWren
    Some might ask why I don't just use a modulus. Its because I can't stand the %. It looks like its staring at me man... really creeps me out.
    Gee, I hate the =, it looks mean to me. How can I get around not using it?
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    8
    ohnononono!
    I don't know how not to use the =.
    I would guess you could black box it a bit. Maybe open up the language files if you were using Kdev or something and edit it...
    but I haven't really experimented with language modification.
    If I had, I would probably modify my IDE to make my source code self documenting .
    Now that would be a feat.

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    8
    Code:
    // StringtoDecimalArray.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include "conio.h"
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    const int MAX_ARRAY_SIZE = 200;
    
    int CharToDigit( char ); 
    void Output(int[], int);
    
    int main()
    {
    	int* number;
    	string inputNumber;
    
    	cout << "Enter an arbitrary precision number" << endl;
    	cin >> inputNumber;
    
    	number = new int[ inputNumber.length() ];
    
    	for ( unsigned ii = 0; ii < inputNumber.length(); ii++)
    	{
    		number[ii] = CharToDigit(inputNumber[ii] );
    	}//for
    
    
    	Output(number, inputNumber.length());
    
    	delete [] number;
    	
    	getch();
    	return 0;
    }
    
    int CharToDigit( char argument )
    {
    	return(static_cast<int>(argument) - 48 );
    } //CharToDigit
    
    void Output( int array[], int nbOfElements)
    {
    	//for (unsigned ii = 0; ii < nbOfElements; ii++)
        for (int ii = nbOfElements + 1 ; ii > 0; ii--)
    	{
    		cout << array[ii - 1] << endl;
    	}//for
    }//output
    and here is the code I came up with so far.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 07:42 AM
  4. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM