Calling Prewritten Functions in C

Similar documents
Functions. Functions are everywhere in C. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

Functions. Prof. Indranil Sen Gupta. Dept. of Computer Science & Engg. Indian Institute t of Technology Kharagpur. Introduction

C Programs: Simple Statements and Expressions

ECET 264 C Programming Language with Applications

Lecture 3. Review. CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions. Conditions: Loops: if( ) / else switch

Functions. Autumn Semester 2009 Programming and Data Structure 1. Courtsey: University of Pittsburgh-CSD-Khalifa

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

C Functions. 5.2 Program Modules in C

BSM540 Basics of C Language

static int min(int a, int b) Returns the smaller of two int values. static double pow(double a,

The Math Class (Outsource: Math Class Supplement) Random Numbers. Lab 06 Math Class

INTRODUCTION TO C++ FUNCTIONS. Dept. of Electronic Engineering, NCHU. Original slides are from

Function Usage. Lecture 15 Sections 6.3, 6.4. Robb T. Koether. Hampden-Sydney College. Mon, Oct 1, 2018

Preview from Notesale.co.uk Page 2 of 79

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 4

(2-2) Functions I H&K Chapter 3. Instructor - Andrew S. O Fallon CptS 121 (January 18, 2019) Washington State University

Programming in C Quick Start! Biostatistics 615 Lecture 4

Chapter 5 C Functions

Functions. Systems Programming Concepts

Function. specific, well-defined task. whenever it is called or invoked. A function to add two numbers A function to find the largest of n numbers

Functions. Computer System and programming in C Prentice Hall, Inc. All rights reserved.

Arithmetic Expressions in C

Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)

CSE123. Program Design and Modular Programming Functions 1-1

Chapter 4 Functions By C.K. Liang

Introduction to Computers II Lecture 4. Dr Ali Ziya Alkar Dr Mehmet Demirer

Introduction to Functions in C. Dr. Ahmed Telba King Saud University College of Engineering Electrical Engineering Department

Beginning C Programming for Engineers

Fundamentals of Programming Session 8

Introduction To Computer Programming Laboratory Source code of a computer program is given below. What do you expect to be displayed?

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG

CpSc 1111 Lab 6 Conditional Statements, Loops, the Math Library, and Random Numbers What s the Point?

Computer Programming 5th Week loops (do-while, for), Arrays, array operations, C libraries

Lecture 04 FUNCTIONS AND ARRAYS

12. Numbers. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Expressions and operators

Computer Science & Engineering 150A Problem Solving Using Computers

Programming Language A

Lecture 14. Daily Puzzle. Math in C. Rearrange the letters of eleven plus two to make this mathematical statement true. Eleven plus two =?

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 3. Existing Information. Notes. Notes. Notes. Lecture 03 - Functions

Chapter 5 Methods. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Programming for Engineers Functions

Using Free Functions

Assoc. Prof. Dr. Tansu FİLİK

C Program Structures

Fundamental of Programming (C)

Use of scanf. scanf("%d", &number);

Programming Assignment #4 Arrays and Pointers

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

6-1 (Function). (Function) !*+!"#!, Function Description Example. natural logarithm of x (base e) rounds x to smallest integer not less than x

Conditional Expressions

Chapter 3. Computer Science & Engineering 155E Computer Science I: Systems Engineering Focus. Existing Information.

DUBLIN CITY UNIVERSITY

Fundamentals of Computer Programming Using C

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Lecture 5. Functions II. Functions with Arguments. CptS 121 Summer 2016 Armen Abnousi

Functions. Angela Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

Function Call Stack and Activation Records

Programming I Lecture 7

CS110: PROGRAMMING LANGUAGE I

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

بسم اهلل الرمحن الرحيم

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

ECET 264 C Programming Language with Applications. C Program Control

Writing to and reading from files

CS101 Introduction to computing Function, Scope Rules and Storage class

CSC 270 Survey of Programming Languages

Unit 1. Word Definition Picture. The number s distance from 0 on the number line. The symbol that means a number is greater than the second number.

ANSI C Programming Simple Programs

1. Variables 2. Arithmetic 3. Input and output 4. Problem solving: first do it by hand 5. Strings 6. Chapter summary

Function Example. Function Definition. C Programming. Syntax. A small program(subroutine) that performs a particular task. Modular programming design

Chapter 3 - Functions

Input and Expressions Chapter 3 Slides #4

DUBLIN CITY UNIVERSITY

Excerpt from "Art of Problem Solving Volume 1: the Basics" 2014 AoPS Inc.

The cin Object. cout << "Enter the length and the width of the rectangle? "; cin >> length >> width;

Functions in C C Programming and Software Tools

The Absolute Value Symbol

5. In the Cartesian plane, a line runs through the points (5, 6) and (-2, -2). What is the slope of the line?

Introduction to Computer Science Unit 2. Notes

Computer Programming: Skills & Concepts (CP) arithmetic, if and booleans (cont)

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

"'"' .;, b) F or ( c = l ; c <= 10,"' c++ ) X { JJ. ~. Computer Programming I COME Midterm Examination. Fall November 2016

Lectures 4 and 5 (Julian) Computer Programming: Skills & Concepts (INF-1-CP1) double; float; quadratic equations. Practical 1.

Introduction to Computer Science Unit 2. Notes

Functions in C. Lecture Topics. Lecture materials. Homework. Machine problem. Announcements. ECE 190 Lecture 16 March 9, 2011

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

Math 2 Coordinate Geometry Part 2 Lines & Systems of Equations

Engineering 12 - Spring, 1999

Macro Programming Reference Guide. Copyright 2005 Scott Martinez

CSC Algorithms and Data Structures I. Midterm Examination February 25, Name:

2. Numbers In, Numbers Out

Structured Programming. Dr. Mohamed Khedr Lecture 4

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

LAB: INTRODUCTION TO FUNCTIONS IN C++

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

Programming for Engineers Introduction to C

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 5. Playing with Data Modifiers and Math Functions Getting Controls

Skill 1: Multiplying Polynomials

Transcription:

Calling Prewritten Functions in C We've already called two prewritten functions that are found in the C library stdio.h: printf, scanf. The function specifications for these two are complicated (they allow a variable number of parameters), so when first learning functions, we don't typically look at these specifications. The easiest prewritten function specifications included in C libraries to understand are related to mathematics functions and functions used to generate pseudorandom numbers. The former are in the library math.h and the latter are in stdlib.h. Here is an abbreviated listing of some of these functions we will use: Library math.h double cos(double x); // in radians double acos(double x); double cosh(double x); double exp(double x); double ceil(double x); double floor(double x); double fabs(double x); int abs(int x); double pow(double x, double y); double sqrt(double x); Library stdlib.h(to use random numbers) int rand(void); void srand(unsigned seed);

Reading a Function Specification Here is the written description along with the function specification for the pow function in the math library: // Returns x raised to the power of y. double pow(double x, double y) The first item listed, double, represents the return type of the function. Some functions return things and others don't. If a function doesn't return something then void must be written as its return type. The second item listed is the name of the function. This is always followed by parentheses in the function specification (and also when you call the function). Inside the parentheses, we have listed the formal parameters. There can be 0 or more of these. The pow function has two. For each formal parameter, we list its type and its variable name. The first is a double named x while the second is a double named y. The job of the function is to take whatever you give it for x and whatever you give it for y, calculate x y and return that value to you. When you call a function, you do NOT list any type information. Instead, you just use the name of the function and in place of each formal parameter, you put the actual parameter you want. An actual parameter can be any expression that matches the type of the formal parameter given. Consider the example of

calculating compounded interest. The mathematical formula to calculate the end value of the account, given the initial investment, P, the yearly rate, r, and the number of years, t, of investment is: f(p, r, t) = P(1 + r 12 )12t (Note that the rate is a decimal, typically in between 0 and 1, which is NOT in percentage form.) Imagine that in code we have variables, principle, rate and time, respectively, representing the variables in the formula above. Assume that these three variables have been given values by the user and we wanted to store the value of the end investment in a variable called money. Here is how we could utilize a call to the pow function to accomplish this task: double money = principle*pow(1+rate/12, 12*time) Notice that when we call the pow function, it gets called as part of a bigger line of code. In no place in the function call do you see any type information. Notice that neither of the actual parameters we passed to the function, 1+rate/12 and 12*time, are variables and neither have an x or y in them. The power of the pow function (sorry for the pun), is that the user can call it with any set of actual parameters she sees fit, to make ANY exponentiation calculation that needs to be made. The way this works is that the computer sees that we want to calculate pow of something. It figures out the current value of both actual parameters (say rate is.12 and time is 2, then the two actual parameters would be 1.01 and 24, respectively), then it calculates the value of the first value raised to the power of the second one (for this example, it would return 1.01 24 ~ 1.27). From here, the computer would replace the section of the line of

code with the function call with this value and then proceed with what the line of code says to do. In this case, the line of code would take 1.27 and multiply it by whatever was stored in the variable P. Then it would take that value and assign it to the variable money. The key is this: functions that return values should not be called on a line by themselves. They should be called as part of a bigger line. When calling them, replace the formal parameters with actual parameters that are any expression of your choosing. These actual parameters need not have any connection to the formal parameters, in terms of their name. Rather, their values at the time should indicate whatever calculation you wish for the function to do. Note that the order you pass in the parameters mattered, had we done pow(12*time, 1+rate/12), the function would have instead returned 24 1.01 ~ 24.77 for the example we discussed. I am sure the bank would never make this error!!! Example of a Program Calling the pow Function Utilizing the example from above, here is a complete program that calculates the value of an account that uses monthly compounded interest. #include <stdio.h> #include <math.h> int main() { double principle, rate; int time; printf("how much are you investing (in dollars)?\n"); scanf("%lf", &principle); printf("what is the yearly rate of return,years of investment?\n"); scanf("%lf%d", &rate, &time); } double money = principle*pow(1+rate/12, 12*time); printf("in %d years, you will have $%.2lf.\n", time, money); return 0;

Example of a Program Calling the abs Function Consider the problem of finding the distance between two intersections on a city grid. On a city grid, one must walk in one of the four cardinal directions down a street or avenue. We can give Cartesian coordinates to each city intersection. For example, the distance between (2, 3) and (7, 1), when one can only walk parallel to the x or y axis (same as walking in one of the four cardinal directions) is (7-2) + (3-1) = 7. This distance calculation is called Manhattan distance, since Manhattan has streets and avenues arranged in a grid like fashion. In the following program, we utilize the abs function (since we don't know in advance which x coordinate is bigger or which y coordinate is bigger), to calculate the Manhattan distance between two intersections. Mathematically, the Manhattan distance between (x1, y1) and (x2, y2) is x1 - x2 + y1 - y2. (Of course, one could switch the ordering of the x's or y's within either absolute value symbol ) #include <stdio.h> #include <math.h> int main() { int x1, y1, x2, y2; printf("please enter the starting x and y coordinates.\n"); scanf("%d%d", &x1, &y1); printf("please enter the ending x and y coordinates.\n"); scanf("%d%d", &x2, &y2); int dx = abs(x2-x1); int dy = abs(y2-y1); int dist = dx + dy; printf("you have to walk %d blocks.\n", dist); } return 0;

Example calling sqrt, cos Functions Imagine that we want to write a program to solve the following problem: Prompt the user for two sides lengths of a triangle and the included angle. The program should read these values in, calculate and output the length of the third side. For those of you who have not taken trigonometry, there is a formula to calculate this third side length from the given information. Let a and b be the given side lengths and angle C be the included angle, then the third side length c can be determined as follows: c = a 2 b 2 2abcosC Assume that the user will enter the measure of the included angle in degrees and not radians. Here is a general plan for our program: 1) Ask the user for a, b, and angle C. 2) Calculate the square of a, the square of b and add these. 3) Convert angle C into radians so that we can use the prewritten cos function in the math library. 4) Subtract 2abcosC from the value in 2. 5) Take the square root of the value from step 4. 6) Output the value calculated in step 5.

// Calculates the length of the third side of a triangle given the // other two sides and their included angle.. #include <stdio.h> #include <math.h> #define PI 3.14159265358979 int main(void) { double a, b, anglec, Crad; double sumsq; // Get user input printf( Enter the lengths of sides a and b.\n ); scanf("%lf %lf", &a, &b); printf( Enter the measure of angle C, in degrees.\n ); scanf("%lf", &anglec); sumsq = pow(a,2) + pow(b,2); // Calculate sum of squares Crad = anglec*pi/180; // Convert C to radians. sumsq = sumsq - 2*a*b*cos(Crad); // Subtract appropriate term // Calculate and output the square root of the value above. printf( The length of side c is %lf\n, sqrt(sumsq)); } return 0;

Example of Calling a Void Function Programs are more fun with random numbers. Let's consider a simple program that we'll build on later. Imagine a program that gives a student multiplication practice. If it always gave the same problems to the student, they might just memorize those answers but not know how to multiply all possible pairs of numbers (within some range). Thus, we want to write a program that generates two random numbers and asks the user for the product of those numbers. Then, our program will tell the user how close they were to the correct answer. (Once we get to the if statement in the next lecture, we can print out different messages depending on whether the student gets the question right or wrong.) For any program that uses random numbers, we should seed the random number generator. This seeding should occur only once, even though the program itself may generate many random numbers. The function to seed the random number generator is srand and here is its specification: // Seeds the random number generator with // the integer seed. void srand(int seed); In reality, a computer can't generate truly random numbers. Instead, it uses a complicated mathematical formula to generate a sequence of numbers that appear to be random. To start the generation, a seed is needed. If you don't seed the random number generator, C always seeds it with 0. What this will do is generate the same stream of random numbers every time you run your program. You don't want to do this, so it's best to seed the random number generator with a different integer each

time. The easiest way to do this is to seed it with something based on the system time. In the time.h library, there is a function time. If you pass this function the parameter 0, it just returns the current time (in seconds after January 1, 1970). What it returns precisely isn't that important. All that matters is that each time you run the program, it returns something different. Thus, the line of code we want near the beginning of main for any program where we want to generate random numbers is: srand(time(0)); Void functions get called on their own line. Since nothing is returned, a return value doesn't have to be stored anywhere. The function just does its task and completes, and then we can move onto the next line of code. Example Program using Random Numbers Now, we can write the program described above, after examining the rand function. Here is the specification: // Returns a random integer in between 0 and // 32767 int rand(); Thus, we call the function rand without any parameters and it will return to us a random integer in the range designated above. In most applications, we don't desire random integers in this range. The easiest way to constrain a random number is to use mod. In general, the expression rand()%n will always evaluate to an integer in between 0 and n-1, inclusive, since mod returns a remainder which is guaranteed to be in between 0 and 1 less than the number we are dividing by. In general, if we want a

random integer in the range [a, b], where a and b are both positive and smaller than 32767, we can use the following expression: a + rand()%(b-a+1). The latter portion of the expression will always be an integer in the range 0 to b-a. When we add a to this range, we get the range a to b, as desired. Now, let's look at our multiplication program, where we give the user a random problem where both operands are in between 1 and 12, inclusive. Notice the use of the abs function to print out how close to the answer the user was. Notice that it can be "inside of" a printf and that the actual parameter can be a complicated expression. #include <stdio.h> #include <stdlib.h> #include <math.h> #include <time.h> #define MAX 12 int main() { // Seed the random number generator (just one time) srand(time(0)); int x, y, answer; // Generate our two operands and ask the user for the answer. x = 1 + rand()%max; y = 1 + rand()%max; printf("what is %d x %d?\n", x, y); // Read the answer in. scanf("%d", &answer); // Print out how close the user was. printf("you were %d away from the answer.\n", abs(answer-x*y)); } return 0;