Functions
Callculator app
Build a calculator app. Begin with the following starter code:
#include <iostream>
#include <limits>
using namespace std;
// variables to store the two input numbers
int a, b;
// definitions for functions
void add(); // adds a and b, prints result
void sub();
void mul();
void div();
int main()
{
while (true) {
char op;
cin >> a >> op >> b;
if (cin.fail()) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
continue;
}
switch (op) {
case '+':
add();
break;
case '-':
sub();
break;
case '*':
mul();
break;
case '/':
div();
break;
default:
cout << "unknown operator, " << op << endl;
break;
}
}
return 0;
}
Note that this code won’t compile yet, because the four functions add(), sub(), mul() and div() aren’t defined (only declared.
- First, declare the four functions. It’s conventional to do this below main.
- Complete each of the functions to add, subtract, multiply and divide the two numbers
aandb(you can assume these have been read fromcinalready) and print out the answer. - In general, global variables are bad practice. Move the line
int a, b;into the bofe ofmain(). This will eliminate the problem of global variables, but will also break your program since now the four functions no longer have access toaandb. In order to fix this, modify the definitions, declarations and calls toadd,sub,mulanddivto pass in the two integers as arguments. - It is good practice to write functions that do exactly one thing. At the moment, the function
addnot only performs addition, but also prints the result. Modifyaddto return the result of the addition instead of printing it. Back inmainyou can store the result returned byadd, and print it out. Repeat for the other three functions.
Arrays
- Declare an arry (type and size of your choice), e.g.,
int numbers[100]; - Write (declare and define) a function
void set(int arr[], int i, int v);which sets the element at indexito the valuev. Note, make sure the types ofarrandvmatch the type of array you declared in your program.
Caveat! this function will modify an element of the array passed in as an argument. This won’t work with primitive types such as ‘int’ and ‘float’. I.e., try writing a functions void test(int a) which modifies the value of ‘a’. If you call this function with a variable, you’ll see that the value of the variable remains unchanged. This is because the argument a is local to the function ‘test’.
- Write a function
int get(int arr[], int i), which returns the element of the array at index ‘i’. -
Write another function
void print(int arr[], int length)to print out all the elements ofarrin order.Test your program. It’s recommended that you only move onto the next step once you’re comfortable with how this part works.
- Now let’s modify the program to handle 2-dimensional arrays, the catch is, the underlying array will still be one-dimensional. I.e., instead of creating an array of arrays, you’ll treat a long array as a 2-dimensional array. E.g., an array with
12elements can be treated as a 2d array with3rows and4columns as follows0 1 2 3 4 5 6 7 8 9 10 11
Accessing elements involves a calculation based on the width of the grid, e.g.,
- If you wanted to get the element at row
0column1, you’d retrieve element1form the 1d array, - if you wanted the element at row
1, column2, you’d need element6 = 1 * 4 + 2 = 1 * width + 2 = rowIndex * width + columnIndex, - and if you wanted the element from row 2, column
0, you’d retrieve element8 = 2 * 4 + 0 = 2 * width + 0 = rowIndex * width + columnIndex.
While these calculations aren’t exactly complicated, it would be more convenient to simply ask for element 2 0. With this in mind, modify your functions to represent a 2d array. I.e., implement
int get(int arr[], int row, int col, int width)void set(int arr[], int row, int col, int value, int width)- ‘void print(int arr[], int width, int height)`