Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

Lab Assignment 8

Uploaded by

230107165
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Lab Assignment 8

Uploaded by

230107165
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

CSCI 151 – Programming for Scientists and Engineers Fall 2024

Lab Assignment 8
Lab Instructions
During the lab you should be doing the following things:
1. You need to open any IDE for C on your own laptops.
2. Read the following task carefully and start implementing it.
3. Implement the tasks in separate .c files.
4. During the lab you are allowed to ask for the help from one of our TAs and Instructors.
5. You can discuss the tasks with the classmates and friends during the lab, but you are NOT
allowed to do the tasks instead of each other.
6. During the lab you should finish all the required tasks and submit to Moodle before the
deadline to get the grades.
7. Ensure that your solutions adhere to the material that has been taught before or will be
taught in the current week.
8. The deadline for submission is October 17, Thursday, at 11:00 pm.
9. During the work, please make sure that you save your work frequently.

Lab Exercises

Task 1. Write a C program that reads positive integer numbers from the “number.txt” file and
performs several operations on them that require using recursive functions. Below is the sample content
of the text file. The numbers are separated by single-space. The number of pairs is not restricted, i.e. it
can be less or more than 5 shown below. Therefore, please do not hardcode the number of pairs.

Your program must perform the following operations with these numbers:

1. Find the greatest common divisor (GCD) of the pair of numbers.


Use the Euclidean Algorithm to find the GCD of N1 and N2:
• If N1 = 0, then GCD(N1,N2)=N2, since the GCD(0,N2)=N2, and we can stop.
• If N2 = 0 then GCD(N1,N2)=N1, since the GCD(N1,0)=N1, and we can stop.
• Write N1 in quotient remainder form (N1 = N2⋅Q + R)
• Find GCD(N2,R) using the Euclidean Algorithm since GCD(N1,N2) = GCD(N2,R)
2. Calculate factorial of the GCD, i.e., GCD!
3. Find nth element of the Fibonacci series, where n = GCD.
Remember, the Fibonacci sequence is a sequence in which each number is the sum of the two
preceding ones. For example: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, … (Note: we use 1 as the 1st element)
4. Calculate the sum of digits of the nth element of the Fibonacci series, where n = GCD.

For each of the operations above you must create a separate function which will call itself recursively.
You must use recursion for each of the function to get a credit for this lab assignment.
Sample Input/Output:

The pair of numbers #1: 2 and 3.


The greatest common divisor for these two numbers is 1.
1! = 1.
Element #1 in the Fibonacci sequence is 1.
The sum of digits of the number 1 is 1.

The pair of numbers #2: 3 and 6.


The greatest common divisor for these two numbers is 3.
3! = 6.
Element #3 in the Fibonacci sequence is 2.
The sum of digits of the number 2 is 2.

The pair of numbers #3: 4 and 12.


The greatest common divisor for these two numbers is 4.
4! = 24.
Element #4 in the Fibonacci sequence is 3.
The sum of digits of the number 3 is 3.

The pair of numbers #4: 5 and 25.


The greatest common divisor for these two numbers is 5.
5! = 120.
Element #5 in the Fibonacci sequence is 5.
The sum of digits of the number 5 is 5.

The pair of numbers #5: 10 and 100.


The greatest common divisor for these two numbers is 10.
10! = 3628800.
Element #10 in the Fibonacci sequence is 55.
The sum of digits of the number 55 is 10.

Note: The contents of the txt file will be restricted only to positive integer numbers. You can also
ignore the issue of exceeding the max value for ints. The pairs of numbers tested during grading will
be selected such that this issue will not happen.

Task 2. Imagine you are working for some company. Your boss asked you to create a C program
that reads a file containing employees’ information, and then given an employee name it must
recursively output the information of the line managers of that employee (a.k.a. boss of that
employee), together with some information about them.

The file provided from the HR manager will be names as “employees.txt” and will contain the
following information:
All entrees in each line are separated by a single space. The format of each line (no title row):

Name of the Employee’s boss Position title Phone number Office number
employee name (can save as int) (can save as int)

You should implement a function that reads this employees list and saves it as Employee structs.

Afterward, you need to write a function with a recursion that prints the hierarchy of line managers
together with some information about them.

The program must ask the name of the employee from the user.

Sample Input/Output: (in italic red is the user input, do not hardcode it)

Example 1:
Type a name of the staff member: Aron
Aron’s position title: Senior. Aron’s phone and office: 601515 205

The boss of Aron is John.


The boss of John is Mary.

Example 2:
Type a name of the staff member: Basilius
Basilius’s position title: Senior. Basilius’s phone and office: 701011 108

The boss of Basilius is Bruno.


The boss of Bruno is John.
The boss of John is Mary.

In this task, you are allowed to external libraries such as string.h and stdbool.h in case you think you
need them.

Note: The contents of the txt file will not contain the same names for different employees. All
employees are assigned only to one boss. Information about the topmost boss (such as Mary in the
example above) will not be tested while grading since there is no information about that employee.
Assume that all other employees will have the full information about them, and all of them have some
line manager (boss). We will test only the names of the employees who do exist in the testcase txt list.

Grading and Submission


Create 2 separate files for each of the tasks. Name them as follows: lab8task1.c and lab8task2.c
To receive credit for this lab exercise, you should submit the solution to these problem to Moodle
before the deadline. You also will have some extra hours after the lab session to finish and submit
your work as .c file to Moodle. Do not wait until the last minute!

You might also like