c109f06f - CSE 109 Final Examination Thursday 14 December...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 109 Final Examination Thursday 14 December 2006 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<SUGGESTED ANSWERS>>>>>>>>>>>>>>>>>>>>>>> 1. For parts (a) and (b) below, your code should be in C, where the g++ compiler is called with the -xc option. (a) Write C code that prompts the user to enter an integer, reads in the integer, and displays the square of the integer entered. (b) Write a C function "minmax" which is meant to store the larger of two integers in the first int and the smaller in the second int. Given int x, y;, write the call to the function. After the call we should have x>=y. <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< (a) int n; printf("Enter an integer, and I square it- "); scanf("%d",&n); printf("The square of %d is %d \n",n,n*n); (b) void minmax(int *a,int *b) {int temp; if(*a<*b) {temp=*a; *a=*b; *b=temp; } } minmax(&x,&y); >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> > 2. Write a program that determines whether a line of text entered from the console satisfies the syntax for A below, where () denotes ovals and [] denotes boxes. Note that blank characters (' ') are not allowed. Thus, the input is processed character by character. t A ------------------>[B]--->(&)----->[C]----> ^ | | | +---(!)---+ C -------->(+)------->[A]----+ +--->(a)---+ | | | | B |--->(b)---| | +-->(-)---+ | ------+--->[C]--------> +--->(r)--->(s)---------------> <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< #include <iostream> using namespace std; u void getch(char &ch); void check(bool b, char *mess,char &ch); void A(char &ch); void B(char &ch); void C(char &ch); v int main() {char ch; getch(ch); A(ch); cout<<"Successful parse\n"; } void getch(char &ch)
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
{cin.get(ch); cout<<ch; } void check(bool b, char *mess,char &ch) {if(!b) {cerr<<" <---ERROR: "<<mess<<endl; exit(1); } getch(ch); } void A(char &ch) {while(ch=='!') getch(ch); B(ch); check(ch=='&'," '&' expected",ch); C(ch); } void B(char &ch) {if(ch=='a' || ch=='b') getch(ch); else C(ch); } void C(char &ch) {if(ch=='r') {getch(ch); check(ch=='s'," 's' expected",ch); } else {check(ch=='+' || ch=='-'," '+' or '-' expected",ch); A(ch); } } >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 3. An "association" consists of a "key" and a "value" (e.g., a dictionary is a collection of associations, whose keys are the words to be defined, and the values are the definitions), where the key is set at creation, but the value may be changed later. Write a template for an Association class.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern