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...
Write a program that asks the user for the name of a file. The program should...
Write a program that asks the user for the name of a file. The program should display only the first five lines of the file’s contents. If the file contains less than five lines, it should display the file’s entire contents. by python using while loop
Programing lanugaue is C++ Plan and code a menu-driven modular program utilizing an array An input...
Programing lanugaue is C++ Plan and code a menu-driven modular program utilizing an array An input file has an unknown number of numeric values(could be empty too).Read the numbers from the input fileand store even numbers in one arrayand odd numbers in another array.Create menu options to Display count of even numbers, count of odd numbersand the sum values in each array Determine the average of each array Determine the median of each array Sort each array in ascending order(use...
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...
Develop a C++ program that determines the largest and second largest positive values in a collection...
Develop a C++ program that determines the largest and second largest positive values in a collection of data Prompt the user to enter integer values until they enter any negative value to quit You may presume the user will input at least two valid integer values Create a loop structure of some sort to execute this input cycle Maintain the largest and second largest integer as the user inputs data This logic should be placed inside your loop structure Arrays...
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...
Write a C++ program. 1) Write a program that writes the grades for 3 students. Declare...
Write a C++ program. 1) Write a program that writes the grades for 3 students. Declare a variable of type ofstream which is used to output a stream into a file: ofstream output; // output is the name of the variable Prompt the user to input values for the grades of 3 students. Use the output operator (<<) to write the grades into grades.txt: output << grade1 << " " << grade2 << " " << grade3 << endl; You...
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....
The class name is StudentTest and the filename is StudentTest.java. The program must do the following...
The class name is StudentTest and the filename is StudentTest.java. The program must do the following (all in main): The program must create three Student objects using the Student constructor. Use literal data for the arguments to the constructor for the first two Students, and variables for the third Student. Prompt for and input the first name, last name and GPA for the third Student object. The program must display the name and GPA for each Student. Use printf and...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT