Question

In: Computer Science

CSCE 1030: Lab 7 General Guidelines: (for ALL of your programming assignments and labs)  Use...

CSCE 1030: Lab 7 General Guidelines: (for ALL of your programming assignments and labs)  Use meaningful variable names.  Use appropriate indentation.  Use comments, especially for the header, variables, and blocks of code. Please make sure that your submitted code has comments as points may be deducted for a lack of comments. Example Header: /* Author: Jane Doe ([email protected]) Date: Instructor: Description: A small description in your own words that describe what the program does. Any additional flags needed to allow the program to compile should also be placed here. */ Example Function Header: /* Function: Deposit Parameters: a double representing account balance and a double representing the deposit amount Return: a double representing account balance after the deposit Description: This function computes the account balance after a deposit. */ A. Working with Functions Declare and define a function that computes bonus for an employee depending on the base salary and the number of years of experience of the employee. Inside the function, check the number of experience. If the number of experience is greater than 15 years, the bonus is 5% of the base salary, otherwise it is 2.5% of the base salary. You will need to call this function from your main function and pass the base salary and the number of years as parameters/arguments. Start with the following template to write you program. 2 Now, modify the above program, referring to the comments included in the code. Complete the requested changes, and then save the file as Lab7A.cpp, making sure it compiles and works as expected. Note that you will submit this file to Canvas. B. Function calling another function You have already seen a function that has called another function, but you may not have paid close attention to it. In order to call a programmer-defined function inside another programmer-defined function, you need to have the declaration of the function that is being called before the declaration of the calling function. In this program you will write a function of void return type named compare that accepts an integer parameter named guess. This function will compare the value of guess with a seeded randomly generated integer between 1 to 100, inclusive, and let the user know what the random number was as well as whether the guess was larger than, smaller than or equal to the random number. NOTE THAT: You will NOT generate a random number inside the compare function. Rather, you will write another function named getRandom of int return type to do it. You will need to call getRandom from compare, no parameters are necessary. Inside your main function, get the guess from user and pass it to the compare function. 3 Start with the following template to write your code. Complete the requested changes, and then save the file as Lab7B.cpp, making sure it compiles and works as expected. Note that you will submit this file to Canvas. C. A function with Boolean return type Write a programmer defined function that compares the ASCII sum of two strings. To compute the ASCII sum, you need to compute the ASCII value of each character of the string and sum them up in a loop of your choice by processing one character at time. You will thus need to calculate two sums, one for each string. Then, return true if the first string has a larger ASCII sum compared to the second string, otherwise return false. In your main function, prompt the user to enter two strings with a suitable message and provide the strings to the function during function call. The user may enter multiple words for either string. Then, use the Boolean data returned by the function to inform which string has the higher ASCII sum. Use the following template to write your code. 4 Complete the requested changes, and then save the file as Lab7C.cpp, making sure it compiles and works as expected. Note that you will submit this file to Canvas. Now that you have completed this lab, it’s time to turn in your results. Once you've moved the files to your windows machine (using winscp), you may use the browser to submit them to Canvas for the Lab7 dropbox. You should submit the following files:  Lab7A.cpp  Lab7B.cpp  Lab7C.cpp Ask your TA to check your results before submission. The above three files MUST be submitted to Canvas by the end of your lab section. Now that you've finished the lab, use any additional time to practice writing simple programs out of the textbook, lectures, or even ones you come up with on your own to gain some more experience.

Solutions

Expert Solution

/*Program to calculate bonus for an employee*/

#include<iostream>
using namespace std;

/* Function: Bonus Parameters: a double representing base salary and an int representing number of years of experience.
Return: a double representing the bonus computed for an employee.
Description: This function computes the bonus based on the number of years experience of an employee. */
double Bonus(double baseSalary, int experience)
{
    double bonus;
    if(experience>15)                   //Checking the number of years of experience.
        bonus=0.05*baseSalary;         //If number of years experience > 15 then bonus is 5% of base salary
    else
        bonus=0.025*baseSalary;         //If number of years experience < 15 then bonus is 2.5% of base salary
    return bonus;                       //Returning the bonus as computed from the function
}

int main()
{
    int experience;
    double baseSalary,bonus;
    cout<<"Enter the base salary : ";
    cin>>baseSalary;                        //Taking base salary as input from the user
    cout<<"Enter the number of years experience : ";
    cin>>experience;                        //Taking the number of years of experience as input from the user
    bonus=Bonus(baseSalary,experience);     //Calling the Bonus function to calculate the bonus
    cout<<"The bonus for an employee is : "<<bonus<<endl;       //Printing the bonus as returned from the Bonus function
    return 0;
}


Output will be:

/*Program to compare a number with a randomly generated number*/

#include<stdlib.h>
using namespace std;


/*Function: getRandom
Parameters : no parameters passed to the function
Return: an int representing the random number generated from the function
Description: This function generates a random integer between 1 to 100, inclusive */
int getRandom()
{
    int random_number;     //variable representing the random number
    random_number=rand()%100+1;     //generating the random number between 1 to 100,both inclusive using rand()function which is defined in stdlib.h header file
    return random_number;       //returning the randomly generated integer
}

/*Function: compare
Parameters : an integer representing the guessed the number of the user
Return type : void.
Description: This function compares the value of guess with a seeded randomly generated integer between 1 to 100, inclusive*/
 void compare(int guess)
 {
    int rand_number;                                                 //variable representing the random number
    rand_number=getRandom();                                        //Calling getRandom function to generate the random number
    cout<<"The random  number generated is : "<<rand_number<<endl; //Printing the randomly generated integer
    if(guess>rand_number)                                       //Checking whether guess is greater than random number
        cout<<"Guess was larger than random number."<<endl;
    else if(guess<rand_number)                                  //Checking whether guess is smaller than random number
        cout<<"Guess was smaller than random number."<<endl;
    else                                                //Checking whether guess is equal to random number
        cout<<"Guess is equal to random number."<<endl;
 }
int main()
{
    int guess;
    cout<<"Enter the number : ";
    cin>>guess;                 //Taking input of a number from user
    compare(guess);             //Calling the compare function
    return 0;
}

Output will be:

/*Program to compare ascii sum of two strings*/

#include<iostream>
#include<stdlib.h>
#include <bits/stdc++.h>
using namespace std;

/*Function : ASCII_SUM
Parameters : a string representing the first string and a string representing the second string
Return: a boolean value to indicate which string is bigger
Description: This function compares the ASCII sum of two strings. */
bool ASCII_SUM(string A, string B)
{
    int sum1=0,     //sum1 to calculate ascii sum of string A i.e the first string. It is initialized to zero
        sum2=0,     //sum2 to calculate ascii sum of string B i.e the second string. It is initialized to zero
        i;
    int size1=A.size();     //size function to calculate the size of a string.It is defined in bits/stdc++.h header file.
    int size2=B.size();
    for(i=0;i<size1;i++)    //loop to calculate the ascii sum of first string
    {
        sum1=sum1+(int)A[i];
    }
    for(i=0;i<size2;i++)    //loop to calculate the ascii sum of second string
    {
        sum2=sum2+(int)B[i];
    }
    if(sum1>sum2)           //comparing the ascii sum of two strings
        return true;
    else
        return false;
}
int main()
{
    bool result;
    string str1,str2;
    cout<<"Enter the first string : ";
    getline(cin,str1);                  //Taking first string as input from the user
    cout<<"Enter the second string : ";
    getline(cin,str2);                  //Taking second string as input from the user
    result=ASCII_SUM(str1,str2);        //Calling the ascii_sum function
    if(result==true)                    //if result is true then printing First string has larger ascii sum as compared to second string.
        cout<<"First string has larger ascii sum as compared to second string."<<endl;
    else                                   //if result is false then printing Second string has larger ascii sum as compared to first string.
        cout<<"Second string has larger ascii sum as compared to first string."<<endl;
    return 0;
}

Output will be:

PLEASE LIKE THE ANSWER IF YOU FIND IT HELPFUL OR YOU CAN COMMENT IF YOU NEED CLARITY / EXPLANATION ON ANY POINT.


Related Solutions

Directions Follow the Written Assignment Guidelines in this and all other assignments in this course. Below...
Directions Follow the Written Assignment Guidelines in this and all other assignments in this course. Below are the directions specific to this assignment. Jane Elliott created prejudice among the students in her elementary school classroom in one week. Watch the footage of her classic 1968 demonstration: A Class Divided (Links to an external site.)Links to an external site. The study starts at about 3:20 minutes into the episode and runs until about 17:50 if you want to be selective in...
General guidelines: Use EXCEL or PHStat to do the necessary computer work. Do all the necessary...
General guidelines: Use EXCEL or PHStat to do the necessary computer work. Do all the necessary analysis and hypothesis test constructions, and explain completely. Read the textbook Chapter 11. Solve the textbook example on page 403, "Mobile Electronics," in order to compare four different in-store locations with respect to their average sales. Use One-Way ANOVA to analyze the data set, data, given for this homework. Use 5% level of significance. 1) Do the Levene test in order to compare the...
Instructions (In C++ Please) Verification This programming lab makes use of an array of characters. The...
Instructions (In C++ Please) Verification This programming lab makes use of an array of characters. The program will validate the characters in the array to see if they meets a set of requirements. Requirements: Must be at least 6 characters long (but not limited to 6, can be greater) Contains one upper case letter Contains one lower case letter Contains one digit Complete the code attached. Must use pointer while checking the characters. Download Source Lab 3 File: #include using...
Describe how you might organize files that contain your term papers, lab assignments, and class reports...
Describe how you might organize files that contain your term papers, lab assignments, and class reports on your hard disk drive. Would this be any different than the workplace?
Lab - Validate all of the row in the puzzle. // //   - Use this code...
Lab - Validate all of the row in the puzzle. // //   - Use this code as a start of the program. //   - Catch all duplicate entries in each row. //   - Catch any numbers that are not 1 - 9. //   - Display an error msg for each error found. //   - At end, display a msg stating how many errors were found. //===================================================================== import java.util.*; public class Lab_ValidateAllRows    {    public static void main (String[] args)...
Like all our lab exercises, the waves and sound lab will use a PhET simulator. Some...
Like all our lab exercises, the waves and sound lab will use a PhET simulator. Some of these simulators require you to enable Java or Flash. You can download and use the simulator on your desktop, or for some labs you can run it in your browser. Preferred browser settings and system requirements can be found at the “Running Sims” FAQ:  https://phet.colorado.edu/en/help-center/running-sims (Links to an external site.) For this lab, we’ll use the “Sound Waves” simulator: https://phet.colorado.edu/en/simulation/legacy/sound (Links to an external...
Exercise 1: Lab 2-general lab safety- A physics laboratory contains special equipment to use while you...
Exercise 1: Lab 2-general lab safety- A physics laboratory contains special equipment to use while you are performing an experiment. Locate each of the items pictured on the following pages in your lab kit, and place a check mark in the appropriate place when you find it(Hanging Mass Set ,Caliper,Spring Scale ,Ramp). After you have completed this, sketch a picture and name any additional items that are located in your lab kit, classroom, or home that are likely to be...
(Use R Programming to Code) Use the Monte Carol simulation to estimate the probability that all...
(Use R Programming to Code) Use the Monte Carol simulation to estimate the probability that all six faces appear exactly once in six tosses of fair dice.
(Leadership)Your objective in both assignments is to use TWO course concepts or models to reflect on...
(Leadership)Your objective in both assignments is to use TWO course concepts or models to reflect on an actual situation, event, or person. It must be something or someone that you have personally experienced, or a current situation in which you are involved. It can be self-reflection or reflection on something or someone observed. Roughly 8-10 Pages double spaced generally does it. (More is not better). Structure the Paper in the following way: The first part of the paper (maybe 20%)...
Instructions Use your solution from Programming Assignment 5 (or use your instructor's solution) to create a...
Instructions Use your solution from Programming Assignment 5 (or use your instructor's solution) to create a modular Python application. Include a main function (and a top-level scope check),and at least one other non-trivial function (e.g., a function that deals the new card out of the deck). The main function should contain the loop which checks if the user wants to continue. Include a shebang, and ID header, descriptive comments, and use constants where appropriate. Sample Output (user input in red):...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT