UM-SJTU Joint Institute Sample Midterm 2008 for Vg101 Introduction to Computer and Programming You Name (Printed): Student ID: General instructions You exam will be divided into two parts.A part on paper and the second one is on computer. For the on paper exam,you directly write down your answers on the examination paper,including any work that you wish to be considered for partial credit. Each question is marked with the number of points assigned to that problem.The total number of points is 50.We intend for the number of Total points to be roughly comparable to the number of minutes you should spend on that problem. Unless otherwise indicated as part of the instructions for a specific problem,comments will not be required on the exam.Uncommented code that gets the job done will be sufficient for full credit on the problem.On the other hand,comments may help you to get partial credit if they help us determine what you were trying to do. The examination is open-book,and you may make use of any texts,handouts,or course notes.You may not,however,use a computer of any kind for the paper test. For the computer part,you will be given a problem and you will need to design it,implement it, compile it,debug it and test it on computer.You need to summit your results to the ftp server specified in the problem description. THE UM-SJTU JI HONOR CODE I accept the letter and spirit of the honor code: "I have neither given nor received unauthorized aid on this examination,nor have I concealed any violations of the Honor Code." signature Date
UM-SJTU Joint Institute Sample Midterm 2008 for Vg101 Introduction to Computer and Programming You Name (Printed) : Student ID: General instructions You exam will be divided into two parts. A part on paper and the second one is on computer. For the on paper exam, you directly write down your answers on the examination paper, including any work that you wish to be considered for partial credit. Each question is marked with the number of points assigned to that problem. The total number of points is 50. We intend for the number of points to be roughly comparable to the number of minutes you should spend on that problem. Unless otherwise indicated as part of the instructions for a specific problem, comments will not be required on the exam. Uncommented code that gets the job done will be sufficient for full credit on the problem. On the other hand, comments may help you to get partial credit if they help us determine what you were trying to do. The examination is open-book, and you may make use of any texts, handouts, or course notes. You may not, however, use a computer of any kind for the paper test. For the computer part, you will be given a problem and you will need to design it, implement it, compile it, debug it and test it on computer. You need to summit your results to the ftp server specified in the problem description. THE UM-SJTU JI HONOR CODE I accept the letter and spirit of the honor code: "I have neither given nor received unauthorized aid on this examination, nor have I concealed any violations of the Honor Code." signature Date
Problem 1(20 pts).Expressions,Loops and Program Trace 1-a(10 pts):Find the value of x after each of the following expressions is executed.For each expression,assume that x and y are integers and that x has the value 15 prior to the execution of the expression.Put U in the answer box if you think the value cannot be determined. a)x/=x/y; b)x=sqrt (x+1)+2; c)x=y/x*x+y%x; d)x=!(x=x%4&&x<y%x); e)x=y *y--+--y; 1-b (10 pts):Loops a)(5 pts)Convert the following code containing a do/while loop to a code containing a while loop in such a way that the execution of the code is identical. intj=0; do j=j+1; console.printLine (j,"\n"); while(j<=10); console.printLine j,"\n"); b)(5pts):Given bellow is an algorithm in pseudo-code: START For which of the following inputs will the OUTPUT“Enter a positive integer'” algorithm say“YES"? NPUT N i=1 (a28 YES▣ NO▣ WHILE(i*i<N) (b)36 YES▣ NO▣ i=i+1 (c)54 YES口 NO▣ END WHILE (d)81 YES▣ NO▣ IFi×i=N) OUTPUT“YES” What property of the input is the algorithm ELSE determining? OUTPUT“NO” END IF STOP
Problem 1 (20 pts).Expressions, Loops and Program Trace 1-a (10 pts): Find the value of x after each of the following expressions is executed. For each expression, assume that x and y are integers and that x has the value 15 prior to the execution of the expression. Put U in the answer box if you think the value cannot be determined. a) x /= x / y; b) x = sqrt (x+1) + 2; c) x = y / x * x + y % x; d) x = !(x = x % 4 && x < y % x); e) x = y * y-- + --y; 1-b (10 pts): Loops a) (5 pts) Convert the following code containing a do/while loop to a code containing a while loop in such a way that the execution of the code is identical. int j = 0; do { j = j + 1; console.printLine ( j, "\n"); } while (j <= 10); console.printLine ( j, "\n"); b) (5pts): Given bellow is an algorithm in pseudo-code: START OUTPUT “Enter a positive integer” NPUT N i = 1 WHILE (i * i < N) i = i + 1 END WHILE IF (i × i = N) OUTPUT “YES” ELSE OUTPUT “NO” END IF STOP For which of the following inputs will the algorithm say “YES”? (a) 28 YES NO (b) 36 YES NO (c) 54 YES NO (d) 81 YES NO What property of the input is the algorithm determining?
1-c(10 pts):Think like a computer //Consider the following C++code. #include using namespace std; #define NMax 1000 #define EPSILON 0.0001 int main(void) ConsoleT console; double x console.readDouble("Input x(must be positive):") double xPower 1.0,lastTerm,sum =0.0; int i=0; do i=i+1; xPower x*xPower; lastTerm xPower/i; sumsum lastTerm; while(iEPSILON); if (i =NMax)console.printLine("Series did not convergeln"); else f console.printLine ("i=",i,"x=",x,"lastTerm "lastTerm,"sum ="sum,endl); i return 0; } (a)What series is being summed by this code?(4 pts) sum=x+ (b)If the user inputs-1.0 for x,what will be printed out by this code?(2 pts) (c)If the user inputs 1.0 for x,what will be printed out by this code?(2 pts) (d)If the user inputs 0.01 for x,what will be printed out by this code?(2 pts)
1-c (10 pts): Think like a computer //Consider the following C++ code. #include using namespace std; #define NMax 1000 #define EPSILON 0.0001 int main(void) { ConsoleT console; double x = console.readDouble ("Input x (must be positive): "); double xPower = 1.0, lastTerm, sum = 0.0; int i = 0; do { i = i + 1; xPower = x*xPower; lastTerm = xPower/i; sum = sum + lastTerm; } while (i EPSILON); if (i == NMax) console.printLine ("Series did not converge\n"); else { console.printLine ("i = ", i, "; x = " , x, "; lastTerm = ", lastTerm, "; sum = " , sum, endl); }; return 0; } (a) What series is being summed by this code? (4 pts) sum = x + (b) If the user inputs -1.0 for x, what will be printed out by this code? (2 pts) (c) If the user inputs 1.0 for x, what will be printed out by this code? (2 pts) (d) If the user inputs 0.01 for x, what will be printed out by this code? (2 pts)
Problem 2(10 pts):Functions and Parameter Passing:write out the outputs of the following program #include using namespace std; int SpaceMountain(double donald,int walt); void Matterhorn (int&mickey,int disney); int main (void) ConsoleT console: int mickey; int goofy; double donald 2.6; Mickey Goofy Donald mickey goofy donald; console.printLine (mickey,""goofy,"",donald,endl); for (int i=0;i<2;i++) mickey SpaceMountain (donald,goofy mickey); } console.printLine (mickey,"",goofy,""donald,endl); goofy++; goofy /mickey: console.printLine(mickey,"".goofy,"",donald,endl); Matterhorn(mickey,goofy): console.printLine (mickey,"",goofy,""donald,endl); return 0: int SpaceMountain(double donald,int walt) donald +3; return (walt *2); } void Matterhorn(int&mickey,int disney) disney mickey +(2 disney); mickey disney 3;
Problem 2 (10 pts): Functions and Parameter Passing: write out the outputs of the following program #include using namespace std; int SpaceMountain(double donald, int walt); void Matterhorn (int& mickey, int disney); int main (void) { ConsoleT console; int mickey; int goofy; double donald = 2.6; mickey = goofy = donald; console.printLine (mickey, " ", goofy, " ", donald, endl); for (int i = 0; i < 2; i++) { mickey = SpaceMountain (donald, goofy + mickey); } console.printLine (mickey, " ", goofy, " ", donald, endl); goofy++; goofy /= mickey; console.printLine (mickey, " ", goofy, " ", donald, endl); Matterhorn (mickey, goofy); console.printLine (mickey, " ", goofy, " ", donald, endl); return 0; } int SpaceMountain(double donald, int walt) { donald += 3; return (walt * 2); } void Matterhorn (int& mickey, int disney) { disney = mickey + (2 * disney); mickey = disney % 3; } Mickey Goofy Donald
3:Debug Programming (10 pts) Write a C++Program to calculate the following sum: S=1-X2+X4-X5+X8-X10 The code has both syntax errors as well as "design"flaws in some lines.Design flaws are those that compile without error but do not provide correct answers upon execution.Fix these as well.The corrected code should be able to compile and calculate the above sum. #include using namespace std; main(void) int x console.readDouble ("Input x:) int xPower =1; int sum 0; int sign; for (int term =1,;term 10;term ++ xpower *sign *x xPower; sumsum xPower; ; console.printLine ("S ="sum,endline);
3: Debug & Programming (10 pts) Write a C++ Program to calculate the following sum: S = 1 – X2 + X4 – X6 + X8 – X10 The code has both syntax errors as well as “design” flaws in some lines. Design flaws are those that compile without error but do not provide correct answers upon execution. Fix these as well. The corrected code should be able to compile and calculate the above sum. #include using namespace std; main(void) { int x = console.readDouble ("Input x: ); int xPower = 1; int sum = 0; int sign; for (int term = 1,; term < 10; term ++) { xpower *= sign * x * xPower; sum = sum + xPower; }; console.printLine ("S = ", sum, endline); }
Problem 4(50 pts):Computer Part In this part of the test,you are asked to design,implement,debug and test your program on computer.You need to make it work right to get full credits. Although the comments and pseudo codes are not necessary if your program run correctly,it can help you to get partial credits if your program cannot compile or run correctly. Problem Description: Write a program which will be used to solve the quadratic equation like a x^2 b x c 0 You need to implement 3 functions with given prototype and a main function toto the job. #include #include #define EPSILON 0.000001 /define an error tolerance we can accept This function is responsible for reading in the coefficients of a quadratic equation.The values of the coefficients are passed back to the main program in the variables a,b,and c, which are passed by reference. 制 void getCoefficients(double &a,double &b,double &c); This function solves a quadratic equation using standard formular x=(-b +/sgrt (b^2-4ac))/2a The coefficients are supplied as the arguments a,b,and c,and the roots are returned in x1 and x2,which are passed by reference. your program should check for invalid inputs.If an invalid input are found,you should display the error messasge and quit the program. void solveQuadratic(double a,double b,double c,double &x1,double &x2); This function displays the values x1 and x2,which are the roots of a quadratic equation. if x1 and x2 are the same,it print like"root[1]root[2]=XXX" otherwise,it print like "root[1]XXX;root[2]XXX" 利 void displayRoots(double x1,double x2); ∥global object ConsoleT console; int main(void /your implementation
Problem 4 (50 pts): Computer Part In this part of the test, you are asked to design, implement, debug and test your program on computer. You need to make it work right to get full credits. Although the comments and pseudo codes are not necessary if your program run correctly, it can help you to get partial credits if your program cannot compile or run correctly. Problem Description: Write a program which will be used to solve the quadratic equation like a x^2 + b x + c = 0 You need to implement 3 functions with given prototype and a main function to to the job. #include #include #define EPSILON 0.000001 // define an error tolerance we can accept /* * This function is responsible for reading in the coefficients of a quadratic equation. The values of * the coefficients are passed back to the main program in the variables a, b, and c, * which are passed by reference. */ void getCoefficients(double &a, double &b, double &c); /* * This function solves a quadratic equation using standard formular * x = (-b +/- sqrt (b^2-4ac)) / 2a * The coefficients are supplied as the arguments a, b, and c, and the roots are * returned in x1 and x2, which are passed by reference. * your program should check for invalid inputs. If an invalid input are found, you should * display the error messasge and quit the program. */ void solveQuadratic(double a, double b, double c, double &x1, double &x2); /* * This function displays the values x1 and x2, which are * the roots of a quadratic equation. * if x1 and x2 are the same, it print like “root[1] = root[2] = XXX” * otherwise, it print like “root[1] = XXX; root[2] = XXX” */ void displayRoots(double x1, double x2); // global object ConsoleT console; int main(void ) { // your implementation
Problem 4(50 pts):Another computer programming sample This is an extension to your homework,you are required to read some date from a input file and compute the GPA for each students as well as the average GPA for all the students in course 3. Each record of the input file contain the following informations: Student ID(string) Score for course 1(double), 50611091183.342.353.7 41.7 4-1 50614190513.3 43.7 53.3 43 4.1 Credits for course 1 (double) 5061509069 44 53.7 43.3 4-1 Score for course 2(double), 50615090813.743.353.7 43 4-1 Credits for course 2(double) 50615090833.3 43.353.7 43 4-1 5063709030 444 53.744 4-1 Score for course n(double) 5063709031 44 45 4444.1 50637090323.3 43.7 5 3 43.3 4-1 Credits for course n(double) -1(or any negative number)denotes the end of the score for the student with given ID The input file contains unknown number The Sample Output of records Here is where you start: Avarage of student 50611091181s:2.72353 Avarage of student 5061419051is:3.34706 #include Avarage of student 5061509069is:3.76471 #include "FstreamT.h" Avarage of student 5061509081is:3.41765 Avarage of student 5061509083is:3.32353 using namespace std; Avarage of student 50637090301s:3.92941 Avarage of student 5063709031is:4 int main ( Avarage of student 50637090321s:3.34706 Avarage of student 50637090331s:1.04118 Avarage of student 5063709034is:2.74706 ConsoleT console; Avarage of student 5063709035 is:3.92941 IfstreamT inFile; Avarage of student 50637090361s:3.3 Avarage of student 5063709057 is:3.13529 Avarage of student 5063709058 is:3.85882 Avarage of student 5063709059 is:3.01765 Avarage GPA in course 3 is:3.32353
Problem 4 (50 pts): Another computer programming sample This is an extension to your homework, you are required to read some date from a input file and compute the GPA for each students as well as the average GPA for all the students in course 3. Each record of the input file contain the following informations: Student ID (string) Score for course 1 (double), Credits for course 1 (double) Score for course 2 (double), Credits for course 2 (double) … Score for course n (double) Credits for course n (double) -1 (or any negative number) denotes the end of the score for the student with given ID The input file contains unknown number The Sample Output of records Here is where you start: #include #include "FstreamT.h" using namespace std; int main () { ConsoleT console; IfstreamT inFile;