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 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
A&B are same codes for finding the volume of a sphere
Inputs :
Outputs :
Formuals used :
Assumption/Precondition :
Algorithm:
Example:
Enter value of diameter between 8 to 60 inches:
61
Invalid input Enter again:
10
4188.78
Test plan :
Evaluation:
Code C
Inputs :
Outputs :
Formuals used :
Assumption/Precondition :
Algorithm:
Example:
7
4.5
1
-1
-2
-8
Total count above 3.0 is 2
Total invalid data is 3
Test plan:
Evaluation: