Download presentation
Presentation is loading. Please wait.
1
Tuesday, January 09, 2007 Memory is necessary for all operations of reason. - Blaise Pascal
2
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
3
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
4
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; }
5
int g=100; int main(){ int a=30; int b=40; cout<<a<<" "<<b<<" "<<g<<endl; 30 40100 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; }
6
int g=100; int main(){ int a=30; int b=40; cout<<a<<" "<<b<<" "<<g<<endl; 30 40100 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; }
7
int g=100; int main(){ int a=30; int b=40; cout<<a<<" "<<b<<" "<<g<<endl; 30 40100 function1(a,b); cout<<a<<" "<<b<<" "<<g<<endl; 30 40-10 a=function2(a,b); cout<<a<<" "<<b<<" "<<g<<endl; 24 4040 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; }
8
int g=100; int main(){ int a=30; int b=40; cout<<a<<" "<<b<<" "<<g<<endl; 30 40100 function1(a,b); cout<<a<<" "<<b<<" "<<g<<endl; 30 40-10 a=function2(a,b); cout<<a<<" "<<b<<" "<<g<<endl; 24 4040 b= a + function3(a,b); cout<<a<<" "<<b<<" "<<g<<endl; 24 2726 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; }
9
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
10
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
11
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
12
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
13
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
14
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
15
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…
16
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.
17
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
18
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
19
Strings char myString[80]="i am a string"; int i=0; while(myString[i]){ char Upper=myString[i]-(97-65); cout<<Upper; i++; }
20
Strings I AM A STRING
21
char words[3]; strcpy (words, “Now, here is a long string.”); What is wrong here?
22
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?
23
char name[9]={'p', 'a', 'k', 'i', 's', 't', 'a', 'n'}; cout<<name<<endl; What is wrong here?
24
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?
25
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
26
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;
27
The name of the array is the address of the first element. It is a constant. 5 34 6 0x0012FEC4
28
char str[]="fruit"; cout<<str[1]<<endl; cout<<str[3]<<endl; cout<<str[4]<<endl; cout<<str<<endl;
29
r i t fruit
30
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”;
31
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)
32
What is wrong with following? char str1[80]="cs192"; char str2[80]; str2=str1;
33
What is wrong with following? char str1[80]="cs192"; char str2[80]; str2=str1; //Error Use strcpy(str2, str1);
34
int twoD[4][2]={ 1, 10, 2, 13, 3, 34, 4, 15 }; Picture of array twoD[1][1] is twoD[2][0] is
35
int twoD[4][2]={ 1, 10, 2, 13, 3, 34, 4, 15 }; Picture of array twoD[1][1] is 13 twoD[2][0] is 3
36
Arrays of strings char 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;
37
Arrays of strings char strings[6][80]={ "one", "two", {'t','h','r','e','e', '\0'}, "four", "five", "" }; Picture of array cout<<strings[1][1]<<endl; // w cout<<strings[2][0]<<endl; // t cout<<strings[1]<<endl; //two cout<<strings[2]<<endl; //three
38
Arrays of strings char 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++; }
39
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
40
Assumptions: characters are one byte in length integers are four bytes long floats are four bytes long doubles are eight bytes long Pointers
41
A pointer is a variable that holds a memory address. Pointers
42
int x=5; int* xptr; 5 x of type int0x0012F578 0x0012F690xptr of type int*
43
int x=5; int* xptr; xptr=&x; //points to x 5 0x0012F578 x of type int0x0012F578 0x0012F690xptr of type int*
44
int x=5; int* xptr; xptr=&x; //points to x 5 x of type int0x0012F578 xptr of type int*
45
int x=5; int* xptr; xptr=&x; //points to x 5 x of type intxptr of type int*
46
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*
47
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
48
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
49
& address of operator * value of pointee (de-referencing operator) Pointer Operators
50
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.