Question

In: Computer Science

Intro C++ Programming Chapter 6 Functions You have been tasked to write a new program for...

Intro C++ Programming Chapter 6 Functions

You have been tasked to write a new program for the Research Center's shipping department. The shipping charges for the center are as follows:

Weight of Package (in kilograms)                Rate per mile Shipped
2 kg or less                                                      $0.05
Over 2 kg but no more than 6 kg    $0.09
Over 6 kg but not more than 10 kg    $0.12
Over 10 kg    $0.20

Write a function in a program that asks for the weight of a package in kilograms and the distance it is to be shipped. Using that data, write another function that calculates the shipping charge and returns it to the main program and displays the value there.

For the second function, pass the two values (weight and distance) into the function as arguments. In the function if the shipping distance the user provided earlier is a distance greater than 200 miles, there will be an additional charge of $25 added to the order. For example, if an item weighing 20 kg is shipped 250 miles, the total cost will be $75, calculated by the formula: ((250 miles * 0.20) + 25).

input validation: Do not accept any weights less than 0.5 kilograms, nor any distances less than 0 miles.

Solutions

Expert Solution

#include <iostream>
#include<stdlib.h>
using namespace std;
int shipping_charge(float weight ,int distance){   //second function to calculate the shipping charge
  
   int additional_charge=0;
   float rate_per_mile,result;
   if(weight<=2){           //if weight of package in kg is 2kg or less
       rate_per_mile=0.05;
   }
   else if(weight>2 && weight<=6){   //if weight of package in kg over 2kg but no more than 6kg
       rate_per_mile=0.09;
   }
   else if(weight>6 && weight<=10){ //if weight of package in kg over 6kg but no more than 10kg
       rate_per_mile=0.12;
   }
   else if(weight>10){   //if weight of package over 10kg
       rate_per_mile=0.20;
   }
   if(distance>200){   //if distance is greater than 200 update additional_charge=25
       additional_charge=25;
   }
   result=(distance*rate_per_mile)+additional_charge;
   return result;       //return the result to the main function
}

int main(){       //main function that asks for the weight of a package in kilograms and the distance in miles it is to be shipped
  
   int distance;
   float weight,output;
   cout<< "Enter Weight of Package in Kilograms: ";
   cin>> weight;
   if(weight<=0.5){   //displaying instant error message to enter valid data and exiting from the program
       cout<< "Enter Weights greater than 0.5 Km" <<endl;
       exit(0);   //must include<stdlib.h>
   }
   cout<< "Enter distance to be Shipped: ";
   cin>> distance;
   if(distance<=0){ //displaying instant error message to enter valid data and exiting from the program
       cout<< "Enter Distance greater than 0 miles" <<endl;
       exit(0);   //must include<stdlib.h>
   }
   output=shipping_charge(weight,distance);   //storing returned value from second function in the output  
   cout<< "Total Cost: $"<<output<<endl;   //displaying the total cost
}

Output: //Hope it helps.If you have any doubts feel free to add comments.


Related Solutions

Write a C++ program that will use good object-oriented principles. You have been tasked to write...
Write a C++ program that will use good object-oriented principles. You have been tasked to write an application that will allow a user to change their system password. The XYZ Corporation has the following rules for passwords: each password should have a minimum of 8 characters each password should have a minimum of 2 uppercase characters (A - Z) each password should have a minimum of 2 lowercase characters (a - z) each password should have a minimum of 2...
Time Calculator – Intro To Programming - JAVA Write a program that asks the user to...
Time Calculator – Intro To Programming - JAVA Write a program that asks the user to enter a number of seconds. • There are 60 seconds in a minute. If the number of seconds entered by the user is greater than or equal to 60, the program should display the number of minutes in that many seconds. • There are 3,600 seconds in an hour. If the number of seconds entered by the user is greater than or equal to...
6. Write a program in C programming (compile and run), a pseudocode, and draw a flowchart...
6. Write a program in C programming (compile and run), a pseudocode, and draw a flowchart for each of the following problems: a) Obtain three numbers from the keyboard, compute their product and display the result. b) Obtain two numbers from the keyboard, and determine and display which (if either) is the smaller of the two numbers. c) Obtain a series of positive numbers from the keyboard, and determine and display their average (with 4 decimal points). Assume that the...
C PROGRAMMING – Steganography In this assignment, you will write an C program that includes processing...
C PROGRAMMING – Steganography In this assignment, you will write an C program that includes processing input, using control structures, and bitwise operations. The input for your program will be a text file containing a large amount of English. Your program must extract the “secret message” from the input file. The message is hidden inside the file using the following scheme. The message is hidden in binary notation, as a sequence of 0’s and 1’s. Each block of 8-bits is...
In C Programming Language Write a program to output to a text log file a new...
In C Programming Language Write a program to output to a text log file a new line starting with day time date followed by the message "SUCCESSFUL". Please screenshot the results.
Congratulations! You have been appointed as a new product manager with Samsung and have been tasked...
Congratulations! You have been appointed as a new product manager with Samsung and have been tasked to introduce the latest smartphone in a new country. Choose and research a country other than the United States and India, and then formulate a plan to effectively integrate this product from a global and social standpoint. Speculate on the major challenges and opportunities you anticipate facing in this role. Provide a reference source.
write pseudocode not c program If- else programming exercises 1.    Write a C program to find...
write pseudocode not c program If- else programming exercises 1.    Write a C program to find maximum between two numbers. 2.    Write a C program to find maximum between three numbers. 3.    Write a C program to check whether a number is negative, positive or zero. 4.    Write a C program to check whether a number is divisible by 5 and 11 or not. 5.    Write a C program to check whether a number is even or odd. 6.    Write...
This is C# programming. In Chapter 2, you created an interactive application named GreenvilleRevenue. The program...
This is C# programming. In Chapter 2, you created an interactive application named GreenvilleRevenue. The program prompts a user for the number of contestants entered in this year’s and last year’s Greenville Idol competition, and then it displays the revenue expected for this year’s competition if each contestant pays a $25 entrance fee. The programs also display a statement that compares the number of contestants each year. Now, replace that statement with one of the following messages: If the competition...
A new warehouse is to be constructed and you have been tasked to provide the initial...
A new warehouse is to be constructed and you have been tasked to provide the initial design. The warehouse is primarily used for pick/pack of grocery items with a small amount of frozen goods and some pharmaceuticals. Incoming goods are palletized and outgoing orders are as per the customers’ orders. The warehouse will contain some 8000 SKU’s with the majority considered, 80%, to be C class items. The building is only framed right now and is waiting on your design...
You are the HR manager for a new company and you have been tasked with developing...
You are the HR manager for a new company and you have been tasked with developing a concise employee manual for your new middle managers. This manual must help the managers understand certain HR initiatives so that they can recruit, select, and manager their staff effectively. The manual will consist of explaining and providing guidelines for best practices in the areas below. Explain the importance of a job description and briefly describe the necessary components Describe an effective recruiting process...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT