Eliminating duplicates Q - c++

I had a task from one course on Udemy to create a program which eliminates duplicates from array and i failed so I have looked to my instructors code and I don't understand few things.
Why don't you have to write the capacity of array (function input) ? it is because of that you already has written the elements of array so it was recognized how long it is ?
the if statement for not true Boolean: the counter is initialized to zero so for the first time when unique_data[counter++]=numbers[i=0] is counter also zero a then it is incremented ?
sorry for asking these questions but I am little bit worried that i have not been able to solve this myself
void unique_numbers( int numbers[], unsigned int collection_size){
int unique_data [20];
unsigned int unique_counter{};//Initialized to zero. Counts the elements we have in
//the unique_data array
for(unsigned int i{}; i < collection_size; ++i){
bool already_in{false};
//See if the element is not already in our unique_data
for(unsigned int j{};j < unique_counter; ++j){
if(unique_data[j] == numbers[i]){
already_in = true;
break;
}
}
if(!already_in){
unique_data[unique_counter++] = numbers[i];
}
}
std::cout << "The collection contains " << unique_counter << " unique numbers, they are : ";
for(unsigned int i{};i < unique_counter ; ++i){
std::cout << unique_data[i] << " ";
}

I have modified your program with std::vector and std::find . The logic is the same. Now it is more readable. Hope you will understand it.
#include <iostream>
#include <vector>
#include <algorithm>
void unique_numbers(int numbers[], unsigned int collection_size)
{
std::vector<int> unique_data;
for (unsigned int i{}; i < collection_size; ++i)
{
//Find numbers[i] in unique_data. If it is not found insert it in unique_data
auto it = std::find(unique_data.begin(), unique_data.end(), numbers[i]);
if (it == unique_data.end())
{
unique_data.push_back(numbers[i]);
}
}
//print unique_data vector
std::cout << "The collection contains " << unique_data.size() << " unique numbers, they are : ";
for (const auto& v : unique_data){
std::cout << v << " ";
}
std::cout << std::endl;
}
int main()
{
int a[] = { 1, 2, 3, 4, 5, 4, 3, 2, 1 };
unsigned int n = sizeof(a)/sizeof(int);
unique_numbers(a, n);
}

Related

The numbers in my bubble sort array output incorrectly after being sorted [duplicate]

This question already has an answer here:
Array not playing back properly inside function - bubble sort
(1 answer)
Closed 2 years ago.
I am learning bubble sort. In my output array, the numbers in my presort array output correctly, but the second time it outputs with the sorted numbers it omits the 0 in the 10. I get an output of: Before sort: Array = {10, 2, 3, 1} After sort: Array = {1, 2, 3, 1} Any ideas?
#include <iostream>
using namespace std;
void showArray(int sortMe[], int size);
int main()
{
int sortMe[4] = {10, 2, 3, 1}; // Original Array
int numElements = 4;
int temp; // For swapping
cout << "Before sort: ";
showArray(sortMe, numElements);
for (int i=numElements-1; i>0; i--) { // For loop1
for(int j=0; j<i; j++) {
// Checks if the value on left is bigger than the right
if(sortMe[j] > sortMe[j+1]) {
// If bigger swap values
temp = sortMe[j];
sortMe[j] = sortMe[j+1];
sortMe[j+1] = temp;
}
}
}
cout << "After sort: ";
showArray(sortMe, numElements);
}
void showArray(int sortMe[], int size) {
// Outputs array in format array = {num1, num2, etc.}
int i = 0;
cout << "Array = {";
for (int i = 0; i < size - 1; i++) {
cout << sortMe[i] << ", ";
}
cout << sortMe[i] << "}" << endl;
}
Your problem is not the sorting, but printing. You define i two times.
If you rewrite your for loop inside the print like this your problem is solved for (; i < size - 1; i++).
The way you've written it the last element is always the element at index 0 because of int i = 0; outside of the loop.

Deleting element from an array in c++

I have read others posts, but they don't answer my problem fully.
I'm learning to delete elements from an array from the book and try to apply that code.
As far as I can grasp I'm passing array wrong or it is sending integer by address(didn't know the meaning behind that).
#include <iostream>
#include <cstdlib>
using namespace std;
void delete_element(double x[], int& n, int k);
int main()
{
// example of a function
int mass[10]={1,2,3,45,12,87,100,101,999,999};
int len = 10;
for(int i=0;i<10;i++)
{
cout<<mass[i]<<" ";
};
delete_element(mass[10],10&,4);
for(int i=0;i<10;i++)
cout<<mass[i]<<" ";
return 0;
}
void delete_element(double x[], int& n, int k)
{
if(k<1 || k>n)
{
cout<<"Wrong index of k "<<k<<endl;
exit(1); // end program
}
for(int i = k-1;i<n-1;i++)
x[i]=x[i+1];
n--;
}
There are a couple of errors in your code. I highlight some of the major issues in question 1-3:
You call exit, which does not provide proper cleanup of any objects since it's inherited from C. This isn't such a big deal in this program but it will become one.
One proper way too handle such an error is by throwing an exception cout<<"Wrong index of k "<< k <<endl;
exit(1);
Should be something like this:
throw std::runtime_error("invalid index");
and should be handled somewhere else.
You declare function parameters as taking a int& but you call the function like this: delete_element(mass[10],10&,4); 10& is passing the address of 10. Simply pass the value 10 instead.
You are "deleting" a function from a raw C array. This inherently doesn't make sense. You can't actually delete part of such an array. It is of constant compile time size created on the stack. The function itself doesn't do any deleting, try to name the functions something more task-oriented.
You are using C-Arrays. Don't do this unless you have a very good reason. Use std::array or std::vector. These containers know their own size, and vector manages it's own memory and can be re sized with minimal effort. With containers you also have access to the full scope of the STL because of their iterator support.
I suggest you rewrite the code, implementing some type of STL container
Line 15: syntax error
you can't pass a number&
If you want to pass by reference, you need to create a variable first, like:
your delete_element function signature conflicts with your declared arrays. Either use a double array or int array and make sure the signatures match.
delete_element(mass, len , 4);
when you write the name of an array without the brackets, then it's the same as &mass[0]
ie. pointer to the first element.
complete changes should be:
#include <iostream>
#include <cstdlib>
using namespace std;
void delete_element(int x[], int& n, int k);
int main(){
// example of a function
int mass[10] = { 1, 2, 3, 45, 12, 87, 100, 101, 999, 999 };
int len = 10;
for (int i = 0; i<10; i++){ cout << mass[i] << " "; };
cout << endl;
delete_element(mass, len , 4);
for (int i = 0; i<10; i++)cout << mass[i] << " ";
cout << endl;
cin.ignore();
return 0;
}
void delete_element(int x[], int& n, int k){
if (k<1 || k>n){
cout << "Wrong index of k " << k << endl;
exit(1); // end program
}
for (int i = k - 1; i<n - 1; i++)
x[i] = x[i + 1];
n--;
}
There are a couple of mistakes in your program.
Apart from some syntax issues you are trying to pass an int array to a function which wants a double array.
You cannot pass a lvalue reference of a int literal. What you want is to pass a reference to the length of the int array. see also http://en.cppreference.com/w/cpp/language/reference.
Here is an updated version of your program.
#include <iostream>
#include <cstdlib>
using namespace std;
void delete_element(int x[], int& n, int k);
int main() {
// example of a function
int mass[10] = { 1,2,3,45,12,87,100,101,999,999 };
int len = 10;
for (int i = 0;i < len;i++)
cout << mass[i] << " "; ;
cout << endl;
delete_element(mass, len, 4);
for (int i = 0;i < len;i++) // len is 9 now
cout << mass[i] << " ";
cout << endl;
return 0;
}
void delete_element(int x[], int& n, int k) {
if (k<1 || k>n) {
cout << "Wrong index of k " << k << endl;
exit(1); // end program
}
for (int i = k - 1;i<n - 1;i++)
x[i] = x[i + 1];
n--;
}
Although it does not answer your question directly, I would like to show you how you can use C++ to solve your problem in a simpler way.
#include <vector>
#include <iostream>
void delete_element(std::vector<int>& v, const unsigned i)
{
if (i < v.size())
v.erase(v.begin() + i);
else
std::cout << "Index " << i << " out of bounds" << std::endl;
}
int main()
{
std::vector<int> v = {1, 2, 3, 4, 5, 6, 7};
delete_element(v, 4);
for (int i : v)
std::cout << i << std::endl;
return 0;
}
You cannot delete elements from an array, since an array's size is fixed. Given this, the implementation of delete_element can be done with just a single call to the appropriate algorithm function std::copy.
In addition, I highly suggest you make the element to delete a 0-based value, and not 1-based.
Another note: don't call exit() in the middle of a function call.
#include <algorithm>
//...
void delete_element(int x[], int& n, int k)
{
if (k < 0 || k > n-1 )
{
cout << "Wrong index of k " << k << endl;
return;
}
std::copy(x + k + 1, x + n, x + k);
n--;
}
Live Example removing first element
The std::copy call moves the elements from the source range (defined by the element after k and the last item (denoted by n)) to the destination range (the element at k). Since the destination is not within the source range, the std::copy call works correctly.

How to count duplicated numbers in a sorted array

I am trying to finish a problem where I read a file into the program and output a file with the average, min, max, and the count of how many times that number occurred in the program. However, I cannot figure out how to create an array for duplicated number of the "counts".
If the file that I was trying to read in had the values 19 5 26 5 5 19 16 8 1,
I need the outputted file to read 5---3 times; 8---1 time; 16---1 time; 19--2 times; 26--1 times.
I first sorted my array to read 5 5 5 8 16 19 19 26.
Below is my code with explanations of what I was trying to do:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
double averageArray(int a[], int length); // user defined function to get average
int maxval(int a[], int length); //user defined function to get max val
int minval(int a[], int length); //user defined function to get min val
void bsort(int arr[], int length);// udf to sort array from min to max
int countArray(int a[], int length); //attempt to create a function to get the number of occurrences of a number that is duplicated
int main()
{
char infilename[16];
int nums[50];//newly created array to read in the numbers from the file
int length(0);//array has a length defined by "length"
ifstream fin;
ofstream fout;
cout << "Please enter an input file name: ";
cin >> infilename;
cout << endl;
fin.open(infilename);
if (fin.fail())
{
cerr << "The file " << infilename << " can't be open!"<<endl;
return 1;
}
cout<<"The output to the file statistics.txt should be as follows: "<<endl;
fout.open("statistics.txt");
fout<<"N"<<"\t"<<"Count"<<endl;
cout<<"N"<<"\t"<<"Count"<<endl;
while (fin >> nums[length])
length++;
bsort(nums, length);
for (int i=0; i<length; i++) {
if (nums[i]==nums[i-1]) {
continue;
}
cout<<nums[i]<<"\t"<<countArray(nums,length)<<endl;
fout<<nums[i]<<"\t"<<endl;
}
cout << "\nAverage: " << averageArray(nums,length) << endl;
cout << "Max: "<< maxval(nums,length)<<endl;
cout << "Min: "<< minval(nums,length)<<endl;
fin.close();
return 0;
}
double averageArray (int a[], int length)
{
double result(0);
for (int i = 0; i < length ; i++)
result += a[i];
return result/length;
}
int maxval(int a[], int length)
{
int max(0);
for (int i=1; i<length; i++)
{
if (a[i]>max)
max=a[i];
}
return max;
}
int minval(int a[], int length)
{
int min(100);
for (int i=1; i<length; i++)
{
if (a[i]<min)
min=a[i];
}
return min;
}
void bsort(int a[], int length)
{
for (int i=length-1; i>0; i--)
for (int j=0; j<i; j++)
if (a[j]>a[j+1])
{
int temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
}
}
int countArray(int a[], int length)
{
int counter(0);
for (int i=0; i<length; i++){
if (a[i]==a[i+1]) //loop through array and if the number after is the same as the previous number, then count one
counter++;
}
return counter;
}
Though it compiles, the count only shows "3"s as shown in the picture below:
.
Before I give you the solution, please take a moment to remember, you are programming in C++, not C. As such, you ought to use vectors, istream iterators and std::sort. You also ought to use std::map, which easily accomplishes this purpose:
template <typename It>
std::map<int, int> count_occurrences(It it, It end)
{
std::map<int, int> output;
while (it != end) output[*it++]++;
return output;
}
How to combine this with your existing code is left as an exercise for the reader. I suggest you ought to read about iterators.
Your function int countArray(int a[], int length) has no input for the actual number. It always counts how often there are the same numbers behind each other in your array. That happens two times for fives and once for 19 => 3 times.
Solution:
int countArray(int a[], int length, int num)
{
int counter(0);
for (int i=0; i<length; i++){
if (a[i]==num) //loop through array and if the number is the one you are looking for
counter++;
}
return counter;
}
and call you function: countArray(nums, length, nums[i]);
void countArray(int a[], int length)
{
int counter(1);
bool flag(false);
//you take (i+1) index, it can go out of range
for (int i = 0; i < length - 1; i++){
if (a[i]==a[i+1]){ //loop through array and if the number after is the same as the previous number, then count one
flag = true;
counter++;
}
else {
if (flag){
cout<<a[i] << counter << endl;
}
flag = false;
counter = 1;
}
}
}
I didn't code on C for a long time, but I hope it'll help.
This procedure will print you the answer, and you have to call it just once.
I suggest to use std::map which is the best solution to solve your problem. I will try to explain easily the differents steps to do this:
I consider your variables initialized, for instance:
int length = 9;
int nums[length] = {19, 5, 26, 5, 5, 19, 16, 8, 1};
Create the std::map<int,int>, where the key (first int) will be your number and the value (second int) the number of occurence of this number store in the key.
std::map<int,int> listNumber;
Fill your map
// For all numbers read in your file
for(int i=0; i<length; ++i)
{
// Get number value
int n = nums[i];
// Find number in map
std::map<int, int>::iterator it = listNumber.find(n);
// Doesn't exists in map, add it with number of occurence set to 1...
if(it == listNumber.end())
{
listNumber.insert(std::pair<int,int>(n,1));
}
// ... otherwise add one to the number of occurence of this number
else
{
it->second = it->second+1;
}
}
Read the map
// Read all numbers and display the number of occurence
std::cout << "N" << "\t" << "Count" << std::endl;
for(std::map<int, int>::iterator it = listNumber.begin(); it!=listNumber.end(); ++it)
{
std::cout << it->first << "\t" << it->second << std::endl;
}

swap array values in c++

I want to shift left array values if my v=4 is in a[n],remove 4 from a[n] and at the end index add 0,how i can do this?
#include <iostream>
using namespace std;
const int n=5;
int main()
{
int a[n]={1,5,4,6,8}, v=4;
int b[n];
cout << "Enter a Value" << endl;
cout<<v<<endl;
for(int i=0; i<n; i++){
cout<<a[i];
}
cout<<endl;
for(int j=0; j<n; j++){
b[j]=a[j];
if(a[j]==v)
b[j]=a[++j];
cout<<b[j];
}
return 0;
}
#include <vector> // needed for vector
#include <algorithm> // needed for find
#include <iostream> // needed for cout, cin
using namespace std;
// Vectors are just like dynamic arrays, you can resize vectors on the fly
vector<int> a { 1,5,4,6,8 }; // Prepare required vector
int v;
cout << "enter value"; // Read from user
cin >> v;
auto itr = find( a.begin(), a.end(), v); // Search entire vector for 'v'
if( itr != a.end() ) // If value entered by user is found in vector
{
a.erase(itr); // Delete the element and shift everything after element
// Toward beginning of vector. This reduces vector size by 1
a.push_back(0); // Add 0 in the end. This increases vector size by 1
}
for( int i : a ) // Iterate through all element of a (i holds element)
cout << i; // Print i
cout << '\n'; // Line end
a few helpful links:
vector , find , iterator , erase , push_back
You could use std::rotate. I suggest that you use std::vector instead of C arrays and take full advantage of the STL algorithms. Nevertheless, below I'm illustrating two versions one with C arrays and one with std::vector:
Version with C array:
#include <iostream>
#include <algorithm>
int main()
{
int const n = 5;
int a[n] = {1,5,4,6,8};
std::cout << "Enter a Value" << std::endl;
int v;
std::cin >> v;
for(auto i : a) std::cout << i<< " ";
std::cout << std::endl;
auto it = std::find(std::begin(a), std::end(a), v);
if(it != std::end(a)) {
std::rotate(it + 1, it, std::end(a));
a[n - 1] = 0;
}
for(auto i : a) std::cout << i<< " ";
std::cout << std::endl;
return 0;
}
Version with vector:
#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
std::vector<int> a{1,5,4,6,8};
std::cout << "Enter a Value" << std::endl;
int v;
std::cin >> v;
for(auto i : a) std::cout << i<< " ";
std::cout << std::endl;
auto it = std::find(std::begin(a), std::end(a), v);
if(it != std::end(a)) {
std::rotate(it + 1, it, std::end(a));
a.back() = 0;
}
for(auto i : a) std::cout << i<< " ";
std::cout << std::endl;
return 0;
}
Here's an example using std::array
#include <array>
#include <algorithm>
// defines our array.
std::array<int, 5> a = {{ 1, 2, 3, 4, 5 }};
// find the position of the element with the value 4.
auto where = std::find(a.begin(), a.end(), 4);
// if it wasn't found, give up
if (where == a.end())
return 0;
// move every element past "where" down one.
std::move(where + 1, a.end(), where);
// fill the very last element of the array with zero
a[ a.size() - 1] = 0;
// loop over our array, printing it to stdout
for (int i : a)
std::cout << i << " ";
std::cout << "\n";
Why would anyone use these awkward algorithms? Well, there are a few reasons. Firstly, they are container-independant. This will work with arrays and vectors and deques, no problem. Secondly, they can be easily used to work with a whole range of elements at once, not just single items, and can copy between containers and so on. They're also type-independant... you acn have an array of strings, or an vector of ints, or other more complex things, and the algorithms will still work just fine.
They're quite powerful, once you've got over their initial user-unfriendliness.
You can always use either std::array or std::vector or whatever without using the standard library algorithms, of course.
std::array<int, 5> a = {{ 1, 2, 3, 4, 5 }};
size_t where = 0;
int to_remove = 4;
// scan through until we find our value.
while (a[where] != to_remove && where < a.size())
where++;
// if we didn't find it, give up
if (where == a.size())
return 0;
// shuffle down the values
for (size_t i = where; i < a.size() - 1; i++)
a[i] = a[i + 1];
// set the last element to zero
a[ a.size() - 1] = 0;
As a final example, you can use memmove (as suggested by BLUEPIXY) to do the shuffling-down operation in one function call:
#include <cstring>
if (where < a.size() - 1)
memmove(&a[where], &a[where + 1], a.size() - where);

How To Shift Array Elements to right and replace the shifted index with string in Visual C++

VISUAL C++ Question
Hi,
I have array of 3 elements and I want to shift its elements to the right and replace the shifted index cell with "SHIFTED" string and this should loop until all the cells has "SHIFTED" string.
For example:
int a[x]={0,1,2};
Initial index and elements Order:
[0]=0
[1]=1
[2]=2
should become in the:
1st loop:
[0]=SHIFTED
[1]=0
[2]=1
2nd Loop:
[0]=SHIFTED
[1]=SHIFTED
[2]=0
3rd Loop:
[0]=SHIFTED
[1]=SHIFTED
[2]=SHIFTED
I know I can do that with memmove() but I don't want to use any function in it.
Would you please help me; here is my work:
#include <iostream>
#include <string>
#include <stdio.h>
#include <cstdlib>
using namespace std;
int const ARRAY_SIZE=3;
int main()
{
int Array[ARRAY_SIZE];
int iniPostion,newPostion;
string x="SHIFTED";
for(iniPostion=0; iniPostion<ARRAY_SIZE; iniPostion++)
{
Array[iniPostion] = iniPostion;
cout << "Cell [" << iniPostion << "] Initial Element is: (" << Array[iniPostion] << ")" << endl;
}
cout << endl;
for(newPostion=0; newPostion<ARRAY_SIZE; newPostion++)
{
Array[newPostion]=newPostion;
cout << "Cell [" << newPostion << "] New Element is: (";
if(Array[newPostion-1]<0)
{
cout << x << ")\n";
}
else
{
cout << Array[newPostion-1] << ")" << endl;
}
}
return 0;
}
As Peter mentioned in his answer, you cannot assign a string to a int. I've assumed SHIFTED to be -1. So every time you shift you bring in a -1 in the gap created.
You need two loops. The outer loops iterates N (3) times and inner loop starts at the end of the array and copies (n-1)th element to nth position:
for(int count = 0;count < N;count++){
for(newPostion=ARRAY_SIZE-1; newPostion > count;newPostion--)
Array[newPostion]=Array[newPostion - 1]; // copy
Array[newPostion]= -1; // fill the gap.
// print.
for(iniPostion=0; iniPostion<ARRAY_SIZE; iniPostion++) {
cout << "Cell [" << iniPostion << "] Initial Element is: (" << Array[iniPostion] << ")" << endl;
}
cout<<endl;
}
Sample run:
# g++ a.cpp && ./a.out
Cell [0] Initial Element is: (0)
Cell [1] Initial Element is: (1)
Cell [2] Initial Element is: (2)
Cell [0] Initial Element is: (-1)
Cell [1] Initial Element is: (0)
Cell [2] Initial Element is: (1)
Cell [0] Initial Element is: (-1)
Cell [1] Initial Element is: (-1)
Cell [2] Initial Element is: (0)
Cell [0] Initial Element is: (-1)
Cell [1] Initial Element is: (-1)
Cell [2] Initial Element is: (-1)
It is a bit unclear, how you can expect to have string ("SHIFTED") in an array of integers.
However, for this operation you can use the rotate algorithm:
#include <iostream>
#include <algorithm>
#include <string>
int const ARRAY_SIZE=3;
void print(std::string* array) {
for (int i = 0; i != ARRAY_SIZE; ++i) {
std::cout << array[i] << ' ';
}
std::cout << '\n';
}
int main()
{
std::string Array[ARRAY_SIZE] = {"0", "1", "2"};
print(Array);
//the last item ends up at the beginning of the array
std::rotate(Array, Array + ARRAY_SIZE - 1, Array + ARRAY_SIZE);
//now overwrite the item that used to be last
Array[0] = "SHIFTED";
print(Array);
return 0;
}
It would probably be simpler and more efficient with a suitable container, such as std::deque or std::list where you could pop_back the last value and push_front the new value.
I have array of 3 elements and I want to shift its elements to the right and replace the shifted index cell with "SHIFTED" string and this should loop until all the cells has "SHIFTED" string.
This doesn't make sense at all. You have an array of numbers (integers), which of course can't contain a string. What you can do is e.g. inserting 0 or -1 to denote the shifted element.
The shifting itself can be easily implemented through a std::rotate operation.
But since all elements contain the same thing in the end, why don't you just assign them directly without all the shifting?
Here is my simple solution in plain old C
#include <stdio.h>
#include <stdlib.h>
void main(int argc, char **argv) {
int MAX_LEN = 11;
int numOfShifts = 1;
if ( argc > 1 ) {
numOfShifts = atoi(argv[1]);
}
printf("Number of shifts = %d\n",numOfShifts);
int arr[] = { 0,1,2,3,4,5,6,7,8,9,10 };
int i;
int n; // number of shifts index
for ( n = 0; n < numOfShifts; n++ ) {
for ( i = MAX_LEN - 1; i >= 0; i-- ) {
if ( i == 0 ) {
arr[i] = -1;
} else {
arr[i] = arr[i-1];
}
}
}
// output
for( i = 0; i < MAX_LEN; i++ ) {
printf("Arr[%d] = %d\n", i, arr[i]);
}
}
This reeks of homework...
Is there a reason you can't just keep track of how many shifts you've done and just take that into account when you print?
#include <iostream>
int const ARRAY_SIZE=3;
class Shifter
{
public:
Shifter(const int* array_, size_t size_)
: array(array_), size(size_), shifts(0) {}
void Shift(size_t steps = 1) { shifts += steps; if(shifts > size) shifts = size; }
void Print(std::ostream& os) const;
private:
const int* array;
size_t size;
size_t shifts;
};
void Shifter::Print(std::ostream& os) const
{
for(size_t i = 0; i < size; ++i)
{
os << "Cell [" << i << "] = ";
if(i < shifts)
os << "SHIFTED";
else
os << array[i - shifts];
os << std::endl;
}
}
std::ostream& operator <<(std::ostream& os, const Shifter& sh)
{
sh.Print(os);
return os;
}
int main(void)
{
// Initialize the array.
int a[ARRAY_SIZE];
for(size_t iniPostion=0; iniPostion < ARRAY_SIZE; iniPostion++)
a[iniPostion] = iniPostion;
Shifter sh(a, ARRAY_SIZE);
std::cout << "Initial contents:" << std::endl;
std::cout << sh << std::endl;
// Do the shifts.
for(size_t newPostion = 0; newPostion < ARRAY_SIZE; newPostion++)
{
std::cout << "Shift by 1..." << std::endl;
sh.Shift();
std::cout << sh << std::endl;
}
return 0;
}
You can simple move memory.
// shift down
int a[ 10 ];
memmove( a, a +1, sizeof( a ) -sizeof( a[ 0 ] ) );