my_operation
| my_operation | |
|---|---|
| Turn-in directory | 01_my_operation/ |
| Files to turn in | my_operation.c |
| Allowed external functions | None |
| Type of work | Function |
- Write a program which displays the character
con standard output, followed by a newline. - To help you get started, we will provide some code for you to copy/paste/edit. Make sure you understand every line before you hand it in!
- You need to write a function that takes two integers as parameters and returns the result of the operation specified by the third parameter.
- The third parameter is a character that can be one of the following: '+', '-', '*', '/' or '%'.
- If the operation is illegal, the function returns 0.
- If the third parameter is not one of the above, the function returns 0.
#include <stdio.h> int my_operation(int a, int b, char operation) { if (operation == '+') return a + b; return 0; } // LAST WARNING // WHEN YOU ARE ASKED FOR A FUNCTION // WE WILL USE OUR OWN MAIN FUNCTION TO TEST YOUR EXERCISE // SO YOU NEED TO REMOVE THE MAIN FUNCTION // OR COMMENT IT OUT int main() { char c = '+'; int a = 1; int b = 2; int result = my_operation(a, b, c); printf("%d\n", result); return 0; }
Example output:
$ ./a.out
3$