CSE 109 Final Examination FALL 2006

CSE 109 Final Examination FALL 2006 - CSE 109 Final...

Info iconThis 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. A ^ | | | +---(!)---+ C -------->(+)------->[A]----+ +--->(a)---+ | | | | B |--->(b)---| | +-->(-)---+ | ------+--->[C]--------> +--->(r)--->(s)---------------> <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< #include <iostream> using namespace std; void getch(char &ch); void check(bool b, char *mess,char &ch); void A(char &ch); void B(char &ch); void C(char &ch); int main() {char ch; getch(ch); A(ch); cout<<"Successful parse\n";
Background image of page 1

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

View Full DocumentRight Arrow Icon
} {cin.get(ch); cout<<ch; } {if(!b) {cerr<<" <---ERROR: "<<mess<<endl; exit(1); } getch(ch); } {while(ch=='!') getch(ch); B(ch); check(ch=='&'," '&' expected",ch); C(ch); } {if(ch=='a' || ch=='b') getch(ch); else C(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
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/05/2008 for the course CSE 109 taught by Professor Kay during the Spring '08 term at Lehigh University .

Page1 / 6

CSE 109 Final Examination FALL 2006 - CSE 109 Final...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online