For my mini assignment, in a 30-floor building, I have to gather the floors people press in a lift, then find the difference between each floor.
So, I plan to set an array (We have only been taught arrays as our only container) of 30 floors. The people in the lift will click on the buttons of the lift, so assuming (5, 10, 14, 19, 29).
I then plan to pass this array into a function which will calculate the difference between each floor.
Here is my code so far, I know its wrong since its not compiling and I may be wrong somewhere else too.
Here is the error message:
main.cpp: In function 'int* calculateDiff(int*, int)': main.cpp:26:7: warning: address of local variable 'floorsDiffResult' returned [-Wreturn-local-addr]
CODE
#include <iostream>
#include <numeric>
#include <algorithm>
using std::cout;
using std::endl;
int* calculateDiff(int floors[], int floorsSize);
int main()
{
int floorsPressed[30] = {5, 10, 14, 19, 29};
int floorsCounter = 5;
int* ptr = calculateDiff (floorsPressed, floorsCounter);
int floorsDiffResult[30];
for (int i = 0; i < floorsCounter; i++)
{
floorsDiffResult[i] = *(ptr + i); //Storing the difference into floorsDiffResult array
cout << "Difference: " << *(ptr + i) << endl;
}
}
int* calculateDiff(int floors[], int floorsSize)
{
int floorsDiffResult[30]; //Create another array to store the difference for other calculations later on such as finding the biggest difference, average of the difference etc.
std::adjacent_difference(floors, floors + floorsSize, floorsDiffResult);
std::move(floors + 1, floors + floorsSize, floorsDiffResult); //First element does not give the difference
return floorsDiffResult;
}
std::move? weird teaching approach :-/