Pascal - sum of digits in binary/hexadecimal

In summary: If not, you need to write your own functions to convert integers to strings and strings to integers.In summary, the conversation is about converting numbers from one numeral system to another and then finding the sum of the digits in the converted number. The code provided includes two functions, one for conversion and one for finding the sum, but the first function outputs a string while the second outputs an integer. The problem is to find a solution to convert the numbers without using strings. It is suggested to use string functions in Pascal or to write custom functions for converting between integers and strings.
  • #1
tawi
33
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
  • #2
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.
 
  • #3
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.

Does your Pascal have string functions that you are allowed to use, including convert integer to string and string to integer?

htîtp://www.baskent.edu.tr/~tkaracay/etudio/ders/prg/pascal/PasHTM1/pas/pasl1007.html
 

1. How is the sum of digits calculated in binary and hexadecimal numbers in Pascal?

The sum of digits in binary is calculated by converting the number to its decimal form, adding all the digits, and then converting back to binary. In hexadecimal, the sum of digits is calculated similarly by converting to decimal, adding the digits, and converting back to hexadecimal.

2. Can the sum of digits be calculated without converting to decimal in Pascal?

Yes, there are algorithms and procedures in Pascal specifically designed to calculate the sum of digits in binary and hexadecimal without converting to decimal. These methods involve using bitwise operations and looping through the digits of the number.

3. Is the sum of digits the same for a number in both binary and hexadecimal in Pascal?

No, the sum of digits will be different in binary and hexadecimal since they have different bases and number systems. For example, the binary number 1111 would have a sum of 4, while the hexadecimal number F would have a sum of 15.

4. What is the maximum sum of digits in binary and hexadecimal numbers in Pascal?

The maximum sum of digits in binary is the number of digits in the binary number, while the maximum sum of digits in hexadecimal is 15 (since it is a base 16 number system). For larger numbers, the sum of digits may be calculated in multiple steps.

5. How can I use the sum of digits in binary and hexadecimal to solve problems in Pascal?

The sum of digits in binary and hexadecimal can be useful in various applications, such as error detection and correction, data compression, and cryptography. It can also be used to convert between number systems and perform arithmetic operations. Understanding how to calculate the sum of digits in these number systems is important for solving these types of problems in Pascal.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
26
Views
7K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
998
  • Engineering and Comp Sci Homework Help
Replies
1
Views
5K
  • Programming and Computer Science
Replies
4
Views
2K
Back
Top