Answer to Question #256641 in C for Lucifer

Question #256641

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);


1
Expert's answer
2021-10-25T17:56:14-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS