Question

I am trying to write the following code using pointers and traversal by pointers instead of...

I am trying to write the following code using pointers and traversal by pointers instead of indexing (in C++).

The following code is correct:

void shift(int arr[], int n) {

int temp = arr[n - 1];

int temp1;

for (int i = 0; i < n; i++) {

temp1 = arr[i];

arr[i] = temp;

temp = temp1;

}

}

This is my ATTEMPT at writing it with pointers (and traversal by pointers!) but I know it is wrong. I believe the loop is correct but the body is wrong. Please remember int arr[] is an array. I cannot use indexing AT ALL. Pointers and traversal by pointers ONLY. Can someone help fix it?

void shift(int arr[], int n) {

int temp = arr + (n - 1);

int temp1;

for (int *ptr = arr; ptr < arr + n; ++ptr) {

temp1 = *ptr;

*ptr = temp;

temp = temp1;

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The code given by you:

void shift(int arr[], int n) {

int temp = arr + (n - 1);

int temp1;

for (int *ptr = arr; ptr < arr + n; ++ptr) {

temp1 = *ptr;

*ptr = temp;

temp = temp1;

}

}

Modified code (without using indexing):

#include <iostream>

using namespace std;

void shift(int arr[], int n)

{

int *t1 = arr + n-1;

int temp;

//the shifting operation

for (int i = 0; i < n; i++) {

temp = *(arr+i);

*(arr+i) = *t1;

*t1 = temp;

}

//this loop traverses the array

for (int i = 0; i < n; i++)

cout<<*(arr+i);

}

//driver function

int main()

{

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

   shift(a,5);

   return 0;

}

OUTPUT:

24 //driver function 25 int main() 26 27 28 int a[5J-(1,2,3,4,5); 29 shift (a,5): 3 return ; 31 32 Get URL options compilatio

Add a comment
Know the answer?
Add Answer to:
I am trying to write the following code using pointers and traversal by pointers instead of...
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
  • The following code is a Java code for insertion sort. I would like this code to...

    The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...

  • Hi, I am trying to figure out how write a code to read in a list...

    Hi, I am trying to figure out how write a code to read in a list of integers separated by commas into an array in c++, while ignoring the commas. example input 5, 2, 8, 6, 3, 6, 9, 7 example array: int arr[] = {5,2,8,6,3,6,9,7};

  • Assume that arr[] is an array of 5 values. Write code within the FOR loop below...

    Assume that arr[] is an array of 5 values. Write code within the FOR loop below so that it finds the min value in the array. You MUST use the x pointer within the FOR loop. It is the only pointer you can use. You are not allowed to made additional function calls or modify anything else within the function. Do not modify the FOR loop conditions or anything outside of the FOR loop. You must use complete C++ code...

  • I am trying to reverse this array explain why it is not working and fix it...

    I am trying to reverse this array explain why it is not working and fix it thank you! Also can you explain what ncurses.h is and its utalizations. Thanks! #include <stdio.h> array[10] = {1,2,3,4,5,6,7,8,9,10}; int i,j,temp; int main(){ for(i = 0; i < 10; i++) { for (j = 9; j >= 0; j--) { temp = array[i]; array[i] = array[j]; array[j] = temp; } } for(i = 0; i < 10; i++) { printf(" %d", array[i]); } }

  • I'm trying to code a C program so it sorts an array of integer numbers of...

    I'm trying to code a C program so it sorts an array of integer numbers of size n in ascending order. My code is written below but its not working properly, its giving me errors, I think my sort and swap functions aren't done right maybe, please help and fix. It says "undefined reference to "SelectionSort" as an error. But the question asks to not change the PrintArray and Main function. #include <stdio.h> void PrintArray(int size, int array[]) { for...

  • How can I convert the following C code to MIPS Assembly? +++++++++++++++++++++++++++++++++ MIPS main program ++++++++++++++++++++++++++++++++...

    How can I convert the following C code to MIPS Assembly? +++++++++++++++++++++++++++++++++ MIPS main program ++++++++++++++++++++++++++++++++ .data # Defines variable section of an assembly routine. array: .word x, x, x, x, x, x, x, x, x, x # Define a variable named array as a word (integer) array # with 10 unsorted integer numbers of your own. # After your program has run, the integers in this array # should be sorted. .text # Defines the start of the code...

  • Hello, I need help with my code. The code needs to display random number from 1...

    Hello, I need help with my code. The code needs to display random number from 1 to 50 every time the program runs but the program displays the same random numbers every time. Thanks #include #include using namespace std; void dynAlloc(int size, int *&arr); void displayArray(int *arr, int n); void insertionSort(int *arr, int n, int *temp); void linear_search(int *arr, int n, int key); void binary_search(int *arr, int n, int key); int main() {   const int n = 50; int *arr,...

  • 2. Pointer arithmetic: a) Write a printString function that prints a string (char-by-char) using pointer arithmetics...

    2. Pointer arithmetic: a) Write a printString function that prints a string (char-by-char) using pointer arithmetics and dereferencing. b) Write a function “void computeMinMax(double arr[], int length, double * min, double * max)” that finds the minimum and the maximum values in an array and stores the result in min and max. You are only allowed to use a single for loop in the function (no while loops). Find both the min and the max using the same for loop....

  • Below I have done a bubble sort on an array. I need to figure out how...

    Below I have done a bubble sort on an array. I need to figure out how to change the --- bubbleSort() method... with a new method called oddEvenSort() method.. I know I have to do two passes one for odd, one for even, can I have someone help me do this please I keep getting a lot of errors when I attempt to do it. Java code to change is below. I have to sort the array using one pass...

  • I am trying to write a package in go. I am given two files: I need...

    I am trying to write a package in go. I am given two files: I need to modify the function to return the min of an array of ints. do not change the first line of code. Please write in go or I will mark this answer as incorrect. func Min(arr []int) int { } I am also given this tester code: package min import "testing" func TestMin(t *testing.T) {    tests := []struct {        in []int   ...

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