โŒ 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.

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?

โŒ
โŒ