Math Operation
Lesson 8 of 18 in Coddy's Functions in C++: Building Your Own Functions course.
Challenge
EasyWrite a function to perform a mathematical operation.
The function should take a list of numbers and an operation symbol (+, -, or *). Based on the operation provided, the function will return the result of applying that operation to all the numbers in the list.
Example:
- Input: numbers =
<strong>[2, 3, 4]</strong>, operation = '+' - Output: 9
- Input: numbers =
<strong> [10, 2, 5]</strong>, operation = '-' - Output: 3
- Input: numbers =
<strong>[1, 2, 3, 4]</strong>, operation = '*' - Output: 24
If the operation is not -, +, or *, print the message: Invalid operation.
Example:
- Input: numbers =
<strong>[1, 2, 3, 4]</strong>, operation = '#' - Output: “Invalid operation: #”
Try it yourself
#include <iostream>
#include <vector>
using namespace std;
int math_operation() {
}All lessons in Functions in C++: Building Your Own Functions
1Introduction
Introduction2Beginner-level function
Repeat a StringReverse a StringMax ValueFill stringCenter TextFormat TextMath Operation