Question

In: Computer Science

C programming #include <stdio.h> #include <math.h> int main() { printf("============== Problem #1 =================\n"); printf("This should print...

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.

Solutions

Expert Solution

#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);

}


Related Solutions

CODE A #include<stdio.h> #include<math.h> #include<stdlib.h> #define PI 3.14159265358979323846 int main(){ int diameter; printf("Enter value of diameter...
CODE A #include<stdio.h> #include<math.h> #include<stdlib.h> #define PI 3.14159265358979323846 int main(){ int diameter; printf("Enter value of diameter between 8 to 60 inches: "); scanf("%d",&diameter); // if(diameter>60 || diameter<=8){ // printf("Error! invalid input"); // exit(0); // } // else{ // float radius = diameter/2; // float volume = (4/3)*PI*radius*radius*radius; // printf("%.2f",volume); // } //check through the while loop if it is valid or in valid while(diameter>60 || diameter<=8){ printf("Invalid input Enter again: "); scanf("%d",&diameter); }    //caluclate the volume of sphere float...
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x...
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x = 3;     i = fun(x);     printf("%d\n", i);     return 0; } int fun(int i) {      int res = 0;      res = pow (i , 3.0);      return ( res); }
#include <stdio.h> int sum(int n); //prototype int main() {     int number, result;     printf("Enter a positive integer:...
#include <stdio.h> int sum(int n); //prototype int main() {     int number, result;     printf("Enter a positive integer: ");     scanf("%d", &number);     result = sum(number);     printf("sum = %d", result);     return 0; } int sum(int n) {     if (n != 0)         return n + sum(n-1);     else         return n; } What does the code above do?
My current code that is not working correctly #include<stdio.h> #include<math.h> int main() { //variable declaration int...
My current code that is not working correctly #include<stdio.h> #include<math.h> int main() { //variable declaration int a,b,c,D,n; double x1,x2; double realPart, imagPart; do { // this set of code blocks prompts a message to the user and then read the integer entered and stores it as an integer printf("Enter a value for a:\n"); scanf("%d",&a); printf("Enter a value for b:\n"); scanf("%d",&b); printf("Enter a value for c:\n"); scanf("%d",&c); printf("You entered the Equation \n"); printf("%dx^2%+dx%+d=0\n",a,b,c); D = b*b - 4*a*c;    if (D<0)...
Please implement the 5 questions in source code: #include <stdio.h> #include <stdlib.h> #include <math.h> int main(...
Please implement the 5 questions in source code: #include <stdio.h> #include <stdlib.h> #include <math.h> int main( int argc, char* argv[] ) { // Size of vectors int n = 10000; // Input vectors double *restrict a; double *restrict b; // Output vector double *restrict c; // Size, in bytes, of each vector size_t bytes = n*sizeof(double); /* Q1: Allocate memory for vector a (10 points)*/ /* Q2: Allocate memory for vector b (10 points)*/ /* Q3: Allocate memory for vector...
Can you please write a pseudocode for the following: #include <stdio.h> int main(){    printf("Welcome to...
Can you please write a pseudocode for the following: #include <stdio.h> int main(){    printf("Welcome to my Command-Line Calculator (CLC)\n");    printf("Developer: Your name will come here\n");    printf("Version: 1\n");    printf("Date: Development data Will come here\n");    printf("----------------------------------------------------------\n\n");    //choice stores users input    char choice;    //to store numbers    int val1,val2;    //to store operator    char operation;    //flag which leths the loop iterate    //the loop will break once the flag is set to 0...
#include <stdio.h> int main() { int i, j; printf("Enter two positive integers: "); scanf("%d%d", &i, &j);...
#include <stdio.h> int main() { int i, j; printf("Enter two positive integers: "); scanf("%d%d", &i, &j); // need to validate both being positive while (i != j) { i > j ? (i -= j) : (j -= i); } printf("GCD: %d\n", i); return 0; } The above code returns a GCD from two numbers entered. Use the above program to design a C program to compute of integer coefficients a and b such that gcd(i, j) = a xi...
*Answer in C program* #include <stdio.h> int main() {      FILE *fp1;      char c;     ...
*Answer in C program* #include <stdio.h> int main() {      FILE *fp1;      char c;      fp1= fopen ("C:\\myfiles\\newfile.txt", "r");      while(1)      {         c = fgetc(fp1);         if(c==EOF)             break;         else             printf("%c", c);      }      fclose(fp1);      return 0; } In the program above which statement is functioning for opening a file Write the syntax for opening a file What mode that being used in the program. Give the example from the program Referring to...
#include <stdio.h> #include<math.h> int main (void). { int I, j, k, num_elem; double x[20], y[20],z[20]; FILE*infile,...
#include <stdio.h> #include<math.h> int main (void). { int I, j, k, num_elem; double x[20], y[20],z[20]; FILE*infile, *outfile; infile = fopen (“c6_3.IN”, “r”); outfile = fopen (“c6_3.OUT”, “w”); k = fscanf (infile, “%lf %lf”, &x[0], &y[0]); fprintf (outfile,”k= %d\n”,k); fprintf (outfile, “value of EOF = %d\n”, EOF); i =1; while ( fscanf(infile, “%lf %lf”, &x[i], &y[i]) != EOF) i++; num_elem =I; fprintf(outfile,” x[i] y[i] z[i]\n”); For (j=0; j<num_elem; j++) { Z[j] =sqrt(x[j] *x[j] +y[j]*y[j]); fprintf(outfile,”%7.1f %7.1f %7.1f\n”, x[j] , y[j], z[j]);...
What is the output of the following C program? #include<stdio.h> int fac (int x); void main(...
What is the output of the following C program? #include<stdio.h> int fac (int x); void main( ) {                         for (int i=1; i<=2; i++)                                     printf("%d", fac(i)); } int fac(int x) {                         x = (x>1) ? x + fac(x-1) : 100);                         return x; }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT