12
Chocolate Code-Off!! First Chocolate Code-off for CISC220 Fall 18 Instructions: 1. Get into groups of 3 or 4 people 2. Create one answer sheet with all group member names on it! (to be given to another group for grading No running code (you may use class notes) 3. You have 1 hour to create an answer sheet 4. You may ask yes/no questions (NOT “Is this the answer…?”) 5. Prizes: a. First Place: 3 E.C. pts on upcoming celebration of knowledge, b. 2 nd Place: 2 E.C. pts, c. 3 rd Place: 1 E.C. pt PLUS CHOCOLATE!!! P1: Given the following code, what is printed string s1 = "snickers"; string *s2= &s1; s1 = "starburst"; cout << *s2 << endl; //prints _________________________________ *s2 = ""; cout << s1 << endl; //prints _________________________________ /*****************************************************************************/ P2:: Whats printed in main? void func1(char *i, char &j, char k); int main() { char a = 'h'; char b = 't'; char c = 'x'; char d = 'w'; char f = 'i'; cout << b << d << f << c << endl; //__________________ func1(&c,d,f); cout << d << f << b << d << c << b <<endl;//__________________ return(0); } void func1(char *i, char &j, char k){ *i = 'a'; j = 'k'; k = 'u'; }

Chocolate Code-Off!!yarringt/CISC220/Fall18/CCO1... · 2018-10-04 · Chocolate Code-Off!! First Chocolate Code-off for CISC220 Fall 18 Instructions: 1. Get into groups of 3 or 4

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Chocolate Code-Off!! First Chocolate Code-off for CISC220 Fall 18

Instructions: 1. Get into groups of 3 or 4 people 2. Create one answer sheet with all group member names on it! (to be given to another group for grading

No running code (you may use class notes) 3. You have 1 hour to create an answer sheet 4. You may ask yes/no questions (NOT “Is this the answer…?”) 5. Prizes:

a. First Place: 3 E.C. pts on upcoming celebration of knowledge, b. 2nd Place: 2 E.C. pts, c. 3rd Place: 1 E.C. pt

PLUS CHOCOLATE!!!

P1: Given the following code, what is printed string s1 = "snickers"; string *s2= &s1; s1 = "starburst"; cout << *s2 << endl;

//prints _________________________________

*s2 = ""; cout << s1 << endl;

//prints _________________________________

/*****************************************************************************/ P2:: Whats printed in main? void func1(char *i, char &j, char k); int main() { char a = 'h'; char b = 't'; char c = 'x'; char d = 'w'; char f = 'i'; cout << b << d << f << c << endl; //__________________ func1(&c,d,f); cout << d << f << b << d << c << b <<endl;//__________________ return(0); } void func1(char *i, char &j, char k){ *i = 'a'; j = 'k'; k = 'u'; }

/*****************************************************************************/ P3: What is printed out in main? void func2(string *arr); int main() { string a[5] = {"c","a","n","d","y"}; string s1 = ""; for (int i = 0; i < 5; i++) { s1+= a[i]; } cout << s1 << endl;//______________________ func2(a); string s2 = ""; for (int i = 0; i < 5; i++) { s2+=a[i]; } cout << s2 << endl;//______________________ return(0); } void func2(string *arr) { arr[3] = "t"; arr[2]="vi"; }

/*****************************************************************************/ P4: what is printed in main? void func2(char a[], string *s ) ; string *func(char arr[], int iarr[], int len); int main() { char arr[9] = {'l','e','r','t','o','b','o','n','e'}; int iarr[4] = {3,0,6}; string *s = func(arr,iarr,3); cout << *s << endl; ___________________________________ return 0; } void func2(char a[], string *s ) { for (int i = 0; i<3; i++) { *s += string(1,a[i]); //converts type (char to string) } } string *func(char arr[], int iarr[], int len) { string *k = new string; *k = ""; // makes blank string for (int i = 0; i < len; i++) { func2(&arr[iarr[i]], k); } return k; }

/*****************************************************************************/

P5: Given the following, what is printed out? _____________________________

class Hobbit{ public:

string fname; string book

}; void changeHobbit(Hobbit x) { x.fname = "Meriadoc"; } int main() {

Hobbit h1; h1.fname = "Frodo"; changeHobbit(h1); cout << h1.fname << endl; // what is printed here? return 0;

} //main /*****************************************************************************/ P6: Fix the following code by adding the appropriate line class ThisClass{ int *arr; int len; public: ThisClass(int x); ~ThisClass(); }; ThisClass::ThisClass(int x) { len = x; arr = new int[len]; for (int i = 0; i < len; i++) { arr[i] = pow(i,3); } } ThisClass::~ThisClass() { _____________________________________; cout << "bye now" << endl; }

/*****************************************************************************/

P7: What is wrong with the following code (look in main)? __________________________________ class Rect { int length; int width; int area; public: Rect (int x, int y); ~Rect(); int getArea(); }; //Rect header Rect::Rect(int x, int y) { // constructor length = x; width = y; area = length * width; } Rect::~Rect() { cout << "destroying: " << area << endl; } int Rect::getArea() { return area; } int main() {

Rect r(3,4); cout << r.getArea() << endl; delete r; // here? return 0;

} //main

/*****************************************************************************/ P8: What is wrong with the following code (it compiles!) __________________________________

int *MakeIt(int size) { int arr[size]; for (int i = 0; i < size; i++) { arr[i] = pow(i,2); // pow(x,y) returns x to the yth power } for (int i = 0; i < size; i++) { cout << arr[i] << ", "; } cout << endl; return arr; } int main() { int x = rand()%10+5; int *array = MakeIt(x); for (int i = 0; i < x; i++) { cout << array[i] << ", "; } cout << endl; return(0); }

/*****************************************************************************/ P9: Given the following code, why is it bad coding (even though it works!)? __________________________________

class Rect { int len; int width; int area; public: Rect(int x,int y); Rect(); Rect operator+(int z); int getArea(); }; Rect::Rect(int x, int y) { len = x; width = y; area = x*y; } Rect::Rect() { len = 0; width = 0; area = 0; } int Rect::getArea() { return area; } Rect Rect::operator+(int z) { Rect r; r.len = len - z; r.width = width - z; if ((r.len>0) && (r.width > 0) ) { r.area = r.len *r.width; } else { r.area = 0; } return r; } int main() { /* Prob 10 */ Rect arect(4,6); cout << arect.getArea() << endl; Rect rect2 = arect + 3; cout << rect2.getArea() << endl;

return(0);

}

/*****************************************************************************/

P10: Given the following code, what is printed out?:

class MugWump { int x; string y; public: MugWump(int i); void Action(); void printIt(); }; MugWump::MugWump(int i) { x = i; y = ""; } void MugWump::Action() { int k = x; while (k > 0) { char c = ('0'+k%2); // converts an int to a char // the '0' just turns k%2 into a char, it doesn’t change it in // any other way – it’s like saying: char c = k%2 // in other words,

// if int x = 3, then char y = ('0'+x) would make y hold '3' y = c + y; k = k/2; } } void MugWump::printIt() { cout << x <<": " << y; //__________________________________________ } int main(){ MugWump m(13); m.Action(); m.printIt();

}

/*****************************************************************************/

P 11: What is printed out? _________________________________ class MyClass { int i; string j; public: MyClass(int x,string s); ~MyClass(); }; MyClass::MyClass(int x,string s) { i = x; j = s; } MyClass::~MyClass() { cout << j; } void f(int x) { MyClass t(2,"n"); if (x == 3) { MyClass t2(3,"c"); } else { MyClass t2(7,"t"); } cout <<"a"; } int main() { MyClass t(3,"s"); MyClass t2(4,"u"); f(3); MyClass t3(2,"ro"); f(2); MyClass t4(1,"ke"); return(0); }

**************************************************************************

P11: Given the following struct definition, what is printed out?:_______________________________

struct Info { string a; string b; int c; Info *next; }; int main() {

Info *first = new Info({"tun","ne","l",NULL}); first->next = new Info({"ha","rd","ware",NULL }); Info *tmp = first->next; tmp->next = new Info({"recur","s","ion",NULL }); for (Info x= first; x != NULL; x = x->next) { //What is printed here? cout << x->b; } //for cout << endl;

}//main

/******************************************************************************/ P12: Given the following linked list: a->k->b->o->t->a->h->l->v->a-> If you run func4(), then print out the linked list (characters), what do you get?_____________________ class SNode { friend class SLL; char c; // as opposed to int data; SNode *next; }; void SLL::func4() { SNode *tmp = first->next; delete first; first = tmp; while ((tmp->next != NULL)&&(tmp->next->next != NULL)) { Snode *t2 = tmp->next; tmp->next = tmp->next->next; tmp = tmp->next; delete t2; } if (tmp->next != NULL) { tmp->next = NULL; } last = tmp; } /******************************************************************************/ P13:Given the following linked list: ______________________ a->c->a->p->l->a If you run the following function, then print out the linked list, what do you get? void SLL::f3() { SNode *tmp; SNode *tmp2 = first; while (tmp2->next != NULL) { tmp = tmp2->next; tmp2->next = tmp2->next->next; tmp->next = first; first = tmp; } last = tmp2; } ********************************************************************

Problem 14 (challenging!): Assume: class SNode { friend class SLL; int data; string s; // as opposed to int data; SNode *next; }; Given the following list: rm->u->o->g->s->o->in If you run the following function, then print out the list characters, what do you get? void SLL::MakeIt() { last->next = first;

SNode *tmp = first; int ct = 0; int s2 = 0; SNode *tmp2; while (size > 0) { if (ct ==2) { if (s2 == 0) { first = tmp->next; tmp->next = tmp->next->next; first->next = NULL; tmp2 = first; } else { tmp2->next = tmp->next; tmp->next = tmp->next->next; tmp2->next->next = NULL; tmp2 = tmp2->next; } s2++; ct = 0; size--; } ct++; tmp = tmp->next; } last = tmp2; }

**************************************************************************

P15: (challenging!): Assume you have two linked lists as follows: list1 is b->k->e->z->l->d->u->g->v->e-> list2 is s->k->r->l->t->u->p->g->x->e-> And SLL *n = new SLL(); SNode *tmp = list1->first; SNode *tmp2 = list2->first; If you run the following function with: is(tmp,tmp2,n); then print out the new list (n), what do you get?

void SLL::is(SNode *tmp, SNode *tmp2, SLL *n) { if (tmp2 == NULL) { first = n->first; size = n->size; last = n->last; return; } else if (tmp == NULL) { return(is(first,tmp2->next,n)); } else if (tmp->c == tmp2->c) { if (n->size == 0) { n->first = new SNode(0); n->first->c = tmp->c; n->last = n->first; } else { n->last->next = new SNode(0); n->last->next->c = tmp->c; n->last = n->last->next; } n->size++; n->last->next = NULL; return(is(first, tmp2->next, n)); } else { return(is(tmp->next, tmp2, n)); } }

/*****************************************************************************/ P 16:(CHALLENGING): ____________________ Given the following code, what is printed in main? void f4(char ***k, int *x,int *y, string s) { *x = 5; *y = 5; *(k) = new char* [*x]; for (int i = 0; i < *x; i++) { (*k)[i] = new char [*y]; for (int j = 0; j < *y; j++) { (*k)[i][j] = s[*x*i+j]; } } } void print(char **k, int x, int y) { for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { if (i == j+1) { cout << k[i][j] ; } } } cout << endl << endl; } int main() { string s = "cgeidhjklmaeqrkuvaxypbcpeghijsmnopqursuvwxyzabcd"; int x = -1; int y = -1; char **cc = NULL; f4(&cc,&x,&y,s); print(cc,x,y); }

/*****************************************************************************/ KNOW:

Addressing: how to get an address, what holds an address, how to print an address

Passing into functions by value/pointer/reference for primitive types

Passing into functions and returning: arrays, classes

Stack versus heap

Creating and deleting on the heap (including arrays, matrices)

Header class versus .cpp class

Classes: constructors, destructors, operator overload

When to use a pointer dot versus a dot to access fields and methods

Linked lists versus arrays: advantages and disadvantages of both data types

ADT

Creating and using linked lists

Everything covered in class, on powerpoints, and in labs.