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

Post on 20-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

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

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

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; }

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; }

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; }

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; }

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; }

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

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

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

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

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

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

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…

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.

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

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

Strings

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

int i=0;

while(myString[i]){

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

cout<<Upper;

i++;

}

Strings

I AM A STRING

char words[3];

strcpy (words,

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

What is wrong here?

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?

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

cout<<name<<endl;

What is wrong here?

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?

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

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;

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

53460x0012FEC4

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

ritfruit

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”;

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)

•What is wrong with following?

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

•What is wrong with following?

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

Use strcpy(str2, str1);

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

};

• Picture of array

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

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

};

• Picture of array

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

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;

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

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

}

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

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

Pointers

A pointer is a variable that holds a memory address.

Pointers

int x=5;

int* xptr;

5 x of type int0x0012F578

0x0012F690 xptr of type int*

int x=5;

int* xptr;

xptr=&x; //points to x

5

0x0012F578

x of type int0x0012F578

0x0012F690 xptr of type int*

int x=5;

int* xptr;

xptr=&x; //points to x

5 x of type int0x0012F578

xptr of type int*

int x=5;

int* xptr;

xptr=&x; //points to x

5

x of type intxptr of type int*

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*

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

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

& address of operator

* value of pointee(de-referencing operator)

Pointer Operators

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