Question

c++ rotating array to right side is working, but rotating to left side is not working...

c++

rotating array to right side is working, but rotating to left side is not working with some numbers

how do i fix this with using below code

#include<iostream>

using namespace std;

void rotate(int a[], int n, int k)

{

int i, b[n];

int np=-1;

if(k>=0)

{

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

{

b[(i+n+k)%n]=a[i];

}

}

else

{

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

{

b[(i+n+k)%n]=a[i];

}

}

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

{

cout<<b[i]<<" ";

}

}

int main()

{

int a[8]={0,1,2,3,4,5,6,7};

rotate(a, 8 ,-64);

return 0;

}

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

using namespace std;

void rotate(int a[], int n, int k)

{

int i, b[n];

int np=-1;
k = k % n;
if(k>=0)

{

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

{

b[(i+n+k)%n]=a[i];

}

}

else

{

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

{

b[(i+n+k)%n]=a[i];

}

}

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

{

cout<<b[i]<<" ";

}

}

int main()

{

int a[8]={0,1,2,3,4,5,6,7};

rotate(a, 8 ,-64);

return 0;

}

Add a comment
Know the answer?
Add Answer to:
c++ rotating array to right side is working, but rotating to left side is not working...
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
  • 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;...

  • One dimensional array What this code print #include <iostream> using namespace std; int main () {...

    One dimensional array What this code print #include <iostream> using namespace std; int main () { const int SIZE = 7; int numbers [SIZE] = {1, 2, 4, 8): // Initialize first 4 elements cout << “Here are the contents of the array:\n"; for (int index = 0; index < SIZE: index++} cout << numbers[index] << “ “; cout << endl; return 0; }

  • 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(){...

  • using C++ only. The findFirstZero function is supposed to find the first element in an array...

    using C++ only. The findFirstZero function is supposed to find the first element in an array whose value is zero, and sets the parameter  p to point to that element, so the caller can know the location of that element holding the value zero. Explain why this function won't do that, and show how to fix it. Your fix must be to the function only; you must not change the  main routine below in any way. As a result of your fixing...

  • PLEASE HELP ME WITH THIS HOMEWORK. Create a template function that, given an array of elements of any template type, deletes an element on a given position. Submit in the standard format in Dropbox Hw...

    PLEASE HELP ME WITH THIS HOMEWORK. Create a template function that, given an array of elements of any template type, deletes an element on a given position. Submit in the standard format in Dropbox Hw 4 before the deadline. Example for insertion (needs a fix!!! - 1st e-mail gets extra-credit) #include <iostream> using namespace std; // A template function to implement element insertion on given position in array. template <class T> void insert(T a[], int &n,T el, int place )...

  • Fix the following code to print whether the input is positive or negative #include <iostream> #include...

    Fix the following code to print whether the input is positive or negative #include <iostream> #include <string> using namespace std; void positiveOrNegative(int); // declare function int main(){    int y; int result; cin >> y; result = positiveOrNegative(y);    return 0; } void positiveOrNegative(int x){ if (x>=0) cout << "Positive"; else cout << "Negative"; }

  • Please help! Studying for my c++ test! What does the following code produce? include <iostream>   ...

    Please help! Studying for my c++ test! What does the following code produce? include <iostream>    using namespace std; void my_function(int n); void main() {     my_function(546); } void my_function(int n) {     if (n < 10)         cout << n << endl;     else     {         my_function(n/10);         cout << (n%10) << endl;     } } What is the output of the following code?                   int j=32, k=5, r;                   r = j ^ k;                   cout << r...

  • 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...

  • Q2. Consider the following C++ program that declares, allocates and fills in a1D array with random...

    Q2. Consider the following C++ program that declares, allocates and fills in a1D array with random numbers between 0 and 100. The array is sent to a function that finds the indices of all items > 50. A new array is created and the indices are stored inside it. The size of the new arrays MUST BE the same as the number of items > 50. The function returns the new array which is then printed out by main. Here...

  • Re-type the code and fix any errors. The code should convert non-positive numbers to 1. if...

    Re-type the code and fix any errors. The code should convert non-positive numbers to 1. if (userNum > 0) cout << "Positive." << endl; else cout << "Not positive, converting to 1." << endl; userNum = 1; cout << "Final: " << userNum << endl; answer: #include <iostream> using namespace std; int main() { int userNum; cin >> userNum; /* Your solution goes here */ return 0; }

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