C for Engineers and Scientists An Interpretive Approach

  • Slides: 25
Download presentation
C for Engineers and Scientists: An Interpretive Approach Chapter 2: Getting Started Outline A

C for Engineers and Scientists: An Interpretive Approach Chapter 2: Getting Started Outline A Simple C Program Run Program hello. c in Ch. IDE Startup in Ch Compile and Link Program hello. c in Windows and Unix in a Command shell Compile and Link Program hello. c Using Ch. IDE Command chmod in Unix Commonly Used Commands A Practical Engineering Problem of Solving Acceleration Setup Command Search Paths Created by Harry H. Cheng, 2009 Mc. Graw-Hill, Inc. All rights reserved.

C for Engineers and Scientists: An Interpretive Approach A Simple C Program /* File:

C for Engineers and Scientists: An Interpretive Approach A Simple C Program /* File: hello. c Print ‘Hello, world’ on the screen */ #include <stdio. h> int main() { printf(“Hello, worldn"); return 0; } Created by Harry H. Cheng, 2009 Mc. Graw-Hill, Inc. All rights reserved.

C for Engineers and Scientists: An Interpretive Approach • Comments – Text surrounded by

C for Engineers and Scientists: An Interpretive Approach • Comments – Text surrounded by /* and */ is ignored by computer – Used to describe program • #include <stdio. h> – Preprocession directive - tells computer to load contents of a header file – <stdio. h> allows standard input/output operations Created by Harry H. Cheng, 2009 Mc. Graw-Hill, Inc. All rights reserved.

C for Engineers and Scientists: An Interpretive Approach • int main() – C programs

C for Engineers and Scientists: An Interpretive Approach • int main() – C programs contain one or more functions, exactly one of which must be main – Parenthesis used to indicate a function – int means that main "returns" an integer value – Braces indicate a block • The bodies of all functions must be contained in braces • printf( “hello, worldn" ); – Instructs computer to perform an action • Specifically, prints string of characters within quotes – Entire line called a statement • All statements must end with a semicolon – - escape character • Indicates that printf should do something out of the ordinary • n is the newline character • a is the character for a sound. Created by Harry H. Cheng, 2009 Mc. Graw-Hill, Inc. All rights reserved.

C for Engineers and Scientists: An Interpretive Approach • return 0; – A way

C for Engineers and Scientists: An Interpretive Approach • return 0; – A way to exit a function – return 0, in this case, means that the program terminated normally – The returned value of 0 is displayed in the exit status in Ch. IDE • Right brace } – Indicates end of main has been reached Created by Harry H. Cheng, 2009 Mc. Graw-Hill, Inc. All rights reserved.

C for Engineers and Scientists: An Interpretive Approach Executing Program hello. c Using Ch

C for Engineers and Scientists: An Interpretive Approach Executing Program hello. c Using Ch in Ch. IDE Click “Run” or “Start” to execute the program Created by Harry H. Cheng, 2009 Mc. Graw-Hill, Inc. All rights reserved.

C for Engineers and Scientists: An Interpretive Approach Startup a Ch Shell Startup in

C for Engineers and Scientists: An Interpretive Approach Startup a Ch Shell Startup in Unix If Ch is the login shell, you can readily use the Ch language environment. If not, you can type command ch at a terminal prompt to launch the Ch language environment. Startup in Windows There are five ways to get into the Ch language environment. For example, to start Ch Standard Edition 6. 1 – – – Click the icon Ch Standard on the Desktop screen Click Start->Programs->Soft. Integration Ch 6. 1 Standard->Ch 6. 1. Click Start, followed by Run, then type ch. exe. Go to the MS-DOS prompt, and type ch. In Ch. IDE, click Ch Created by Harry H. Cheng, 2009 Mc. Graw-Hill, Inc. All rights reserved.

C for Engineers and Scientists: An Interpretive Approach Run Program hello. c • Run

C for Engineers and Scientists: An Interpretive Approach Run Program hello. c • Run program hello. c using Visual C++ in Windows > cl hello. c /*. . . create hello. exe */ > hello. exe Hello, world • Run program hello. c using gcc in Unix/Linux/Mac OS X > cc hello. c > gcc hell. c Or /*. . . create a. out */ > a. out Hello, world • Run program hello. c in Ch. > hello. c Hello, world Created by Harry H. Cheng, 2009 Mc. Graw-Hill, Inc. All rights reserved.

C for Engineers and Scientists: An Interpretive Approach Compile hello. c Using Visual C++

C for Engineers and Scientists: An Interpretive Approach Compile hello. c Using Visual C++ in Windows and gcc in Linux from Ch. IDE Created by Harry H. Cheng, 2009 Mc. Graw-Hill, Inc. All rights reserved.

C for Engineers and Scientists: An Interpretive Approach Command chmod in Unix A C

C for Engineers and Scientists: An Interpretive Approach Command chmod in Unix A C program can be executed without compilation in a Ch language environment. C programs are called command files or simply commands in Ch. A command file shall have both read and execute permissions. Command chmod in Unix/Linux/Mac OS X can be used to change the permission mode of a file. • Make a file, say hello. c, executable > chmod +x hello. c • Make a file readable > chmod +r hello. c • Make a file both executable and readable > chmod +xr hello. c Created by Harry H. Cheng, 2009 Mc. Graw-Hill, Inc. All rights reserved.

C for Engineers and Scientists: An Interpretive Approach Interactive Command Mode Accept the input

C for Engineers and Scientists: An Interpretive Approach Interactive Command Mode Accept the input values and print out the results directly or use the function printf() in C. C: /Ch> 7 C: /Ch> 0. 4794 C: /Ch> hello, C: /Ch> 1+2*3 sin(0. 5) printf("hello, world") world Created by Harry H. Cheng, 2009 Mc. Graw-Hill, Inc. All rights reserved.

C for Engineers and Scientists: An Interpretive Approach Commonly Used Commands Ch supports most

C for Engineers and Scientists: An Interpretive Approach Commonly Used Commands Ch supports most Unix and Windows commands Command Usage Description cd cd Change to the home directory cd dir Change to the directory dir cp cp file 1 file 2 Copy file 1 to file 2 ls ls List contents in the working directory mkdir dir Create a new directory dir pwd Print (display) the name of the working directory rm rm file remove file chmod +x file Change the mode of file to make it executable chide file. c Edit and execute program file. c vi vi file Edit file Created by Harry H. Cheng, 2009 Mc. Graw-Hill, Inc. All rights reserved.

C for Engineers and Scientists: An Interpretive Approach • Examples of Commands C: /Documents

C for Engineers and Scientists: An Interpretive Approach • Examples of Commands C: /Documents C: /Documents hello. c C: /Documents and and and Settings/Administrator> mkdir eme 5 Settings/Administrator> cd eme 5 Settings/Administrator/eme 5> pwd Settings/Administrator/eme 5> cp C: /Ch/demos/bin/hello. c Settings/Administrator/eme 5> ls and Settings/Administrator/eme 5> chide hello. c Created by Harry H. Cheng, 2009 Mc. Graw-Hill, Inc. All rights reserved.

C for Engineers and Scientists: An Interpretive Approach • Files in Ch C: /Ch>

C for Engineers and Scientists: An Interpretive Approach • Files in Ch C: /Ch> pwd C: /Ch> ls bin/ demos/ docs/ include/ license/ README. TXT sbin/ config/ dl/ extern/ lib/ package/ release/ toolkit/ C: /Ch> cd docs C: /Ch/docs> ls README. TXT chguide. pdf chinstall. pdf chref. pdf man/ Created by Harry H. Cheng, 2009 Mc. Graw-Hill, Inc. All rights reserved.

C for Engineers and Scientists: An Interpretive Approach A Sample Problem: The system in

C for Engineers and Scientists: An Interpretive Approach A Sample Problem: The system in Figure 1 (a) consists of a single body with mass m moving on a horizontal surface. An external force p acts on the body. The coefficient of kinetic friction between body and horizontal surface is . The freebody diagram for the system is shown in Figure 1 (b). Figure 1: The system diagram and FBD of a sample problem Created by Harry H. Cheng, 2009 Mc. Graw-Hill, Inc. All rights reserved.

C for Engineers and Scientists: An Interpretive Approach The nomenclature related to the modeling

C for Engineers and Scientists: An Interpretive Approach The nomenclature related to the modeling of the system is listed below. m -- mass of the body x -- position of the body v -- velocity of the body a -- acceleration of the body g -- gravitational acceleration -- friction coefficient f -- friction force N -- normal force p -- applied external force Equation of motion: The equation of the motion of the system can be derived based on the Newton's second law. N = mg (1) f = N (2) p-f = ma (3) From equation (1), (2) and (3), the formula for calculating the acceleration of the rigid body can be derived as follows. a = (p- mg)/m (4) Created by Harry H. Cheng, 2009 Mc. Graw-Hill, Inc. All rights reserved.

C for Engineers and Scientists: An Interpretive Approach Problem Statement: For the system shown

C for Engineers and Scientists: An Interpretive Approach Problem Statement: For the system shown in Figure 1(a), given m = 5 kg, g = 9. 81 m/s 2, = 0. 2. The external force p is expressed as a function of time t, p(t) = 20 when t >= 0 calculate the acceleration a when t = 2 seconds. Solutions. 1. Interactive solution. 2. Write a simple C program to print out the value of acceleration directly. Created by Harry H. Cheng, 2009 Mc. Graw-Hill, Inc. All rights reserved.

C for Engineers and Scientists: An Interpretive Approach Interactive Solution > (20 -0. 2*5*9.

C for Engineers and Scientists: An Interpretive Approach Interactive Solution > (20 -0. 2*5*9. 81)/5 2. 0380 > Created by Harry H. Cheng, 2009 Mc. Graw-Hill, Inc. All rights reserved.

C for Engineers and Scientists: An Interpretive Approach Program 1: A simple C program.

C for Engineers and Scientists: An Interpretive Approach Program 1: A simple C program. /* File: accel. c */ #include <stdio. h> int main() { printf("Acceleration a = %f (m/s^2)n", (20 -0. 2*5*9. 81)/5); return 0; } Output: Acceleration a = 2. 038000 (m/s^2) Created by Harry H. Cheng, 2009 Mc. Graw-Hill, Inc. All rights reserved.

C for Engineers and Scientists: An Interpretive Approach • Finding Commands in Ch Shell

C for Engineers and Scientists: An Interpretive Approach • Finding Commands in Ch Shell – The system variable _path of string type contains the directories to be searched for the command. – When a command shell is launched, the system variable _path contains some default search paths. – The user can add new directories to the search paths for the command shell by the following statements. Create an individual startup file. chrc in Unix and _chrc in Windows in your home directory by typing the command > ch –d in a Ch command shell. Created by Harry H. Cheng, 2009 Mc. Graw-Hill, Inc. All rights reserved.

C for Engineers and Scientists: An Interpretive Approach Run programs without typing the complete

C for Engineers and Scientists: An Interpretive Approach Run programs without typing the complete directory Windows: adding the following statement in the startup file _chrc in the user’s home directory can add C: /Documents and Settings/Administrator/eme 5 directory to the command search paths. _path = stradd(_path, “C: /Documents and Settings/Administrator/eme 5; ”); Unix, Linux, and Mac machines: adding following two statements in the startup file. chrc in the user’s home directory can add /home/harry/eme 5 directory and the current working directory to the command search paths. _path = stradd(_path, “/home/harry/eme 5; ”); Add an alias(“go”, “cd C: /Documents and Settings/Administrator/eme 5”); // for Windows alias(“go”, “cd /home/harry/eme 5”); Created by Harry H. Cheng, 2009 Mc. Graw-Hill, Inc. All rights reserved. // for Unix

C for Engineers and Scientists: An Interpretive Approach Open startup file _chrc in Windows

C for Engineers and Scientists: An Interpretive Approach Open startup file _chrc in Windows or. chrc for Unix for editing through Ch. TE Created by Harry H. Cheng, 2009 Mc. Graw-Hill, Inc. All rights reserved.

C for Engineers and Scientists: An Interpretive Approach ‡ Slides for optional topics in

C for Engineers and Scientists: An Interpretive Approach ‡ Slides for optional topics in Ch Created by Harry H. Cheng, 2009 Mc. Graw-Hill, Inc. All rights reserved.

C for Engineers and Scientists: An Interpretive Approach Ch Scripts The function main() is

C for Engineers and Scientists: An Interpretive Approach Ch Scripts The function main() is optional for a Ch script program. Like a command file, a script file shall have both read and execute permissions. /* File: welcome. ch */ printf(“Welcome to Chn”); Created by Harry H. Cheng, 2009 Mc. Graw-Hill, Inc. All rights reserved.

C for Engineers and Scientists: An Interpretive Approach Ch Scripts (Cont. ) A program

C for Engineers and Scientists: An Interpretive Approach Ch Scripts (Cont. ) A program with #!/bin/ch can be invoked by other programs such as Bash, C shell. Statements, functions, and commands can be grouped as a script file or script in Ch #!/bin/ch /* File: hello. ch */ #include <stdio. h> int main() { printf(“Hello, Worldn”); return 0; } Created by Harry H. Cheng, 2009 Mc. Graw-Hill, Inc. All rights reserved.