Sei sulla pagina 1di 69

MECH 482

Computer Technology in Engineering

MECH482-SKL-EME-FST-UM

Contact Information
Instructor: Lao Seng Kin, Skelton Office: N327C Phone: 8397-4379 e-mail: skeltonl@umac.mo Course webpage: UMMoodle TA: Leong Chan Long ()
MECH482-SKL-EME-FST-UM 2

Course Description
An overview of modern computer hardware, software and their interrelationship. Applications of the computer technologies in engineering. Basic knowledge in object-oriented programming (OOP) using Visual C++, fundamentals of software development.
MECH482-SKL-EME-FST-UM 3

Course Objectives
Learn to apply knowledge of computing and mathematics to programming. [a] Learn to design, implement, and evaluate a computerbased system, process, or program to meet desired needs. [c] Acquire abilities to analyze a problem and identify the computing requirements appropriate for its solution. [e] Learn to recognize the need for an ability to engage in continuing professional development. [i] Learn to use current techniques, skills, and tools necessary for computing practice. [l]

MECH482-SKL-EME-FST-UM

Related Programme Outcomes


(a) an ability to apply knowledge of mathematics, science, and engineering. (c) An ability to design a system, component, or process to meet desired needs within realistic constraints, such as economic, environmental, social, political, ethical, health and safety, manufacturability and sustainability; (e) an ability to identify, formulate, and solve engineering problems. (i) an ability to recognize the need for, and to engage in lifelong learning. (l) an ability to use the computer/IT tools relevant to the discipline along with an understanding of their processes and limitations.
MECH482-SKL-EME-FST-UM 5

Course Assessment
Homework: Quizzes: Mid-term: Final Exam: Design Project: 10% 10% 20% 30% 30%

MECH482-SKL-EME-FST-UM

Downloadable Visual C++ 2010


All UM students are entitled to have FREE license for the following software:
Microsoft Developer Studio 2008 R3 Microsoft Embedded CD 6.0 Microsoft Expression Studio 4 Ultimate Microsoft MultiPoint Microsoft SQL Server 2008 Developer Microsoft SQL Server Express Microsoft Virtual PC Microsoft Visual Basic 2008 Express Edition Microsoft Visual C# 2008 Express Edition Microsoft Visual C++ 2008 Express Edition Microsoft Visual Studio 2005 Professional Edition Microsoft Visual Studio 2010 Express Microsoft Visual Studio 2010 Professional Microsoft Visual Web Developer 2008 Express Edition Microsoft Windows Phone Microsoft Windows Server 2003 Microsoft Windows Server 2008 R2 Standard Microsoft Windows Server 2008 Standard

Please visit Microsoft DreamSpark Program for Student web page: http://www.umac.mo/icto/intranet/pclan/stud_dreamspark.html
MECH482-SKL-EME-FST-UM 7

Part I Introduction

MECH482-SKL-EME-FST-UM

Definition of Computer

Computer a device that


Accepts Input Processes data Stores data Produces output

MECH482-SKL-EME-FST-UM

Overview of Computer Hardware


Computer hardware
Input devices:
Keyboard, mouse, scanner, digital camera, microphone, fingerprint scanner, touch screen, stylus

Data processing device:


CPU, GPU

Data storage devices:


RAM, hard drive, SD, MO, CDRW, DVDRW ROM, CD-ROM, DVD-ROM

Output devices:
Monitor, printer, loud speaker

MECH482-SKL-EME-FST-UM

10

Connection between Hardware


by ports:
Printer port / parallel port COM port / serial port (RS-232) USB port Firewire / iLink / IEEE-1394 port, etc. HDMI

by slots:
Slot 1, PCI bus, AGP slot, IDE, PCIe, etc.

and wirelessly:
wi-fi, bluetooth, etc.
MECH482-SKL-EME-FST-UM 11

Overview of Computer Software


A set of instructions, also referred to as a computer program, which tells the computer how to perform a particular task.
Operative system, such as Windows, MacOS. Productivity programs, such as PowerPoint. Tools, such as antivirus programs.

MECH482-SKL-EME-FST-UM

12

Software - Operative System


Decides how everything looks on the screen, how you access archives, how smoothly everything functions, like: Apple MacOS, Apple iOS, Linux, Ubuntu, Microsoft Windows, etc. with extensions like
Drivers for printers, scanners, modem Controls for screen, sound, time Typefaces/fonts Automated tasks Conversion between different formats
MECH482-SKL-EME-FST-UM 13

Software - Productivity Programs


For dealing with
Problem Solving Words Pictures Sounds Animations Movies Entertainment Time management, like personal organizers

MECH482-SKL-EME-FST-UM

14

Software - Tools
For
Maintenance, problem diagnosis and solving File compression/decompression Network connection & management Security Encryption/decryption Antivirus protection

MECH482-SKL-EME-FST-UM

15

Applications of the computer technology in engineering


Modeling, simulation, & visualization
Automotive engineering Computer-aided engineering Indoor air quality / Thermal engineering Fire safety engineering Material engineering Mechanical engineering Quality management Medical / Biotechnology

MECH482-SKL-EME-FST-UM

16

Part II

C & C++

MECH482-SKL-EME-FST-UM

17

Review of C
Ken Thompson and Dennis Ritchie

The Origins of C
BCPL (Basic Compiled Programming Language), invented by Martin Richard in 1969 B, invented by Ken Thompson in 1970 C, developed by Dennis Ritchie in 1972 ANSI C standard, adopted in December, 1989 ANSI/ISO C standard, 1990
MECH482-SKL-EME-FST-UM 18

C is a Middle-Level Language
Highest Level
Ada, Modula-2, Pascal, COBOL, FORTRAN, BASIC, MATLAB

Middle Level
Java, C#, C++, C, Objective-C , FORTH

Lowest Level
Macro-assembler, Assembler

MECH482-SKL-EME-FST-UM

19

Expressions
5 basic types:
char, single character int, integer float, floating point value, 6 digits of precision double, ten digits of precision void, empty, nothing

MECH482-SKL-EME-FST-UM

20

Data Types in ANSI/ISO C


Size in Bits char 8 unsigned char 8 int 16 or 32 long int 32 float double long double
MECH482-SKL-EME-FST-UM

Type

32 64 80

Minimal Range -127 to 127 0 to 255 -32767 to 32767 -2147483647 to 2147483647 6 digits of precision 10 digits 10 digits
21

Identifier Names
Correct
Count test23 high_balance

Incorrect
4count hi!there highbalance

MECH482-SKL-EME-FST-UM

22

Variables
type variable_list;
int i,j,k; short int si; unsigned int ui; double balance, profit, loss; char pname;

MECH482-SKL-EME-FST-UM

23

Local Variables
Variables that are declared inside a function are called local variables. The integer x in func1() has no bearing on or relationship to the x in func2().
void func1(void) { int x; x = 10; } void func2(void) { int x; x = -20; }

MECH482-SKL-EME-FST-UM

24

Local Variables
For reasons of convenience and tradition, most programmers declare all the variables used by a function immediately after the functions opening curly brace and before any other statements.
void f(void) { int t; t = 1; if (t==1) { char s[80]; printf(Enter name:); gets(s); /* do something */ } }

MECH482-SKL-EME-FST-UM

25

Global Variables
Global variables are known throughout the program and may be used by any piece of code. They will hold their value throughout the programs execution.

MECH482-SKL-EME-FST-UM

26

Variable Initializations
You can give variables a value as you declare them by placing an equal sign and a value after the variable name.
char ch = a; int first = 0; float balance = 123.33;

MECH482-SKL-EME-FST-UM

27

Storage Class Specifiers


There are 4 storage class specifiers in C:
extern static register auto (means local variable)

MECH482-SKL-EME-FST-UM

28

extern Variables
When linking two or more files together, extern tells what are the global variables in the program. File One
int x, y; char ch; int main(void) { /* */ } void func1(void) { x = 124; }

File Two
extern int x, y; extern char ch; void func22() { x = y / 10; } void func23() { y = 10; }

MECH482-SKL-EME-FST-UM

29

static Variables
static variables are permanent variables within their own function or file. Unlike global variables, they are not known outside their function or file, but they maintain their values between calls. An initialization value to a static local variable is assigned only once, at program start-up.
MECH482-SKL-EME-FST-UM 30

int func1() { static int san; san = san + 23; return san; }

int main() { int x, y; x = func1(); x = func1(); y = func2(); y = func2(); return 0; }

23 46 33 56

int func2() { static int sen = 10; sen = sen + 23; return sen; }
MECH482-SKL-EME-FST-UM

31

register Variables
register specifier requests that the compiler keep the value of a variable in a register of the CPU rather than in memory. Operations on a register variable could occur much faster than on a normal variable. Larger register objects like arrays may receive preferential treatment by the compiler. Only local variables can be applied as register.

MECH482-SKL-EME-FST-UM

32

Constants Hexadecimal and Octal


The number system based on 8 is called octal, and uses the digit 0 through 7. The number 10 is the same as 8 in decimal. The based 16 number system is called hexadecimal, and uses the digits 0 to 9 plus the letters A to F, which stands for 10, 11, 12, 13, 14, 15.

MECH482-SKL-EME-FST-UM

33

Hex and Oct


A hex constant begins with a 0x. An oct constant begins with a 0. int hex = 0x80; /* 128 in decimal */ int oct = 012; /* 10 in decimal */

MECH482-SKL-EME-FST-UM

34

Type conversion
When variables of one type are mixed with variables of another type, a type conversion will occur.
int x = 65; char ch; float f = 13.2; void func(void) { ch = x; x = f; f = ch; f = x; }

MECH482-SKL-EME-FST-UM

35

Multiple Assignment
C/C++ allows you to assign many variables the same value by using multiple assignments in a single statement. x = y = z = 3;

MECH482-SKL-EME-FST-UM

36

Increment and Decrement


C/C++ includes two useful operators not generally found in other computer languages. These are the increment and decrement operators, ++ and --. The operator ++ adds 1 to its operand, and -substracts one. x = x + 1; is the same as ++x;

MECH482-SKL-EME-FST-UM

37

++x and x++


prefix, ++x:
x = 10; y = ++x; /* y will be set to 11 */

postfix, x++:
x = 10; y = x++; /*y will be set to 10, then x is set to 11*/

MECH482-SKL-EME-FST-UM

38

Relational and Logical Operators


In C, true is any value other than zero. false is zero, 0. AND OR p && q p || q 0 0 0 1 0 1 1 1 NOT !p 1 1 0 0
39

p 0 0 1 1
MECH482-SKL-EME-FST-UM

q 0 1 0 1

The [] and () Operators


Parentheses are operators that increase the precedence of the operators inside them. Square brackets perform array indexing.
void func(void) { char s[10]; }

MECH482-SKL-EME-FST-UM

40

Spacing and Parentheses


You can add tabs and spaces to expressions to make them easier to read.
x=10/y+5; is the same as x = 10 / y + 5;

Redundant or additional parentheses do not cause errors or slow down the execution of an expression.
x = y/334*temp+127; is the same as x = (y/3) (34*temp) + 127;

MECH482-SKL-EME-FST-UM

41

Shorthand Assignments
There is a variation on the assignment statement, sometimes referred to as a shorthand assignment, that simplifies the coding of a certain type of assignment operation.
x = x + 10; is the same as x +=10; x = x 100; is the same as x =100;

MECH482-SKL-EME-FST-UM

42

Statements

MECH482-SKL-EME-FST-UM

43

Selection Statements
Selection statements
if if-else-if Exp1 ? Exp2 : Exp3 switch

MECH482-SKL-EME-FST-UM

44

Switch Statements
void menu(void) { char ch; ch = getchar(); switch(ch){ case 1: check_spelling(); break; case 2: correct_errors(); break; default : printf(No option selected); } }
MECH482-SKL-EME-FST-UM 45

Iteration Statements
for loop infinite loop while loop do-while loop:
do{ statement; }while(condition);

MECH482-SKL-EME-FST-UM

46

The for Loop


The general form of the for statement is for (initialization; condition; increment) { statement; } initialization: to set the loop control variable(s). condition: to determine when the loop exits. increment: define how the loop control variable(s) changes each time the loop is repeated.
MECH482-SKL-EME-FST-UM 47

Examples of the for loop


for (int x=0; x==100; x++) {} for (x=0, y=0; x+y<10; ++x) {} for (prompt(); t=readnum(); prompt()) {} for (x=0; x!=123; ) {} for ( ; ; ) {}

MECH482-SKL-EME-FST-UM

48

Jump Statement
return statement goto statement break statement exit() function continue statement

MECH482-SKL-EME-FST-UM

49

Arrays & Null-Terminated Strings

MECH482-SKL-EME-FST-UM

50

Single-Dimension Arrays
Arrays must be explicitly declared so that the compiler may allocate space for them in memory. double bal[100]; bal[3] = 12.33; All arrays have 0 as the index of their first element. Only bal[0] to bal[99].

MECH482-SKL-EME-FST-UM

51

Array Location
Single-dimension arrays are essentially lists of information of the same type that are stored in contiguous memory locations in index order. char a[5];
Element a[0] a[1] a[2] a[3] a[4] Address 1033 1034 1035 1036 1037

MECH482-SKL-EME-FST-UM

52

Generating a Pointer to an Array


You can generate a pointer to the first element of an array by simply specifying the array name, without any index.
int *p; int sample[10]; p = sample; /* the address, not the data value */

p is the address of the first element of sample.

MECH482-SKL-EME-FST-UM

53

Null-Terminated Strings
When declaring a character array that will hold a null-terminated string \0, you need to declare it to be one character longer than the largest string that it is to hold.
To declare an array str that can hold a 10character string, you would write:
char str[11];

0 H

1 e

2 l

3 l

4 o

6 R

7 e

8 x

9 10 . \0
54

MECH482-SKL-EME-FST-UM

Two-dimensional Arrays
To declare a two-dimensional integer array d of size 3 x 2, you would write:
int d[3][2];

To access point (3,2) of array d:


d[2][1] 0 1 2
MECH482-SKL-EME-FST-UM

0 13 124 87

1 44 45 27
55

Multi-Dimensional Arrays
The general form of a multidimensional array declaration is:
type name[size1][size2][sizeN];

Arrays of more than three dimensions are not often used because of the amount of memory they require.

MECH482-SKL-EME-FST-UM

56

Array Initialization
C/C++ allows the initialization of arrays at the time of their declaration.
int i[5] = {1, 3, 5, 7, 9}; char str[9]= Keep up!; int sqrs[3][2] = {1, 1, 2, 4, 3, 9};

MECH482-SKL-EME-FST-UM

57

Pointers

MECH482-SKL-EME-FST-UM

58

What are Pointers?


A pointer is a variable that holds a memory address. If a variable is going to hold a pointer, it must be declared as such. A pointer declaration consists of a base type, an *, and the variable name:
type *name;

MECH482-SKL-EME-FST-UM

59

The Pointer Operators


There are two pointer operators: * and &. The & is a unary operator that returns the memory address of its operand. You can think of & as returning the address of.

MECH482-SKL-EME-FST-UM

60

x y #include <stdio.h> int main(void) 100.1 { p double x = 100.1, y; double *p; /* the next statement causes p (which is an integer pointer) to point to a double. */ p = &x; /* the next statement sets y to be the value in the address that p points to. */ y = *p; printf(%f, y); /* output 100.1 */ return 0; }
MECH482-SKL-EME-FST-UM 61

Pointers: Variable vs. Array


p and q are pointers
x is a variable p = &x Correct! p = x Wrong! q = s q = &s s[80] is an array q points to the 1st array element in s Wrong!

*p = x Prevent!

q = &s[0] Correct! *q = s[0] Prevent! q = s[0] Wrong!


62

MECH482-SKL-EME-FST-UM

Functions
Functions are the building blocks of C and C++ and the place where all program activity occurs. The general form of a function:
return-type function-name(parameter list) { body of the function; }

MECH482-SKL-EME-FST-UM

63

argc and argv


Two built-in arguments to main()
The argc parameter holds the number of arguments on the command line and is an integer. The argv parameter is a pointer to an array of character pointers.

MECH482-SKL-EME-FST-UM

64

#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { if (argc!=2){ printf(You forgot to type your name.\n); exit(1); } printf(Hello %s, argv[1]); return 0; }
MECH482-SKL-EME-FST-UM 65

The return Statement


return causes an immediate exit from the function that it is in. It may be used to return a value. All functions, except those of type void, return a value. This value is specified by the return statement.

MECH482-SKL-EME-FST-UM

66

Function: Returning a value


#include <stdio.h> int mul(int a, int b); int main(void) { int x, y, z; x = 10; y = 20; z = mul(x, y); /* return value is assigned to z */ printf(%d, mul(x,y)); /* return value used by function */ mul(x,y); /* return value is lost */ return 0; } int mul(int a, int b) { return a*b; }
MECH482-SKL-EME-FST-UM 67

What Does main() Return?


the main() function returns an integer to the calling process, which is generally the operating system. In practice, most C/C++ compilers automatically return 0, but do not rely on this if portability is a concern.

MECH482-SKL-EME-FST-UM

68

Recursion
In C/C++, a function can call itself. A function is said to be recursive if a statement in the body of the function calls itself.
/* recursive */ int factr(int n){ int answer; if (n==1) return(1); answer = factr(n-1)*n; /* recursive call */ return(answer); }
MECH482-SKL-EME-FST-UM 69

Potrebbero piacerti anche