with Ada.Text_IO, Ada.Integer_Text_IO;
use Ada.Text_IO, Ada.Integer_Text_IO;

PROCEDURE HelloWorld is

   My_Integer_File : FILE_TYPE;
   Index : INTEGER;
   No_of_questions : INTEGER;
   Line_count : INTEGER;
   MaxQs : CONSTANT Positive := 50;
   MaxMarks : CONSTANT Positive := 100;
   NoOfQs : INTEGER;
   FileName : String(1..30);
   
   type FREQ_ARRAY is array(1..101) of INTEGER;
   type KEY_ARRAY is array(1..20) of INTEGER;
   type RECORD_ARRAY is array(1..1000) of INTEGER;


PROCEDURE PrintRecord(R : in RECORD_ARRAY; TotalMarksOfStud : in Integer) is
    BEGIN
        Ada.Text_IO.Put(Item => "Student ID: ");
        Ada.Integer_Text_IO.Put(Item=> R(1), Width =>4);
        Ada.Text_IO.Put(Item => "Total marks: ");
        Ada.Integer_Text_IO.Put(Item=> TotalMarksOfStud, Width =>4);
        Ada.Text_IO.New_Line;
END PrintRecord;

PROCEDURE GetKeyArray( Q : Integer;  My_Integer_File : FILE_TYPE; Index : Integer) IS
    type KEY_ARRAY is array(1..Q) of INTEGER;
    key : KEY_ARRAY;
    
    BEGIN
        FOR I IN Q LOOP
            key(I) := Get(My_Integer_File, Index);
        END LOOP;
END GetKeyArray;

PROCEDURE GetRecordArray( Q : Integer;  My_Integer_File : FILE_TYPE; Index : Integer) IS
    type RECORD_ARRAY is array(1..(Q+1)) of INTEGER;
    --type RECORD_FILE is array(1..) of RECORD_ARRAY;
    stud_rec : RECORD_ARRAY;
    
    BEGIN
        FOR I IN Q+1 LOOP
            stud_rec(I) := Get(My_Integer_File, Index);
        END LOOP;
END GetRecordArray;
 
PROCEDURE PrintScoreFrequency( F : FREQ_ARRAY) IS
    type RECORD_ARRAY is array(1..(Q+1)) of INTEGER;
    --type RECORD_FILE is array(1..) of RECORD_ARRAY;
    stud_rec : RECORD_ARRAY;
    
    BEGIN
        FOR I IN MaxMarks+1 LOOP
            IF F(I) > 0 THEN 
                Ada.Text_IO.Put(Item => "Score: ");
                Ada.Integer_Text_IO.Put(Item=> I, Width =>4);
                Ada.Text_IO.Put(Item => "Frequency: ");
                Ada.Integer_Text_IO.Put(Item=> F(I), Width =>4);
                Ada.Text_IO.New_Line;
            END IF;
            
        END LOOP;
END PrintScoreFrequency;

PROCEDURE CalMarks(K : in FREQ_ARRAY; R : in RECORD_ARRAY; Q : in Integer) is
NoOfCorrectAnswers : INTEGER;
MarksPerQ : INTEGER := 4;
TotalMarksOfStud : INTEGER := 0;
F : FREQ_ARRAY := 0;
I : Integer := 1;

    BEGIN
        NoOfCorrectAnswers := 0;
        
        --Initializing the frequency array
        FOR I IN MaxMarks+1 LOOP
            F(I) := 0; 
        END LOOP;
        
        FOR I IN Q LOOP
            IF K(I) = R(I+1) THEN 
                NoOfCorrectAnswer := NoOfCorrectAnswer + 1;
            END IF;
        END LOOP;
        TotalMarksOfStud := NoOfCorrectAnswer * MarksPerQ;
        --Updating the frequency array
        F(I) := F(I) + 1;
        Ada.Text_IO.Put(Item => "Total marks of student ID ");
        Ada.Integer_Text_IO.Put(Item=>R(1), Width =>4);
        Ada.Text_IO.Put(Item => " is: ");
        Ada.Integer_Text_IO.Put(Item=>TotalMarksOfStud, Width =>4);
        Ada.Text_IO.New_Line;
        --Calling Procedure to print frequency
        PrintScoreFrequency(F);
        --Calling Procedure to print student ID and corresponding total marks
        PrintRecord(R, TotalMarksOfStud);
        
END CalMarks;

BEGIN
    Ada.Text_IO.Put (Item => "Enter the name of the file > ");
    Ada.Text_IO.Get_Line(Item => FileName);
    Ada.Text_IO.New_Line;
    Ada.Text_IO.Open(File => My_Integer_File, Mode => Ada.Text_IO.In_File, Name => FileName);                    
    --Open(My_Integer_File, In_File, "SCANTRON.TXT");

   WHILE NOT End_Of_File(My_Integer_File) 
    LOOP
        IF End_Of_Line(My_Integer_File) THEN
            New_Line;
            Skip_Line(My_Integer_File);
            Line_count := Line_count + 1;
        ELSE
           Get(My_Integer_File, Index);
            IF (Line_count = 1) THEN
               No_of_questions := Index;
               Ada.Text_IO.Put("The no. of questions tested is: ");
               Ada.Text_IO.Put(Index, 6);
               Ada.Text_IO.New_Line;
            ELSIF (Line_count = 2) THEN
               --Second line of the file has the answer key
               GetKeyArray(No_of_questions, My_Integer_File, Index);
            ELSIF (Line_count > 2) THEN
               --Get the student recors
               GetRecordArray(No_of_questions, My_Integer_File, Index);
            END IF;
        END IF;
    END LOOP;

    Ada.Text_IO.Close(My_Integer_File);

END HelloWorld;
 

Ada online compiler

Write, Run & Share Ada code online using OneCompiler's Ada online compiler for free. It's one of the robust, feature-rich online compilers for Ada language, running the latest Ada version 2012. Getting started with the OneCompiler's Ada editor is easy and fast. The editor shows sample boilerplate code when you choose language as Ada and start coding.

Read inputs from stdin

OneCompiler's Ada online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample Ada program which takes name as input and prints hello message with your name.

with Ada.Text_IO; use Ada.Text_IO;
procedure Hello is
begin
  declare
  name : String := Ada.Text_IO.Get_Line;
  begin
  Ada.Text_IO.Put ("Hello ");
  Ada.Text_IO.Put_Line (name);
	end;
end Hello;

About Ada

Ada is suitable for all development needs and it is extremely good for developing very large applications with built-in features which supports structured, object-oriented, generic, distributed and concurrent programming directly. Ada was designed by Jean Ichbiah.

It's a Good choice for Rapid Application Development, Extreme Programming. It is a very strong and statically typed language.

Syntax help

Loops

1. Infinite loop:

This is the simplest loop

begin
   Index := 1; --initialization
    loop                            
     --code
	exit when Index = n;
	end loop;

2. while loop

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

   Count := 1;    --initialization
   while Count < n loop  
   --code
   end loop;

3. for loop

For loop is used to iterate a set of statements based on a condition. Usually for loop is preferred when number of iterations are known in advance.

for Index in 1..n loop          
--code
end loop;

Sub programs

Ada distinguishes functions and procedures. In simpler terms, functions return some value and must be called as part of larger expressions. Procedures never return a value.

Functions and procedures are collectively called as sub-programs.

Syntax for procedure

procedure proc-name
(X : in Integer ; Y : out Integer ; Z : in out Integer ) is
begin
X := 10; −− it's an Error as you can’t modify an in parameter.
Y := X; −− can modify Y as it's an out parameter.
Z := Z + 1; −− can read and write as it's an in out parameter.
end proc-name;

Syntax for functions

function function_name(declaration) return value is
begin
--code
end function-name;