









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










void alphaTally(int occs[], char * str) { // REQUIRES: occs is an array of 26 integers where each is initialized to zero // str is a non-empty C-string composed exclusively of lowercase letters // EFFECTS: counts the number of times each lowercase letter in the alphabet // appears in str and stores the counts in the occs array where, for i = 1..26, // index (i-1) in the array corresponds to the ith^ letter in the alphabet. For // example, occs[0] corresponds to 'a', occs[1] to 'b', ..., and occs[25] to 'z'. char alphabet[] = "abcdefghijklmnopqrstuvwxyz"; char * current = str; for (int i = 0; i < 26; i++) { while (*current) { if (alphabet[i] == *current) occs[i]++; current++; } current = str; } return; }
bool isReversed(char s1, char s2){ // REQUIRES: s1 and s2 are non-empty C-strings. // EFFECTS: returns true if s2 is composed of the string in s // except in reverse order, false otherwise. char t = s2; while (t) { t++; } while(s1 && (t!=s2)) { if(s1=*t){ s1++; t-1; } else return false; } return true; }
int * addition(int a, int b) { // EFFECTS: Calculates a + b and returns a pointer to the result int result = a + b; return &result; } int foo() { int * result1 = addition(13, 37); int * result2 = addition(73, 31); cout << *result1 << " " << *result2 << endl; return 0; }
list_t modItUp(list_t in_list, int (fn1)(int), int (fn2)(int)) { // EFFECTS: If in_list is empty, returns an empty list. // Otherwise returns a list, out_list, where: // out_list[i] = fn1(in_list[i]) when i % 2 = 0 // out_list[i] = fn2(in_list[i]) when i % 2 = 1 // // For example, if fn1 returned its input squared and fn2 returned its // input multiplied by 2, then: // // modItUp((1, 2, 3, 4, - 5), fn1, fn2) yields the list (1, 4, 9, 8, 25) }
bool list_isEmpty(list_t list); // EFFECTS: returns true if list is empty, false otherwise list_t list_make(); // EFFECTS: returns an empty list. list_t list_make(int elt, list_t list); // EFFECTS: given the list (list) make a new list consisting of // the new element followed by the elements of the original list. int list_first(list_t list); // REQUIRES: list is not empty // EFFECTS: returns the first element of list list_t list_rest(list_t list); // REQUIRES: list is not empty // EFFECTS: returns the list containing all but the first element // of list list_t reverse(list_t list); // EFFECTS: returns the reverse of list list_t rotate(list_t list, unsigned int n); // EFFECTS: returns a list equal to the original list with the // first element moved to the end of the list n times.
bool tree_isBalanced(tree_t tree); // EFFECTS: returns true if tree is balanced, false otherwise
bool tree_isEmpty(tree_t tree); // EFFECTS: returns true if tree is empty, false otherwise tree_t tree_make(); // EFFECTS: creates an empty tree. tree_t tree_make(int elt, tree_t left, tree_t right); // EFFECTS: creates a new tree, with elt as it's element, left as // its left subtree, and right as its right subtree int tree_elt(tree_t tree); // REQUIRES: tree is not empty // EFFECTS: returns the element at the top of tree. tree_t tree_left(tree_t tree); // REQUIRES: tree is not empty // EFFECTS: returns the left subtree of tree tree_t tree_right(tree_t tree); // REQUIRES: tree is not empty // EFFECTS: returns the right subtree of tree
/ \ Height = 2 4 8 Height = 3 2 5 / \ /
3 5 7 9
/ Height = 2 5 8 Height = 3 2 / /
4 7 9
const int MAX_EVENTS = 100; struct Athlete { // this struct stores information about a specific athlete char name[50]; // the athlete's name stored as a C-string char country[50]; // the athlete's country stored as a C-string int score; // the athlete's score } struct Event { // this struct stores information about a specific event char name[50]; // the name of this event stored as a C-string Athlete medalist[3]; // the list athletes who won medals for this event where: // medalist[0] = gold medal, medalist[1] = silver medal, // medalist[2] = bronze medal }; struct Olympics { // this struct stores information about a specific Olympics Event events[MAX_EVENTS]; // the events in this Olympics int year; // the year of this Olympics int numEvents; // number of Olympic events in this Olympics } Olympics history[ 30 ]; // global array that can hold results for 30 Olympics
void addResult(int year, char * eventName, char * medal, Athlete winner); // REQUIRES: "winner" is a properly formatted and initialized Athlete struct // MODIFIES: global "history" array // EFFECTS: If no Olympic struct exists in the history array whose year element // is equal to the input "year", throws a - 1. For the Olympic struct in the // history array (if it exists) whose year element is equal to "year", if no // Event struct exists in the Olympic's events array whose name element is // equal to the input "eventName", throws a - 2. If the input "medal" is not // one of the strings "gold", "silver", or "bronze", throws a - 3. If none of // the first three inputs causes an exception to be thrown, this function // copies "winner" into the appropriate medalist array location, depending on // "medal", for the corresponding Olympic year and event as described above.
void strcpy(char *dest, const char *src) // REQUIRES: src is a NULL-terminated string, dest can hold a copy of src // MODIFIES: dest // EFFECTS: place a copy of src in dest bool strcmp(const char *dest, const char *src) // REQUIRES: src and dest are NULL-terminated strings // EFFECTS: returns FALSE if dest is equal to src, TRUE otherwise