❌ About FreshRSS

Normal view

There are new articles available, click to refresh the page.
Before yesterdayNews from the Ada programming language world

How can I optimize my code so that I dont duplicate it

I'm trying to create a procedure that puts "-" between different dates and "0" if the is single digit, but i'm having a very hard time not duplicating my code.

   procedure put (Date : in Date_Type) is
      
   begin
    
      Put(Date.Y, Width => 1);
      Put("-");
      if Date.M <= 9 then
         Put("0");
      end if;
      Put(Date.M, Width => 1);
      Put("-");
      if Date.D <= 9 then
         Put("0");
      end if;
      Put(Date.D, Width => 1);
      
   end put;

This is the best solution I came up with

How can I make my If-statements less complicated

I'm making a program that lets you write in the date and then it returns the previous day aswell as the next day. If you write a date that doesnt exist, the program will tell you that it doesnt exist and exit the program. When making the procedure to check the day and month I used if statements. I always have issues using if-statements because I always make them too complicated but this time I atleast got it to work but as I guessed its overly complicated and I dont know how to shorten it.

   procedure Check_Day_And_Month (Date : in Date_Type) is 
      
   begin
      
     if Date.M <= 0 or Date.M > 12 then
        raise Month_error;
     
     elsif Date.D > 31 or Date.D <= 00 then
           raise Day_Error;
     
     elsif Date.D = 31 and(Date.M = 4 or Date.M = 6 or Date.M = 9 or Date.M = 11) then
           raise day_Error; 
     
     elsif Date.D > 29 and Date.M = 2 then 
           raise Day_Error;
     
     elsif Date.D = 29 and Date.M = 2 and Check_Leap(Date) = false then
           raise Day_Error;
     end if;
      
   end Check_Day_And_month;

I feel like every elsif I have is needed and I dont know how I could shorten this and make it less complicated. But at the same time i'm very new to coding so I most likely am missing something.

Return the greatest of 3 float values in Ada

I'm trying to create a subprogram that receives 3 float values and returns the greatest of the 3.

My code:

procedure is

   function Biggest(A, B, C: in Float) return Float is 
      
      X: Float; -- Greatest value
      
   begin
      
      X:= Float'Max(B, C);
      if A <= X then
         null;
      else
         X:= A;
      end if;         
          
      return Float'Ceiling(X); 
      
   end Biggest;

A, B, C: Float;
begin 

   Put("Mata in tre flyttal: ");
   Get(A);
   Get(B);
   Get(C);
   Put("Det stΓΆrsta av dessa vΓ€rden Γ€r: ");
   Put(Biggest(A,B,C), Fore=>0, Aft=>0, Exp=>0);

I dont really understand where I went wrong.

How to return the median value of 3 float values using if statements in Ada

My assignment is to create a subprogram that takes in 3 float values in returns the median of those 3 float values as an integer using if statements. IΒ΄ve tried a couple of ways to write this if statement but it just gives me a random value back of those 3 values I put in. My code:

   function Median(Fl1, Fl2, Fl3: in Float) return Integer is
       
      
   begin
      
     if Fl3 >= Fl1 then
        if Fl1 >= Fl2 then 
           return Integer(Fl1);
        else 
           return Integer(Fl2);
        end if;
    elsif Fl1 >= Fl3 then
       if Fl3 >= Fl2 then 
          return Integer(Fl3);
       else 
          return Integer(Fl2);
      end if; 
    elsif Fl2 >= Fl3 then
       if Fl3 >= Fl1 then 
          return Integer(Fl3);
       else 
          return Integer(Fl1);
       end if;
   end if;
   end Median;

How can I rewrite this to work?

Actual for "item" must be a variable - Ada

procedure Summera(Int1: in Integer;
             Int2: in Integer;
             Summa: out Integer) is 
      
      
   begin
      
      Put("Mata in ett heltal: ");
  48  Get(Int1);
      Put("Mata in ett heltal: ");
  50  Get(Int2);
      Summa:= Int1 + Int2;
      Put("Du matade in heltalet: ");
      Put(Int1, Width=>0);
      Put(" och heltalet: ");
      Put(Int2, Width=>0);
      Put(" och summan blev ");
      
   end Summera; 

I know there is something wrong but not really what. on the lines 48 and 50 which are I get the error message "actual for item must be a variable." I am not allowed to put the string/text outside of the "subprogram" bit I have no idea how to make this work. Why do I get this error messeage?

after begin I have this

Int1, Int2: Integer; 
Summa: Integer; 

begin 

Summera(Int1, Int2, Summa);
Put(Summa);

end program;

How to divide a character with a string in Ada

I'm trying to figure out how to divide a character with a string and get a float as the quota. I've done the following in my code:

 Procedure extwo is

 function  "-"(Strang: in String;
               Char: in Character) return Float is
  
   F: Float := Float'Value(Strang);
  
 begin
        
   return Float(Character'Pos(Char)-48) - Float'Value(Strang);
 
 end "-"; 

 Differensen: Float;
 Char: Character;
 Strang: String(1.
                
Begin 

 Put("Mata in ett tecken: ");
 Get(Char);
  
 Put("Mata in en strΓ€ng med exakt 3 tecken: ");
 Get(Strang);

 Differensen:= Char-Strang;

 Put("Du matade in tecknet: ");
 Put(Char);
 Put(" och strΓ€ngen: ");
 Put(Strang);
 Put(" och differensen blev ");
 Put(Differensen, Fore=> 0); 

end extwo;

With this I get the error messages: " invalid operand types for operator "-" ", " left operand has type "Standard.Character" " and " right operand has subtype of "Standard.String" defined at line 59 " all on line 95:22 which is the line where it says "Differensen:= Char-Strang;"

Dividing a character with a Float in Ada

So iΒ΄m trying to divide a character with a float using an operator, but I dont know why my code gets the error message "unexpected argument for "Position" attribute"

function "/"(Teck: in Character;
             Flyt: in Float) return Float is
  
  
begin
  
  return Float(Character'Position(Teck))/Flyt;
  
end "/";

Can somebody explain how Character'position works and what I need to change here, because IΒ΄ve used pretty much the same code previously in a different assigment.

How to make Boolean write True or False in Ada

I'm very new to programming and have just gotten started with Records and boolean in Ada. I'm trying to make it so that whenever I write 'T' it will say "True" and whenever I write 'F' it will say False, but IΒ΄m not sure how to do that. In this assignment I'm not allowed to change T: Boolean:= False; this has to be in the code, but IΒ΄m not sure what I have to do to get what I want. I guess I have to do some sort of if statement like But iΒ΄m not sure how to do that. My code looks like the following:

type Sub_J is 
  record
 
     Y: Character:= '9';
     Q: Character:= 'p';
 
  end record; 

type Sub_B is
  record
     Y: Character:= 'J';
     Q: Character:= 'o';
  end record;

type Sub_O is 
  record
 
     T: Boolean:= False;
     L: Character:= '5';
 
  end record;

type DS3 is 
  record
 
     J: Sub_J;
     B: Sub_B;
     O: Sub_O;
 
  end record;

procedure Get_3(DSThree: out DS3) is
  
   Space: Character;            
  
begin 
  
   Put("Mata in datamΓ€ngd: ");              
   Get(DSThree.J.Y);
   Get(Space);
   Get(DSThree.J.Q);
   Get(Space);
   Get(DSThree.B.Y);
   Get(Space);
   Get(DSThree.B.Q);
   Get(Space);              
   Get(DSThree.O.T);      
   Get(Space);
   Get(DSThree.O.L);
  
end Get_3;

procedure Put_3(DSThree: in DS3) is
    
begin 
  
   Put("Inmatad datamΓ€ngd: ");
   Put(DSThree.J.Y);
   Put(" ");
   Put(DSThree.J.Q);
   Put(" ");
   Put(DSThree.B.Y);
   Put(" ");
   Put(DSThree.B.Q);
   Put(" ");                 
   Put(DSThree.O.T);
   Put(" ");
   Put(DSThree.O.L);           
  
end Put_3;
❌
❌