Knowee
Questions
Features
Study Tools

Given an address of the last element from an integer array and number of elements to a function traverse_back(), do print the value inside array from last element to first element without using subscriptoperator([]).

Question

Given an address of the last element from an integer array and number of elements to a function traverse_back(), do print the value inside array from last element to first element without using subscriptoperator([]).

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a simple function in C++ that does exactly that:

#include<iostream>
using namespace std;

void traverse_back(int* ptr, int n) {
    for(int i = 0; i < n; i++) {
        cout << *(ptr - i) << " ";
    }
}

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int n = sizeof(arr)/sizeof(arr[0]);
    traverse_back((arr + n - 1), n);
    return 0;
}

Here's how it works:

  1. The function traverse_back() takes a pointer to the last element of the array and the number of elements in the array as arguments.

  2. It then enters a loop that runs n times, where n is the number of elements in the array.

  3. Inside the loop, it uses the pointer arithmetic to access the elements of the array in reverse order. The expression *(ptr - i) gives the ith element from the end of the array.

  4. It prints the value of each element.

  5. In the main() function, we calculate the number of elements in the array using sizeof(arr)/sizeof(arr[0]) and pass the address of the last element of the array (arr + n - 1) to the traverse_back() function.

This problem has been solved

Similar Questions

Print Array Elements (Recursive): Write a function that recursively prints the elements of an array in order.

A multidimensional integer array is initialized in the code editor. Print the elements by row starting from the first row going down to the last row.

Complete the function print_left_to_right() , which receives number of elements and array as input. Function should print the array elements from left to right.Input Format:No Need to read any input, as it is predefined.Output Format:Print the array elements as per the problem statement.

Write a program that takes an array of integers as input and prints all its subarrays, ensuring that they are sorted based on the forward direction and the count of elements.Constraints:The length of the array is at least 1 and at most 100.Each element of the array is an integer between -1000 and 1000.Input:The input consists of two lines. The first line contains an integer, n, which is the length of the array. The second line contains n space-separated integers, representing the elements of the array.Output:The program should print all the subarrays of the given array in increasing order, one subarray per line.

Write a program that takes an array of integers as input and prints all its subarrays, ensuring that they are sorted based on the forward direction and the count of elements.Constraints:The length of the array is at least 1 and at most 100.Each element of the array is an integer between -1000 and 1000.Input:The input consists of two lines. The first line contains an integer, n, which is the length of the array. The second line contains n space-separated integers, representing the elements of the array.Output:The program should print all the subarrays of the given array in increasing order, one subarray per line.Sample Input:4 //size of array1 2 3 4Sample Output:11 21 2 31 2 3 422 32 3 433 44

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.