0

I'm working on a project that does an algorithm with the given input. I have the input stored in a long. with the given input, I need to convert “number” into an array so I can have access to each digit. for example, if “number = 73757383” I need to convert that into an array: array = [7, 3, 7, 5, 7...] to be able to do the algorithm (multiply every other digit, then add the others together). But I can't seem to figure out how to do this. Any help is appropriate. Also just as a note, I'm using the cs50 library. Thank you!

#include <stdio.h>
#include <cs50.h>
#include <math.h>

int main(){

    long credit;
    int number;
    int arr[digit];
    int i;

    do
    {
        credit = get_long("Number: ");
        number = floor(log10(credit) + 1); 

    }
    while (number < 13 || number > 16); 
Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
  • 2
    Does this answer your question? [Getting each individual digit from a whole integer](https://stackoverflow.com/questions/3118490/getting-each-individual-digit-from-a-whole-integer) –  Mar 13 '21 at 14:03
  • Which part of the problem are you having trouble with? Are you unable to determine which decimal digits a `long` consists of? Or are you having trouble storing these digits in an array? – Andreas Wenzel Mar 13 '21 at 14:03
  • 1
    It looks as though you are processing a card number. If so, don't use integers or math functions, use a string. "Number" does not always mean "integer" and IMO it's a *mistake* to use an integer for card numbers, phone numbers, house numbers, etc. – Weather Vane Mar 13 '21 at 14:07
  • 3
    If you use CS50's `get_string` function, then you will have the number stored as individual decimal digits (provided the user actually entered digits). Since a string is nothing else than a `char` array, you can access the individual elements of the array using the `[]` operator. – Andreas Wenzel Mar 13 '21 at 14:17
  • planetoidLV, With input like `“number = 00073757383”`, are you looking for 8 or 11? – chux - Reinstate Monica Mar 13 '21 at 14:25

4 Answers4

1

The easiest way is to create a string out of the number and then process 1 digit after another:

#include <stdio.h>
#include <string.h>

int main(){

    long credit = 4564894564846;
    int arr[20];
    char buffer[21];

    sprintf(buffer, "%ld", credit);
    int n = strlen(buffer);
    for (int i = 0; i < n; i++)
        arr[i] = buffer[i] - '0';
    
    for (int i = 0; i < n; i++)
        printf("%d\n", arr[i]);
}

https://godbolt.org/z/YY4znd

Another possibility is to extract the digit from the number with % 10 and divide by 10 afterwards, but then you get the digits in the wrong order.

mch
  • 9,424
  • 2
  • 28
  • 42
1

You could simply compare the number with 1000000000000 (at least 13 digits) and 9999999999999999 (at most 16 digits), but you probably need the number of digits later in the code:

#include <stdio.h>
#include <cs50.h>

int main() {
    long credit;
    int arr[16];
    int i;

    do {
        credit = get_long("Number: ");
    }
    while (credit < 1000000000000 || credit > 9999999999999999);

    ...

Alternative:

#include <stdio.h>
#include <cs50.h>

int main() {
    long credit;
    int arr[16];
    int number, i;

    do {
        credit = get_long("Number: ");
        number = snprintf(NULL, 0, "%lu", credit);
    }
    while (number < 13 || number > 16);

    for (i = number; i --> 0;) {
        arr[i] = credit % 10;
        credit /= 10;
    }

Also note that type long may be too small to accommodate numbers larger than 231. You should use long long or even unsigned long long for this or better use a string.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • Nicely gets to the heart of the matter. Takes care of negative numbers too unlike `log10(credit)`. – chux - Reinstate Monica Mar 13 '21 at 14:28
  • @chux-ReinstateMonica: too bad we cannot write `credit < 1000_000_000_000 || credit > 9999_9999_9999_9999` – chqrlie Mar 13 '21 at 14:32
  • Rumor is the next C version might use ` (back-quote) to allow 1000 (bq) 000 (bq) 000 (bq) 000. (comment auto format makes display weird.) For now, could use `1000LL * 1000 * 1000 * 1000`, minding the [LL](https://stackoverflow.com/a/40637622/2410359). – chux - Reinstate Monica Mar 13 '21 at 14:37
  • 2
    I see someone is having fun using the [--> operator](https://dev.to/somedood/demystifying-the-long-arrow-operator-4711). – chux - Reinstate Monica Mar 13 '21 at 14:43
  • @chux-ReinstateMonica: backquote is used in C++, for some reasons they seem to always go for the weirdest choices. – chqrlie Mar 13 '21 at 16:28
0

You may use a long to ascii function:

ltoa()

or the the well known and standard library function:

sprintf()

In the ascii buffer you can access each individual digit as a char digit.

If you need an array of integer instead, just subtract '0' to each char digit:

buffer[k] - '0'

Enrico Migliore
  • 201
  • 3
  • 9
0
  1. using % operator can be a good solution
while(input != 0) {
  digit = input%10;   // store this one
  input = input/10;
}
  
  1. alternatively, you can use 'sprintf' for converting the input to a string. Then, you can access every digit using the index of each digit.