Ask your own question, for FREE!
Computer Science 25 Online
OpenStudy (anonymous):

Write a program that uses a two-dimensional array of integers (3 lines of 5 columns). Begin by displaying some explanations, then ask the user to enter an integer. Store the value entered in the first element of the array by using a std::cin. Implement the code necessary to fill the next four columns of the first row of the array with the value entered multiplied by 2, 3, 4 and 5.

OpenStudy (anonymous):

in which programming language u need the answer??

OpenStudy (anonymous):

if you want the answer in C language or JAVA u can message me or comment your reply

OpenStudy (anonymous):

The program being requested needs to be written in C++. Notice the use of std::cin for input; cin is an object within the std namespace - both of which don't exist in C. Here's my solution with commentary and minimal I/O; you may want to add a few std::cout's to completely fulfill the requirements of the assignment: #include <iostream> int main() { const unsigned int ROWS = 3; const unsigned int COLUMNS = 5; int** array; int entry; // Initialize the array of arrays array = new int*[ROWS]; for (unsigned int i = 0; i < ROWS; i++) { // Initialize the arrays within array[i] = new int[COLUMNS]; for (unsigned int j = 0; j < COLUMNS; j++) { // Set their initial values to 0 array[i][j] = 0; } } // Get a value from the user std::cin >> entry; // Assign the user's input to the first element of the array array[0][0] = entry; // Loop through the first row and assign the multiplied values for (unsigned int i = 1; i < COLUMNS; i++) array[0][i] = array[0][0]*(i + 1); // Output for (unsigned int i = 0; i < COLUMNS; i++) std::cout << array[0][i] << ' '; delete(array); return 0; }

OpenStudy (anonymous):

what do you do with the other 2 rows?

OpenStudy (anonymous):

I guess you just don't do anything. "Implement the code necessary to fill the next four columns of the first row of the array with the value entered multiplied by 2, 3, 4 and 5." *ends*

OpenStudy (anonymous):

thats only the first part of the assignment. theres more: " Implement the code necessary to compute the square of the value entered and store it in the first column of the second row of the array. Then add the code to fill the next four columns of the second row with the squared value multiplied by 2, 3, 4 and 5. Then implement the code necessary to compute the cube of the value entered and store it in the first column of the third row of the array. Add the code to fill the next four columns of that third row with the cubed value multiplied by 2, 3, 4 and 5" And thank you so much for your help :)

OpenStudy (anonymous):

OpenStudy (anonymous):

I see - and you're welcome. Here's my new solution (without the formatting): #include <iostream> #include <math.h> int main() { const unsigned int ROWS = 3; const unsigned int COLUMNS = 5; int** array; // Initialize the array of arrays array = new int*[ROWS]; for (unsigned int i = 0; i < ROWS; i++) { // Initialize the arrays within array[i] = new int[COLUMNS]; } // Get a value from the user std::cout << "Enter a number: "; std::cin >> array[0][0]; // Loop through the first row and assign the multiplied values for (unsigned int i = 0; i < ROWS; i++) { for (unsigned int j = 0; j < COLUMNS; j++) { array[i][j] = pow(array[0][0], i + 1)*(j + 1); // Output std::cout << array[i][j] << ' '; } std::cout << std::endl; } delete(array); return 0; }

OpenStudy (anonymous):

so for the second row, how do you square the value and then multiply it by 2,3,4 and 5 ? i am so clueless on this assignment...

OpenStudy (anonymous):

the squaring/cubing in my code above happens with the pow(x, y) function, where x is the starting value and y is the power to raise it to. I'll break down the loop: // Loop through the first row and assign the multiplied values for (unsigned int i = 0; i < ROWS; i++) { for (unsigned int j = 0; j < COLUMNS; j++) { array[i][j] = pow(array[0][0], i + 1)*(j + 1); // Output std::cout << array[i][j] << ' '; } std::cout << std::endl; } So for each one of the rows.. ` for (unsigned int i = 0; i < ROWS; i++) {` We want to modify each of it's column entries.. ` for (unsigned int j = 0; j < COLUMNS; j++) {` By assigning the column: \[A_{ij} = A_{00}^{j + 1}*(i + 1)\]So each cell is assigned the (0, 0) cell's value (the user's entry) to the power of it's row number plus 1, times the number of it's column plus 1. ` array[i][j] = pow(array[0][0], i + 1)*(j + 1);` An entry of 10 gives the following output: Enter a number: 10 10 20 30 40 50 100 200 300 400 500 1000 2000 3000 4000 5000

OpenStudy (anonymous):

Just a tiny correction: the `j` and `i` should be flipped in the formula I wrote above: \[A_{ij} = A_{00}^{i + 1}*(j + 1)\]

OpenStudy (anonymous):

okay i got it to work now, still gotta figure out the formatting, but this is still very helpful. Thank you again :)

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!