Functions : Laboratory 5 - Introduction to Computer Programming | CPS 196, Lab Reports of Computer Science

Material Type: Lab; Class: Introduction to Computer Programming; Subject: Computational Science; University: Syracuse University; Term: Unknown 1989;

Typology: Lab Reports

Pre 2010

Uploaded on 09/17/2009

koofers-user-lrn
koofers-user-lrn ๐Ÿ‡บ๐Ÿ‡ธ

9 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lab 5: Functions
Todayโ€™s lab has us playing with functions.
Problem 0:
Given the following function declarations give the return
type and list the parameters:
(1) void function () ;
Return type:
Parameters:
(2) int f u n c t i o n 1 ( ) ;
Return type:
Parameters:
(3) float f u n c t i o n 2 ( in t n umbe r ) ;
Return type:
Parameters:
(4) float f u n c t i o n 3 ( in t number ,
double doub , cha r c ) ;
Return type:
Parameters:
Problem 1:
Write a program that has two functions:
odd ev e n
The return type of the functions should be void and both
should take a single int as a parameter. Function odd should
display a message only if the number that was passed in as a
parameter is odd. Function even should only display a message
if the number was odd.
Your program should accept an integer, n, from the user via
scanf and then count up to nfrom 1 calling both functions on
each number (use a loop).
Problem 2:
Write a program that accepts a quadratic formula from the user
one term at a time. Your program should have the following
functions:
float pow( float x , float y ) ;
Consider using a loop to compute pow. Remember that x0= 1.
float evaluateTerm ( float x ) ;
This function will prompt the user for the coefficient and expo-
nent of the term in this polynomial. The function will use the
value of x passed in as a parameter to evaluate this term and
then return the resulting value to evaluateExpression (where it
was called from).
evaluateExpression () ;
This function prompts the user for the value of x and contin-
uously loops calling evaluteTerm. The user is done entering in
data when they have entered an exponent of 0. Use a global
variable to keep track of whether or not the user has entered
CPS 196: Introduction to C (Summer 2009) Page 1
pf2

Partial preview of the text

Download Functions : Laboratory 5 - Introduction to Computer Programming | CPS 196 and more Lab Reports Computer Science in PDF only on Docsity!

Lab 5: Functions

Todayโ€™s lab has us playing with functions.

Problem 0:

Given the following function declarations give the return type and list the parameters:

(1) void f u n c t i o n ( ) ;

Return type:

Parameters:

(2) int f u n c t i o n 1 ( ) ;

Return type:

Parameters:

(3) f l o a t f u n c t i o n 2 ( int number ) ;

Return type:

Parameters:

(4) f l o a t f u n c t i o n 3 ( int number , double doub , char c ) ;

Return type:

Parameters:

Problem 1: Write a program that has two functions: odd even The return type of the functions should be void and both should take a single int as a parameter. Function odd should display a message only if the number that was passed in as a parameter is odd. Function even should only display a message if the number was odd. Your program should accept an integer, n, from the user via scanf and then count up to n from 1 calling both functions on each number (use a loop). Problem 2: Write a program that accepts a quadratic formula from the user one term at a time. Your program should have the following functions: f l o a t pow ( f l o a t x , f l o a t y ) ; Consider using a loop to compute pow. Remember that x^0 = 1. f l o a t evaluateTerm ( f l o a t x ) ; This function will prompt the user for the coefficient and expo- nent of the term in this polynomial. The function will use the value of x passed in as a parameter to evaluate this term and then return the resulting value to evaluateExpression (where it was called from). e v a l u a t e E x p r e s s i o n ( ) ; This function prompts the user for the value of x and contin- uously loops calling evaluteTerm. The user is done entering in data when they have entered an exponent of 0. Use a global variable to keep track of whether or not the user has entered

CPS 196: Introduction to C (Summer 2009) Page 1

Lab 5: Functions

in 0 for the exponent. Update this variable from evaluate term and check its value in evaluateExpression(). All main has to do is call evaluateExpression(); The program should work as follows: Sample display

โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€” What value of x should I use: 5 What is the coefficient for this term: 1 What is the exponent for this term: 2 What is the coefficient for this term: - What is the exponent for this term: 1 What is the coefficient for this term: 12 What is the exponent for this term: 0 Your result is : 22

โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”

The user entered the following expression: 1 x^2 โˆ’ 3 x + 12

Evaluating this expression at 5 we get:

1 ยท 52 โˆ’ 3 ยท 5 + 12 25 โˆ’ 15 + 12 = 22

CPS 196: Introduction to C (Summer 2009) Page 2