Uploaded by Bob Saggot

Exam1-Fall17

advertisement
Honors Introduction to C
Exam #1
9/27/2017
Name: ____________________________________________
Directions: Please answer questions 1 - 5 in C, and questions 6 - 10 in C++.
Part I - C
1) (12 pts) Denise has an infinite supply of quarters, dimes and pennies, but no other coins or
bills. Complete the program below so that it asks the user how much Denise owes for an item
and prints out how many quarters, dimes and pennies she'll pay. Assume she uses the fewest
number of coins possible. (It turns out we can do this by first paying as many quarters without
going over, followed by paying as many dimes without going over, followed by paying for the
rest in pennies.)
#include <stdio.h>
int main() {
int n;
printf("What is the price of the item in cents?\n");
scanf("%d", &n);
int quarters = __________________ ;
n = _______________________ ;
int dimes = ________________ ;
int cents = ________________ ;
printf("%d quarters, %d dimes, %d cents\n", quarters, dimes,
cents);
return 0;
}
2) (5 pts) Determine the value of the following arithmetic expressions in C.
a) 13 – (4 – (3 – 7))
________
d) 3/2 + 3/2.0
________
b) 15 + 2*7
________
e) 15%(14 - 3%5)
________
c) 48/(17%5 + 9)
________
3) (10 pts) A geometric sequence is one where the ratio of successive terms is constant. For
example, 3, 6, 12, 24, 48 and 96 is a geometric sequence of 6 terms with a first term of 3, a
common ratio of 2. Complete the program below to ask the user to enter in the number of terms
in a geometric sequence, its first term and common ratio, and prints out each term in the
sequence.
#include <stdio.h>
int main() {
int n, first, ratio;
printf("Enter the number of terms of the sequence.\n");
scanf("%d", &n);
printf("Enter the first term and common ratio.\n");
scanf("%d%d", &first, &ratio);
return 0;
}
4) (12 pts) What is the output of the following program?
#include <stdio.h>
int main() {
int a=1, b=5, c=0;
while (a<b) {
a+=2;
b++;
c = c + a + b;
printf("a = %d, b = %d, c = %d\n", a, b, c);
}
return 0;
}
_____________________
_____________________
_____________________
_____________________
5) (10 pts) What is the output of the following code segment?
int i, j;
for (i=0; i<5; i++) {
for (j=5-i; j>=0; j--)
printf("%d ", j);
printf("\n");
}
_______________________
_______________________
_______________________
_______________________
_______________________
Part II - C++
6) (5 pts) What is the output of the following segment of code? (If there is no output, just write,
"There is no output.")
int age = 15;
if (age < 21)
if (age >= 16)
cout << "You can drive, but can't have a beer." << endl;
else
cout << "You can have a beer." << endl;
___________________________________________________
7) (12 pts) Complete the program below (in C++) that calculates the cost of buying pairs of shoes
in bulk at Shoes 'R Us. (The cost for the first ten pairs is $50/pair, the cost for the next ten pairs
is $40/pair and the cost for any pairs above 20 pairs is $30/pair.
using namespace std;
#include <iostream>
int main() {
int n, cost=0;
cout << "How many pairs of shoes are you buying?" << endl;
cin >> n;
return 0;
}
8) (12 pts) Write a full program below in C++ that asks the user for an initial positive value (real
number) greater than 2 and counts and prints out the number of times you have to take the
square root of that number to obtain a number less than 2. (For example, if we started with 17,
the square root of 17 is roughly 4.12, and the square root of that value is roughly 2.03. Finally
the square root of that value is roughly 1.42. Thus, we have to take the square root of 17 three
times to obtain a value less than 2.) Don't worry about small floating point error.
using namespace std;
#include <iostream>
#include <cmath>
int main() {
return 0;
}
9) (20 pts) An abundant number is one where the sum of its proper divisors (all of its divisors
except itself) is greater than the number. For example, 12 is abundant because 1, 2, 3, 4, and 6
are all divisors of 12 less than 12 and 1 + 2 + 3 + 4 + 6 = 16, which is greater than 12. Complete
the program below in C++ so that it asks the user to enter a positive integer n and then it prints
out every abundant number less than or equal to n.
using namespace std;
#include <iostream>
int main() {
int n;
cout << "Please enter your upper bound." << endl;
cin >> n;
cout << "All abundant integers up to " << n << ": ";
cout << endl;
return 0;
}
10) (2 pts) What herb is contained in Jeremiah's Italian Ice flavor Lemon Basil?
_______________________
Scratch Page - Please clearly mark any work on this page you would like graded.
Download