Storing large numbers as an array

This is a discussion on Storing large numbers as an array within the C Programming forums, part of the General Programming Boards category; i've got an assignment which i have absolutly no idea how to start.. i'm not asking anyone to do it ...

  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,006
    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,006
    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,006
    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
    21,915
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 02:41 AM
  2. HELP:::MIX numbers in array
    By ypramesh in forum C Programming
    Replies: 9
    Last Post: 03-30-2006, 06:47 PM
  3. Representing a Large Integer with an Array
    By random_accident in forum C++ Programming
    Replies: 3
    Last Post: 03-03-2005, 11:23 AM
  4. Standard Deviation of an array of numbers...
    By Xenofizz in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2003, 09: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, 10:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21