C Programming for Scientists & Engineers phần 6 pptx

15 262 0
C Programming for Scientists & Engineers phần 6 pptx

Đ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

Introduction to functions 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, b, c, s; /* distance between points and /* distance between points and /* distance between points and /* half perimeter of triangle a = sqrt((triangle_ptr->x[1] (triangle_ptr->x[1] (triangle_ptr->y[1] (triangle_ptr->y[1] - triangle_ptr->x[0]) * - triangle_ptr->x[0]) + - triangle_ptr->y[0]) * - triangle_ptr->y[0])); b = sqrt((triangle_ptr->x[2] (triangle_ptr->x[2] (triangle_ptr->y[2] (triangle_ptr->y[2] */ */ 7 - triangle_ptr->x[1]) * - triangle_ptr->x[1]) + - triangle_ptr->y[1]) * - 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; 69 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 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 coordinates, respectively, for each point Chapter review This chapter has introduced the detailed 'mechanics' of using functions in C programs All C programs contain a function called main Many C programs use main to co-ordinate the use of other functions, 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 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 4 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 variables, 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 or task may also be an ifelse 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 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 statement2 AM FL Y 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 and statement1 is executed, otherwise expression is FALSE, with a numerical value of 0, and statement2 is executed Consider the following: TE 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 character strings) cannot be compared numerically For example, given: int a[2J = {3, 265}, b[2] = {302, 0}; if(a

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

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan