// Zeinalipour-Yazti Demetrios // csyiazti // cs10xx - Lab Sec#25-#29 // lab10 /* This program deals with arrays. It declares an array of 10 integers and fill * this array with values at compile-time. It then shifts the values in the array * to the right (circularly) by a user specified amount. Finally it prints * the array before and after the shifting. */ #include using namespace std; const int SIZE = 10; // the size of the array /* Get value from user along with error checking etc * @return : the INT value given by the user */ int getUserTimes() { int val; cout << "Please enter how many elements to shift the array: "; cin >> val; while (cin.fail()) { cout << "#ERROR - Only integer values are allowed!\n"; cin.clear(); cin.ignore(80, '\n'); cout << "Please enter how many elements to shift the array: "; cin >> val; } return val; } /* Print the passed array on the screen * @return : void */ void printArray(int myarray[]) { cout << "Array: ["; for (int i=0; i< SIZE; i++) { cout << myarray[i]; if (i!=SIZE-1) { cout << ", "; } } cout << "]\n"; } /* Shift the elements in the passed array by 1 position (circularly) * @return : void */ void shiftOne(int myarray[]) { int temp = myarray[SIZE-1]; // assign last element to a temp var int temp2; // another temp variable for (int i=0; i> value; while (cin.fail() || !(value=='y' || value=='n')) { cout << "#Error - only (y/n) are accepted!\n"; cin.clear(); cin.ignore(80, '\n'); cout << "Do you want to run the program again (y/n) ? : "; cin >> value; } cout << endl; return value; } void main() { /* define variables */ int myarray[10] = {3, 17, 2, 9, 12, 8, 43, 100, 55, 6}; int x; // how many elements to shift the array // declare functions int getUserTimes(); void printArray(int array[]); void shiftArray(int array[], int times); cout << "Array Shifter by Demetris Zeinalipour - cs10 LAB10\n\n"; // run the program as many times as the user wants do { // print the array (unmodified) printArray(myarray); // how many elements to shift the array x = getUserTimes(); // shift the elements shiftArray(myarray,x); // print the array (modified) printArray(myarray); } while (doAgain()=='y'); }