Question

In: Computer Science

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 radius = diameter/2;
float volume = (4/3)*PI*radius*radius*radius;
printf("%.2f",volume);
  
return 0;
}

CODE B

#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 radius = diameter/2;
float volume = (4/3)*PI*radius*radius*radius;
printf("%.2f",volume);
  
return 0;
}

CODE C

#include <stdio.h>
#include<stdlib.h>

int main()
{
int num;
FILE *fptr;

// use appropriate location if you are using MacOS or Linux
fptr = fopen("epquakes.txt","r");

// if fptr is null means file not exists
if(fptr == NULL)
{
printf("Error!");   
exit(1);   
}

// c for coutn and invalid count
int c = 0, invalid = 0;
// iterate with while loop until end of file
while((fgetc(fptr)) != EOF )
{
double num;
  
//scan the float values
fscanf(fptr,"%lf",&num);
  
//check for above 3.0
if(num>3.0){
c++;
}
  
//check for invalid values
if(num<0.0){
invalid++;
}
}
//print output
printf("Total count above 3.0 is %d\n Total invalid data is %d",c,invalid);
fclose(fptr);

return 0;
}

For each code write:

-Inputs

-Outputs

-Formulas used (include explanation, units, definition of any constants, and any sketches)

-Assumptions/Pre-conditions (ex: valid/invalid inputs, restrictions, factors disregarded)

-Algorithm

-Example worked by hand (may include sketches)

-Test Plan: Included your test plan

-Evaluation/ Conclusions based on program execution

Solutions

Expert Solution

A&B are same codes for finding the volume of a sphere

Inputs :

  • diameter in integer data type.

Outputs :

  • volume in float data type, the volume of the sphere.

Formuals used :

  • radius = diameter/2
  • volume = (4/3)*PI*radius*radius*radius

Assumption/Precondition :

  • The diameter should be greater than 8 and less than 60

Algorithm:

  1. Start
  2. Initialize int diameter, decalre PI, float volume, float radius
  3. Read diameter
  4. If (diameter >60 || diameter<=8)
    1. Print ("Invalid input")
  5. else
    1. radius = diameter/2;
    2. volume = (4/3)*PI*radius*radius*radius;
    3. print volume
  6. while (diameter >60 || diameter<=8)
    1. print ("Invalid input Enter again:")
    2. Read diameter
    3. radius = diameter/2;
    4. volume = (4/3)*PI*radius*radius*radius;
    5. print volume
  7. stop

Example:

Enter value of diameter between 8 to 60 inches:

61

Invalid input Enter again:

10

4188.78

Test plan :

  • Trying different numbers which are not in the list
  • Checking the complxity of program

Evaluation:

  • The program can be simplified further
  • Same calculation is written two times. This can be avoided using conditional statements

Code C

Inputs :

  • file name (in code)
  • num value

Outputs :

  • Total count value above 3.0
  • Total invalid count

Formuals used :

  • Conditional statement and increment only

Assumption/Precondition :

  • Checking whether the file is there or not
  • Checking num value greater than or less than 3

Algorithm:

  1. Start
  2. Initialize num, file pointer, c=0, invalid=0;
  3. Point and open file epquakes.txt
  4. If pointer is null
    1. Print error
  5. while pointer is not at end of the file, ((fgetc(fptr)) != EOF )
    1. Initialize double num;
    2. Read num;
    3. If (num>3.0)
      1. Increment c by one, (c++)
    4. If(num<0.0)
      1. Increment invalid by one, (invalid++)
    5. End while
  6. Print ("Total count above 3.0 is %d\n Total invalid data is %d",c,invalid);
  7. Close file
  8. Stop

Example:

7

4.5

1

-1

-2

-8

Total count above 3.0 is 2

Total invalid data is 3

Test plan:

  • Testing with pointing to null address
  • Testing maximum numbers
  • Testing negatives and floats

Evaluation:

  • The program can be simplified to more readable form for eeasy understanding.

Related Solutions

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...
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)...
#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?
#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); }
Can you translate this C code into MIPS assembly? #include <stdio.h> #include <math.h> #include <stdlib.h> double...
Can you translate this C code into MIPS assembly? #include <stdio.h> #include <math.h> #include <stdlib.h> double fact (double); void main () { int angle_in_D; double term, angle_in_R; float sine = 0; unsigned int i = 1; double sign = 1; int n = 1000; printf ("Please enter an angle (Unit: Degree): "); scanf ("%d", &angle_in_D); angle_in_R = angle_in_D * M_PI / 180.0; do { term = pow(-1,(i-1)) * pow (angle_in_R, (2*i - 1)) / fact (2*i - 1); sine =...
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { int count; if ((argc...
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { int count; if ((argc != 2) || (sscanf(argv[1],"%d",&count) != 1)) { fprintf(stderr,"Usage: %s <integer>\n", argv[0]); exit(1); } pid_t pid1, pid2; while (count > 0) { pid1 = fork(); if (pid1 > 0) { pid2 = fork(); count = count - 2; } else if (pid1 == 0) { count = count - 1; } } exit(0); } Question #1 [2 pts] If the command-line argument passed to this...
#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...
please fix code #include <stdio.h> #include <stdlib.h> #include <string.h> // function declarations int getValidJerseyNumber(); int getValidRating();...
please fix code #include <stdio.h> #include <stdlib.h> #include <string.h> // function declarations int getValidJerseyNumber(); int getValidRating(); int main() { // declaring variables int size = 5; int jerseyNo[size]; int rating[size]; int i = 0, jno, rate; char option; /* Getting the inputs entered by the user * and populate the values into arrays */ for (i = 0; i < size; i++) { printf("Enter player %d's jersey number:", i + 1); jerseyNo[i] = getValidJerseyNumber(); printf("Enter player %d's rating:\n", i +...
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc, char *argv[]) {     FILE *myFile;...
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc, char *argv[]) {     FILE *myFile;     char fname[20];     //int sum = 0;     int i, j, k, tmp =0;     int num = 0;     int mass = 0;     int count = 0;     int fuel = 0;     int total = 0;     int M[1000];     char ch;     char buffer[32];     printf(" Input the filename to be opened : ");     scanf("%s",fname);     myFile = fopen(fname, "r");     if(myFile == NULL)     {         printf("Can't open file\n");     } while(1)     {         ch =...
#include <stdio.h> #include <stdlib.h> #include <math.h> #include "../include/cis1057.h" /* * Programmer: << MA >> * Class:...
#include <stdio.h> #include <stdlib.h> #include <math.h> #include "../include/cis1057.h" /* * Programmer: << MA >> * Class: Introduction to C Programming 1057 Spring 2019 Section 004 * Assignment: Number 5 “estimate the value of a factorial using Gosper's algorithm." * Date: << 02-19-2019 >> * Version: 1 * Description: Program will prompt for some data, read in the values, perform the calculation using library math functions, and display the result. * File: lab5.c */ # define M_PI 3.14159265358979323846 /* pi */...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT