Recursive Algorithm for CHKSUM Calculation: div and mod Functions | n >= 0

  • Thread starter Thread starter teng125
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around the development of a recursive algorithm for calculating the checksum of an integer, defined as the sum of its digits. Participants explore the requirements of the algorithm, the use of division and modulus functions, and seek clarification on the problem statement.

Discussion Character

  • Exploratory
  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant clarifies that CHKSUM(n) is intended to find the sum of the digits of n, using the example of n = 198.
  • Another participant expresses confusion about whether the algorithm should sum all values from 1 to 198 instead of the digits of 198.
  • There is a request for an explanation of the necessity of using the div and mod functions in the algorithm.
  • A participant provides the checksum of 198 as 1 + 9 + 8 = 18 and hints at how to use modulus to extract digits.

Areas of Agreement / Disagreement

Participants generally agree on the interpretation of CHKSUM(n) as the sum of the digits of n, but there is confusion regarding the algorithm's requirements and the use of specific functions. The discussion remains unresolved regarding the exact implementation details of the recursive algorithm.

Contextual Notes

Some participants express uncertainty about the problem statement and the role of the div and mod functions, indicating a need for clearer definitions and assumptions.

Who May Find This Useful

This discussion may be useful for individuals interested in algorithm development, particularly those working on recursive functions and digit manipulation in programming.

teng125
Messages
416
Reaction score
0
pre n >= 0
post checksum(n) = CHKSUM(n)
reads n
changes -
mem -

Remark: Let n be an integer number. Furthermore, CHKSUM(n) = “sum of digits of n”. checksum is the name of the module to be developed.

Develop a recursive algorithm for the above specification. Assume and use functions
div function and mod function.

can smby pls show me how to write this algorithm

thanx
 
Physics news on Phys.org
Well first off, you need to know exactly what your algorithm is supposed to do. Looks like your CHKSUM(n) is supposed to find the sum of the digits of n. So, take an example. If you have n = 198...then checksum should return what value?

After that it should be pretty straightforward, if you know what value it's supposed to be.
 
does it means sum up all the values from 1 to 198 if n=198??
actually i don'tknow what does the algorithm above means at all

why does it needs to use functions div function and mod function.

if possible,pls explain to me

thanx
 
No it does not mean that...it means to sum up the "digits" that make up the number 198. So the number 198 is made up of 3 "digits"...which numbers and what is their sum?
 
pls show me how to write this recursive algorithm.I need it very much,pls

thanx
 
We're not going to do your problem for you.

The checksum of 198 should be 1 + 9 + 8 = 18.
Here is another hint: 198 mod 10 = 8.
(198 - 198 mod 10) / 10 = 19
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
7K
  • · Replies 16 ·
Replies
16
Views
3K
Replies
2
Views
8K
  • · Replies 86 ·
3
Replies
86
Views
24K
  • · Replies 60 ·
3
Replies
60
Views
13K
  • · Replies 52 ·
2
Replies
52
Views
13K
  • · Replies 67 ·
3
Replies
67
Views
17K