In: Computer Science
C programming
#include <stdio.h>
#include <math.h>
int main()
{
printf("============== Problem #1 =================\n");
printf("This should print down from 2 to 0 by 0.1 increments\n");
float f = 2.0;
while (f != 0)
{
printf("%0.1f\n",f);
f = f - 0.1;
}
printf("============== Problem #2 =================\n");
printf("This should find that the average is 5.5\n");
int total_score = 55;
int total_grades = 10;
double avg = total_score/total_grades;
printf("Average: %0.2f\n",avg);
printf("============== Problem #3 =================\n");
printf("If the population increases by 2.5 people per second, how long before the population hits 8 billion?\n");
float current_population = 7600000000;
float increase_per_second = 2.5;
printf("Counting...\n");
int second_count = 0;
while (current_population != 8000000000)
{
current_population += increase_per_second;
second_count++;
}
printf("It will be %d seconds from now.\n",second_count);
return(0);
}
Activity 1: Fix the first problem
The first part of this program should print from 2.0 to 0.0 in decrements of
0.1. It starts off good but then the program doesn't end. Figure out why and
fix the program so it terminates (in that loop at least).
Activity 2: Fix the second problem
The second part should calculate the average when given a total of 55 over 10
sample points. The average should be 5.5 but instead, 5.0 is printed out. Find
and fix that problem.
Activity 3: Fix the third problem
The third part is supposed predict the exact second that the world population
will hit 8000000000. It starts with the current population 7600000000 and adds
2.5 people per second. This is the net population increase per
second. Unfortunately, it seems that it never gets to 8000000000.
#include <stdio.h>
#include <math.h>
int main()
{
printf("============== Problem #1 =================\n");
printf("This should print down from 2 to 0 by 0.1 increments\n");
float f = 2.0;
while (f >= 0.0)
{
printf("%0.1f\n",f);
f -= 0.1;
}
printf("============== Problem #2 =================\n");
printf("This should find that the average is 5.5\n");
int total_score = 55;
int total_grades = 10;
double avg = (double)total_score/total_grades;
printf("Average: %0.1f\n",avg);
printf("============== Problem #3 =================\n");
printf("If the population increases by 2.5 people per second, how long before the population hits 8 billion?\n");
float current_population = 7600000000;
float final_population = 8000000000;
float increase_per_second = 2.5;
printf("Counting...\n");
float second_count = (final_population - current_population)/increase_per_second;
printf("It will be %.3f seconds from now.\n",second_count);
return(0);
}