Beruflich Dokumente
Kultur Dokumente
Page 1
Score
Total
(B) f=3.5
(C) f=3.0
(D) f=2.5
2. (10%) Identify and fix the problem(s) with the following C code to get 8, 7, 6, 5, 4,
as output
#include <stdio.h>
void main (void) {
int i=8;
while (i <=4 );
printf("%d\n",i);
i = i - 1;
}
#include <stdio.h>
void main (void) {
int i=8;
while (i <=4 );
printf("%d, ",i);
i = i - 1;
}
+5 points for while loop condition
+5 points for printf statement
4. (20%) Here is the solution for assignment 3. Answer the questions below.
5. (20%) Write a program to sum the digits for a given number from the user. For
example, the sum of digits for 123 is 1 + 2 + 3 = 6
#include <stdio.h>
void main(void){
int input,sum=0;
printf("Please enter a number: ");
scanf("%d",&input);
while (input>0){
sum+=input%10;
input/=10;
}
printf("the sum of digits is %d",sum);
}
no scanf --> -1
no printf for result --> -1
wrong calculation -->-5
no loop --> -5
using multiple variables to read some digits -->-10
6. (20%) Write a program that prompts the user for their assignment scores, midterm
score and final score. Then calculate the users letter grade assuming the final grade isnt
curved.
Letter Grade
Range
Programming assignments** (30%)
A
100-90
Midterm (30%)
B
89.99-80
Final (40%)
C
79.99-70
D
69.99-60
**There are 6 assignments
F
59.99-0
Output:
x is true
x is invalid
10 points for having both
5 points for X is invalid
8. (20%) Write a program that asks the user for a (positive) input and prints out all the
multiples of that input up to 10 * input. For example, if the user inputs 3, it should
output 3, 6, 9, 12, 15,18, 21, 24, 27, 30.
#include <stdio.h>
int main() {
int i = 1, result = 0, input = 0;
printf("Please enter a positive number greater than 0: ");
scanf("%d", &input);
while (input <= 0) {
printf("Input is not greater than 0!\n");
printf("Please enter a positive number greater than 0: ");
scanf("%d", &input);
}
for (i = 1; i < 10; i++) {
printf("%d, ", i*input);
}
printf("%d.\n", i*input);
return 0;
}
You could've also hardcoded all the prints like the following instead of doing a for/while loop.
printf("%d, ", input);
printf("%d, ", 2*input);
printf("%d, ", 3*input);
printf("%d, ", 4*input);
printf("%d, ", 5*input);
printf("%d, ", 6*input);
printf("%d, ", 7*input);
printf("%d, ", 8*input);
printf("%d, ", 9*input);
printf("%d.\n", 10*input);
5% for 1st prompt (printf + scanf combination).
-2% printf, -3% scanf.
5% for enforcing positive input (normally, some kind of while loop to reprompt printf and scanf if
the user enters something less than or equal to 0).
-1% for incorrect condition, -1% for not using while loop, -1% for incorrect scanf.
10% for implementation of print output (did not enforce any restriction on output in terms of
commas, tabs, newlines, etc. as long as they could print the correct sequence).
-1% if sequence is just off at the ends (e.g. starting 0 or printing 1 additional beyond).
-5% for implementation if it completely prints out a different set of numbers.
-6% to -9% depending on how confusing their logic was.
-1% for each incorrect major syntax error (missing & in scanf, incorrect order of comparison (>=
vs =>), etc.)
-1% to -3% for small syntax errors in cumulative amounts (missing semicolons, missing curly
braces, missing parantheses, etc). I did these in incremental (e.g. 2 missing semicolons = -1%, 4
missing semicolons = -2%, etc.)
-3% for a program that is logically incorrect but portions of the parts were there (e.g. infinite loops,
weird for loop statements, portions of code never reached).