1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Pascal - sum of digits in binary/hexadecimal

  1. Nov 8, 2015 #1
    1. The problem statement, all variables and given/known data
    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.

    2. Relevant 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.
    3. The attempt at a solution
    Here is what I´ve got so far. The first function needs to be replaced with something else.

    Code (Text):

    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: Nov 8, 2015
  2. jcsd
  3. Nov 8, 2015 #2

    Mark44

    Staff: Mentor

    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.
    Indenting your code would make it easier to follow the structure to be able to understand what you're doing.
    Here is your convert function, with indentation added by me.
    Code (Pascal):

    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.
     
  4. Nov 8, 2015 #3

    NascentOxygen

    User Avatar

    Staff: Mentor

    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
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted