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;
@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:
...
constint MAX_ARRAY_SIZE = 10;
int main_array[MAX_ARRAY_SIZE];
...
displayReport(main_array, MAX_ARRAY_SIZE);
...