CS 2073 Lab 6: Functions - Utility File Development, Lab Reports of Computer Science

The objectives, hand-in requirements, and details of lab 6 in cs 2073 at utsa. Students are required to write and call c functions, define and use c preprocessor files, and implement functions for quadratic equations, factorials, divisors, and prime numbers. Prototypes and comments for each function.

Typology: Lab Reports

Pre 2010

Uploaded on 07/31/2009

koofers-user-cqw
koofers-user-cqw 🇺🇸

5

(1)

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 2073 Lab 6: Functions
Chia-Tien Dan Lo
Department of Computer Science, UTSA
I Objectives
Demonstrate your ability to write and call C functions
Define and using C preprocessor files
II Hand-in Requirements
All laboratories will be submitted electronically through WebCT. Zip up your entire project folder to submit as the
source. (Right click on the laboratory folder and follow the SentTo link.)
III Details
In this assignment, you will be writing some C functions, using C include headers, and call them. Design a C file,
“utility.c”, that contains the following functions. The prototypes in the file are as follows.
/* The function quadratic calculates the solutions of the quadratic
equation based on the formula:
ax2+bx +c= 0
x1 = bb24ac
2a
x2 = b+b24ac
2a
It return 0 if no real number solutions, 1 if one (repeated) solution, or
2 if two solutions are found. The solutions are stored in x1 and x2 using
call-by-reference technique. x1 will store the solution if 1 is returned.
Both x1 and x2 store solutions if 2 is returned. Otherwise, x1 and x2
are invalid. The following table lists some test cases:
a b c x1 x2 return value
---- ---- ---- ---- ---- ------------
1 -2 1 1 1 1
1 1 1 x x 0
1 -3 2 1 2 2
Note x in the table means don’t care.
*/
int quadratic(int a, int b, int c, double *x1, double *x2)
/* returns the factorial of number.
The function fact(5) should return 120. (1 * 2 * 3 * 4 * 5 = 120)
*/
double fact(int number)
/* Outputs all of the divisors for number including 1 and the number
The function print_all_divisor(24) outputs 1 2 3 4 6 8 12 24
1
pf2

Partial preview of the text

Download CS 2073 Lab 6: Functions - Utility File Development and more Lab Reports Computer Science in PDF only on Docsity!

CS 2073 Lab 6: Functions

Chia-Tien Dan Lo

Department of Computer Science, UTSA

I Objectives

  • Demonstrate your ability to write and call C functions
  • Define and using C preprocessor files

II Hand-in Requirements

All laboratories will be submitted electronically through WebCT. Zip up your entire project folder to submit as the source. (Right click on the laboratory folder and follow the SentTo link.)

III Details

In this assignment, you will be writing some C functions, using C include headers, and call them. Design a C file, “utility.c”, that contains the following functions. The prototypes in the file are as follows.

/* The function quadratic calculates the solutions of the quadratic equation based on the formula:

ax^2 + bx + c = 0

x1 = −b −

b^2 − 4 ac 2 a

x2 =

−b +

b^2 − 4 ac 2 a It return 0 if no real number solutions, 1 if one (repeated) solution, or 2 if two solutions are found. The solutions are stored in x1 and x2 using call-by-reference technique. x1 will store the solution if 1 is returned. Both x1 and x2 store solutions if 2 is returned. Otherwise, x1 and x are invalid. The following table lists some test cases: a b c x1 x2 return value


1 -2 1 1 1 1 1 1 1 x x 0 1 -3 2 1 2 2

Note x in the table means don’t care. */ int quadratic(int a, int b, int c, double *x1, double *x2)

/* returns the factorial of number. The function fact(5) should return 120. (1 * 2 * 3 * 4 * 5 = 120) */ double fact(int number)

/* Outputs all of the divisors for number including 1 and the number The function print_all_divisor(24) outputs 1 2 3 4 6 8 12 24

void print_all_divisor(int number)

/* returns 1 if the number is prime and 0 otherwise. A number is prime if is has no divisors except itself and 1. The smallest possible prime number is 2. The function is_prime(5) returns 1. The function is_prime(12) returns 0. */ int is_prime(int number)

/* returns the sum of all of the divisors except the number itself The function sum_of_proper_divisor(24) returns 36. 1 + 2 + 3 + 4 + 6 + 8 + 12 = 36 */ int sum_of_proper_divisor(int number)

/* returns 1 if the number is perfect and 0 otherwise. A perfect number is a number such that the sum of its proper divisors equals the number The function is_perfect(6) returns 1 because 1 + 2 + 3 = 6 The function is_perfect(10) returns 0 because 1 + 2 + 5 != 10 */ int is_perfect(int number)

Edit a header file, “utility.h”, that contains all the above function prototypes. Add a line in your main.c file that includes the header. In your main.c file, code test programs to validate the examples listed in the above comments. You need to create a menu similar to the one designed in Lab 4 and Lab 5. Of course, you need to take the user’s inputs accordingly.

IV Setup

  1. Project name: Lab
  2. Solution name: MyCPrograms
  3. Create new source with name: utility.c main.c
  4. Create a header with name: utility.h
  5. Save the source and header
  6. Compile and run the program.

V Coding

  1. Write function prototypes with empty bodies in utility.c.
  2. Write a main function shell in main.c.
  3. Write a header utility.h.
  4. Write down comments whenever applicable.
  5. Implement all the functions.

VI Testing

Compile and run your program until there are no errors. You may test your program at least once on each statement.