1. Trang chủ
  2. » Công Nghệ Thông Tin

C Programming for Scientists & Engineers phần 6 pptx

15 262 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 15
Dung lượng 1,05 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

The function prototype for write_area specifies that main will pass a variable, of type struct triangle, by value.. The calling function can give data to the called function through an a

Trang 1

Introduction to functions 69 fprintf(stdout," Co-ordinates (x,y) of third point (m.):");

fscanf(stdin," %lf %lf ", &triangle_2_ptr->x[2],

&triangle_2_ptr->y[2]);

return;

/* Function: calculate_area */

void calculate_area(struct triangle *triangle_ptr)

{

double a, /* distance between points 1 and 2 */

b, /* distance between points 2 and 3 */

c, /* distance between points 3 and 1 7

s; /* half perimeter of triangle 7

a = sqrt((triangle_ptr->x[1] - triangle_ptr->x[0]) *

(triangle_ptr->x[1] - triangle_ptr->x[0]) +

(triangle_ptr->y[1] - triangle_ptr->y[0]) *

(triangle_ptr->y[1] - triangle_ptr->y[0]));

b = sqrt((triangle_ptr->x[2] - triangle_ptr->x[1]) *

(triangle_ptr->x[2] - triangle_ptr->x[1]) +

(triangle_ptr->y[2] - triangle_ptr->y[1]) *

(triangle_ptr->y[2] - triangle_ptr->y[1]));

c = sqrt((triangle_ptr->x[0] - triangle_ptr->x[2]) *

(triangle_ptr->x[0] - triangle_ptr->x[2]) +

(triangle_ptr->y[0] - triangle_ptr->y[2]) *

(triangle_ptr->y[0] - triangle_ptr->y[2]));

s = (a+b+c)/2.0;

triangle_ptr->area = sqrt(s*(s-a)*(s-b)*(s-c));

return;

/* Function: write_area */

void write_area(struct triangle triangle_3)

{

fprintf(stdout," Calculated area is %lf (m2)\n", triangle_3.area);

return;

Trang 2

70 C programming for scientists and engineers

Looking at main, a variable, triangle_1, and a pointer, triangle_l_ptr, are both declared to be of data type struct triangle Following this, there is a function prototype statement for each function that main will use These statements specify that main will pass a pointer of data type struct triangle to read_points and calculate_area The function prototype for write_area specifies that main will pass a variable, of type struct triangle, by value All of the function prototype

statements specify that no data will be returned from any function

via their return statements.

The first executable statement in main copies the address of

triangle_1 to triangle_1 _ptr This is followed by statements that call

each function in the required sequence When read_points is called,

main passes the value of triangle_l_ptr to it This passes triangle_1 by

reference to read_points Note, at this stage, that no data values

have been stored in the members of triangle_1 However, when

read_points has finished, the x and y arrays inside triangle_1 contain

the co-ordinates of three points defining a triangle

In the next statement, main calls calculate_area, again passing

triangle_1 by reference The calculate_area function uses the values in

the x and y arrays to calculate the area of the triangle, storing this in the area member of triangle_1.

Finally, main calls write_area, this time passing triangle_1 by value Looking at the read_points function, when it is called it copies the pointer that is passed to it into triangle_2_ptr This is then used to access the members of triangle_l and, using the & operator, pass their addresses to fscanf Note how individual elements of the x and

y arrays are accessed.

Looking at the calculate_area function, the pointer passed to it is copied into triangle_ptr, which is then used to access the values in the elements of the x and y arrays inside triangle_1 This data is used to

calculate the length of each side of the specified triangle, the

function storing the lengths in local variables a, b and c These are

then used to calculate the half perimeter of the triangle, 5, before calculating the triangle area The area value is then stored in the

area member of triangle_1.

The main function passes the variable triangle_1 to write_area by value This means that the values of the members inside triangle_1 are copied into the members of triangle_3 Within write_area, the value stored in the area member of triangle_3 is then passed by value

to fprintf, which displays it on the screen.

Trang 3

Introduction to functions 71

Tutorial 3.9

Implement Program 3,9, making brief notes on its operation,

Tutorial 3.10

Re-write Program 3.9 so that the struct triangle structure is

replaced by

struct triangle

where point_1[0] and point_1 [I], etc hold the x and y

co-ordinates, respectively, for each point

Chapter review

This chapter has introduced the detailed 'mechanics' of using

func-tions in C programs All C programs contain a function called main Many C programs use main to co-ordinate the use of other

func-tions, each of these having been designed to perform a particular task When a function uses another function, the former is said to be the calling function and the latter is the called function A calling function must have a function prototype statement for each function that it will use Function prototype statements define the interface between the calling and called functions

The calling function can give data to the called function through

an argument list, variables being passed by value or by reference In either case, the called function makes a copy of each variable passed

to it A function receiving a variable that has been passed by reference can change the value of that variable in the calling function because both calling and called functions have access to the location in memory of the variable This cannot happen if the variable has been passed by value

Trang 4

72 C programming for scientists and engineers

A called function can pass a variable back to the calling function

through the return statement In addition to demonstrating the

mechanics of using functions, later examples in this chapter also introduced the use of functions as a means of partitioning a program to reflect the intended solution to a problem

Trang 5

Decisions and Loops

4.1 Introduction

Programs are much more useful if they can make decisions about what tasks need to be performed Making a decision in a C program usually involves testing the value of one or more

vari-ables, for example, 'if X is greater than Y then carry out task 1,

else carry out task 2' C programs can use a range of tests to satisfy many different circumstances The example given above uses the

if-else construct and is just about the most simple test that a C

program can perform However, it is not too difficult to imagine

that, having made this decision, task 1 or task 2 may also be an if-else type of test, leading to the execution of other, more

specialized, tasks (perhaps including more tests) This can be

achieved in C by using as many nested if-else constructs as

required The switch construct is similar to the nested if-else but is

more appropriate when different tasks must be selected (switch to) depending on the value of a variable Another type of test is required when a particular task has to be performed some number of times If the number of times required is known beforehand then the/or loop can be used The decision that has to

be made in the/or loop involves testing a counter to see if the loop has been performed the required number of times There are other situations where a loop is required, but the number of times that the task has to be carried out is not known beforehand A simple example of this concerns reading input from the user, where the user can enter as many items of data as they wish For

this and many other similar situations, C provides the while and the do-while loops.

4

Trang 6

74 C programming tor scientists and engineers

4.2 The if-else construct

The if-else construct in C has the following form:

if (expression) statement, else

statement 2

In this example, (expression) is a decision that uses relational and

logical operators (Section 2.3) to compare the values of variables and constants

If expression is TRUE then its resultant numerical value is 1 and

statement 1 is executed, otherwise expression is FALSE, with a numerical value of 0, and statement 2 is executed Consider the following:

double temperature, set_point = 21.0;

temperature = 32.0;

if (temperature > set_point) fprintf(stdout,"lt's hot today !\n");

else fprintf(stdout,"lt's cold today !\n");

In this example, the if statement makes a decision by testing for

temperature greater than set_point If this is TRUE the 'hot' message

is displayed Conversely, the test is FALSE if temperature is less than

or equal to set_point, leading to the 'cold' message being displayed.

Comparison expressions only compare individual numerical values This means that collections of values (arrays, data structures and char-acter strings) cannot be compared numerically For example, given:

int a[2J = {3, 265}, b[2] = {302, 0};

if(a<=b) is not allowed, but

if (a[1]<= b[1]) is OK because individual elements of

each array are compared Similarly:

char a[3J="cd", b[3]="tg";

if(a<=b) is not allowed, but

Team-Fly®

Trang 7

Decisions and loops 75

if (a[1]<= b[1]) is OK, since ASCII values of individual characters are

compared Finally, it should be noted that, if a particular decision does not

require the else part of the if-else construct, it need not be included,

for example:

int A =10,B = 9;

if (A < 15) changes the value of B if A satisfies the test

B = 0;

Tutorial 4 1

Implement the "temperature" example as a working program that reads temperature values from the user and displays the appropriate message on the screen Make notes on its operation*

4.3 Compound statements

In many cases, the outcome of a decision is that a group of state-ments must be executed This is achieved by grouping the required

statements within'{ }' brackets to form a compound statement or block, for example:

int get_data_from_user(struct data *);

int copy_users_data_to_file(struct data *);

int get_data from_file(struct data *);

struct data *data_ptr;

int user_data;

if (user_data != 0)

{

get_data_from_user(data_ptr);

copy_users_data_to_file(data_ptr);

}

else

get_data_from_file(data_ptr);

Trang 8

76 C programming for scientists and engineers

In this example, user_data is a flag which is used to indicate that

data must be read either from the user (if user_data is not equal to zero) or from a file (if user_data is equal to zero) In this example, if

the test is TRUE, data read from the user is stored in a file This requires the use of two functions, so their calling statements are enclosed within brackets

In those situations where the comparison expression compares one variable with the value zero, as above, a short hand notation can

be used where if (user_data != 0) and if (user_data) are equivalent In other words, both tests say 'if user_data is not equal to zero then '.

In contrast to this, the if (user_data == 0) test can also be written as

if (!user_data), meaning that the comparison expression is TRUE if

the value of user_data is equal to zero.

Tutorial 4.2

Implement the example in tills section as a working program,

replacing the function calls with calls to fprintf, so that

messages indicating the appropriate action are displayed on the screen The user should be prompted for and supply a

value for user_data prior to the if-else construct.

Modify Che program in the previous question so that it calls the

functions shown in the example Move the calls to fprintf into

the relevant functions Note that this involves some under-standing of functions, discussed in Chapter $ and that you will

need to invent a convenient definition for struct data.

4.4 Nested if-else statements

Nested if-else statements can be used where multiple decisions must

be made For example, consider the situation where a process controller may perform either of three actions depending on the temperature being above an upper set point, below a lower set point,

or in between the two This can be programmed as follows:

Trang 9

Decisions and loops 77

if (temperature > upper_set_ point)

{

report_process(' too_hot");

activate_cooler();

}

else

{

if (temperature < lower_set_point)

{

report_process('too_cold");

activate_heater();

}

else

report_process(' OK");

In this example, if the outcome of the first test is FALSE then a

further test must be performed to decide whether temperature is above or below lower _set_point Hence, the statement associated with the else part of the first test is another if statement If the outcome of this test is FALSE then the final else statement is

executed If the outcome of the first or second test is TRUE then the associated blocks of statements are carried out When this

happens, all of the subsequent statements in the nested if-else construct are ignored Note that the second if-else construct is written as a compound statement associated with else in the first test Whilst this is not strictly necessary in this example, nesting if-else constructs within compound statements can be important in more complex decision-making, especially when if-else constructs and if statements are used together.

Computing efficiency can be a significant point when if-else, and especially when nested if-else, constructs are used within loops If it is

known that a particular outcome is more likely than any other, it is

more efficient to place it with the first 'if.

Trang 10

78 C programming for scientists and engineers

Tutorial 44

Implement the 'temperature' example in this section as a

working program, replacing the function calls with calls to fprintf,

so that messages indicating the appropriate action are displayed

on the screen.The user should be prompted for and supply

values for temperature, upper_set_point and lower_set_point prior to the if-else construct Make notes on the program's operation.

Tutorial 4.5

The program in the previous question will not be efficient if the controlled process is predominantly within the upper and lower set points Assuming this to be the case, re-write the program so that it is more efficient Hint: you will need to use the logical AND operator, &&, discussed in Chapter 2.

Tutorial 4.6

Implement a weather forecasting program that writes the text shown in the rows and columns of the following table, depending on values of temperature and pressure supplied by the user Define high temperatures to be above 20.0°C and high pressures to be above 1000,0 millibar.

Low temperature Clear skies and cold

4.5 The switch construct

The switch construct is similar to nested if-else statements However, it is more appropriate than nested if-else where different

actions are required depending on an integer variable or

expression having a range of values The switch construct is

defined as follows:

Trang 11

Decisions and loops 79

79 switch(expression)

case constant expression:

statements case constant expression:

statements

default:

statements

Typically, expression in the switch statement is the name of an integer variable Also, the subsequent case statements are always enclosed within '{•••}' brackets Each case statement consists of the actual word

'case', followed by an integer constant and a colon When the switch

construct is executed, the value of the expression in the switch statement is compared to the value of the constant expression in the first case statement If the two values are equal then the statements associated with the first case statement are executed By default,

having executed these statements, processing continues by

comparing the switch expression with the constant expression in each of the subsequent case statements If only those statements associated with a particular case statement are to be executed, then the final statement before the next case must be break;.

The default case statement is intended to capture all of the situa-tions that are not trapped by any of the case statements The default case is optional and, if omitted, the switch construct will not perform any action if none of the case constant expressions agree with the expression in the switch statement The following example demon-strates the switch construct.

int user_reply;

int function_0(void);

int function_ 1(void);

int function_2(void);

int report_input_error(int);

fprintf(stdout," Select required function (0, 1 or 2):");

fscanf(stdin," %d", &user_reply);

Ngày đăng: 12/08/2014, 09:22

TỪ KHÓA LIÊN QUAN