// Lab 11 Pre-Lab Assignment // Lab 11 Pre-Lab // Write a function that multipli
ID: 3814746 • Letter: #
Question
// Lab 11 Pre-Lab Assignment // Lab 11 Pre-Lab // Write a function that multiplies each element in the array "myArray" // by the variable "multiplyMe". #include <iostream> using namespace std; // TODO - Write your function prototype here int main() { const int SIZE = 10; int myArray [SIZE] = {5, 10, 15, 20, 25, 30, 35, 40, 45, 50}; int multiplyMe = 5; // TODO - Add your function call here // print the array for(int i=0; i < SIZE; i++) { cout << myArray[i] << " "; } cout << endl; return 0; } // TODO - Write your function definition here
Explanation / Answer
#include <iostream>
using namespace std;
// TODO - Write your function prototype here
void multiply(int myArray[], int multiplyMe , int size);
int main()
{
const int SIZE = 10;
int myArray [SIZE] = {5, 10, 15, 20, 25, 30, 35, 40, 45, 50};
int multiplyMe = 5;
// TODO - Add your function call here
multiply(myArray, multiplyMe, SIZE);
// print the array
for(int i=0; i < SIZE; i++)
{
cout << myArray[i] << " ";
}
cout << endl;
return 0;
}
// TODO - Write your function definition here
void multiply(int myArray[], int multiplyMe ,int size) {
for(int i=0; i<size; i++){
myArray[i] = myArray[i] * multiplyMe;
}
}
Output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
25 50 75 100 125 150 175 200 225 250
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.