Algorithms and Ada Code Implementations for Various Mathematical Operations, Exercises of Engineering

Algorithms and ada code listings for different mathematical operations such as counting sequences, euler's 2nd order integration method, finding averages using a while loop, computing factorials using iteration, and generating fibonacci numbers using recursion.

Typology: Exercises

2011/2012

Uploaded on 07/22/2012

senapati_007
senapati_007 🇮🇳

3.8

(4)

109 documents

1 / 22

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
C11 Solutions
1. Count := 1;
FOR I in 1 .. 10 LOOP
If I MOD 2 = 0 THEN
FOR J in 1 .. 10 LOOP
Count:= Count + 2;
END LOOP;
ELSE FOR J in 1 .. 5 LOOP
Count := Count – 1;
END LOOP;
END IF;
END LOOP;
Count = 76.
Count increments by 20 when I is even and decrements by 5 when I is odd.
2. Write an Ada95 program to implement the Euler’s 2nd order integration method? Turn
in a hard copy of your algorithm and code listing and an electronic copy of your code.
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16

Partial preview of the text

Download Algorithms and Ada Code Implementations for Various Mathematical Operations and more Exercises Engineering in PDF only on Docsity!

C11 Solutions

1. Count := 1;FOR I in 1 .. 10 LOOP

If I MOD 2 = 0 THEN FOR J in 1 .. 10 LOOP

ELSE^ END LOOP;^ Count:= Count + 2;

FOR J in 1 .. 5 LOOP Count := Count – 1;

END LOOP;^ END IF;^ END LOOP;

Count = 76. Count increments by 20 when I is even and decrements by 5 when I is odd.

2.in a hard copy of your algorithm and code listing and an electronic copy of your code. Write an Ada95 program to implement the Euler’s 2nd^ order integration method? Turn

C 11 part b ALGORTIHM

Eluer’s 2 Area of a trapezoid under curve = .5(y1+y2)delta_xnd^ order integration – use trapeziodal rule.

Algortihm: Ask user for inputs:

      • Coefficients of each polynomial term plus constantUpper and Lower Bounds of integrationStep Size

Calculate number of steps = (upper_bound-lower_bound)/step_size and convert it to aninteger

Loop from 0 to the number of steps using a for loop, performing euler’s second order approximation- Integral = Integral + .5(y1+y2)step_size

Print out results--^ Y1 = Y2Y2 = Y2 +Step_Size

C12 1. Algorithm

a. b. Get the number from the userIf the number is >= 1 then i. For I in 1 .. number loop

c. Else^ °^ Factorial := Factorial * I;

d. Display Computed Factorial to the User^ i.^ Display Cannot Compute Factorial

GNAT 3.13p (20000509) Copyright 1992-2000 Free Software Foundation, Inc.^ Code Listing

Compiling: c:/docume~2/ joeb /desktop/16070/codeso~1/factorial_with_iteration.adb (source file timestamp: 2003-10-02 03:57:26)

      1. -------------------------------------------------------- Program to find the factorial of a number using-- iteration.
      1. -- Programmer : Joe B-- Date Last Modified : October 01, 2003------------------------------------------------------
      1. with Ada.Text_Io;
      1. with Ada.Integer_Text_Io;procedure Factorial_With_Iteration is
      1. (^) beginFactorial : Integer := 1; -- initializise factorial to 1Num : Integer; -- variable used to get input from the user
      1. -- get the number from the userAda.Text_Io.Put("Please Enter A Number : ");Ada.Integer_Text_Io.Get(Num);
      1. Ada.Text_Io.Skip_Line;if Num >=1 then
      1. -- compute factorialfor I in 1 .. Num loop
      1. (^) end loop;Factorial:= Factorial * I;
      1. -- display the computed factorial to the userAda.Text_Io.Put("The Factorial of ");Ada.Text_Io.Put(Integer'Image(Num));
      1. Ada.Text_Io.Put("is : ");Ada.Integer_Text_Io.Put(Factorial);
  1. else
      1. (^) end if;Ada.Text_Io.Put("Cannot Compute Factorial"); 38 lines: No errors^ 38.^ end Factorial_With_Iteration;

The program does not work when you have numbers greater than 12 because the value ofthe factorial is larger than Integer’Last

3. Algorithm

From the series, you can see that any number in the series is a sum of the previous two numbers, i.e. numbern = numn-1 + number n-

Main Program

1. 2. Get the number of Fibonacci numbers from the user.If the number is <= 0, then a. Display cannot generate numbers

3. else a. for I in 1 .. Number

ii.^ i.^ Call the recursive function FiboDisplay Value returned by Fibo

Fibo Function

1. 2. If num < 2Else a. Return 1

a. Return (Fib(n-1)+ Fib(n-2));

Code Listing GNAT 3.13p (20000509) Copyright 1992-2000 Free Software Foundation, Inc.

Compiling: c:/docume~2/ joeb /desktop/16070/codeso~1/fibo_with_recursion.adb (source file time stamp:2003-10-02 14:42:40)

      1. -------------------------------------------------------- Program to find N fibonacci numbers using-- recursion.

C13 1. Solve the following recurrence equation using the iteration method. Show all the

steps in your derivation.

T ( n )= aT  n^ c √+^ cn nn^ =>^11

b ↵

Substitute the value of T(n) from the recurrence equation:aT(n/b) + cn

⇒ a(aT((n/b)/b) + c(n/b)) + cn

⇒ a^2 T(n/b^2 ) + cn(a/b) + cn

⇒ a^2 T(n/b^2 ) + cn((a/b) + 1)

⇒ a^2 (aT((n/b^2 )/b) + cn/b^2 ) + cn((a/b) + 1)

⇒ a^3 T(n/b^3 ) + cn(a^2 /b^2 ) + cn((a/b) + 1)

⇒… a^3 T(n/b^3 ) + cn((a^2 /b^2 )+ (a/b )+ 1)

⇒ akT(n/bk) + cn((ak-1/bk-1)+ (ak-2/bk-2)+ … + (a^2 /b^2 )+ (a/b) + 1)

When k = log ⇒ n = bk b n,

T(n) = akT(1) + cn(ak-1/bk-1^ + ... + a^2 /b^2 + a/b + 1)

= a= cakc + cn(ak + cn(ak-1k-1/b/bk-1k-1^ + ... + a+ ... + a^22 /b/b^22 + a/b + 1)+ a/b + 1)

= cnak/bk^ + cn(ak-1/bk-1^ + ... + a^2 /b^2 + a/b + 1)

= cn(ak/bk^ + ... + a^2 /b^2 + a/b + 1)