I'm writing a program that will shift the the array in this code by two, For example, a[5] = {0,1,2,3,4} and output {3,4,0,1,2} I wrote code already, but I'm missing something.. Appreciate any help!
#include <iostream>
using namespace std;
void circularShift(int a[], int size)
{
for (int i = size-2; i >=0; i--)
{
int temp = a[i+1];
a[i+1] = a[i];
}
}
int main()
{
int a[5] = {0,1,2,3,4};
int size = 5;
circularShift(a, 5);
for (int i=0; i < size; i++)
{
cout << a[i]<< " ";
}
return 0;
}
Using std::rotate:
void circularShift(int a[], int size)
{
assert(size >= 2);
std::rotate(a, a + size - 2, a + size);
}
Demo
void circularShift(int a[], int size)
{
int tmp1 = a[size - 1];
int tmp2 = a[size - 2];
for (int i = size-3; i >=0; i--)
{
a[i+2] = a[i];
}
a[1] = tmp1;
a[0] = tmp2;
}
try this -
void circularShift(int a[], int size)
{
int tmp = a[0];
for (int i = 0; i < size-1 ; i++)
{
a[i]=a[i+1];
}
a[size-1] = tmp;
}
Your function is not right:
It accesses a[5] in the 1st iteration
If you need to shift right twice then you need to calculate the new indexes for the elements:
newIndex = (oldIndex+2)%5
It will make sure you have a circular shift in the array.
Although it is high complexity method but it still works for your problem.
#include <iostream>
using namespace std;
void circularShift(int a[], int size)
{
int no;
int rotate_no = 2;
for(int j=0; j<rotate_no; j++)
{
no = a[size-1];
for (int i=0 ; i<size ; i++)
{
int temp = a[i];
a[i] = no;
no = temp;
}
}
}
int main()
{
int a[5] = {0,1,2,3,4};
int size = 5;
circularShift(a, 5);
for (int i=0; i < size; i++)
{
cout << a[i]<< " ";
}
return 0;
}
Try this one
void circularShift(int a[], int size, int rotations)
{
int temp = a[0];
for (int i = 0; i <size; i++)
{
int temp1 = a[((i+1)*rotations)%size];
a[((i+1)*rotations)%size] = temp;
temp = temp1;
}
}
Related
You have been given an array/list(ARR) of size N. You need to swap every pair of alternate elements in the array/list.
You don't need to print or return anything, just change in the input array itself.
#include <iostream>;
using namespace std;
void printArr(int arr[], int n)
{
for (int i = 0; i < n; i++)
cout << arr[i]<<i;
}
void UpdateArr(int arr[], int n)
{
int i = 0, j = n - 1;
while (i < j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i += 2;
j -= 2;
}
cout<<' printArr(arr[], n)';
}
int main()
{
int t;
cin>> t;
int n;
cin>> n;
int input[100];
for(int i=0; i<n; i++) {
cin >>input[i];
}
int arr[100] ;
n = sizeof(arr) / sizeof(arr[0]);
UpdateArr(arr, n);
return 0;
}
I'm not sure what are you exactly expecting the output to be (pls edit it and show the expected output) but I think this is what you need to do
#include <iostream>
#include <iomanip>
using namespace std;
void UpdateArray(int Arr[], size_t n) {
for (size_t i = 0; i < n / 2; i++) {
int Holder = Arr[i];
Arr[i] = Arr[~i + n];
Arr[~i + n] = Holder; } }
int main() {
int Arr[7] = { 1,2,3,4,5,6,7 };
UpdateArray(Arr, 7);
for (int i = 0; i < 7; i++) {
std::cout << Arr[i] << "\n"; }
return 0; }
size_t is like an int but it can't go into negative, but it can take bigger positive numbers, you can replace it with int, it shouldn't make a difference.
so we loop through half the array, replacing first items with last, the [~i + n] flips the value to the other side, so like index 4 in a array size of 20 will become 15
so I had the following sorting example (Count Sort), It's kind of weird actually, but it works, so it's fine :
#include<iostream>
using namespace std;
int getMx(int* arr,int n)
{
int max = arr[0];
for (int i = 1; i < n; i++)
{
if (arr[i] > max)
{
max = arr[i];
}
}
return max;
}
void CountSort(int* arr, int n) {
int* output = new int[n];
int max = getMx(arr, n);
int* count = new int[max + 1];
for(int i = 0; i < max + 1; i++) {
count[i] = 0;
}
for(int i = 0; i < n; i++) {
count[arr[i]]++;
}
for(int i = 1; i <= max; i++) {
count[i] += count[i - 1];
}
for(int i = n - 1; i >= 0; i--) {
output[count[arr[i]] - 1] = arr[i];
count[arr[i]]--;
}
for(int i = 0; i < n; i++) {
arr[i] = output[i];
}
delete[] output;
delete[] count;
}
int main () {
int arr[] = { 100, 5, 2, 0, 125 };
int n = sizeof(arr) / sizeof(arr[0]);
CountSort(arr, n);
for (int i = 0; i < n; i++) {
cout << arr[i] << endl;
}
return 0;
}
So the main idea is just to get rid of the part when I sum the numbers stored in each index on Count[i]. And just get the output after the second cycle.
Example
int arr = [3, 2, 5, 4, 1, 0]
int count=[1,1,1,1,1,1]
and from there I should get
int output= [0, 1, 2, 3, 4, 5]
So I couldn't figue this out ((
You are probably thinking of something like this:
void CountSort(int* arr, int n) {
int max = getMx(arr, n);
int* count = new int[max + 1]{};
for(int i = 0; i < n; i++) {
count[arr[i]]++;
}
int k = 0;
for(int i = 0; i <= max; i++) {
while (count[i]--) {
arr[k++] = i;
}
}
delete[] count;
}
Demo
int arr[] = {7,4,10,8,3,1};
int size = sizeof(arr) / sizeof(arr[0]);
for(int i = 0; i<size-1; i++){
int temp = arr[i];
for(int j = i+1; j < size; j++){
if(arr[j] < temp){
temp = arr[j];
}
}
swap(temp, arr[i]);
}
I am trying to apply the selection sort algorithm on the given array, but the output I am getting is only [1,1,1,1,1,1], I am finding the minimum element through the inner loop, Ican't figure out what is going wrong?
Slightly modified your code;
You need to pass reference(address) to both elements to take place of swapping contents
int arr[] = { 7, 1, 10, 8, 3, 11, 0, 12, 5, 8 };
int size = sizeof(arr) / sizeof(arr[0]);
for(int i = 0; i < size; i++)
{
auto temp = std::min_element( arr + i, arr + size );
std::swap( arr[i], *temp );
}
You have to add algorithm header to use std::min_element
int arr[] = {7,4,10,8,3,1};
int size = sizeof(arr) / sizeof(arr[0]);
for(int i = 0; i<size-1; i++){
int temp = arr[i];
int pos = i;
for(int j = i+1; j < size; j++){
if(arr[j] < temp){
temp = arr[j];
pos = j;
}
}
if(pos != i)
std::swap(arr[pos], arr[i]);
}
This should work.
It is suggested not to use using namespace std;. There is a plethora of reasons why you should not; that I will not mention.
By the way I tried to keep some of your variables the same but to be honest, I didn't. It is better to create variable names that explain what the code is doing. It makes your code a lot more legible and readable.
So opt out of one letter variables. It is fine in for loops, however this is a special case.
Now, here is another alternative suggested by #user4581301 & #Swift -Friday Pie. This method is using std::size using c++17.
For example:
#include <iostream>
#include <utility> // to use the swap() function.
#include <iterator> // to use std::size() function.
int main()
{
int arr[] = { 7,4,10,8,3,1 };
// This --> int size = sizeof(arr) / sizeof(arr[0]); is archaic.
const int length = static_cast<int>(std::size(arr)); // Call this something other than "size"; you can run into issues.
// We use static_cast<int> as a implicit conversion, and the obvious std::size(arr)).
// Going through the elements
for (int StartOfIndex = 0; StartOfIndex < length - 1; ++StartOfIndex)
{
// smallest is the index of the smallest element we’ve encountered this iteration
int smallest = StartOfIndex;
// Looking for a smaller element..
for (int current = StartOfIndex + 1; current < length; ++current)
{
// if we found an element smaller than our last; take note.
if (arr[current] < arr[smallest])
smallest = current;
}
// swap StartOfIndex with smallest.
std::swap(arr[StartOfIndex], arr[smallest]);
}
//Prints array.
for (int index = 0; index < length; ++index)
std::cout << arr[index] << " ";
std::cout << "\n";
return 0;
}
Output: 1 3 4 7 8 10
The first mistake you made in writing for loop's condition, don't use swap(temp, array[i]); yet try to get the basics first.
#include <iostream>
using namespace std;
int findsmall(int arr[], int i, int size){
int s, pos, j;
s = arr[i];
pos = i;
for(j = i+1; j < size; j++){
if(arr[j] < s){
s = arr[j];
pos = j;
}
}
return pos;
}
int main() {
int arr[] = {7,4,10,8,3,1};
int size = sizeof(arr) / sizeof(arr[0]);
int smallnum;
int temp;
int count = 0;
cout << "Original array: ";
for(int i = 0; i < size; i++){
if(i < size - 1){
cout << arr[i] << ", ";}
else{
cout << arr[i];
}
}
cout << endl;
for(int i = 0; i < size; i++){
smallnum = findsmall(arr,i, size);
temp = arr[i];
arr[i] = arr[smallnum];
arr[smallnum] = temp;
count++;
}
cout << "Sorted array: ";
for(int i = 0; i < size; i++){
if(i < size - 1){
cout << arr[i] << ", ";}
else{
cout << arr[i];
}
}
cout << endl;
return 0;
}
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void selectionSort(int arr[], int n)
{
int i, j, min_idx;
// One by one move boundary of unsorted subarray
for (i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
min_idx = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
// Swap the found minimum element with the first element
swap(&arr[min_idx], &arr[i]);
}
}
selectionSort(arr,size);
This should work.
i am doing a shell sort program but there's some problem passing the array into my function, i have searched for some post about passing array into the function but i still don't understand.
#include <iostream>
#include <cmath>
using namespace std;
int shellsort(int arr[]){
int gap = floor(sizeof(arr)/2);
for(int gap = floor(sizeof(arr)/2); gap < 0; gap=gap/2){
for(int i = gap; i < sizeof(arr); i++){
if(arr[i] < arr[i-gap]){
int temp = arr[i];
arr[i] = arr[i-gap];
arr[i-gap] = temp;
if(gap == 1){
if(arr[i-1] < arr[i-2]){
int temp = arr[i-1];
arr[i-1] = arr[i-2];
arr[i-2] = temp;
}
}
}
}
}
return arr;
}
int main(){
int numcount;
cin>>numcount;
int numbers[numcount];
for(int i; i<numcount; i++){
cin>>numbers[i];
}
int numbers = shellsort(numbers);
cout<<numbers;
}
Beware of what you have promised to return and what you have returned.
int shellsort(int arr[]){
//....
return arr;//<--- ****not an int****
}
Now, either change to use a vector:
std::vector<int> shellsort(std::vector<int> arr){
int gap = floor(arr.size()/2);
for(int gap = floor(arr.size()/2); gap < 0; gap=gap/2){
//as above
}
return arr;
}
or send in the size
int * shellsort(int * arr, size_t size){
int gap = floor(size/2);
for(int gap = floor(size/2); gap < 0; gap=gap/2){
//as above
}
return arr;
}
The problem is that inside your function, the actual size of int arr[] is not known, and arr decays to a pointer, hence sizeof(arr) == sizeof(int*).
I suggest you rewrite your code to use std::vector:
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
std::vector<int> shellsort(std::vector<int> arr){
int gap = floor(arr.size()/2);
for(int gap = floor(arr.size()/2); gap < 0; gap=gap/2){
for(int i = gap; i < arr.size(); i++){
// ...as before...
}
}
return arr;
}
int main(){
int numcount;
cin>>numcount;
std::vector<int> numbers(numcount);
for(int i; i<numcount; i++){
cin>>numbers[i];
}
numbers = shellsort(numbers);
for (std::size_t i = 0u; i < numbers.size(); ++i)
cout<<numbers[i];
}
I have error, which is highlighted "cout << array[i] << endl;" in this section. The line is under array[i]. The error is "argument list for class template "std::array" is missing ". i need a function to display the contents of an array, using an insertion sort. If this code is incorrect, does anyone know the code to output the contents of the array, using linear search.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
int numbers[SIZE] = { 6,3,1,9,4,12,17,2 };
for (int i = 0; i < 8; i++)
{
cout << array[i] << endl;
}
system("pause");
}
const int SIZE = 8;
void insertionSort(int numbers[], int arraySize)
{
int i, j, insert;
for (i = 1; i < arraySize; i++)
{
insert = numbers[i];
j = i;
while ((j > 0) && (numbers[j - 1] > insert))
{
numbers[j] = numbers[j - 1];
j = j - 1;
}
numbers[j] = insert;
}
}
You didn't call your function insertionSort(int numbers[], int arraySize) in main(). Therefore nothing will happen to the original array.
Note that you need a return 0; statement inside int main(). And that you need to use numbers[i] instead of array[i]. And you need to set your insertionSort() to return "something" or to pass your numbers[] as a reference. Also not to forget about the function prototype before main().
This should work:
const int SIZE = 8;
void insertionSort(int [], int);
int main()
{
int numbers[SIZE] = { 6,3,1,9,4,12,17,2 };
insertionSort(numbers, SIZE);
for (int i = 0; i < 8; i++)
cout << numbers[i] << endl;
system("pause");
return 0;
}
void insertionSort(int MyArray[], int size)
{
int i, j, insert;
for (i = 1; i < size; i++){
insert = MyArray[i];
j = i;
while ((j > 0) && (MyArray[j - 1] > insert)){
MyArray[j] = MyArray[j - 1];
j = j - 1;}
MyArray[j] = insert;}
}