Pascal - sum of digits in binary/hexadecimal

Click For Summary
SUMMARY

The forum discussion centers on a Pascal programming task that requires converting a decimal integer into a specified numeral system (binary, decimal, or hexadecimal) and calculating the sum of its digits in that system. The user has implemented two functions: one for conversion and another for summing the digits. However, the conversion function outputs a string, while the summation function expects an integer, leading to a type mismatch. The user seeks a solution to perform the conversion without using strings.

PREREQUISITES
  • Understanding of Pascal programming language and syntax
  • Knowledge of numeral systems (binary, decimal, hexadecimal)
  • Familiarity with functions and procedures in Pascal
  • Basic understanding of data types, specifically longint and string
NEXT STEPS
  • Research how to convert integers to different numeral systems in Pascal without using strings
  • Learn about data type conversions in Pascal, particularly between integers and strings
  • Explore error handling in Pascal to manage invalid numeral system inputs
  • Investigate proper code indentation and formatting techniques to improve code readability
USEFUL FOR

Students and developers working with Pascal who need to implement numeral system conversions and digit summation, as well as anyone looking to improve their coding practices in Pascal.

tawi
Messages
33
Reaction score
0

Homework Statement


Read two integers. First one tells you the type of your numeral system (binary, decimal, hexadecimal) the second one will be your number in decimal. Using functions or procedures I need to convert the number into the required system and then count the sum of its digits in that system. For example you are given the integers: 16, 17.
You convert the number 17 into hexadecimal, that is 11 and then you add up its digits and write the result. So your output in this case is 2.

Homework Equations


I´ve written two functions. One to convert the number and second to make the sum. The problem is that the first function output is a string whereas the second is an integer. That means I need to come up with a solution to convert the numbers without using string at all. And I have no idea at all how to do that.

The Attempt at a Solution


Here is what I´ve got so far. The first function needs to be replaced with something else.

Code:
program  abcd;
var  n,x,system: integer;

function convert(sys:byte;Nr:longint):string;

const Letters:string='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';

var temp:string;
  modulo:longint;

begin
  if sys>length(Letters) then
  writeln('system too big');

  temp:='';
  if Nr=0 then
  convert:='0'

  else begin

  modulo:= Nr;
  while modulo > sys - 1 do begin
  temp:=Letters[1+modulo mod sys]+temp;
  modulo:=modulo div sys  ;

  end;
  end;
  convert:=Letters[1+modulo]+temp;
  end;

function digitSum(number: longint): integer;  

var digit, torso, sumt: integer; 
begin
  sum := 0;
  torso := number;
  repeat
  digit := torso mod 10;
  sum := sum+ digit;
  torso := torso div 10;
  until torso = 0;
  digitSum := soucet;
end;

begin

  writeln(digitSum(?));

end.
 
Last edited:
Physics news on Phys.org
tawi said:

Homework Statement


Read two integers. First one tells you the type of your numeral system (binary, decimal, hexadecimal) the second one will be your number in decimal. Using functions or procedures I need to convert the number into the required system and then count the sum of its digits in that system. For example you are given the integers: 16, 17.
You convert the number 17 into hexadecimal, that is 11 and then you add up its digits and write the result. So your output in this case is 2.
Is the base limited to binary, decimal, or hex, or can it be some other base?

Also, what's the largest possible number your program should be able to convert? One parameter of your convert() function is type longint, which has a max. value of
2147483647.
tawi said:

Homework Equations


I´ve written two functions. One to convert the number and second to make the sum. The problem is that the first function output is a string whereas the second is an integer. That means I need to come up with a solution to convert the numbers without using string at all. And I have no idea at all how to do that.

The Attempt at a Solution


Here is what I´ve got so far. The first function needs to be replaced with something else.
Indenting your code would make it easier to follow the structure to be able to understand what you're doing.
tawi said:
Code:
program  abcd;
var  n,x,system: integer;

function convert(sys:byte;Nr:longint):string;

const Letters:string='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';

var temp:string;
  modulo:longint;

begin
  if sys>length(Letters) then
  writeln('system too big');

  temp:='';
  if Nr=0 then
  convert:='0'

  else begin

  modulo:= Nr;
  while modulo > sys - 1 do begin
  temp:=Letters[1+modulo mod sys]+temp;
  modulo:=modulo div sys  ;

  end;
  end;
  convert:=Letters[1+modulo]+temp;
  end;

function digitSum(number: longint): integer; 

var digit, torso, sumt: integer;
begin
  sum := 0;
  torso := number;
  repeat
  digit := torso mod 10;
  sum := sum+ digit;
  torso := torso div 10;
  until torso = 0;
  digitSum := soucet;
end;

begin

  writeln(digitSum(?));

end.

Here is your convert function, with indentation added by me.
Code:
function convert(sys:byte;Nr:longint):string;

const Letters:string='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';

var temp:string;
  modulo:longint;

begin
  if sys>length(Letters) then
     writeln('system too big');

     temp:='';
     if Nr=0 then
         convert:='0'
     else begin
        modulo:= Nr;
        while modulo > sys - 1 do begin
           temp:=Letters[1+modulo mod sys]+temp;
           modulo:=modulo div sys  ;
        end;
     end;
     convert:=Letters[1+modulo]+temp;
  end;
You are missing an end statement. Indenting your code properly would make it easier to catch errors like this.
 

Similar threads

Replies
3
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 26 ·
Replies
26
Views
8K
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K