Presentation is loading. Please wait.

Presentation is loading. Please wait.

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Chapter 2: Getting Started.

Similar presentations


Presentation on theme: "Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Chapter 2: Getting Started."— Presentation transcript:

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

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

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

4 Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. 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, world\n" ); –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.

5 Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. 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 ChIDE Right brace } –Indicates end of main has been reached

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

7 Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. 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->SoftIntegration 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 ChIDE, click Ch

8 Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach 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 Or > gcc hell.c /*... create a.out */ > a.out Hello, world Run program hello.c in Ch. > hello.c Hello, world Run Program hello.c

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

10 Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach 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 Command chmod in Unix

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

12 Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Commonly Used Commands Ch supports most Unix and Windows commands CommandUsageDescription cd Change to the home directory cd dirChange to the directory dir cpcp file1 file2Copy file1 to file2 ls List contents in the working directory mkdirmkdir dirCreate a new directory dir pwd Print (display) the name of the working directory rmrm fileremove file chmodchmod +x fileChange the mode of file to make it executable chidechide file.cEdit and execute program file.c vivi fileEdit file

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

14 Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Files in Ch C:/Ch> pwd C:/Ch 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> C:/Ch/docs> ls README.TXT chguide.pdf chinstall.pdf chref.pdf man/

15 Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach A Sample Problem: The system in Figure1 (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 Figure1 (b). Figure1: The system diagram and FBD of a sample problem

16 Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. 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)

17 Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Problem Statement: For the system shown in Figure1(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.

18 Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach > (20-0.2*5*9.81)/5 2.0380 > Interactive Solution

19 Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach /* File: accel.c */ #include int main() { printf("Acceleration a = %f (m/s^2)\n", (20-0.2*5*9.81)/5); return 0; } Program 1: A simple C program. Acceleration a = 2.038000 (m/s^2) Output:

20 Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. 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.

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

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

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

24 Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach 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 Ch\n”); Ch Scripts

25 Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach 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 int main() { printf(“Hello, World\n”); return 0; } Ch Scripts (Cont.)


Download ppt "Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Chapter 2: Getting Started."

Similar presentations


Ads by Google