Lab 9-5: Passing Arrays to Functions In this lab, you complete a partially writt
ID: 3810550 • Letter: L
Question
Lab 9-5: Passing Arrays to Functions In this lab, you complete a partially written C++ p ogram that reverses the order of five numbers stored in an array. The program should first print the five numbers stored in the array. Next, the program passes the array to a funciler where the numbers are reversed. The program then prints the reversed numbers in the main program. provided for this lab includes the necessary variable declarations. The source code file Comments are included in the file to help you write the remainder of the program 1. open the source code file named Reverse.cpp using Notepad or the text editor of your choice. 2. Write the C++ statements as indicated by the comments. 3. Save this source code file in a directory of your choice, and then make that directory your working directory. 4. Compile the source code file Reverse.cpp. 5. Execute the program.Explanation / Answer
Here is the code for Reverse.cpp:
#include <iostream>
#include <string>
using namespace std;
void reverseArray(int[]);
int main()
{
int numbers[] = {100, 90, 80, 70, 60};
int x;
//Print contents of array.
for(int i = 0; i < 5; i++)
cout << numbers[i] << " ";
cout << endl;
reverseArray(numbers);
//Print contents of array.
for(int i = 0; i < 5; i++)
cout << numbers[i] << " ";
cout << endl;
return 0;
}
void reverseArray(int array[])
{
for(int i = 0; i <= 5/2 - 1; i++)
{
int temp = array[i];
array[i] = array[5-i-1];
array[5-i-1] = temp;
}
}
Here is the code for MultiplyTwo.cpp:
#include <iostream>
using namespace std;
void multiplyNumbers(int, int, int&);
int main()
{
int num1 = 10;
int num2 = 20;
int product = 0;
//Print value of product before function call.
cout << "Value of product is: " << product << endl;
//Call multipyNumbers using pass by reference for product.
multiplyNumbers(num1, num2, product);
//Print the calculate product.
cout << num1 << " * " << num2 << " is " << product << endl;
return 0;
}
void multiplyNumbers(int x, int y, int &z)
{
z = x * y;
}
Here is the code for Cornwall.cpp:
#include<iostream>
#include <cstring>
using namespace std;
double computeRate(int);
double computeRate(int, string);
int main()
{
int days;
string mealPlan;
string question;
double rate = 0.0;
cout << "How many days do you plan to stay?" << endl;
cin >> days;
cout << "Do you want a meal plan? Y or N: " << endl;
cin >> question;
//Figure out which arguments to pass to the computeRate()
//function, and then call the function.
if(mealPlan == "Y")
{
cout << "Enter 'A' for three meal per day." << endl;
cout << "Enter 'C' for to include breakfast as well." << endl;
cout << "Enter your choice: ";
cin >> question;
rate = computeRate(days, question);
}
else
rate = computeRate(days);
cout << "The rate for your stay is $" << rate << endl;
return 0;
}
double computeRate(int days, string question)
{
if(question == "A")
return 169 * days;
else if(question == "C")
return 112 * days;
return 0;
}
double computeRate(int days)
{
return 99.99 * days;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.