Question

Working with pointers in C++. I need a brief step by step explanation of the code...

Working with pointers in C++. I need a brief step by step explanation of the code to help me understand why the output is: 5 4 3 2 1

What will be displayed? Explain each step of the code and the related use of pointers.

#include <iostream>

using namespace std;

int* myfun(int*);

int main()

{

   int x[5] = { 1, 2, 3, 4, 5 };

   int i, *p;

   p = myfun(x);

   for (i = 0; i < 5; i++)

        cout << *(p + i) << " ";

   return 0;

}

int* myfun(int* p)

{

   int i;

   for (i = 0; i < 2; i++) {

        int temp = *(p + i);

        *(p + i) = *(p + 4 - i);

        *(p + 4 - i) = temp;

   }

   return p;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>

using namespace std;

int* myfun(int*);

int main()

{

   int x[5] = { 1, 2, 3, 4, 5 };

   int i, *p;

   // p starts with the result of myFun
   // p is an integer pointer, using which we can
   // access elements in the array to which it points
   p = myfun(x);

   // below *(p + i) means p[i], where p is an array
   // and i is the index.
   for (i = 0; i < 5; i++)
        cout << *(p + i) << " ";

   return 0;

}

int* myfun(int* p) {

   int i;

   // Below code basically swaps the values from both ends
   // For a 5 element array, it loops 5/2 = 2 time
   // i = 0 and i = 1
   for (i = 0; i < 2; i++) {

        // (p+i) means ith Location from left corner of array
        // (p+4-i) means, ith location from right corner or array

        // Keep value of left location in temporary variable
        int temp = *(p + i);

        // copy the value on left location from right location
        *(p + i) = *(p + 4 - i);

        // update the value of right location from temporary variable
        *(p + 4 - i) = temp;

   }

   // return the start of the array
   return p;
}


Hi. please find the answer above.. i have given comments so that it is very easy for you to understand the flow. In case of any doubts, please ask in comments. If the answer helps you, please upvote. Thanks!

Add a comment
Know the answer?
Add Answer to:
Working with pointers in C++. I need a brief step by step explanation of the code...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 15.6: Fix the code to print the count from 1 to x (to loop x times)...

    15.6: Fix the code to print the count from 1 to x (to loop x times) #include <iostream> // include the header file using namespace std; void count(int x){    for(int i=1; i<=x; i++){ cout<<x; } } int main(){    int x,i;    cin >> x; count(x);    return 0; } 15.7 Fix the nested for loops to print the count from 1 to x twice, e.g. "12....x12.....x" #include <iostream> // include the header file using namespace std; int main(){...

  • what is the output for the following code? explain the steps. /*#include <iostream> using namespace std;...

    what is the output for the following code? explain the steps. /*#include <iostream> using namespace std; int f(int &i) { i = 10; return(5 * i); } int main() { int n = 5; f(n); cout << n << "\n"; return 0; } #include <iostream> using namespace std; int sub1(int n) { n--; return n; } int main() { int m = 10; for(int j = 0; j < 10; j++) m -= sub1(j); cout << m << "\n"; return...

  • 2. What is the value of x alter the following code is executed # include<iostream> using...

    2. What is the value of x alter the following code is executed # include<iostream> using namespace std; int main int t-0, c- 0,x-3; while (c < 4) t=t+x; cout << x return 0; a) 3 b) 21 c) 24 d) 48 3. What is the output of the following code? # include<iostream> using namespace std; int main0 int a- 3; for (int i 5; i >0; i) a a+i cout << a << “ “ return 0; d) 8...

  • 6. Please provide step by step and concepts explanation. Thank you ! What is the output...

    6. Please provide step by step and concepts explanation. Thank you ! What is the output of the following program? #include <iostream> using namespace std; int fun (int p 2, int q = 3) return p+ qi int main (void) cout < fun (O +fun (1) + fun (1, 2) <<endl; return 0; 6 O 8 10 O 12

  • Question 1: Fix the 2D dynamic array initialization in following code #include <iostream> using namespace std;...

    Question 1: Fix the 2D dynamic array initialization in following code #include <iostream> using namespace std; int main(){    int rows = 5; int cols = 5; int x;    int** arr = new int[rows][cols]    cin >> x; arr[x][x] = x; cout << "arr[x][x] = " << arr[x][x];    return 0; } Question 2: Fix the code to initialize the 2D array elements to x #include <iostream> using namespace std; int main(){    int rows; int cols; int x;...

  • Linkedlist implementation in C++ The below code I have written is almost done, I only need...

    Linkedlist implementation in C++ The below code I have written is almost done, I only need help to write the definition for delete_last() functio​n. ​ Language C++ // LinkedList.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <string> #include <iostream> using namespace std; struct Node { int dataItem;//Our link list stores integers Node *next;//this is a Node pointer that will be areference to next node in the list }; class LinkedList { private: Node *first;...

  • I have to type and explain in class each code in every detail filled with //...

    I have to type and explain in class each code in every detail filled with // commentary. Explains how does work in every codes. 1) What does the below print #include <iostream> using namespace std ; int main() {    int var1 = 20 ;    int var2 = 30 ;    int* ptr1 ;    int* ptr2 ;    int* temp ;    ptr1 = &var1 ;    ptr2 = &var2 ;    cout << *ptr1 << endl ;...

  • I need to add a for or a while loop to the following code. please help...

    I need to add a for or a while loop to the following code. please help needs to be in c++. #include <iostream> #include <string.h> using namespace std; int main() {    char input[100]; cout << "Please enter a character string: "; cin >> input; char *head = &input[0], *tail = &input[strlen(input) - 1]; char temp = *head; *head = *tail; *tail = temp; tail--; head++; cout << "CString = " << input << "\n"; }

  • C++ 1. A?B?C?D? which one is correct 2. 3A, 3B #include<iostream> using namespace std; void swap0(int*...

    C++ 1. A?B?C?D? which one is correct 2. 3A, 3B #include<iostream> using namespace std; void swap0(int* ptri, int* ptr2) { int *temp; temp = ptr1; ptr1 = ptr2; ptr2 = temp; void swap1(int ptri, int ptr2){ int temp; temp = ptri; ptr1 = ptr2; ptr2 = temp; portion void swap2(int *&ptri, int *&ptr2){ int* temp; temp = ptr1; ptr1 = ptr2; ptr2 = temp; void swap3(int &ptri, int &ptr2) { int temp; temp = ptr1; ptr1 = ptr2; ptr2 =...

  • I need a program in c++ the same as below code but I want it to...

    I need a program in c++ the same as below code but I want it to prompt the user to enter the number of elements after that I want it to ask the user to enter the array elements #include<algorithm> #include<stdio.h> #include<string.h> #include<iostream> using namespace std; int a[50]={2,5,4,3}; bool x[100]; int N=4;//number of elements    int k=10;//target sum int sum;//current target sum int cmp(const void *a,const void *b) { return *(int *)b-*(int *)a; } void backtrace(int n) { if(sum>k) return...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT