In-lab Task 1: Reversing an array of numbers. You have to write a function to reverse the order of integers in an array. Call this function from your main function to demonstrate that this function successfully reverses (in-place) the contents of the input array. You should declare and use a stack within this function. The functions for stack implementation are already given to you. The prototype for the function is given below; void reverse_num_array(int * num_array);
#include <iostream>
#include <stack>
void reverse_num_array(int * num_array) {
std::stack<int> s;
int n = sizeof(num_array) / sizeof(num_array[0]);
for (int i = 0; i < n; i++) {
s.push(num_array[i]);
}
int i = 0;
while (!s.empty()) {
num_array[i++] = s.peek();
s.pop();
}
}
int main() {
int arr[] = {3, -4, 2, -6, 5, 5, -1};
reverse_num_array(arr);
return 0;
}
Comments
Leave a comment