Thread: Luhn Algorithm Program ?: Double digit int in single arr index

  1. #1
    Registered User
    Join Date
    Sep 2019
    Posts
    2

    Lightbulb Luhn Algorithm Program ?: Double digit int in single arr index

    Hello all,

    I am new to C and am doing the cs50 course from edX and I was tasked with creating a program to verify the checksum of a credit card using Luhn's algorithm.

    Currently, I am trying to figure out how the heck to store a double digit integer in a single index of an array. Is that possible, or do I need to do something complex like an object array or something?


    Also, you will see that I am using strings, or character arrays to store the user's credit card number input...
    I realize this might seem stupid, but I just wanted an easy way to determine the length of the array, and strlen() makes that pretty easy.

    I contemplated doing something hackish like storing the number's real value in ascii ... then converting later currently that's my best option but I'm looking for something more legit.

    Any help at all is greatly appreciated!

    For loop I am stuck on completing:

    Code:
     //init array to hold the doubled integers from eOther1    char deOther1[9];
        //limiter for forloop
        int length3 = strlen(eOther1);
        //double every number in eOther1 and store it in deOther2
        for(int t = 0; t < length3; t++){
            printf("Here is eOther1: %c \n", eOther1[t]);
    //      printf("Here is deOther1: %c \n", deOther1[t]);
            //performing an operation on a character requires you to set that character back to its integral value
            int eOther1Holder = ((eOther1[t]) - '0');
            printf("deOther1Holder: %i \n", eOther1Holder);
            int deOther1Sum = (eOther1Holder * 2);
            printf("here is deOther1Sum: %i \n", deOther1Sum);
            //insert the sum of the double of eOther1 into its array, deOther1
            deOther1[t] = deOther1Sum;
        }
            printf("Here is deOther1: %s \n", deOther1);

    All my code so far: luhn-alg.c
    Code:
    #include <stdio.h>#include <string.h>
    #include <cs50.h>
    #define MAX_LIMIT 19
    
    
    int main () {
        char str[MAX_LIMIT]; 
        int length;
        //get input from user & store it in string/array
        printf("enter credit #");
        fgets(str, MAX_LIMIT, stdin);
    
    
        length = strlen(str);
        length -= 1;
        //validation for # of digits
        if(length < 13 || length > 16){
            printf("Invalid\n %d \n", length);
            return 0;
        } else if(length == 14) {
            printf("invalid\n %d \n", length);
            return 0;
        }
        //create the array to copy str into
        //char charArr[length + 1];
        //strcpy(charArr, str);
        
        printf("Length of string - %s  is: %d \n", str, length);   
        //  printf("%c", charArr[2]);
    
    
        //  start with the second to last digit of input and store every other number in eOther1[]
        ////make array eOther1
        char eOther1[9];
        //eOther1 indexer
        int i = 0;
        for(int length2 = (length - 2); length2 >= 0; length2 = (length2 - 2)){
            ////test
            printf("1str[length2]:%c ---", str[length2]);
            printf("1length2:%i --", length2);
            printf("1i:%i ", i);
            ////copy the char from array str[length2] and insert into array eOther[i]
            eOther1[i] = str[length2];
            ////test
            printf("1eOther1[i]:%c \n", eOther1[i]);
            i++;
        }
        printf("1Here is eOther1: %s \n", eOther1);
        //init array to hold the doubled integers from eOther1
        char deOther1[9];
        //limiter for forloop
        int length3 = strlen(eOther1);
        //double every number in eOther1 and store it in deOther2
        for(int t = 0; t < length3; t++){
            printf("Here is eOther1: %c \n", eOther1[t]);
    //      printf("Here is deOther1: %c \n", deOther1[t]);
            //performing an operation on a character requires you to set that character back to its integral value
            int eOther1Holder = ((eOther1[t]) - '0');
            printf("deOther1Holder: %i \n", eOther1Holder);
            int deOther1Sum = (eOther1Holder * 2);
            printf("here is deOther1Sum: %i \n", deOther1Sum);
            //insert the sum of the double of eOther1 into its array, deOther1
            deOther1[t] = deOther1Sum;
        }
            printf("Here is deOther1: %s \n", deOther1);
        
        
        // start with the last digit and store every other number in eOther2
        char eOther2[9];
        //eOther2 indexer
        int j = 0;
        for(int length1 = (length - 1); length1 >= 0; length1 = (length1 - 2)){
            ////test
            printf("2str[length1]:%c ---", str[length1]);
            printf("2length1:%i --", length1);
            printf("2i:%i ", j);
            //copy the char from array str[length2] and insert into array eOther[i]
            eOther2[j] = str[length1];
            ////test
            printf("2eOther2[j]:%c \n", eOther2[j]);
            j++;  
        }
        printf("2Here is eOther2: %s \n", eOther2);
        return 0;
    }
    
    
    
    
    
    
    
    
    //add all digits together in deOther1 and store in total
    //add total to digits in eOther2
    //add all of eOther2 together, store in gTotal
    //if the last number of gTotal is 0, input number is legit

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,530
    > //insert the sum of the double of eOther1 into its array, deOther1
    > deOther1[t] = deOther1Sum;
    1. You don't need to do this.
    2. If you really wanted to do this, you would make it an array of integers.

    Luhn algorithm - Wikipedia

    You basically end up with two partial sums
    oddSum += (str[i] - '0') * 2; // for odd i
    evenSum += (str[i] - '0'); // for even i


    Copying fragments of str to other strings, to just do calculations on them seems like an unnecessary step.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2019
    Posts
    2
    Salem,

    thanks a lot for your reply.

    & you are definitely right... when I realized how stupid it was that I was for using a string I already wrote too much code and just decided to roll with it.

    I think I am going to give in and re-write it with an integer array to solve my problem.

    Do you still think deOther1[t] = deOther1Sum; is stupid if the array was in integral form?


  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,530
    What's your actual homework say?

    If you need to show your calculation of all the intermediate sums to your tutor / print them out, then an int array is fine.

    But if all you're after is a final yes/no validation, then storing anything other than the sums is a waste of effort IMO,
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    650
    Judging by what I read in that wiki link this is what I thought up just now:
    Code:
    _Bool luhn_algorithum( unsigned int ccn ) {
      unsigned int chk_dgt = ccn % 10, tmp, sum = 0;
      _Bool dbl = 1;
      while ( ccn > 10 ) {
        ccn /= 10;
        tmp = ccn % 10;
        if ( dbl ) {
          tmp *= 2;
          if ( tmp > 10 ) tmp -= 9;
        }
        dbl = !dbl;
        sum += tmp;
      }
      return ((sum % 10) == 0);
    }
    Edit: Realised I forgot to add the chk_dgt:
    Code:
    _Bool luhn_algorithum( unsigned int ccn ) {
      unsigned int tmp, sum = ccn % 10;
      _Bool dbl = 1;
      while ( ccn > 10 ) {
        ccn /= 10;
        tmp = ccn % 10;
        if ( dbl ) {
          tmp *= 2;
          if ( tmp > 10 ) tmp -= 9;
        }
        dbl = !dbl;
        sum += tmp;
      }
      return ((sum % 10) == 0);
    }
    Last edited by awsdert; 09-27-2019 at 05:13 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program dosen't work for double digit input
    By Obvious Guy in forum C Programming
    Replies: 10
    Last Post: 05-12-2018, 10:58 AM
  2. Replies: 1
    Last Post: 03-06-2017, 01:08 AM
  3. How to display single digit int as four digit C++ Programming
    By Sloganathan in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2012, 11:30 AM
  4. C: Credit Card Number Validator - Luhn Algorithm
    By Lioneyexp in forum C Programming
    Replies: 1
    Last Post: 02-07-2012, 07:06 PM
  5. Replies: 13
    Last Post: 11-13-2009, 08:47 AM

Tags for this Thread