Why isn't my algorithm code working?

In summary, the code posted is meant to validate credit card numbers using Luhn's algorithm. The program doubles every second digit from right to left, adds them, and then adds the rest of the digits together. The total of both sums is then checked to see if it is divisible by 10. The issue with the code was that the character '0' should have been subtracted instead of the number 0 when converting a digit character to a number. Additionally, using a debugger or print statements can be helpful in troubleshooting code.
  • #1
SMA_01
218
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
  • #2
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.
 
  • #3
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).
 
  • #4
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.
 
  • #5
Got it working, thank you!
 

1. Why does my algorithm code keep giving me errors?

There could be several reasons for this. It could be a syntax error, a logic error, or an issue with the input data. Check your code for any typos or missing semicolons, and make sure your logic is correct. Also, check the input data to ensure it is in the correct format.

2. How can I debug my algorithm code?

There are several ways to debug your code. You can use a debugger tool, add print statements to track the flow of the code, or use a visualizer to see the values of variables at each step. It may also be helpful to get a second pair of eyes to review your code for any potential errors.

3. Why is my algorithm code running slowly?

There are a few potential reasons for this. It could be due to inefficient or redundant code, a large amount of data to process, or using a less efficient algorithm. Try optimizing your code and data, and consider using a more efficient algorithm if applicable.

4. How can I improve the performance of my algorithm code?

To improve the performance of your code, you can try using data structures and algorithms that are known to have better time and space complexity. You can also look into parallelizing your code or using caching techniques. Additionally, make sure to thoroughly test and optimize your code to eliminate any bottlenecks.

5. My algorithm code works on some inputs but not others. Why?

This could be due to edge cases that were not accounted for in your code. Make sure to test your code with various inputs, including extreme cases, to ensure it works correctly. It could also be an issue with the input data, so double check that it is in the correct format.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
14
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
3K
  • Programming and Computer Science
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
0
Views
2K
  • Programming and Computer Science
3
Replies
73
Views
4K
Back
Top