computer engineering 2 nd semester rabie a. ramadan assignment e-mail: [email protected] 1

48
Computer Engineering 2 nd Semester Rabie A. Ramadan http://people.smu.edu/raramada/ Assignment E-mail: [email protected] 1

Upload: dorthy-ramsey

Post on 13-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Computer Engineering 2nd Semester

Rabie A. Ramadan

http://people.smu.edu/raramada/

Assignment E-mail:

[email protected]

1

2

Syllabus

Introduction C++ Crash Course Computer Orgnization

3

Class Rules

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

4

Class Rules 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

5

Class Rules

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

6

Class Rules

Text Books• Rabie Ramadan , Computer Engineering for Developers, 2008

• Other sources

Class Rules

You can bring anything to drink but NO FOOD PLEASE

When you come in , DO not knock on the door

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?

10

Game Conclusion

Stay Focused

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 !

Game No. 2 Draw a square made up of dots like this one on your

piece of paper

Now, without lifting the pencil from the page, draw no more than four straight lines which will cross through all nine dots

Solution 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

14

Introduction

C++ Environment Creating a Program Hello from C++ The Elements of a Program

15

C++ Environment

Libraries • .h• .com• .lib• ..

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

17

Is C++ have a complier or Interpreter?

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

18

C++ Input/output 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

19

Hello from C++ 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

20

Hello from C++ /*********************************************************** * hello -- program to print out "Hello World". * * Not an especially earth-shattering program. * * Author: Steve Oualline * * Purpose: Demonstration of a simple program * * Usage: * * Run the program and the message appears * ***********************************************************/ #include <iostream.h> main () { // Tell the world hello cout << "Hello Dear!\n"; return (0); }

Comments

Library

Your code

21

Elements of a Program Plan your program before writing it

• Think as house builder

Decide on variables • Data • Must be declared before using them

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++

Be a good C++ Builder

C++ Basic Data Types

22

bool char int float double long void

Enumeration Data Types

23

holds a set of values specified by the user Once defined, an enumeration is used very much like an integer

type. Example:

enum keyword {ASM ,AUTO, BREAK }; enumerator values are assigned increasing from 0 , so ASM

==0 , AUTO ==1 , and BREAK ==2 .

void f (keyword key ){

switch (key ) {case ASM :

/ / do somethingbreak ;

case BREAK :/ / do something

break ;} } 

Expression Statements

24

Computes an expression, such as a function call or assignment

Example:

42; // Valid but pointlesscout << 42; // More typicalx = y * z; // Remember that assignment is an expression; // Null statement

Declarations

25

In C++ , declarations appear anywhere a statement appears

Example :

int y = 0; while ( test( ) )

int x = init( );

Comments

26

• 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 */

Arrays

27

A consecutive group of memory locations that all have the same type

Definition Datatype name [size] ;

Example: int c[10]; c[ 0] = 2;c[a+b] += 5;X= c[0];

Group Activity

28

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 ?

Pointers

A variable that holds a memory address

Address is the location of another object (typically, a variable) in memory.

Memory Address 1000 is a pointer

to 1004 Pointer Declaration

type *name;

The type refers to the type of

memory contents

Pointers (Cont.)

Pointers Operators

• * and &. & is a unary operator that returns the memory address of its operand.

p = &num; p holds the memory address of the variable num

* 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 = &num; /* p receives num's address */

q = *p; /* q is assigned num's value

indirectly through p */

cout << q; /* prints 100 */

return 0;

}

Group Activity

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 2What is the Output?

#include <stdio.h>

int main(void)

{

int x;

int *p1, *p2;

p1 = &x;

p2 = p1;

printf("%p %p", p1, p2);

return 0;

}

Displays the addresses held byp1 and p2. They will be

the same.

Pointer Increment/Decrement

If p is an integer pointer and holds 2000. The int is 4 bytes long , then

p++

increment p to ? 2004

p -- decrement it to ? 1996

Be carful, you might get a negative address

Example Assume 2-byte short integers

Functions/Methods 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.)

Call by Value,

• 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)

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 Arrays:

• Stores a group of similar data types

Structure:

• Could have different data types

General Form : struct structure-name {

field-type field-name // Comment

field-type field-name // Comment

....

} variable-name;

 

Structures (Cont.)

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)

} printer_cable_box; // 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.)

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 Allows you to define your own variable types

Format:

typedef type-declaration;

Example: typedef int width; // Define a type that is the width of an object

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.