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;
}
#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; }
c++ rotating array to right side is working, but rotating to left side is not working...
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 () { 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) #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 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 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 <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"; }
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 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...
9. At your job, you are creating a library. A co-worker brought this test code to you. They expect that the output would be "12 12 12 12 12". However, they are getting "Empty List" (a) Describe why the error is occurring. (b) Explain how to fix the code. #include <iostream> using namespace std; CON void increaseArray (int* array, int size, int value) { int newSize = size + 5; if (size ==0) { size = 5; O int* newArray...
Fix this C++ code so that the function prototypes come before main. Main needs to be declared first. Then call to the functions from inside of main. #include<cstdlib> using namespace std; //prompt user for input void getGrades(int gradeArray[],int size) { for(int i=0; i<size; i++) { cout<<"Enter the grade "<<i+1<<": "; cin>>gradeArray[i]; } } // finding average of all numbers in array float computeAverage(int numbers[],int size) { float sum=0; for(int i=0; i<size; i++) { sum = sum + numbers[i]; //compute sum...