0

So I'm prompting the user for a number, stored as a long int, then I want to do a for loop and iterate that long int, getting all the odd position numbers in a odd array and all the even position numbers in a even array. I'm trying to resolve the credit problem set from CS50

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

int main(void)
{
    long even[] = {}, odd[] = {};
    long cc = get_long("Number: ");
}

Basically I'm trying to get this:

If long cc = 12345678912345, then even should be even[7] = {1, 3, 5, 7, 9, 2, 4} and odd should be odd[7] = {2, 4, 6, 8, 1, 3, 5}

I don't really know how I would be able to iterate over long cc to then add the specific number I need into the array

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Welcome to SO. Please take the [tour], read [ask], and post a [mcve]. You need to show a bit of effort here if you expect someone to help you. thanks. – OldProgrammer Jun 20 '20 at 16:18

3 Answers3

0

You can use the modulo operator % to get each digit. The modulo operator basically gives you the remainder so if you do % 10 then it will give you the last digit. So 123 % 10 = 3. Then you divide the credit card number by 10 and repeat the process to get the next digit. Ex 123/10 = 12, then 12 % 10 is 2. If you make a counter variable to count how many times you've done the loop, that will give you the position. If you do position % 2, that will tell you if it is odd or even.

while (CC > 0)
    digit = CC % 10
    if (position % 2 == 1) //it's odd

    else //its even
    
    CC = CC/10  //to prepare for next iteration of the loop
    position = position + 1  //position starts at right side of CC number

One of the other proposed answers here determines if the digit's value is odd or even, but you're trying to determine if the digit is in an even or odd position. The other proposed answer uses an array, which is something you learn about later in the course and isn't what the class is trying to teach you now.

The solution I've suggested is in line with what has been covered by your class so far. Later you will learn about arrays and you can approach this problem differently. Later in the course, you need to do this same problem in Python and that time I took in the value as a string because you can treat the full credit card number as an array and then each digit is just an element in that array. creditcardnumberarray[0] would be the first digit for example. You would need to change each digit from a Char to a Int before doing your checksum. You can do this conversion by subtracting the digit char from the char '0'. So int digitAsInt = digitAsChar - '0'

rfii
  • 562
  • 3
  • 14
0

that's fairly easy, just allocate two array with how many numbers are in cc using something like

int temp = cc, i = 0;
while (temp > 0) {i++; cc /= 10;}

Then calculate ur stuff like this:

while (cc > 0) 
{
newNumber = cc/10; 
newDigit = CC % 10
if (newNumber % 2 == 0) /*newDigit is even*/
else /*newDigit is odd*/
cc = newNumber;
}
rfii
  • 562
  • 3
  • 14
Massaynus
  • 322
  • 2
  • 9
0

The main problem you have is that arrays in C have a fixed size, set when they are created, and that size cannot be changed. So there's no way to "append" to an array.

The usual way to deal with that is to create an array with a maximum size (or capacity) and track the "in use" part of the array with a separate variable (often called 'size'). At any given time, the array elements from 0 to size-1 are "in use" and valid, while those from size to capacity-1 are "free" and might contain garbage values that will be ignored.

Since the array and the size variable are so intimately connected, it is common to combine them into a struct to make things easier to manage:

#define MAX_ARRAY_SIZE 100
struct long_array {
    size_t size;
    long data[MAX_ARRAY_SIZE];
};

Now you can initialize an empty array with struct long_array odd = { 0 }; and you could then append to this array with

if (odd.size == MAX_ARRAY_SIZE) {
    /* always check for errors or unexpected situations! */
    fprintf(stderr, "array overflow\n");
    exit(1); }
odd.data[odd.size++] = new_value;

of course, this will end up allocating the maximum amount for every array, so the limit needs to be fairly small. You can make this more flexible by allocating the array on the heap and storing the capacity in the array as well as the size:

struct long_array {
    size_t size, capacity;
    long *data;
};

but this requires more management to track when the array needs to be resized or freed.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226