Question
NEED HELP! Functional Requirements: Same program requirements as Chapter 6/7 except rewrite your program to utilize pointer addressing instead of array subscripting. Programming Requirements: 1.
NEED HELP!
Functional Requirements:
Same program requirements as Chapter 6/7 except rewrite your program to utilize pointer addressing instead of array subscripting.
Programming Requirements:
1. Declare an int array and initialize it as before. (You can't point at a 2D array in C++, so create an 1-dim array with 25 elements.)
2. Declare an int pointer and point to the array. From then on in the program, you may only use the pointer. You may not use the array or any array subscripting in any other part of your program. You must only use pointer addressing.
3. Always display the array and manipulate it as if it were still a 2-dim array of 5x5.
4. Make sure your code is well-organized and well-documented. No global variables. Use functions and parameters appropriately.
Programming Notes:
Here's a smaller example of what I mean about setting up the pointer and not using array subscripts:
int balance[4] = {100, 2, 3, 17}; //here I am declaring my array
int *point; // declaring the pointer point = balance; //pointing to the array
for ( int i = 0; i < 3; i++ )
cout << *(point + i) << endl; //using the pointer to access array elements
What to submit:
1. .cpp file
2. a link to a jing that demo's your program.
HERE IS MY CODE FROM CH 6/7:
#include #include using namespace std;
// Function prototypes int displayMenu(); void displayArray(int[5][5]); void multiplyArray(int[5][5]); void swapRows(int[5][5]); void sumArray(int[5][5]); void quitMessage(); void defaultMessage(); int main() { const int rows = 5; // rows will have 5 const int columns = 5; // columns will have 5 // intializes the 2D array int myArray[rows][columns] = {{1,2,3,4,5}, {-20,-40,-60,-80,-100}, {3,3,3,3,3}, {5,7,9,11,13}, {-15,-25,-35,-45,-55}}; int choice; // creates a constant for each choice in order for the choices to work const int DISPLAYARRAY = 1, MULTIPLY = 2, SWAP = 3, SUM = 4, QUIT = 5; do { choice = displayMenu(); //calls the displayMenu function
switch (choice) { case DISPLAYARRAY: { displayArray(myArray); // calls the displayArray function break; } case MULTIPLY: { multiplyArray(myArray); // calls the multiplyArray function break; } case SWAP: { swapRows(myArray); // calls the swapRows function break; } case SUM: { sumArray(myArray); // calls the sumArray function break; } case QUIT: { quitMessage(); //calls the quitMessage function break; } default: { defaultMessage(); //calls the defaultMessage function break; }
} } while (choice != 5); system("pause"); return 0; }
/****************************************************** //name: displayMenu //called by: main //passed: nothing //returns: choice //calls: nobody // The displayMenu displays a menu with 5 choices, which the user will input // a valid choice (1-5). *******************************************************/ int displayMenu() { int choice; cout << "\tMenu " << "1. Display array " // choice 1 << "2. Multiply the array by 3 " // choice 2 << "3. Swap row two with row three " // choice 3 << "4. Display the sum of all elements " // choice 4 << "5. Quit " // choice 5 << " Enter your choice (1-5): "; // user will input a number of their choice cin >> choice; return choice; // returns the choice the user inputs. }
/****************************************************** //name: displayArray //called by: main //passed: myArray //returns: nothing //calls: nobody // The displayArray function displays the array in an // organized 5x5 order in the console. *******************************************************/ void displayArray(int myArray[5][5]) { cout << " Display the current array: " << endl; // displays a message about the array // displays each value in the row and column, this creates a new line for each for (int row = 0; row < 5; row++) { for (int column = 0; column < 5; column++) { //include the to use setw cout << setw(5) << right << myArray[row][column] << " "; //displays the array in an organized row and column. } cout << endl; } cout << endl; }
/****************************************************** //name: multiplyArray //called by: main //passed: myArray //returns: nothing //calls: nobody // The multiplyArray function multiplies each of the // values in the array by 3. Therefore, when displaying // the array, it will display all values, multiply by 3. *******************************************************/ void multiplyArray(int myArray[5][5]) { // Goes through each each value in the rows and columns for (int row = 0; row < 5; row++) { for (int column = 0; column < 5; column++) { myArray[row][column] = myArray[row][column] * 3; // multiples each value by 3 } } cout << " The values has been multiply by 3! " << endl; // displays a message that it was multiplied }
/****************************************************** //name: swapRows //called by: main //passed: myArray //returns: nothing //calls: nobody // The swapRows function swaps the values in row two // with row three. It will then display a messages indicating // that row two has swapped with row three. *******************************************************/ void swapRows(int myArray[5][5]) { for (int i = 0; i < 5; i++) { int tempArray = myArray[1][i]; // set up a third variable as a temporary storage myArray[1][i] = myArray[2][i]; // row 2 swaps with row three myArray[2][i] = tempArray; // row three is now store in the tempory storage } cout << " Row two has been swapped with Row three! " << endl; //indicates a message that it has been swapped. }
/****************************************************** //name: sumArray //called by: main //passed: myArray //returns: nothing //calls: nobody // The sumArray calculates the sum of all the elements // in the array and will display the total number. *******************************************************/ void sumArray(int myArray[5][5]) { int totalSum = 0; // accumulator // goes through each value in the row and columns for (int row = 0; row < 5; row++) { for (int column = 0; column < 5; column++) { totalSum += myArray[row][column]; // sums all the array elements } } cout << " The sum of all elements is " << totalSum << endl; //displays the sum }
/****************************************************** //name: quitMessage //called by: main //passed: nothing //returns: nothing //calls: nobody // The quitMessage function displays a message that the program // is now ending. ******************************************************/ void quitMessage() { cout << " The program is ending... "; //displays the quit message }
/***************************************************** //name: defaultMessage //called by: main //passed: nothing //returns: nothing //calls: nobody // The defaultMessage function displays a message whenenever // the user enters a invalid choice that is not within the choice // 1-5. ******************************************************/ void defaultMessage() { cout << "Sorry, that was an invalid choice " << "The valid choices are 1-5 " << "Run the program again! "; // displays the default message }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started