Why isn't my algorithm code working?

  • Thread starter Thread starter SMA_01
  • Start date Start date
  • Tags Tags
    Algorithm Code
Click For Summary

Discussion Overview

The discussion revolves around troubleshooting a program that validates credit card numbers using Luhn's algorithm. Participants are examining the code implementation, identifying potential issues, and clarifying the algorithm's steps.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes their implementation of Luhn's algorithm and expresses difficulty with the final function of their code.
  • Another participant points out the importance of using code formatting for readability and suggests checking for syntax errors in the code.
  • Concerns are raised about the logic in the functions doubleEvenSum and sumOddDigits, particularly regarding the different starting indices used in each function.
  • A participant clarifies their approach to doubling every second digit and starting positions for the calculations, but still encounters issues with valid numbers being deemed invalid.
  • Further suggestions include ensuring the correct method for converting character digits to integers and using debugging techniques to trace the code execution.

Areas of Agreement / Disagreement

Participants express differing views on specific aspects of the code and its logic, indicating that the discussion remains unresolved regarding the exact cause of the issues in the implementation.

Contextual Notes

There are potential limitations in the code regarding the conversion of character digits to integers, and the starting indices for processing digits in the Luhn algorithm are not consistent, which may affect the outcomes.

SMA_01
Messages
215
Reaction score
0
I have to write a program validating credit card numbers using Luhn's algorithm. I am having trouble getting my last function to work. For a credit card number I'm supposed to double every second digit from right to left, and sum them (note double digits, like 10=1+0), and then add the rest of the digits together, and total both sums. After that, I make sure it's divisible by 10. Is there anything wrong with my code? Thank you.


bool isValid(string creditCardNumber)
{
int totalSum;
int evenSum;
int oddSum;


doubleEvenSum(creditCardNumber);

sumOddDigits(creditCardNumber);

evenSum = doubleEvenSum(creditCardNumber);
oddSum = sumOddDigits(creditCardNumber);

totalSum = evenSum + oddSum;

modTen(totalSum);

if (modTen(totalSum))
return true;
else return false;
}


int doubleEvenSum(string creditCardNumber)
{
int evenSum;
int countLength;
char charVal;
int intVal;
int doubleIntVal;


countLength = creditCardNumber.length()-2;
evenSum = 0;
while(countLength>=0)
{
charVal = creditCardNumber.at(countLength);

intVal = charVal - 0;

doubleIntVal = 2 * intVal;

if (doubleIntVal > 9)
{
doubleIntVal = doubleIntVal - 9;
}

evenSum = evenSum + doubleIntVal;
countLength = countLength - 2;
}
return evenSum;
}


int sumOddDigits(string creditCardNumber)
{
int oddSum;
int countLength;
char charVal;
int intVal;


countLength = creditCardNumber.length()-1;
oddSum = 0;
while (countLength>=0)
{
charVal = creditCardNumber.at(countLength);
intVal = charVal - 0;
oddSum = oddSum + intVal;
countLength = countLength - 2;
}
return oddSum;
}


bool modTen(int totalSum)
{
if (totalSum % 10 == 0)
return true;
else return false;
}
 
Physics news on Phys.org
First off, when you post code here, put a [ code ] tag at the top and a [ /code ] tag at the bottom (both without extra spaces that I showed). This preserves any indentation you have used, which makes your code easier to read.
Second, USE INDENTATION! The compiler doesn't care if you use indentation, but if you use indentation appropriately, it's easier for human readers to follow your code.



SMA_01 said:
I have to write a program validating credit card numbers using Luhn's algorithm. I am having trouble getting my last function to work. For a credit card number I'm supposed to double every second digit from right to left, and sum them (note double digits, like 10=1+0), and then add the rest of the digits together, and total both sums. After that, I make sure it's divisible by 10. Is there anything wrong with my code? Thank you.
Did you try compiling your code? If there are syntax errors in your code, the compiler will give a list of errors. The compiler won't tell you about errors in your logic, though.

Look for my comments throughout your code.
SMA_01 said:
Code:
bool isValid(string creditCardNumber)
{
   int totalSum;
   int evenSum;
   int oddSum;


   doubleEvenSum(creditCardNumber); // Why is this here? 

   sumOddDigits(creditCardNumber); // Ditto. Both functions are supposed to return a value.
                                                // Calling them like this causes the return value to be discarded.

   evenSum = doubleEvenSum(creditCardNumber);
   oddSum = sumOddDigits(creditCardNumber);

   totalSum = evenSum + oddSum;

   modTen(totalSum); // You should not call this function like this, because it doesn't do anything useful.

   if (modTen(totalSum))
      return true;
   else return false;
}


int doubleEvenSum(string creditCardNumber)
{
   int evenSum;
   int countLength;
   char charVal;
   int intVal;
   int doubleIntVal;


   countLength = creditCardNumber.length()-2;
   evenSum = 0;
   while(countLength>=0)
   {
      charVal = creditCardNumber.at(countLength);

      intVal = charVal - 0;

      doubleIntVal = 2 * intVal;

      if (doubleIntVal > 9)
      {
         doubleIntVal = doubleIntVal - 9;
      }

      evenSum = evenSum + doubleIntVal;
      countLength = countLength - 2;
   }
   return evenSum;
}


int sumOddDigits(string creditCardNumber)
{
   int oddSum;
   int countLength;
   char charVal;
   int intVal;


   countLength = creditCardNumber.length()-1;
   oddSum = 0;
   while (countLength>=0)
   {
      charVal = creditCardNumber.at(countLength);
      intVal = charVal - 0;
      oddSum = oddSum + intVal;
      countLength = countLength - 2;
   }
   return oddSum;
}


bool modTen(int totalSum)
{
   if (totalSum % 10 == 0)
      return true;
   else return false;
}

In doubleEvenSum, you do this:
Code:
countLength = creditCardNumber.length()-2;

In sumOddDigits, you do this:
Code:
countLength = creditCardNumber.length()-1;

Why are they different? Are these two functions supposed to do the same things except that one works on the digits in the even places, and the other works on the digits in the odd places? If so, why did you subtract 2 in one function and subtract 1 in the other? A comment would have helped explain your logic. Also, if both functions are intended to do similar things, you should give them names that are more similar.
 
Sorry, I did use indentation, I guess I should have fixed it beforehand.

You are right about calling those functions, I fixed that thanks.

In doubleEvenSum, you doulble every second digits from right to left. and then add them. For example, for a number of length 16, the first position you would begin is 14:

4 1 3 0 1 3 2 5 6 4 7 9 8 5 3 2
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

And for odd it you would start at first place to the right, so at position length-1.

My problem, is that even if I enter a valid number, the calculations deem it invalid, so I am not sure what is off with my code (I tried it again after I fixed the part you mentioned).
 
You haven't said what language you're using - I suspect it's Java.

In doubleEvenSum and in sumOddDigits, you have some code that I think is incorrect.
Code:
intVal = charVal - 0;

If you want to convert a digit character to a number, subtract the character '0', not the number 0.

If that doesn't fix things, please give me an example of what doubleEvenSum should do to a number. Your explanation wasn't enough to give me an idea of the algorithm you're trying to implement.

Also, if there is a debugger available, it's a good investment of your time to learn how to use it. Otherwise, you can fall back to the way things were done in the old days - adding temporary print statements through your code to print the values of variables. When your code is running as expected, you can remove the print statements that aren't needed any more.
 
Got it working, thank you!
 

Similar threads

  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 14 ·
Replies
14
Views
5K
  • · Replies 17 ·
Replies
17
Views
6K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K