Numerical Range / Sum Calculator §
#include <iostream>
#include <cmath>
int main()
{
// caleb b
// numerical range / sum calculator
int startVal;
int endVal;
int sum = 0;
int product = 1;
std::cout << "Enter a starting value: " << std::endl;
std::cin >> startVal;
std::cout << "Enter an ending value: " << std::endl;
std::cin >> endVal;
for(int i = startVal; i <= endVal; i++)
{
sum = sum + i;
product = product * i;
}
std::cout << "The sum of the numbers between " << startVal << " and " << endVal << " is " << sum << std::endl;
std::cout << "The product of the numbers between " << startVal << " and " << endVal << " is " << product << std::endl;
}
Average Calculator §
#include <iostream>
#include <cmath>
int main()
{
// caleb b
// average calculator
// prompt the user to input their number of classes
std::cout << "How many classes are you taking? " << std::endl;
int numClasses;
std::cin >> numClasses;
double grades = 0;
// prompt the user to input their grades
std::cout << "Enter your grades: " << std::endl;
for(int i = 0; i < numClasses; i++)
{
std::cout << "Class " << i + 1 << ": ";
double grade;
std::cin >> grade;
if (grade < 0 || grade > 100) {
std::cout << "Invalid grade. Please enter a grade between 0 and 100." << std::endl;
i--;
continue;
}
grades += grade;
}
double average = grades / numClasses;
std::cout << "Your average is: " << average << std::endl;
return 0;
}
100m Race Simulator §
#include <iostream>
#include <cmath>
int main()
{
// caleb b
// 100m race simulator
bool running = true;
int totalDistance = 0;
// loop while the distance is less than 100
while(running) {
int distanceTraveled;
std::cout << "How far did the runner run this second? (10-25m): " << std::endl;
std::cin >> distanceTraveled;
// check the validity of the distance traveled in this second
if (distanceTraveled < 10 || distanceTraveled > 25) {
std::cout << "Invalid distance. Please enter a distance between 10 and 25 meters." << std::endl;
continue;
}
// if it's valid, add to the total
totalDistance += distanceTraveled;
// print the total distance
std::cout << "The runner has run " << totalDistance << " meters." << std::endl;
// check if the runner has finished
if (totalDistance >= 100) {
running = false;
}
}
std::cout << "DONE" << std::endl;
}