Study Guide for Test 2 Topics: decisions, loops, arrays, c-strings, linux Material Selected from: Chapters 4, 5, 6, 7, 10.1, 10.2, 10.3, 10.4 Examples 14 33 Assignments 4 8 Any syntax errors are unintentional 1. Know what is meant by and how to use the following linux commands: pwd, cd, rm, cp, mkdir, mv 2 10. Problems 31 40, pp 218-219, Gaddis 11-12. Problems 63 64, p. 221, Gaddis 13. Using a while loop, display the first five non-negative integers 14. Using a for loop, display the first five alphabetic characters 15. Using a while loop, display the first five non-negative integers in reverse 16. Using a for loop, display the first five non-negative integers in reverse
What is the output? int main() int x, y; x = y = 0; while(x < 5) y += x; ++x; cout << x << << y << endl ; 16. Write the following function: pre: limit is an integer >= 2 post: returns the sum of the integers in the closed interval [1..limit] using a for loop int add(int limit) 17. Do problem 16 using a while loop
18. What is the output?... cout << whatisit(9, 3, 17) << endl;... int whatisit(int x, int y, int z) if (x > y && x > z) return x; else if (y > x && y > z) return y; else if (z > x && z > y) return z; else return 0; 19. Rewrite problem 18 without using a nested if 20. Write the following function: pre: x, y, z are integers post: returns the smallest of the arguments. If two or three of the arguments are tied for the smallest, returns one of them. int findsmallest(int x, int y, int z) 21. Write the following function: pre: base and exp are integers with base >= 1 and exp >= 0 post: returns base to the exp power without using a built-in function int myexp(int base, int exp)
22. Write the following function using a loop construction of your choice pre: value is an integer >= 0 post: returns the factorial of num. Example: fact(0) = 1, fact(3) = 3 * 2 * 1 int fact(int num) 23. Write the following function: pre: num is an integer > 1 pre: returns true if num is prime, false otherwise bool isprime(int num) 23. Write the following function pre: num is a positive integer post: returns true if num is odd, false otherwise bool isodd(int num) 24. Write the following function pre: num is an integer > 0 post: displays all integers in the closed interval [0..num] but in reverse void disprev(int num) 25 Write the following function pre: stuff is an integer array of size howmany. It is filled with integers post: returns the sum of the integers in the array int add(int stuff[], int howmany)
26. Write the following function using the function you wrote for problem 23 pre: stuff is an array containing size integers post: returns the number of even integers in the array int evens(int stuff[], in size) 27. Write the following function Pre: stuff is an C-string Post: if target is in the array, its position is returned, else returns -1 is returned int linsrch(char stuff[], ch target) 29. What is the big O complexity of linsrch in problem 27? 30. Rewrite the following to its equivalent using DeMorgan s Law: if (!(x == 1 && y == 2)) cout << Negation with logical AND << endl; 31. Write the following function: pre: data is a c-string post: returns the number of upper case characters in the string using either a function that you write or one of the character functions found on p. 558 int howmany(char data[]) 32. Create an character array of 6 positions. Store the first five lower case characters of the alphabet in the array using a loop. Transform the character array to a c-string. 33. Display the characters stored in the array from problem 32 in reverse using a loop and strlen. 34. How many bytes are allocated in this declaration: int data[10];
35. How many bytes are allocated in this declaration: char data[10]; 36. Write a piece of code that a. asks the user to input a line of < 80 characters b. accepts the characters into a previously declared 81 position character array c. displays the result with a single statement d. why does c work? 37. Write a piece of code that a. declares a ten position integer array b. Stores the first ten non-negative integers in the array c. Displays the contents of the array after the array has been filled with values. 38. Write a piece of code that: a. declares a 6 position character array b. uses cin.get() to fill the array with the first five upper case alphabetic characters c. transforms the array to a c-string. d. displays the array with a single statement after it has been filled.
39. Write the following function: Pre: data is a c-string which holds alphabetic characters Post: returns the number of alphabetic characters in data int mystrlen(char data[]) 40. What is the output? #include<iostream> using namespace std; void swap(int,int); int main() int x = 7; int y = 8; swap(x,y); cout << x << << y << endl; return 0; void swap(int x, int y) int buf = x; x = y; y = buf;
41. What is the output? #include<iostream> using namespace std; void swap(int&,int&); int main() int x = 7; int y = 8; swap(x,y); //here 1 cout << x << << y << endl; return 0; void swap(int& x, int& y) //here 2 int buf = x; x = y; y = buf; 42. The parameters in the line labeled //here1 in problems 41 are called: parameters 43. The parameters in the line labeled //here 2 in problems 41 are called: parameters
44. What is the output? #include<iostream> using namespace std; void swap(int,int); int main() int x = 7; int y = 8; swap(x,y); cout << x << << y << endl; return 0; void swap(int a, int b) int buf = a; a = b; b = buf;
45. What is the output? #include<iostream> using namespace std; void swap(int&,int&); int main() int x = 7; int y = 8; swap(x,y); cout << x << << y << endl; return 0; void swap(int& a, int& b) int buf = a; a = b; b = buf;