Top Banner
Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal
50

Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.

Dec 20, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.

Tuesday, January 09, 2007

Memory is necessary for all operations of reason.

- Blaise Pascal

Page 2: Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.

Write C++ code that prints out all of the powers of 2 from 2 to 128 i.e., 2 4 8 16 32 64 128. Multiple cout statements are not acceptable, you must use some looping mechanism.

SELF TEST

Page 3: Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.

What is the output of the following program segment?int i, j, N=5;for(i=0; i<N; i++){

for(j=N-1; j>0; j--){if(j>i) continue;cout<<j<<" ";

}cout<<endl;

}

SELF TEST

Page 4: Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.

int g=100;

int main(){

int a=30;

int b=40;

cout<<a<<" "<<b<<" "<<g<<endl;

function1(a,b);

cout<<a<<" "<<b<<" "<<g<<endl;

a=function2(a,b);

cout<<a<<" "<<b<<" "<<g<<endl;

b= a + function3(a,b);

cout<<a<<" "<<b<<" "<<g<<endl;

return 0;}

int function1(int a, int b){

a=30;

b=40;

g=a-b;

return a+b; }

int function2(int a, int b){

g=b;

a=3;

b=4;

return 2*b*a;}

int function3(int a, int b){

g=50-a;

a=b-a;

b=3-a;

return a+b; }

Page 5: Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.

int g=100;

int main(){

int a=30;

int b=40;

cout<<a<<" "<<b<<" "<<g<<endl;

30 40 100

function1(a,b);

cout<<a<<" "<<b<<" "<<g<<endl;

a=function2(a,b);

cout<<a<<" "<<b<<" "<<g<<endl;

b= a + function3(a,b);

cout<<a<<" "<<b<<" "<<g<<endl;

return 0;}

int function1(int a, int b){

a=30;

b=40;

g=a-b;

return a+b; }

int function2(int a, int b){

g=b;

a=3;

b=4;

return 2*b*a;}

int function3(int a, int b){

g=50-a;

a=b-a;

b=3-a;

return a+b; }

Page 6: Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.

int g=100;

int main(){

int a=30;

int b=40;

cout<<a<<" "<<b<<" "<<g<<endl;

30 40 100

function1(a,b);

cout<<a<<" "<<b<<" "<<g<<endl;

30 40 -10

a=function2(a,b);

cout<<a<<" "<<b<<" "<<g<<endl;

b= a + function3(a,b);

cout<<a<<" "<<b<<" "<<g<<endl;

return 0;}

int function1(int a, int b){

a=30;

b=40;

g=a-b;

return a+b; }

int function2(int a, int b){

g=b;

a=3;

b=4;

return 2*b*a;}

int function3(int a, int b){

g=50-a;

a=b-a;

b=3-a;

return a+b; }

Page 7: Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.

int g=100;

int main(){

int a=30;

int b=40;

cout<<a<<" "<<b<<" "<<g<<endl;

30 40 100

function1(a,b);

cout<<a<<" "<<b<<" "<<g<<endl;

30 40 -10

a=function2(a,b);

cout<<a<<" "<<b<<" "<<g<<endl;

24 40 40

b= a + function3(a,b);

cout<<a<<" "<<b<<" "<<g<<endl;

return 0;}

int function1(int a, int b){

a=30;

b=40;

g=a-b;

return a+b; }

int function2(int a, int b){

g=b;

a=3;

b=4;

return 2*b*a;}

int function3(int a, int b){

g=50-a;

a=b-a;

b=3-a;

return a+b; }

Page 8: Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.

int g=100;

int main(){

int a=30;

int b=40;

cout<<a<<" "<<b<<" "<<g<<endl;

30 40 100

function1(a,b);

cout<<a<<" "<<b<<" "<<g<<endl;

30 40 -10

a=function2(a,b);

cout<<a<<" "<<b<<" "<<g<<endl;

24 40 40

b= a + function3(a,b);

cout<<a<<" "<<b<<" "<<g<<endl;

24 27 26 return 0;}

int function1(int a, int b){

a=30;

b=40;

g=a-b;

return a+b; }

int function2(int a, int b){

g=b;

a=3;

b=4;

return 2*b*a;}

int function3(int a, int b){

g=50-a;

a=b-a;

b=3-a;

return a+b; }

Page 9: Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.

void f1();

int main() {

char str[]="this is str in main()";

cout << str << '\n';

f1();

cout << str << '\n';

return 0;

}

void f1() {

char str[80];

cout << "Enter something: ";

cin >> str;

cout << str << '\n';

}

SELF TEST: FUNCTIONS : Local variables

Page 10: Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.

int main() {

char s1[80]="main string";

int choice;

cout << "(1) add numbers or ";

cout << "(2) concatenate strings?: ";

cin >> choice;

if(choice == 1) {

int a, b; /* activate two integer vars */

cout << "Enter two numbers: ";

cin >> a >> b;

cout << "Sum is " << a+b << '\n';

char s1[80]="if block string";

cout << "s1= " << s1 << '\n';

}

SELF TEST: Local to a block

Page 11: Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.

else {

char s1[80], s2[80]; /* activate two strings */

cout << "Enter two strings: ";

cin >> s1;

cin >> s2;

strcat(s1, s2);

cout << "Concatenation is " << s1 << '\n';

}

int a=34;

cout << "a= " << a << '\n';

cout << "s1= " << s1 << '\n';

return 0;

}

SELF TEST: Local to a block

Page 12: Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.

int main() {

int i, j;

i = 10;

j = 100;

if(j > 0) {

int i; // this i is separate from outer i

i = j / 2;

cout << "inner i: " << i << '\n';

}

cout << "outer i: " << i << '\n';

return 0;

}

SELF TEST: Local to a block

Page 13: Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.

void drill();

int count; // count and num_right are global

int num_right;

int main() {

cout << "How many practice problems: ";

cin >> count;

num_right = 0;

do {

drill();

count--;

} while(count);

cout << "You got " << num_right << " right.\n";

return 0;

}

SELF TEST: Global Variables

Page 14: Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.

void drill() {

int count; /* This count is local and unrelated to the global one.*/

int a, b, ans;

// Generate two numbers between 0 and 99.

a = rand() % 100;

b = rand() % 100;

// The user gets three tries to get it right.

for(count=0; count<3; count++) {

cout << "What is " << a << " + " << b << "? ";

cin >> ans;

if(ans==a+b) {

cout << "Right\n";

num_right++;

return; }

}

cout << "You've used up all your tries. The answer is " << a+b << '\n';

}

SELF TEST: Global Variables

Page 15: Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.

Applications of sorting An important key to algorithm design is to use

sorting as a basic building block, because once a set of items is sorted, many other problems become easy.

Employee records, telephone books, tax records, student records, frequency distribution, CS192 grades…

Page 16: Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.

Applications of sorting

Sorting is fundamental to most other algorithmic problems, for example binary search.

Speeding up searching is perhaps the most important application of sorting.

Page 17: Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.

Searching Sequential Search

3, 6, 7, 10, 14, 15, 19, 23, 26, 28, 31, 34, 35, 39, 43, 46, 47, 49, 51, 52, 57, 58, 60, 61, 65, 67, 70, 72, 73, 78, 85, 87, 88, 90, 93, 94, 97, 98, 102, 104, 105, 111, 120, …1000

Page 18: Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.

Searching Binary Search

3, 6, 7, 10, 14, 15, 19, 23, 26, 28, 31, 34, 35, 39, 43, 46, 47, 49, 51, 52, 57, 58, 60, 61, 65, 67, 70, 72, 73, 78, 85, 87, 88, 90, 93, 94, 97, 98, 102, 104, 105, 111, 120, …1000

Break problem down into a smaller problem

Page 19: Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.

Strings

char myString[80]="i am a string";

int i=0;

while(myString[i]){

char Upper=myString[i]-(97-65);

cout<<Upper;

i++;

}

Page 20: Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.

Strings

I AM A STRING

Page 21: Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.

char words[3];

strcpy (words,

“Now, here is a long string.”);

What is wrong here?

Page 22: Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.

char words[3];

strcpy (words,

“Now, here is a long string.”);

Will continue to fill whatever memory follows last indexed variable of words, even though this memory may be used for something else.

What is wrong here?

Page 23: Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.

char name[9]={'p', 'a', 'k', 'i', 's', 't', 'a', 'n'};

cout<<name<<endl;

What is wrong here?

Page 24: Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.

Arrays int a[10] = {1,2,3,4,0,3,4,6,7,8};

int x=3; y=1;

cout<< a[x+2*y] <<“\n”;

cout<<“Answer= “<<a[a[a[4]]]<<“\n”;

Output?

Page 25: Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.

Arrays int a[10] = {1,2,3,4,0,3,4,6,7,8};

int x=3; y=1;

cout<< a[x+2*y] <<“\n”;

cout<<“Answer= “<<a[a[a[4]]]<<“\n”;

Output:

3

Answer= 2

Page 26: Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.

The name of the array is the address of the first element. It is a constant.

int a[5]={13, 5, 6, 34, 6};cout<<a[1]<<endl;cout<<a[3]<<endl;cout<<a[4]<<endl;cout<<a<<endl;

Page 27: Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.

The name of the array is the address of the first element. It is a constant.

53460x0012FEC4

Page 28: Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.

char str[]="fruit";cout<<str[1]<<endl;cout<<str[3]<<endl;cout<<str[4]<<endl;cout<<str<<endl;

Page 29: Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.

ritfruit

Page 30: Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.

What is wrong with following?

char str1[80]=“i am a string”;char str2[80]=“i am a string”;

if (str1==str2) cout<<“equal”;

else cout<<“not equal”;

Page 31: Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.

What is wrong with following?

char str1[80]=“i am a string”;char str2[80]=“i am a string”;

if (str1==str2) cout<<“equal”;

else cout<<“not equal”;

// Use strcmp(str1, str2)

Page 32: Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.

•What is wrong with following?

char str1[80]="cs192";char str2[80];str2=str1;

Page 33: Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.

•What is wrong with following?

char str1[80]="cs192";char str2[80];str2=str1; //Error

Use strcpy(str2, str1);

Page 34: Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.

int twoD[4][2]={1, 10,2, 13,3, 34,4, 15

};

• Picture of array

twoD[1][1] is twoD[2][0] is

Page 35: Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.

int twoD[4][2]={1, 10,2, 13,3, 34,4, 15

};

• Picture of array

twoD[1][1] is 13twoD[2][0] is 3

Page 36: Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.

Arrays of stringschar strings[6][80]={

"one","two",{'t','h','r','e','e',‘\0’},"four","five",""

}; • Picture of array

cout<<strings[1][1]<<endl;cout<<strings[2][0]<<endl;cout<<strings[1]<<endl;cout<<strings[2]<<endl;

Page 37: Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.

Arrays of stringschar strings[6][80]={

"one","two",{'t','h','r','e','e', '\0'},"four","five",""

}; • Picture of array

cout<<strings[1][1]<<endl; // wcout<<strings[2][0]<<endl; // tcout<<strings[1]<<endl; //twocout<<strings[2]<<endl; //three

Page 38: Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.

Arrays of stringschar strings[6][80]={

"one","two",{'t','h','r','e','e’,‘\0’},"four","five",""

};int i=0;while(strings[i][0]){

cout<<strings[i]<<endl;i++;

}

Page 39: Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.

Most of C++’s power is derived from pointers.

They allow different sections of code to share information easily.

Pointers enable complex data structures like linked lists.

Pointers

Page 40: Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.

Assumptions:characters are one byte in lengthintegers are four bytes longfloats are four bytes longdoubles are eight bytes long

Pointers

Page 41: Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.

A pointer is a variable that holds a memory address.

Pointers

Page 42: Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.

int x=5;

int* xptr;

5 x of type int0x0012F578

0x0012F690 xptr of type int*

Page 43: Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.

int x=5;

int* xptr;

xptr=&x; //points to x

5

0x0012F578

x of type int0x0012F578

0x0012F690 xptr of type int*

Page 44: Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.

int x=5;

int* xptr;

xptr=&x; //points to x

5 x of type int0x0012F578

xptr of type int*

Page 45: Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.

int x=5;

int* xptr;

xptr=&x; //points to x

5

x of type intxptr of type int*

Page 46: Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.

int x=5;

int* xptr;

xptr=&x; //points to x

The most common error is forgetting to initialize the pointer

5

x of type intxptr of type int*

Page 47: Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.

There are two special operators that are used with pointers: * and &The & is a unary operator that returns the memory address of its operand.int balance = 350;int *balptr;balptr = &balance;This address is the location of the variable balance in memory, it has nothing to do with the value of balance.

Pointer Operators

Page 48: Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.

The second operator * is the complement of &. It is a unary operator that returns the value of variable located at address specified by its operand.

int balance = 350;int *balptr;balptr = &balance;int value;value = *balptr; //what does value contain?

Pointer Operators

Page 49: Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.

& address of operator

* value of pointee(de-referencing operator)

Pointer Operators

Page 50: Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal.

When a pointer is first allocated it does not point to anything.

Trying to de-reference an un-initialized pointer is a serious runtime error.

If you are lucky the dereference will crash or halt immediately.

If you are unlucky it will corrupt a random area of memory – so that things go wrong after some indefinite time.

Pointers