How to print value in binary using shift operator in ada as we do in c/c++ below is my code in c which is good but same implementation in ada is bad. please help me
int num=10;
for( i=31;i >=0; i--)
printf(" %d",num>>i &1) // it s good but how to implement in ada i tired same in ada but its bad?
In ada
for i in 1..31 loop
put(num>>i &1); -- error msg is missing ")" plz help
end loop;
The way to write this loop in Ada would be something like this. I'm assuming Num is an Integer.
declare
use Interfaces;
function To_Unsigned_32 is new
Unchecked_Conversion (Integer, Unsigned_32);
N : Unsigned_32 := To_Unsigned_32 (Num);
Bit : Unsigned_32;
begin
for I in reverse 0 .. 31 loop
Bit := Shift_Right (N, I) and 1;
Ada.Text_IO.Put (Unsigned_32'Image (Bit));
end loop;
end;
Unsigned_32 and Shift_Right are defined in Interfaces. I'm taking advantage of the fact that your C code output a space before each bit; that happens to be what Unsigned_32'Image does--an extra space is included for nonnegative numbers. If you don't want that space, you could use Ada.Strings.Fixed.Trim to get rid of it.
Perhaps:
with Ada.Text_IO.Integer_IO;
procedure Foo is
package IO is new Ada.Text_IO.Integer_IO (Integer);
begin
IO.Put (42232, Base => 2);
end Foo;
You can write a function (maybe cleaner)
function binary (e:natural) return string is
(if e=0 then "" else binary (e/2) & (if e mod 2=0 then '0' else '1'));
Related
I have a specific problem that I got some issues figuring out. I want to create an operator in Ada that can divide a float value with a character and then return as an integer.
I understand that the operator should be a function with "/" and some kind of type conversion from float value and a character to an integer. But how would the return value look like and which rounding of the float value would be appropriate?
update.
Let's say I would like to put in the float value -21.8 and the character '2'. The answer should be -11. I created a subprogram for this but I feel like the solution could be something more simple.
function "/"(Float_Val : in Float;
Ch : in Character) return integer is
begin
if Float_Val < Float(0) then return
(Integer(Float'Rounding(Float_Val)) - (Character'Pos(Ch) - (Character'Pos('0'))) + 1) / (Character'Pos(Ch) - (Character'Pos('0')));
else return
(Integer(Float'Rounding(Float_Val)) + (Character'Pos(Ch) - (Character'Pos('0'))) - 1) / (Character'Pos(Ch) - (Character'Pos('0')));
end if;
end "/";
and "/" is called in my main program by
Put(Float_Val / Ch, Width => 0);
I wouldn’t want to call this weird function "/", but if you must ... the skeleton body would be
function "/" (L : Float; R : Character) return Integer is
begin
-- this is where the magic happens
end "/";
I don’t see why you’d need to round L.
Without a bit more explanation as to what algorithm you want to use, that’s all I can say.
Update:
This seems to work quite well:
function "/" (L : Float; R : Character) return Integer
with Pre => R in '1' .. '9'
is
begin
return Integer (L) / (Character'Pos (R) - Character'Pos ('0'));
end "/";
See ARM 4.6(33).
To answer your question about rounding, and to fill in the "magic" in Simon's answer, you must explain better what you want to compute. For example, what should the result of 4.567 / "A" be? And what would do you do to compute it by hand?
Sounds to me like you want to translate something from a poor type language to the rich type language Ada.
If you like to do this the Ada way it will require some reverse engineering to find out what the types ‘integer‘, ‘float’, and ‘character‘ really means. Then I guess an explaining name for ‘/‘ will emerge.
I am a beginner and using pascal and i am trying this problem that i attach. What should i do next? I am trying to make this program work by showing the "ayam" Function. But i don't know how to code the rest.
function ayam(a, b:integer) :
integer;
var
m: integer;
begin
readln(a) ;
readln(b) ;
readln(m) ;
begin
if (b=0) then
ayam:= 1 else
ayam:= a * ayam(a, b-1) mod m;
end;
writeln(ayam);
end;
The first thing I should say is that when a question is to do with coursework, as is obviously the case here, there is an unwritten convention that we do not write an answer which simply does your programming task for you. So in the following, I have not commented on whether the code inside your ayam function actually correctly implements what is required - that is for you to do.
Secondly, you did not say which Pascal implementation you are using. I have written the code of this answer in Lazarus, the IDE for FreePascal (aka FPC), which is the best of the freeware Pascal implementations currently available.
The first thing to do with your code is to re-format it the way it should be formatted,
using indentation of statements to reflect the structure of the steps you are
trying to code. Basically, your eye should be able to follow the code structure
as easily as possible. This gives something like this:
function ayam(a, b:integer) : integer;
var
m: integer;
begin
readln(a) ;
readln(b) ;
readln(m) ;
begin
if (b=0) then
ayam:= 1
else
ayam:= a * ayam(a, b-1) mod m;
end;
writeln(ayam);
end;
With that change made, it is pretty obvious that there is something wrong with
the logic of your code, because a and b are declared as input variables to
the ayam function but your code actually reads in their values via the
readln statements inside the function. This is what made me think that you think
you have done all that's necessary to write your program. In fact, you have not. What you are missing
is some code which sets up the input parameters a and b (and m, see below) in some way and then
invokes your ayam function to calculate its value and then does something with
the result. At the moment, your code simply defines ayam but takes no steps
to use it. So, to get your ayam function to do anything, you need to write a complete
Pascal program that uses it. Something like this:
program ayamprogram;
{ the following declares some variables for the program to use }
var
varA,
varB,
varM,
varResult : Integer;
{ next, we define the ayam function }
function ayam(a, b, m:integer) : integer;
begin
begin
if (b=0) then
ayam:= 1
else
ayam:= a * ayam(a, b-1, m) mod m;
end;
end;
{ now we write the code of the program, which reads in the values to be
used by ayam, then invokes ("calls") ayam to calculate its value and
assign its value to the varResult variable and then outputs the calculated
result using the writeln statement
}
begin
{ this is the code of the program }
readln(varA);
readln(varB);
readln(varM);
varResult := ayam(varA, varB, varM);
writeln('function ayam evaluates to ', varResult);
readln; { this causes the program to pause so you can see the result of the writeln statement }
end. { end of program }
Note that I have used different variable names for the variables which are supplied to ayam, varA, varB, varM, from the variable names used inside ayam to avoid confusion between them.
Note also that as it is bad practice to read user input inside an executing function, the value to be used for M is read in before ayam is called and is supplied to ayam as a third parameter.
A point you need to consider regarding the expression
ayam:= a * ayam(a, b-1, m) mod m
is whether the mod m should operate on the value of ayam(a, b-1, m) or on the entire expression including the a * as well; if so, parentheses can enforce the evaluation order you need, by writing
ayam:= (a * ayam(a, b-1, m)) mod m
I'm just starting out with Object Pascal for my computer studies so this is probably an easy question for many of you here. I'm trying to build a verification system for a Sum and Average Calculator so that answers which are not integers cannot be accepted but also don't crash the software. I've been trying for hours to get a solution for this and whilst it's in its current state, if I input an integer it would interpret it as a noninteger, whilst if I input a noninteger the program just crashes. Is there anyway around this?
The coding currently looks like this:
Program SumAverageCalculator;
{$APPTYPE CONSOLE}
uses
SysUtils;
Const
NumberOfIntegers = 3;
Var
NumberOne, NumberTwo, NumberThree: integer;
Sum: integer;
Average: real;
Begin
Writeln ('=======================================');
Write ('What is your first number? '); readln(NumberOne);
If NumberOne-sqr(0) <> 1 then
Begin
Write ('Please write an integer only. What is your first number? '); readln(NumberOne);
End
Else
Begin
Write ('Great, that is an Integer! ');
End;
Write ('And the second number? '); readln(NumberTwo);
If NumberTwo-sqr(0) <> 1 then
Begin
Write ('Please write an integer only. What is your second number? '); readln(NumberOne);
End
Else
Begin
Write ('Great, that is an Integer! ');
End;
Write ('And the third number? '); readln(NumberThree);
If NumberThree-sqr(0) <> 1 then
Begin
Write ('Please write an integer only. What is your third number? '); readln(NumberOne);
End
Else
Begin
Write ('Great, that is an Integer! ');
End;
Sum := NumberOne + NumberTwo + NumberThree;
Average := Sum/NumberOfIntegers;
Writeln;
Writeln ('=======================================');
Writeln ('The number of given integers was ', NumberOfIntegers);
Writeln ('Your first number was ', NumberOne);
Writeln ('Your second number was ', NumberTwo);
Writeln ('Your third number was ', NumberThree);
Writeln ('=======================================');
Writeln ('The Sum of your numbers is ', Sum);
Writeln ('The Average of your numbers is ', Average: 1:2);
Writeln ('=======================================');
Readln;
End.
Thank you for any help given. :)
This is really because you passed an integer variable to the readln call, and it really wants to put an integer there. If it can't (the input is not an integer), it will crash. A solution is to first read the input in the most general form possible, that is, a string, check that it is an integer, and then convert it to one.
Of course, you don't have to do all that yourself. The sysutils unit has some helpful functions, and among them the TryStrToInt function, which does what it says: it will try to convert a string input to an integer, and will let you (the developer) know if it fails instead of crashing and burning.
uses
SysUtils;
Var
Input: String;
IsInteger: Boolean;
Value: Integer;
begin
Write('Enter an integer: ');
ReadLn(Input); // will work, user input can always be represented by a string
IsInteger := TryStrToInt(Input, Value);
if IsInteger then
begin
// Do stuff with "Value" which contains the input integer
end
else
begin
WriteLn('Sorry, that''s not an integer.');
end;
end.
Of course, if you're going to be doing this often, it may make sense to implement a helper function that acts like readln but does the checking itself and prints out an error without crashing (perhaps the program could keep asking for the integer until the user complies, or perhaps it should gracefully terminate). For instance, some of the code above could be wrapped up in a utility function readint.
Once you come across exceptions, you'll find a more general way to handle failures in your program and respond to them properly to avoid your program crashing on the slightest user error, however at this point this is probably what you are looking for.
If you are wondering what the out thing means in the TryStrToInt function, it's similar to var, but it basically means "I am going to be filling out this value, but I won't try to read it" (a write-only parameter) unlike var which means "I am going to fill out this value, but I might try to read it before" (a read-write parameter). So an out parameter need not be initialized before being used (so in a way, an out parameter is a "second return value", which makes sense in this case since the TryStrToInt function needs to return two things: whether the input was an integer, and what that integer was, but functions can only have one "standard" return value).
I'm writing long digit arythmetics. This is a function for adding to longint long binary digits. I need to output the sum inside the function, to debug it. How could I do it, without creating new variables?
function add(var s1,s2:bindata;shift:longint):bindata;
var l,i:longint;
o:boolean;
begin
writeln(s1.len,' - ',s2.len);
o:=false;
l:=max(s1.len,s2.len);
add.len:=0;
for i:=1 to l do begin
if o then Begin
if s1.data[i+shift] then Begin
if (s2.data[i]) then add.data[i+shift]:=true
Else add.data[i+shift]:=false;
End
else if s2.data[i] then add.data[i+shift]:=false
else Begin
add.data[i+shift]:=true;
o:=false;
End;
End
Else Begin
if s1.data[i+shift] then Begin
if s2.data[i] then
Begin
add.data[i+shift]:=false;
o:=true;
End
Else add.data[i+shift]:=true;
End
else if s2.data[i] then add.data[i+shift]:=true
else add.data[i+shift]:=false;
End;
output(add); //Can I output a variable?
end;
add.len:=l;
if o then Begin
inc(add.len);
add.data[add.len]:=true;
End;
end;
You are accumulating the result of the function within the function result variable, which is generally fine, but uses an outdated style, and leads to exactly the problem you're facing here. You're trying to report an intermediate value of the function result, and to do that, you're trying to reference the name of the function, add. When you do that, though, the compiler interprets it as an attempt to report the function itself, rather than the expected return value of this particular invocation of the function. You'll get the address of the function, if output is defined to accept function addresses; otherwise, you'll get a compiler error.
If your compiler offers a certain common language extension, then you should use the implicit Result variable to refer to the intermediate return value instead of continuing to refer to it by the function name. Since Result is declared implicitly, you wouldn't have to create any other variables. The compiler automatically recognizes Result and uses it as an alias for the function's return value. Simply find every place you write add within the function and replace it with Result. For example:
if o then begin
Inc(Result.len);
Result.data[Result.len] := True;
end;
Turbo Pascal, Free Pascal, GNU Pascal, and Delphi all support the implicit Result variable, but if you've managed to get stuck with a compiler that doesn't offer that extension, then you have no choice but to declare another variable. You could name it Result, and then implement your function with one additional line at the end, like so:
function add(var s1, s2: bindata; shift: longint): bindata;
var
l, i: longint;
o: boolean;
Result: bindata;
begin
{
Previous function body goes here, but with
`add` replaced by `Result`
}
{ Finally, append this line to copy Result into the
function's return value immediately before returning. }
add := Result;
end;
I keep getting a error when i run this code, What wrong with the code?
create or replace function f_vars(line varchar2,delimit varchar2 default ',')
return line_type is type line_type is varray(1000) of varchar2(3000);
sline varchar2 (3000);
line_var line_type;
pos number;
begin
sline := line;
for i in 1 .. lenght(sline)
loop
pos := instr(sline,delimit,1,1);
if pos =0 then
line_var(i):=sline;
exit;
endif;
string:=substr(sline,1,pos-1);
line_var(i):=string;
sline := substr(sline,pos+1,length(sline));
end loop;
return line_var;
end;
LINE/COL ERROR
20/5 PLS-00103: Encountered the symbol "LOOP" when expecting one of
the following:
if
22/4 PLS-00103: Encountered the symbol "end-of-file" when expecting
one of the following:
end not pragma final instantiable order overriding static
member constructor map
Stack Overflow isn't really a de-bugging service.
However, I'm feeling generous.
You have spelt length incorrectly; correcting this should fix your first error. Your second is caused by endif;, no space, which means that the if statement has no terminator.
This will not correct all your errors. For instance, you're assigning something to the undefined (and unnecessary) variable string.
I do have more to say though...
I cannot over-emphasise the importance of code-style and whitespace. Your code is fairly unreadable. While this may not matter to you now it will matter to someone else coming to the code in 6 months time. It will probably matter to you in 6 months time when you're trying to work out what you wrote.
Secondly, I cannot over-emphasise the importance of comments. For exactly the same reasons as whitespace, comments are a very important part of understanding how something works.
Thirdly, always explicitly name your function when ending it. It makes things a lot clearer in packages so it's a good habit to have and in functions it'll help with matching up the end problem that caused your second error.
Lastly, if you want to return the user-defined type line_type you need to declare this _outside your function. Something like the following:
create or replace object t_line_type as object ( a varchar2(3000));
create or replace type line_type as varray(1000) of t_line_type;
Adding whitespace your function might look something like the following. This is my coding style and I'm definitely not suggesting that you should slavishly follow it but it helps to have some standardisation.
create or replace function f_vars ( PLine in varchar2
, PDelimiter in varchar2 default ','
) return line_type is
/* This function takes in a line and a delimiter, splits
it on the delimiter and returns it in a varray.
*/
-- local variables are l_
l_line varchar2 (3000) := PLine;
l_pos number;
-- user defined types are t_
-- This is a varray.
t_line line_type;
begin
for i in 1 .. length(l_line) loop
-- Get the position of the first delimiter.
l_pos := instr(l_line, PDelimiter, 1, 1);
-- Exit when we have run out of delimiters.
if l_pos = 0 then
t_line_var(i) := l_line;
exit;
end if;
-- Fill in the varray and take the part of a string
-- between our previous delimiter and the next.
t_line_var(i) := substr(l_line, 1, l_pos - 1);
l_line := substr(l_line, l_pos + 1, length(l_line));
end loop;
return t_line;
end f_vars;
/