Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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
Course Assessment
Homework: Quizzes: Mid-term: Final Exam: Design Project: 10% 10% 20% 30% 30%
MECH482-SKL-EME-FST-UM
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
MECH482-SKL-EME-FST-UM
Output devices:
Monitor, printer, loud speaker
MECH482-SKL-EME-FST-UM
10
by slots:
Slot 1, PCI bus, AGP slot, IDE, PCIe, etc.
and wirelessly:
wi-fi, bluetooth, etc.
MECH482-SKL-EME-FST-UM 11
MECH482-SKL-EME-FST-UM
12
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
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
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
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; }
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
MECH482-SKL-EME-FST-UM
33
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
MECH482-SKL-EME-FST-UM
37
postfix, x++:
x = 10; y = x++; /*y will be set to 10, then x is set to 11*/
MECH482-SKL-EME-FST-UM
38
p 0 0 1 1
MECH482-SKL-EME-FST-UM
q 0 1 0 1
MECH482-SKL-EME-FST-UM
40
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
MECH482-SKL-EME-FST-UM
48
Jump Statement
return statement goto statement break statement exit() function continue statement
MECH482-SKL-EME-FST-UM
49
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
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];
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
MECH482-SKL-EME-FST-UM
59
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
*p = x Prevent!
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
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
MECH482-SKL-EME-FST-UM
66
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