Shifting array elements

I have to write a program that reads 10 values from an input file and stores them in an array. It should ask the user to enter a positive number n
and then shifts n times the contents of array cells one cell to the right, with the last cell's contents moved to the left end. This is what I managed to code so far, but I don't know how to continue.
#include <iostream>
#include <fstream>
using namespace std;
bool readData(int toRight[], int toLeft[]);
void displayReport (int toRight[], int toLeft[])
void shift( int toRight[], int toLeft[])
int main ()
{
int toLeft[8], toRight[1], n;

readData( toRight[], toLeft[]);
displayReport (toRight[], toLeft[]);
shift( num[]);

return 0;
}
bool readData(int toRight[], int toLeft[])
{
ifstream infile;
infile.open("data.txt");

if (!infile)
{
cout << "Error opening input file!" << endl;
return false;
}

for(int i=0; i<9; i++)
infile >> toRight[i];

for(int i=0; i<1; i++)
infile >> toLeft[i];

return true;

}
void displayReport (int toRight[], int toLeft[])
{

for(int i=0; i<9; i++)
{
cout << toRight[i];
}

for(int i=0; i<1; i++)
{
cout << toLeft[i];
}
}
void shift( int toRight[], int toLeft[])
{
cout << "Enter the number of times to swap: ";
cin >> n;
int temp;
int temp1;
for(int i=0; i<n; i++)
{
temp = toRight[size-1];
toRight[size-1] = toRight[i];
toRight[i] = temp;
}
}
Last edited on
std::rotate() if you're allowed to use algorithms and iterators from the standard library:
http://en.cppreference.com/w/cpp/algorithm/rotate
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <algorithm>
#include <iterator>

int main ()
{

  int v[] {2, 4, 2, 0, 5, 10, 7, 3, 7, 1};
  std::cout << "how many right shifts ( < 10)? \n";
  size_t num{};
  std::cin >> num;
  std::rotate (std::begin(v), std::begin(v) + std::distance(std::begin(v), std::end(v)) - num, std::end(v));
  for (auto i = 0; i < 10; ++i)std::cout << v[i] << " "; std::cout << "\n";
}

edit: line 12 can also be written in the following manner if you find this clearer:
 
std::rotate (std::begin(v), std::begin(v) + std::distance(std::begin(v)+num, std::end(v)), std::end(v));
Last edited on
@rockandrollhead does your code even compile? It seems to have quite a few compile errors that you need to fix before you try to continue, please post your compiler error messages, all of them exactly as they appear in your development environment, if you need help fixing the errors.

Now to a couple of "logic" errors:

I have to write a program that reads 10 values from an input file and stores them in an array.

So where is your array that can hold 10 values?

What is the purpose of having two arrays with different sizes when both the sizes are too small to hold the required 10 values?

Look at this snippet:
1
2
3
4
5
6
7
8
9
10
11
12
13
void displayReport(int toRight[1], int toLeft[8]) // Note I added the array sizes for clarity.
{

    for(int i = 0; i < 9; i++)  // You are trying to access this array out of bounds, it only has a size of 1. Since this array only has one element no real need for the loop.
    {
        cout << toRight[i];
    }

    for(int i = 0; i < 1; i++) // You're only printing one element here, so no real need for the loop. But what about the rest of the elements in this loop?
    {
        cout << toLeft[i];
    }
}


Really there is no need for two arrays, you should be holding the elements in one array which is large enough to hold 10 elements. Also when dealing with arrays you should be passing the size of the array into the function along with the array. Something more like the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void displayReport(int array[], size_t size_of_array)
{
    for(size_t i = 0; i < size_of_array; i++)
    {
        cout << array[i] << ", ";
    }
    cout << endl;
}

// In main:
...
    const int MAX_ARRAY_SIZE = 10;
    int main_array[MAX_ARRAY_SIZE];
...

    displayReport(main_array, MAX_ARRAY_SIZE);
...


Thank you guys it worked
Topic archived. No new replies allowed.