6c802c180bdbea2b2bbe92000dcb6ba8.ppt
- Количество слайдов: 48
Computer Engineering 2 nd Semester Rabie A. Ramadan http: //people. smu. edu/raramada/ Assignment E-mail: rabieramadan@gmail. com 1
Syllabus l l l Introduction C++ Crash Course Computer Orgnization 2
Class Rules l l • • It will be a tough class , however, Enjoy what you do Do not worry about the exam as long as : You are attending Aware of everything I mention in class Aware of everything said in the section I do not care how are you going to learn programming language The section will help you to do so but it is your responsibility to dig in the subject 3
Class Rules l l • Attendance is a must If you are absent for three time without any reasonable reason do not ask me why I did not pass in the class and there is no need to attend anymore. Assignments must be delivered on time no exception whatever the reason. Assignments must be submitted in electronic format no papers will be accepted The source code must be submitted with any programming assignment 4
Class Rules l • Projects There will be a term project • Maximum 4 persons per project • You can select your own project after my approval; otherwise I will provide a project for you by next lecture • Project report must follow IEEE format • Suggested Projects 5
Class Rules l • • Text Books Rabie Ramadan , Computer Engineering for Developers, 2008 Other sources 6
Class Rules l You can bring anything to drink but NO FOOD PLEASE l When you come in , DO not knock on the door l When you want to leave , do not tell me Just leave but you will counted as absent
Before We Start …. Lets Play Games for 5 Minutes
How many black dots?
Game Conclusion l Stay Focused l Sometimes you might be deceived • • Still early ! It is easy , I can read in one day ! The exam will be easy ! My friend will help me ! My answer is : Do not be deceived ! 10
Game No. 2 l Draw a square made up of dots like this one on your piece of paper l Now, without lifting the pencil from the page, draw no more than four straight lines which will cross through all nine dots
Solution • Lessons Learned • Do not discard small details • Ask questions • You might think that things are very complicated but with little guide it becomes very easy
Hint One line can go out of the paper
Introduction l l C++ Environment Creating a Program Hello from C++ The Elements of a Program 14
C++ Environment l • • Libraries. h. com. lib. . 15
Problem That You Might Face • Programs not always run at the first time • Syntax Errors Check your code • return to the edit phase and modify the code • • Warnings check your logic • return to the edit phase and modify the code • Bugs use debugger • Very important to find the logical errors 16
Is C++ have a complier or Interpreter? l l • • Compiler Goes over the whole program and gives you the errors at the end Interpreter Check the syntax and executes the program line by line 17
C++ Input/output l l Input • cin keyboard • Other devices camera, … • Command line or graphical user interface • • • Output Screen Other devices such as file or printer cerr is a function that is associated with the output devices 18
Hello from C++ l l • • Step 2: Files Extension C++ source code file names often end with the. cpp, . cxx, . cc or. C extensions (note that C is in uppercase) which indicate that a file contains C++ source code Step 3: Is it Compiler or Interpreter ? preprocessor phase is done internally for reformatting the code. It compiles • Read all of your code and gives you the errors. • followed by loading and execution 19
Hello from C++ l l l l /****************************** * hello -- program to print out "Hello World". * Comments * Not an especially earth-shattering program. * * Author: Steve Oualline * * Purpose: Demonstration of a simple program * * Usage: * * Run the program and the message appears * ******************************/ Library #include <iostream. h> main () { // Tell the world hello cout << "Hello Dear!n"; Your code return (0); } 20
Elements of a Program l l l • • Plan your program before writing it Think as house builder Decide on variables Data Must be declared before using them Be a good C++ Builder Decide on instructions Instructions tell the computer what to do with the variables Write a lot of comments Help you and others to remember and understand your code Divide your code into small modules (Functions/Methods) Do not build the house on one shout build room by room Classes are introduced in C++ 21
C++ Basic Data Types l l l l bool char int float double long void 22
Enumeration Data Types l l l holds a set of values specified by the user Once defined, an enumeration is used very much like an integer type. Example: l enum keyword {ASM , AUTO, BREAK }; l enumerator values are assigned increasing from 0 , so ASM ==0 , AUTO ==1 , and BREAK ==2. void f (keyword key ) { switch (key ) { case ASM : / / do something break ; case BREAK : / / do something break ; } } 23
Expression Statements l Computes an expression, such as a function call or assignment l Example: 42; // Valid but pointless cout << 42; // More typical x = y * z; // Remember that assignment is an expression ; // Null statement 24
Declarations l In C++ , declarations appear anywhere a statement appears l Example : int y = 0; while ( test( ) ) int x = init( ); 25
Comments • • • Comments start with /* and end with */. These comments do not nest. Example: /* this is a comment /* still a comment */ int not_in_a_comment; • • A comment can also start with //, extending to the end of the line. Example: const int max_widget = 42; // Largest size of a widget • • You can "nest" one kind of comment within the other kind. Example: /* Comment out a block of code: const int max_widget = 42; // Largest size of a widget */ 26
Arrays l A consecutive group of memory locations that all have the same type l Definition l l Datatype name [size] ; Example: int c[10]; c[ 0] = 2; c[a+b] += 5; X= c[0]; 27
Group Activity l Write a simple program that fills out an array of 5 elements with the numbers 1, 3, 3, 5, 6. Print out the array on the screen ? 28
Pointers l A variable that holds a memory address l Address is the location of another object (typically, a variable) in memory. l Memory Address 1000 is a pointer to 1004 Pointer Declaration l type *name; The type refers to the type of memory contents
Pointers (Cont. ) l Pointers Operators • * and &. & is a unary operator that returns the memory address of its operand. l p = # p holds the memory address of the variable num l l * is the complement of &. • Returns the value of the variable located at the address that follows. q = *p; • If p contains 100 then q =100.
Example #include <stream. h> int main(void) { int num, q; int *p; num = 100; /* num is assigned 100 */ p = # /* p receives num's address */ q = *p; /* q is assigned num's value indirectly through p */ cout << q; /* prints 100 */ return 0; }
Group Activity l What is Wrong ? #include <stdio. h> int main(void) { double x, y; int *p; x = 100. 123; p = &x; y = *p; // this is a warning printf("%f", y); return 0; } Error Wrong data Type
Group Activities 2 What is the Output? #include <stdio. h> int main(void) { int x; int *p 1, *p 2; p 1 = &x; p 2 = p 1; printf("%p %p", p 1, p 2); return 0; } Displays the addresses held by p 1 and p 2. They will be the same.
Pointer Increment/Decrement If p is an integer pointer and holds 2000. l The int is 4 bytes long , then p++ increment p to ? 2004 p -l decrement it to ? 1996 Be carful, you might get a negative address l
Example l Assume 2 -byte short integers
Functions/Methods l l l Building blocks of C and C++ and the place where all program activity occurs. Format : ret-type function_name(parameter list) { body of the function } Example: void pr_reverse(char *s) { int t; for(t=strlen(s)-1; t >= 0; t--) printf("%c", s[t]); }
Function Declaration Pre declaration main() { Function Call } Implementation
Example #include <stdio. h> int mul(int a, int b); int main(void) { int x, y, z; x = 10; y = 20; z = mul(x, y); /* 1 */ printf("%d", mul(x, y)); /* 2 */ mul(x, y); /* 3 */ return 0; } int mul(int a, int b) { return a*b; }
Functions/Methods (Cont. ) l Call by Value, • • l Copies the value of an argument into the formal parameter of the subroutine Has no affect on the original variable Call by Reference • • the address of an argument is copied into the parameter The original argument/variable is affected by any change
Example (Call by value) l Call by value #include <stdio. h> int sqr(int x); int main(void) { int t=10; printf("%d %d", sqr(t), t); return 0; } int sqr(int x) { x = x*x; return x; } Changing x doesn’t affect t
Advanced Data types
Structures l l l Arrays: • Stores a group of similar data types Structure: • Could have different data types General Form : structure-name { field-type field-name // Comment. . } variable-name;
Structures (Cont. ) struct bin { char name[30]; int quantity; int cost; } printer_cable_box; // defines the data type // Name of the part // How many are in the bin // The cost of a single part (in cents) // define a bin variable struct bin { // defines the data type char name[30]; // Name of the part int quantity; // How many are in the bin int cost; // The cost of a single part (in cents) }; // No variable is defined struct bin printer_cable_box; // define a bin variable or bin printer_cable_box; // define a bin variable
Structures (Cont. ) l Assigning data to the structure elements: printer_cable_box. cost = 1295; // $12. 95 is the new price struct bin { char name[30]; // Name of the part int quantity; // How many are in the bin int cost; // The cost of a single part (in cents) } printer_cable_box = { "Printer Cables", // Name of the item in the bin 0, // Start with empty box 1295 // Cost -- $12. 95 };
Array of Structures struct time { int hour; // Hour (24 -hour clock) int minute; // 0 -59 int second; // 0 -59 }; ------------------------------------------#define MAX_LAPS 4 /* We will have only 4 laps*/ /* The time of day for each lap*/ struct time lap[MAX_LAPS]; ---------------------------------------------- lap[count]. hour = hour; lap[count]. minute = minute; lap[count]. second = second; ++count;
Union What is a Union ? Please look it up yourself and tell me if you have any question.
typedef l Allows you to define your own variable types l Format: typedef type-declaration; l Example: typedef int width; // Define a type that is the width of an object l Usage : width box_width; is the same as: int box_width;
Please read till the end of chapter 2 and prepare chapters 3 and 4 for next time.
6c802c180bdbea2b2bbe92000dcb6ba8.ppt