Thread: Breaking down massive numbers

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    32

    Breaking down massive numbers

    I have ideas for the remainder of the program its just getting over the first hump that I need help with.

    I am required to scan in long numerical values from an input file and multiply/add them together. When I assign integar variables to the numbers, one of them doesn't output properly (Its whereabouts of 16 digits long).

    So I was forced to scan them in using a string. My question to you is this, is there any way to break down a numerical string into individual integars so I can manipulate them using stacks?

    I've ran across strtok() as my only viable option thus far, but I don't know what to set as the tokenizer...I believe thats what its called. So naturally that won't work for me.

    My hope to you is that someone can point me in any sort of direction so I can continue on with this program.

    Thank you very much in advance.
    -Michael
    Last edited by iiwhitexb0iii; 09-29-2006 at 05:47 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Subtract the character zero ( '0' ) from the current array index.
    Code:
    foo = array[ x ] - '0';
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Indexing operator.

    Code:
    char number[] = "1234567890";
    int number_len, i, current;
    
    number_len = strlen(number);
    for(i = 0; i < number_len; ++i)
    {
            current = number[i] - '0';
    }
    Should work on for ascii. If there are commas in it, and you wanted those little groupings, then strtok might be appropriate. Edit:

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >is there any way to break down a numerical string into individual integars so I can manipulate them using stacks?
    It depends on how many digits you want per int. If you just want one digit per int, then you'd just use a for-loop, probably starting at the end to grab the lsb first:
    Code:
       digit = num_string[i] - '0';
    If you want three digits per int, you could use strlen to determine the length of the string and then strncpy to grab three digits, and store that in a temp string. Then use strtol or strtoul to convert from string to int.

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    32
    thank you all very much, i never would've thought about that on my own

    -Michael

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    32
    i've used what you all have given me and its throwing up some errors. additionally, i should've mentioned this before hand, but i don't know exactly what i am recieving after subtracting '0' from the array index.

    Code:
    Assignment2.c: In function 'string_2_ints':
    Assignment2.c:57: warning: passing argument 1 of 'strlen' from incompatible pointer type
    Assignment2.c:60: error: invalid operands to binary -
    i'm certain its an error on my end.
    here are the necessary lines of code.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    //Structure declaration
    typedef struct    {
        char first [50];
        char second[50];
    } data;
    
    void string_2_ints(data numbers[], int num);
    
    //Inside of main
    data *numbers;
    
    //num_sets is an integar variable scanned in from the input file
    numbers = malloc(sizeof(data)*(num_sets * 2));
    
    string_2_ints(numbers, num_sets);
    
    //Seperate functions after main
    void string_2_ints(data numbers[], int num)    {
    
        int i, length, digit;
        length = 0, digit = 0;
    
        length = strlen(numbers);
    
        for(i = 0; i < length; ++i)    {
            digit = numbers[i] - '0';
            printf("%d\n", digit);
        }
    }
    i apologize for any small errors inside the code as that was transcribed from a computer w/o internet access.

    thank you for your time,
    -Michael

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    Assignment2.c: In function 'string_2_ints':
    Assignment2.c:57: warning: passing argument 1 of 'strlen' from incompatible pointer type
    Assignment2.c:60: error: invalid operands to binary -
    It's telling you that strlen needs a char*, not a data*. The char arrays in the struct perhaps?
    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.*

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    As Dave mentioned, you probably want to loop thru each string of the structure:
    Code:
    void string_2_ints(data numbers[], int num)    {
    
        int i, j, length, digit;
        length = 0, digit = 0;
    
        for (i=0; i<num; i++)
        {
            length = strlen(numbers[i].first);
    
            for(j = 0; j < length; ++j)    {
                digit = numbers[i].first[j] - '0';
                printf("%d\n", digit);
            }
    
            length = strlen(numbers[i].second);
    
            for(j = 0; j < length; ++j)    {
                digit = numbers[i].second[j] - '0';
                printf("%d\n", digit);
            }
        }
    }

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >i don't know exactly what i am recieving after subtracting '0' from the array index.
    What you're doing is subtracting '0' from the character's encoded value, in order to give you an int value from 0 to 9.

    For example, assuming ascii encoding the hexadecimal values are:
    '0' = 0x30
    '1' = 0x31
    '2' = 0x32
    '3' = 0x33
    '4' = 0x34
    '5' = 0x35
    '6' = 0x36
    '7' = 0x37
    '8' = 0x38
    '9' = 0x39

    Subtracting '0' (or 30 hexadecimal) from any of the above gives a value from 0-9, which is the int value.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with Rational Numbers (C++)
    By cloudjc in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2008, 04:03 PM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. Adding Line numbers in Word
    By Mister C in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 06-24-2004, 08:45 PM
  4. 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
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM