Task: Display a linear combination

C: #include<stdlib.h>
#include<stdio.h>
#include<math.h> 

int main(int argC, char* argV[])
{
	
	int i,zeroCount= 0,firstNonZero = -1;
	double* vector;
	
	if(argC == 1){
		printf("Usage : %s <Vector component coefficients seperated by single space>",argV[0]);
	}
	
	else{
		
		printf("Vector for [");
		for(i=1;i<argC;i++){
			printf("%s,",argV[i]);
		}
		printf("\b] -> ");
		
		
		vector = (double*)malloc((argC-1)*sizeof(double));
		
		for(i=1;i<=argC;i++){
			vector[i-1] = atof(argV[i]);
			if(vector[i-1]==0.0)
				zeroCount++;
			if(vector[i-1]!=0.0 && firstNonZero==-1)
				firstNonZero = i-1;
		}

		if(zeroCount == argC){
			printf("0");
		}
		
		else{
			for(i=0;i<argC;i++){
				if(i==firstNonZero && vector[i]==1)
					printf("e%d ",i+1);
				else if(i==firstNonZero && vector[i]==-1)
					printf("- e%d ",i+1);
				else if(i==firstNonZero && vector[i]<0 && fabs(vector[i])-abs(vector[i])>0.0)
					printf("- %lf e%d ",fabs(vector[i]),i+1);
				else if(i==firstNonZero && vector[i]<0 && fabs(vector[i])-abs(vector[i])==0.0)
					printf("- %ld e%d ",labs(vector[i]),i+1);
				else if(i==firstNonZero && vector[i]>0 && fabs(vector[i])-abs(vector[i])>0.0)
					printf("%lf e%d ",vector[i],i+1);
				else if(i==firstNonZero && vector[i]>0 && fabs(vector[i])-abs(vector[i])==0.0)
					printf("%ld e%d ",vector[i],i+1);
				else if(fabs(vector[i])==1.0 && i!=0)
					printf("%c e%d ",(vector[i]==-1)?'-':'+',i+1);
				else if(i!=0 && vector[i]!=0 && fabs(vector[i])-abs(vector[i])>0.0)
					printf("%c %lf e%d ",(vector[i]<0)?'-':'+',fabs(vector[i]),i+1);
				else if(i!=0 && vector[i]!=0 && fabs(vector[i])-abs(vector[i])==0.0)
					printf("%c %ld e%d ",(vector[i]<0)?'-':'+',labs(vector[i]),i+1);				
			}
		}
	}
	
	free(vector);
	
	return 0;
}











Ada: with Ada.Text_Io;
with Ada.Strings.Unbounded;
with Ada.Strings.Fixed;

procedure Display_Linear is

   subtype Position is Positive;
   type Coefficient is new Integer;
   type Combination is array (Position range <>) of Coefficient;

   function Linear_Combination (Comb : Combination) return String is
      use Ada.Strings.Unbounded;
      use Ada.Strings;
      Accu : Unbounded_String;
   begin
      for Pos in Comb'Range loop
         case Comb (Pos) is
            when Coefficient'First .. -1 =>
               Append (Accu, (if Accu = "" then "-" else " - "));
            when 0 => null;
            when 1 .. Coefficient'Last =>
               Append (Accu, (if Accu /= "" then " + " else ""));
         end case;

         if Comb (Pos) /= 0 then
            declare
               Abs_Coeff   : constant Coefficient := abs Comb (Pos);
               Coeff_Image : constant String := Fixed.Trim (Coefficient'Image (Abs_Coeff), Left);
               Exp_Image   : constant String := Fixed.Trim (Position'Image (Pos), Left);
            begin
               if Abs_Coeff /= 1 then
                  Append (Accu, Coeff_Image & "*");
               end if;
               Append (Accu, "e(" & Exp_Image & ")");
            end;
         end if;
      end loop;

      return (if Accu = "" then "0" else To_String (Accu));
   end Linear_Combination;

   use Ada.Text_Io;
begin
   Put_Line (Linear_Combination ((1, 2, 3)));
   Put_Line (Linear_Combination ((0, 1, 2, 3)));
   Put_Line (Linear_Combination ((1, 0, 3, 4)));
   Put_Line (Linear_Combination ((1, 2, 0)));
   Put_Line (Linear_Combination ((0, 0, 0)));
   Put_Line (Linear_Combination ((1 => 0)));
   Put_Line (Linear_Combination ((1, 1, 1)));
   Put_Line (Linear_Combination ((-1, -1, -1)));
   Put_Line (Linear_Combination ((-1, -2, 0, -3)));
   Put_Line (Linear_Combination ((1 => -1)));
end Display_Linear;