Thread: Storing large numbers as an array

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

    Storing large numbers as an array

    i've got an assignment which i have absolutly no idea how to start..

    i'm not asking anyone to do it for me, but just a little help to get me started off..

    we need to be able to break up numbers and store them as separate elements in an array. so say you have the number 42, create an array which stores the number as array[0] = 4, array[1] = 2. we need to be able to do this up to 100 digits.

    also, the number 42 isn't entered in by the user using scanf or getchar's. so it's something like, number = 42;


    any help is greatly appreciated =)

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can simply read them as a string using fgets. There's an example of its use in the FAQ section. Then loop through the string testing each character with something like isdigit to make sure what they've typed is a number.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by quzah
    You can simply read them as a string using fgets. There's an example of its use in the FAQ section. Then loop through the string testing each character with something like isdigit to make sure what they've typed is a number.


    Quzah.
    Wouldn't it be easier if he uses atoi?

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    On a string containing 100 digits? Tell me about your implementation.
    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.*

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by Dave_Sinkula
    On a string containing 100 digits? Tell me about your implementation.
    Hm I got the oposite idea, he needs itoa.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void){
    	int foo = 1111111;
    	char blah[100];
    
    
    	sprintf(blah,"%d",foo);
    	printf("%s\n",blah);
    	return 0;
    }
    Last edited by Maragato; 05-19-2006 at 09:16 PM.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    So you really have an implementation with at least 329 bits per integer... Cool!
    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.*

  7. #7
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by Dave_Sinkula
    So you really have an implementation with at least 329 bits per integer... Cool!
    I do deserve a candy don't I?

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    No soup for you!
    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.*

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    3
    lol @ replies

    i'm currently trying the fgets thing (without much progress... lol)


    Ahh what we're meant to do is create a program that can calculate up to 70! (70x69x68x67..) and also, calculate up to the 400th Fibbonacci number (1+1+2+3+5+8+13+21...). so each digit needs to be stored in its own element then we need to create functions which will add/multiply the arrays etc..

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Ahh what we're meant to do is create a program that can calculate up to 70! (70x69x68x67..) and also, calculate up to the 400th Fibbonacci number (1+1+2+3+5+8+13+21...). so each digit needs to be stored in its own element then we need to create functions which will add/multiply the arrays etc..
    You most probably will not be allowed to actually use this library in your assignment (or maybe you can), but you could take a look at the GNU Multiple Precision Arithmetic Library. The section in the GMP Manual on the algorithms used may be an interesting starting point for you to find out on algorithms to use.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question: reading numbers into an array
    By Lince in forum C Programming
    Replies: 15
    Last Post: 11-15-2006, 03:41 AM
  2. HELP:::MIX numbers in array
    By ypramesh in forum C Programming
    Replies: 9
    Last Post: 03-30-2006, 07:47 PM
  3. Representing a Large Integer with an Array
    By random_accident in forum C++ Programming
    Replies: 3
    Last Post: 03-03-2005, 12:23 PM
  4. Standard Deviation of an array of numbers...
    By Xenofizz in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2003, 10:48 PM
  5. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM