Question

Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in...

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)

  1. 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


Homework Answers

Answer #1


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;
}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a program in C++ coding that asks the user to input an integer between 25...
Write a program in C++ coding that asks the user to input an integer between 25 and 50, inclusive. Utilize a WHILE loop to test for INVALID input. If the input is INVALID, the loop will repeat, and ask the user to try again. The pseudocode looks like this: Prompt user to input an integer between 25 and 50 Accept the input from the user Test for invalid input (HINT: use a while loop and the OR operator with 2...
Write a program that asks the user to input an integer between 25 and 50, inclusive....
Write a program that asks the user to input an integer between 25 and 50, inclusive. Utilize a WHILE loop to test for INVALID input. If the input is INVALID, the loop will repeat, and ask the user to try again. The pseudocode looks like this: Prompt user to input an integer between 25 and 50 Accept the input from the user Test for invalid input (HINT: use a while loop and the OR operator with 2 relational expressions. You...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
Python: Simple Banking Application Project Solution: • Input file: The program starts with reading in all...
Python: Simple Banking Application Project Solution: • Input file: The program starts with reading in all user information from a given input file. The input file contains information of a user in following order: username, first name, last name, password, account number and account balance. Information is separated with ‘|’. o username is a unique information, so no two users will have same username. Sample input file: Username eaglebank has password 123456, account number of BB12 and balance of $1000....
For this assignment, you will be creating a simple “Magic Number” program. When your program starts,...
For this assignment, you will be creating a simple “Magic Number” program. When your program starts, it will present a welcome screen. You will ask the user for their first name and what class they are using the program for (remember that this is a string that has spaces in it), then you will print the following message: NAME, welcome to your Magic Number program. I hope it helps you with your CSCI 1410 class! Note that "NAME" and "CSCI...
Code a program to reproduce Madame Esmerelda’s clairvoyant test: Mme. Esmerelda conjures up a random number...
Code a program to reproduce Madame Esmerelda’s clairvoyant test: Mme. Esmerelda conjures up a random number 1-10 in her mind and commands the user to guess what it is. If the user’s guess is equal to (remember your double equal sign!) the conjured number, Mme. Esmerelda extends a job offer as a clairvoyant assistant. Add a user-driven loop that allows the user to play repeatedly if desired (but your program should conjure a new random number with every loop iteration)....
Written in MASM Assembly Problem Definition: Write a program to calculate Fibonacci numbers. • Display the...
Written in MASM Assembly Problem Definition: Write a program to calculate Fibonacci numbers. • Display the program title and programmer’s name. Then get the user’s name, and greet the user. • Prompt the user to enter the number of Fibonacci terms to be displayed. Advise the user to enter an integer in the range [1 .. 46]. • Get and validate the user input (n). • Calculate and display all of the Fibonacci numbers up to and including the nth...
This is a Java program Program Description You work for a local cell phone company and...
This is a Java program Program Description You work for a local cell phone company and have been asked to write a program to calculate the price of a cell phone data plan being purchased by a customer. The program should do the following tasks: Display a menu of the data plans that are available to be purchased. Read in the user’s selection from the menu.  Input Validation: If the user enters an invalid option, the program should display an error...
Create a flowgorithm program to calculate the Area of Circle first then do the Circumference of...
Create a flowgorithm program to calculate the Area of Circle first then do the Circumference of Circle. The user will have the ability to convert Area OR the Circumference. That means we need to add a decision structure to the program. Furthermore, they need to be able to do it as many times as they want. That means we need to add a main loop structure to the program. The user will also have the choice of whether to run...
(Use C++ language) Create a program, named threeTypesLoops.cpp, that does the following; 1. Uses a For...
(Use C++ language) Create a program, named threeTypesLoops.cpp, that does the following; 1. Uses a For Loop that asks for a number between 1 and 10; Will double the number each time through the loop and display the answer. Will run five times, so the number will have been doubled five times, and the answer displayed five times. After the loop, display a message saying 'It's done!'. 2. Uses a While Loop; Ask the user to input a friend's name....