0% found this document useful (0 votes)
291 views7 pages

SolutionAPSC 160 (UBC)

The document contains a review session for an engineering undergraduate course with 10 multiple choice questions about C programming concepts like loops, file input/output, and conversions between numeric bases. Sample code is provided for each question to analyze and answer questions about program logic and output.

Uploaded by

JFOXX7777
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
291 views7 pages

SolutionAPSC 160 (UBC)

The document contains a review session for an engineering undergraduate course with 10 multiple choice questions about C programming concepts like loops, file input/output, and conversions between numeric bases. Sample code is provided for each question to analyze and answer questions about program logic and output.

Uploaded by

JFOXX7777
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Engineering Undergraduate Society

c/o Faculty of Applied Science


5000-2332 Main Mall
Vancouver, BC Canada V6T 1Z4
Tel: 604.822.3818

APSC 160 Midterm #1 Review Session


1. Explain in plain English, the function of this code.
/* Assume that VAL is defined to be some positive integer,
but you dont know what it is. */
int main (void)
{
int x=2;
int y=1;
while (x <= VAL)
{
y *= x;
x = x + 2;
}
printf(The mystery value is %d\n, y);
}
Answer: The code prints out the cumulative product of even numbers up to VAL,
inclusively.

2. Explain in plain English what this code does.


int main
{
int
int
int

(void)
x = 1;
y,v;
z = 1;

scanf(%d, &x);
scanf(%d, &y);
if (y == 0)
{ x = 1; }
v = x;

while (z < y)
{
x*=v;
z++;
}
printf(The mystery value is %d, x);
}
Answer: The code prints out xy, where x is the base and y is the exponent.

3. Consider this code fragment. How many times does the printf statement get
executed?
int count = 5;
while (count > 0 && count <= 5)
{
count--;
if (count / 2 > 1)
printf(Printing\n);
else
count--;
}
Answer: 1 time

4. Rewrite the following using an appropriate combination of loops.


int main(void)
{
int count = 5;
++count;
count += 1;
if (count == 7)
count++;
if (count == 8) {
count++;
printf(Hello);
}

else if (count == 9)
{
printf(Hello);
count++;
}
}
Answer: Using a while loop,
int main (void)
{
int count = 5;
while (count <= 8)
{
if (count == 8)
printf(Hello);
count++;
}
}
5. Create this image by using while loop and a do-while loop.
1
11
111
Answer:
Using a while loop

Using a do-while loop

int main(void)
{
int i = 1, j = 0;

int main(void)
{
int i = 1, j = 0;

while (i<=3) {
j = 0;
while ( j<i)
{
printf("1");
j++;
}
printf("\n");
i++;
}

do {
j=0;
do {
printf("1");
j++;
} while ( j<i);
printf("\n");
i++;
} while (i <= 3);

6. Write a small routine that prints the sum of a list of integer values separated by a
space from a text file, data.txt. Also, include a flowchart for this routine.
Answer:
FILE * inFile;
inFile = fopen(data.txt, r);
int sum=0;
if (inFile != NULL)
{
while(fscanf(%d, &x)==1)
sum += x;
printf(Sum=%d \n, sum);
}
else
printf(File is not found!\n);
fclose(inFile);

7. What is the output?


int main( void )
{
double x = 2.0;
int y = 5;
int c;
c= y/x;
printf("%.3f \n",c);

system("PAUSE");
return 0;
}

Answer: 2.000

8. Convert 4810 to binary. Convert 101112 to decimal.


Answer:
48 -> 110000
10111 -> 23
9. Convert 3010 to Hexadecimal. Convert 2A16 to decimal
Answer:
30 -> 1E
2A -> 42
10. Write a program that produces a triangle of even numbers.
Input: 3
Output:
0
0 2
0 2 4
Answer:
int main (void)
{
int count1=0, count2=-1;
int rows;
printf("Input: ");
scanf("%d", &rows);
while (count1 < rows)
{
count2=-1;
while(count2 < count1)
{
printf("%d ", 2*(count2 + 1));
count2++;
}
printf("\n");
count1++;
}
}

APSC 160 Midterm #1 Review Session Pre-Quiz


Pre-quiz (15 min)
1. Write a program that prints out an inverted triangle when reading an input.
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
int count1, count2, x;
printf(Input: );
scanf(%d, &x);
count1 = x;
while (count1 > 0)
{
count2 = count1;
while (count2 > 0)
{
printf(*);
count2--;
}
count1--;
}
}
2. Write a program that prints the factorial of a constant VAL.
int main (void)
{
int result = 1;
int count = 1;
while(count <= VAL)
{
result*=count;
count++;
}
printf(%d, result);
}

APSC 160 Midterm #1 Review Session Post-Quiz


Post-quiz (15 min)
1. Write a program that prints out the following when given an input VAL.
int main (void) {
int count1=1, count2=0, count3=-1;
while(count1 <= VAL)
{
count2=0;
while(count2 < count1)
{
count3=-1;
while(count3 < count2)
{
printf("*");
count3++;
}
count2++;
printf("\n");
}
printf("\n");
count1++;
}
}
2. Write a program that reads in the radius of a circle, and computes its
circumference and area.
#define PI 3.14159
int main (void)
{
double radius, circumference, area;
scanf(%lf, &radius);
circumference = 2 * PI * radius;
printf(Circumference=%.2f, circumference);
area = PI * radius * radius;
printf(Area=%.2f, area);
}

You might also like