You are on page 1of 3

Directions: You will be solving two C++ programming problems detailed below.

Task:

 Write a program that takes in three arguments, a start temperature (in Celsius), an end
temperature (in Celsius) and a step size. Print out a table that goes from the start
temperature to the end temperature, in steps of the step size; you do not actually need to
print the final end temperature if the step size does not exactly match. You should perform
input validation: do not accept start temperatures less than a lower limit (which your code
should specify as a constant) or higher than an upper limit (which your code should also
specify). You should not allow a step size greater than the difference in temperatures.

Problem:

 Write a program that prompts for the user to input a sentence. Then check this sentence
to make sure the first word of the sentence is capitalized and the sentence ends with a
punctuation mark. If it is not properly written, fix the sentence, print the type of error, and
print the fixed sentence.
Answer key:
Task:
#include <stdio.h>
#define LOWER_LIMIT 0
#define HIGHER_LIMIT 50000
int main(void) {
double fahr, cel;
int limit_low = -1;
int limit_high = -1;
int step = -1;
int max_step_size = 0;
/* Read in lower, higher limit and step */
while(limit_low < (int) LOWER_LIMIT) {
printf("Please give in a lower limit, limit >= %d: ", (int) LOWER_LIMIT);
scanf("%d", &limit_low);
}
while((limit_high <= limit_low) || (limit_high > (int) HIGHER_LIMIT)) {
printf("Please give in a higher limit, %d < limit <= %d: ", limit_low, (int) HIGHER_LIMIT);
scanf("%d", &limit_high);
}
max_step_size = limit_high - limit_low;
while((step <= 0) || (step > max_step_size)) {
printf("Please give in a step, 0 < step >= %d: ", max_step_size);
scanf("%d", &step);
}
/* Initialise Celsius-Variable */
cel = limit_low;

/* Print the Table */


printf("\nCelsius\t\tFahrenheit");
printf("\n-------\t\t----------\n");
while(cel <= limit_high) {
fahr = (9.0 * cel) / 5.0 + 32.0;
printf("%f\t%f\n", cel, fahr);
cel += step;
}
printf("\n");
return 0;
}
Problem:
sentence = input("Enter a sentence ").lstrip() # remove trailing whitespaces
# check if first character is uppercase
if not sentence[0].isupper():
print("Sentence does not start with uppercase character")
# correct the sentence
sentence = sentence[0].upper() + sentence[1:]
# check if last character is a punctuation
# (feel free to add other punctuations)
if sentence[-1] not in (['.']):
print("Sentence does not end with punctuation character")
# correct the sentence
sentence += '.'
#finally print the correct sentence
print(sentence)

You might also like