Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c)
1. Place the code you developed in Lab 2 to obtain a diameter value from the user and compute the volume of a sphere (we assumed that to be the shape of a balloon) in a new program, and implement the following restriction on the user’s input: the user should enter a value for the diameter which is at least 8 inches but not larger than 60 inches. Using an if-else statement, test the diameter and based on the result of the test, take one of the following actions:
Display an error message if it is invalid, and do not continue solving the problem
If valid, compute the balloon’s volume
2. Test your program with both valid and invalid diameter values to verify results. Display the values used in the tests, and save screenshots of the output from both tests.
3. Next, comment out the if-else statement and replace it with a while-loop to allow the user to make repeated attempts to enter a valid size for the balloon diameter. Test your program again using both valid and invalid values, and save screenshots of results.
Part B. Menu Display (Name your C program yourLastName_yourFirstName_Lab4b.c)
Write a C program that includes three different versions of an ATM menu. The menu should include options to display the balance, deposit money, withdraw money, or to quit. Assign a starting balance for an account before displaying the menu. The first version should display the menu, prompt and read in the user’s choice from the menu, and print a message about which choice was selected. (It is not necessary to actually execute the action of depositing or withdrawing, although your program should display the balance if selected by the user.) Use an if-else statement in the first version. Test your program twice, using first an option from 1-3, inclusive, and then option 4 to quit. How will you handle the possibility that the user will enter some value other than 1-4? Document test values used and capture screenshots of the output from your tests. Could some other way to list the options be used, other than numbers?
2. The second version should display the same menu repeatedly using a while loop and print the appropriate message until the user wishes to quit. Use a switch statement to test the user’s choices in the second version. Test your program, using an options from 1-3, inclusive, and then option 4 to quit. Document test values used and capture screenshots of the output from your tests.
3. The third version should be the same as version #2, except use a do-while loop instead of a while loop.
Part C. Processing Data from Files (Name your C program yourLastName_yourFirstName_Lab4c.c)
1. Historical records of earthquake activity since 1900 may be found for local regions. Assume that earthquake data for the local area is saved in a file named “epquakes.txt”. Each data entry is expected to be a floating point magnitude value between 0.0 and 10.0, endpoints included. However, occasionally errors occur that result in bad data values (for example, negative numbers).
2. Using your zyBook example in Chap. 5, Figure 5.4.3 (note correction), write a C program that counts and displays the number of values in the data file that are above 3.0. Also count and display the number of entries in the file that are invalid.
3. Use the sample data file provided. First, write your code using a while loop to read and process all values in the file, regardless of how many there are. Then, comment out your while loop and replace it with a for loop to read and process only the first 10 data values from the file. Test both versions of your code, and capture screenshots of the output each time.
For each part write the lab report using the provided template below.
CS 1320 |
Date Due |
Lab Assignment # |
Last Name, First Name |
/ |
|
Problem Statement: |
|||||
Input(s): |
|||||
Output(s): |
|||||
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: Include your test plan |
|||||
Testing--Screenshot of output window, showing one run using same data as example worked by hand and one run using different value: |
|||||
Testing--Screenshot of output window, using other test data (invalid): |
|||||
Evaluation/Conclusions based on program execution |
PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU
NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU
AS I DONE MOST OF YOUR ANSWERS, THOUGH WE ARE ONLY ALLOWED TO
ATTEMPT ONE ANSWER OR FOUR SUB PARTS, PLEASE GIVE IT A THUMBS
UP
PART A
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
//define a constant for pi value
#define PI 3.14159265358979323846
int main(){
int diameter;
printf("Enter value of diameter between 8 to 60 inches: ");
//read from user
scanf("%d",&diameter);
//if diameter is not in range of 60 and 8
if(diameter>60 || diameter<8){
printf("Error! invalid input");
}
//for a valid diameter
else{
//calculate radius
float radius = diameter/2;
//calculate volume
float volume = (4/3)*PI*radius*radius*radius;
printf("%.2f",volume);
}
return 0;
}
Using while loop:
#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;
}
PART B
#include<stdio.h>
int main(){
int initial_bal = 200000;
char ch;
//Display the user to choose an option
printf("1.Display balance\n2.Depostite Money\n3.Withdraw\n4.Quit\n");
//read the input
scanf("%c",&ch);
//check for the options if 1 then display balance
if(ch == '1'){
printf("The balance is : %d",initial_bal);
}
//if 2 then depostie
else if(ch == '2'){
printf("User choose to Deposite\n");
}
//if 3 then withdraew
else if(ch == '3'){
printf("User choose to Withdraw\n");
}
//if 4 then exit
else if(ch == '4'){
printf("User cancel the trancation\n");
}
//else invalid input
else{
printf("Invalid input\n");
}
return 0;
}
Using while loop and switch:
#include<stdio.h>
#include<stdlib.h>
int main(){
int initial_bal = 200000;
int ch;
//display this list of options
printf("1.Display balance\n2.Depostite Money\n3.Withdraw\n4.Quit\n");
while(1){
//scan the input from user
scanf("%d",&ch);
//swithc according to the user choise
switch(ch){
//check balance
case 1:
printf("The balance is : %d\n",initial_bal);
break;
//deposite
case 2:
printf("User choose to Deposite\n");
break;
//Withdraw
case 3:
printf("User choose to Withdraw\n");
break;
//exit
case 4:
printf("User cancel the trancation\n");
exit(1);
default :
printf("Invalid choice\n");
}
}
}
Using do -while and switch:
#include<stdio.h>
#include<stdlib.h>
int main(){
int initial_bal = 200000;
int ch;
printf("1.Display balance\n2.Depostite Money\n3.Withdraw\n4.Quit\n");
do{ //using do while
scanf("%d",&ch);
switch(ch){
case 1:
printf("The balance is : %d\n",initial_bal);
break;
case 2:
printf("User choose to Deposite\n");
break;
case 3:
printf("User choose to Withdraw\n");
break;
case 4:
printf("User cancel the trancation\n");
exit(1);
default: // invalid case:
printf("Invalid choice\n");
break;
} } while(1);
}
PART 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;
}
Using for loop :
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num;
FILE *fptr;
int i;
// 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;
//usinf for loop to iterate
for(i=0;i<10;i++)
{
double num;
//scan float values from file
fscanf(fptr,"%lf",&num);
//count for values gretaer than 3.0
if(num>3.0){
c++;
}
//coutn invalid numbers
if(num<0.0){
invalid++;
}
}
printf("Total count above 3.0 is %d\n Total invalid data is %d",c,invalid);
fclose(fptr);
return 0;
}
Get Answers For Free
Most questions answered within 1 hours.