简体   繁体   中英

Cross sum calculation, Can anyone explain the code please?

i'm going to learn C++ at the very beginning and struggling with some challenges from university. The task was to calculate the cross sum and to use modulo and divided operators only.

I have the solution below, but do not understand the mechanism.. Maybe anyone could provide some advice, or help to understand, whats going on.

I tried to figure out how the modulo operator works, and go through the code step by step, but still dont understand why theres need of the while statement.

#include <iostream>
using namespace std;

int  main()
{

  int input;
  int crossSum = 0;

  cout << "Number please: " << endl;
  cin >> input;

  while (input != 0) 
  {
    crossSum = crossSum + input % 10;
    input = input / 10;
  }

  cout << crossSum << endl;

  system ("pause");
  return 0;
}

Lets say my input number is 27. cross sum is 9

frist step: crossSum = crossSum + (input'27' % 10 ) // 0 + (modulo10 of 27 = 7) = 7

next step: input = input '27' / 10 // (27 / 10) = 2.7; Integer=2 ? input = input '27' / 10 // (27 / 10) = 2.7; Integer=2 ?

how to bring them together, and what does the while loop do? Thanks for help.

Just in case you're not sure:

The modulo operator , or % , divides the number to its left by the number to its right (its operands ), and gives the remainder. As an example, 49 % 5 = 4 .

Anyway,

The while loop takes a conditional statement, and will do the code in the following brackets over and over until that statement becomes false. In your code, while the input is not equal to zero , do some stuff.

To bring all of this together, every loop, you modulo your input by 10 - this will always return the last digit of a given Base-10 number. You add this onto a running sum (crossSum), and then divide the number by 10, basically moving the digits over by one space. The while loop makes sure that you do this until the number is done - for example, if the input is 104323959134, it has to loop 12 times until it's got all of the digits.

It seems that you are adding the digits present in the input number. Let's go through it with the help of an example, let input = 154.

Iteration1
crossSum= 0 + 154%10 = 4 
Input = 154/10= 15 

Iteration2 
crossSum = 4 + 15%10 = 9 
Input = 15/10 = 1 

Iteration3 
crossSum = 9 + 1%10 = 10 
Input = 1/10 = 0 

Now the while loop will not be executed since input = 0. Keep a habit of dry running through your code.

#include <iostream>
using namespace std;

int  main()
{

  int input;
  int crossSum = 0;

  cout << "Number please: " << endl;
  cin >> input;

  while (input != 0)  // while your input is not 0
  {
    // means that when you have 123 and want to have the crosssum
    // you first add 3 then 2 then 1 
    // mod 10 just gives you the most right digit
    // example: 123 % 10 => 3
    //          541 % 10 => 1 etc.

    // crosssum means: crosssum(123) = 1 + 2 + 3 
    // so you need a mechanism to extract each digit
    crossSum = crossSum + input % 10; // you add the LAST digit to your crosssum


    // to make the number smaller (or move all digits one to the right)
    // you divide it by 10 at some point the number will be 0 and the iteration 
    // will stop then.
    input = input / 10;
  }

  cout << crossSum << endl;

  system ("pause");
  return 0;
}

but still dont understand why theres need of the while statement

Actually, there isn't need (in literal sense) for, number of digits being representable is limited.

Lets consider signed char instead of int : maximum number gets 127 then (8-bit char provided). So you could do:

crossSum = number % 10 + number / 10 % 10 + number / 100;

Same for int, but as that number is larger, you'd need 10 summands (32-bit int provided)... And: You'd always calculate the 10 summands, even for number 1, where actually all nine upper summands are equal to 0 anyway.

The while loop simplifies the matter: As long as there are yet digits left, the number is unequal to 0, so you continue, and as soon as no digits are left (number == 0), you stop iteration:

123 -> 12 -> 1 -> 0 // iteration stops, even if data type is able
  ^     ^    ^      // to store more digits

Marked digits form the summands for the cross sum.

Be aware that integer division always drops the decimal places, wheras modulo operation delivers the remainder, just as in your very first math lessons in school:

7 / 3 = 2, remainder 1

So % 10 will give you exactly the last (base 10) digit (the least significant one), and / 10 will drop this digit afterwards, to go on with next digit in next iteration.

You even could calculate the cross sum according to different bases (eg 16; base 2 would give you the number of 1-bits in binary representation).

Loop is used when we want to repeat some statements until a condition is true. In your program, the following statements are repeated till the input becomes 0 .

  • Retrieve the last digit of the input . ( int digit = input % 10; )
  • Add the above retrieved digit to crosssum . ( crosssum = crosssum + digit; )
  • Remove the last digit from the input . ( input = input / 10; )

The above statements are repeated till the input becomes zero by repeatedly dividing it by 10. And all the digits in input are added to crosssum .

Hence, the variable crosssum is the sum of the digits of the variable input .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM