C++ Assignment 1. Write the function described below: The function is named calc
ID: 3803746 • Letter: C
Question
C++ Assignment
1. Write the function described below:
The function is named calculateTip
It takes in two parameters: one double reference parameter for the price, one double value parameter for the tipping percentage.
It updates the price by multiplying it by 1 + the tipping percentage
It returns nothing.
2. Write the following function:
string frontBack(string str);
//Given a string, return a new string where the first and last chars have been exchanged.
//frontBack("code") "eodc"
//frontBack("a") "a"
//frontBack("ab") "ba"
3. Write the following function:
void squareArray(int array[], int size);
//Given an array of ints, add multiplies each element in the array by itself
//return nothing. Remember arrays are automatically pass by reference
//squareArray([1,2,3],3) -> [1, 4, 9]
//squareArray([3, 5, 6, 8, 9], 5) -> [9, 25, 36, 64, 81]
//squareArray([5], 1) -> [25]
Explanation / Answer
//given the fnctions along with main function to test
#include<iostream>
#include<string>
using namespace std;
void calculateTip(double &price, double percentage);
string frontBack(string str);
void squareArray(int arr[], int size);
int main()
{
double price = 12;
string str = "code";
int arr[5] = { 3, 5, 6, 8, 9 };
calculateTip(price, 5);
cout << "Price = " << price<<endl;
cout<<"Calling frontBack: "<<frontBack(str)<<endl;
cout << "Calling squareArray : ";
squareArray(arr, 5);
//print array
cout << "After squaring array: ";
for (int i = 0; i < 5; i++)
{
cout << arr[i] << " ";
}
}
void calculateTip(double &price, double percentage)
{
price *= (1 + percentage/100 );
}
string frontBack(string str)
{
string newstring = str;
newstring[0] = str[str.length() - 1];
newstring[newstring.length() - 1] = str[0];
return newstring;
}
void squareArray(int arr[], int size)
{
for (int i = 0; i < size; i++)
{
arr[i] = arr[i] * arr[i];
}
}
----------------------------------------------------------------------------------------------------------------------
//output
Price = 12.6
Calling frontBack: eodc
Calling squareArray : After squaring array: 9 25 36 64 81
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.