FloridaTech - College of Engineering
CSE1001 - LABORATORY


Lab Assignment 5: Loop Structures

This lab Contains two parts. Part One is to be done this week while part two will be done next week.

Part One

Problem 1: Spaces are characters Write a program that reads a sequence of characters separated by spaces. Character # is considered as the end-of-sequence character. Your program should output the number of non-space characters, the number of spaces and the total number of characters.

Example of execution:
******************************************
********* Characters and Spaces **********
******************************************
Enter a sequence of characters terminated by # :
5 d s A ( k l d j ( c ) ) d e ( ) #

The number of non-space characters is : 17
The number of spaces : 17
The total number of characters is : 35


Problem 2: Counting Parantheses : Write an Ada procedure that reads a sequence of characters terminated by the character # and outputs the number of parantheses, the total number of characters and the percentage of parantheses in the sequence (with two digits right of .). Spaces should be ignored

Example of execution:
******************************************
********* Counting Parenthesis ***********
******************************************
Enter a sequence of characters terminated by # :
5 d s A ( k l d j ( c ) ) d e ( ) #

The number of parenthesis is : 6
The number of characters is : 17
The percentage of presence of paranthesis is : 35.17 %


Problem 3: Statistics : (Optional) Write an Ada procedure that reads a sequence of positive integers terminated by zero then output the maximum, minimum, sum and average of the entered numbers.

Example of execution:
*************************************
********* Statistics ****************
*************************************
Enter a sequence of positive integers terminated by 0 :
5 7854 12 65 4 8 2 10000 678 621 0

The sum is : 19249
The minimum is : 2
The maximum is : 10000
The average is : 1942.9


Part Two

Problems 3 through 5 assumes basic integer arithmetic knowledge. Information will given in class if necessary.

Problem 4: Great Common Divisor (gcd): Using Euclid's Algorithm, You are to write an Ada procedure that computes the gcd of two positive integers. The program should allow the user to compute as many gcds as desired and only stops when two zeros (0 0) are encountered.

Example of execution:
******************************
********* GCD ****************
******************************
Enter two positive integers :
5 7 --> gcd(5,7) = 1.

Enter two postive integers :
6 12 --> gcd(6,12) = 6.

Enter two postive integers :
45 20 --> gcd(45,20) = 5.

Enter two positive integers :
0 12
Done *************************


Problem 5 : Divisors : Write an Ada program that reads a positive integer then lists its positive divisors.
Bonus : How can you make your program run faster ?

Example of execution:
******************************
********* Divisors************
******************************
Enter a positive integer : 60
The divisors of 60 are : 1 2 3 4 5 6 10 12 15 20 30.


Problem 6 : Prime Numbers : Prime numbers are very remarkable in integer arithmetic. A number is prime if its positive divisors are 1 and the number itself. Example of prime numbers are : 2,3,5,7,11... The set of prime numbers is infinite and that's why, in this problem, we won't try to find all prime numbers. Specifically, you are to write an Ada program that reads a positive integer then outputs "prime" if the number read is a prime number and "not prime" otherwise.

Example of execution:
******************************
********* Prime numbers*******
******************************
Enter a positive integer : 40 --> Not Prime
Enter a positive integer : 37 --> Prime


Optional Problems

Problem 7 : Factorial Write an Ada program that reads a postive integer and computes its factorial. Remember that factorial(0) is 1 and factorial(n)=n*(n-1)*...*1 when n > 0.

Example of execution:
******************************
********* Factorial **********
******************************
Enter a positive integer : 6
Foctorial(6) = 6! : 6*5*4*3*2*1 = 720


Problem 8 : Pattern matching Write an Ada program that reads three characters (the pattern) then reads a sequence of characters terminated by # (text); then your output should be the number of occurences of the pattern in the text.

Example of execution:
**********************************
********* Pattern Matching *******
**********************************
Enter the pattern (three characters) : ada
Enter a text terminated by # : abracadabradadafadawe4op7fnad0dadk#
The number of occurences of ada in the text is : 3