Question

Create a square array and fill out as shown below. Use a nested loop for this...

Create a square array and fill out as shown below. Use a nested loop for this step.

1 3 5 7 9
2 4 6 8 10
3 5 7 9 11
4 6 8 10 12
5 7 9 11 13

Then create two 1D arrays to with row and column sum of the items. You have then

15 25 35 45 55

for the column sum, and

25 30 35 40 45

for the row sum.

Print row and column sum arrays on screen.

C++ Programming

0 0
Add a comment Improve this question Transcribed image text
Answer #1

main.cpp :

//header files
#include <iostream>
using namespace std;
//main method
int main()
{
//declaring square array
int squareArray[5][5];
//declaring 1d array to store row sum
int rowSum[5];
//declaring 1d array to store column sum
int columnSum[5];
int k=1;
//using for loop
for(int i=0;i<5;i++)//this loop is used for number of rows
{
for(int j=0;j<5;j++)//this loop is used for number of columns
{
squareArray[i][j]=k;//store value of k in i,j
k=k+2;//increment k by 2
}
k=i+2;//assign i+2 to the k
}
//using for loop find row sum
for(int i=0;i<5;i++)//this loop is used for number of rows
{ int rSum=0,cSum=0;
for(int j=0;j<5;j++)//this loop is used for number of columns
{
rSum=rSum+squareArray[i][j];
cSum=cSum+squareArray[j][i];
}
//store row sum in the array
rowSum[i]=rSum;
columnSum[i]=cSum;
}
cout<<"Square array :"<<endl;
//print array elements
for(int i=0;i<5;i++)//this loop is used for number of rows
{
for(int j=0;j<5;j++)//this loop is used for number of columns
{
cout<<squareArray[i][j]<<" ";//print each array element
}
cout<<endl;//jused for new line
}
cout<<"Column sum :"<<endl;
//using for loop print column sum
for(int i=0;i<5;i++)
{
cout<<columnSum[i]<<" ";//print each column sum
}
cout<<endl;//used for new line
cout<<"Row sum :"<<endl;
//using for loop print row sum
for(int i=0;i<5;i++)
{
cout<<rowSum[i]<<" ";//print each row sum
}
return 0;
}

=================================

Output :

Add a comment
Know the answer?
Add Answer to:
Create a square array and fill out as shown below. Use a nested loop for this...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a square array and fill out as shown below. Use nested loop for this step....

    Create a square array and fill out as shown below. Use nested loop for this step. 1 3 5 7 9 2 4 6 8 10 3 5 7 9 11 4 6 8 10 12 5 7 9 11 13 Then create two 1D arrays to with row and column sum of the items. You have then 15 25 35 45 55 for the column sum, and 25 30 35 40 45 for the row sum. Print row and...

  • C++ Lab 11 – Is This Box a Magic Box? Objectives: Define a two dimensional array Understand h...

    C++ Lab 11 – Is This Box a Magic Box? Objectives: Define a two dimensional array Understand how to traverse a two dimensional array Code and run a program that processes a two dimensional array Instructions: A magic square is a matrix (two dimensional arrays) in which the sum of each row, sum of each column, sum of the main diagonal, and sum of the reverse diagonal are all the same value. You are to code a program to determine...

  • in java / You will: // 1- create a square 2 dimensional array of random size...

    in java / You will: // 1- create a square 2 dimensional array of random size (less than 11) // 2- fill the array with random integers from 1 to the size limit // 3- print the array // 4- prompt to see if the user wants to do another //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // 1- The square can use the same random value for x and y. Don't allow 0 size arrays. // 2- Maybe a nested set of for loops? to...

  • Using Python programming, create a 2-by-3 list, then use a nested loop to: 1) Set each...

    Using Python programming, create a 2-by-3 list, then use a nested loop to: 1) Set each element's value to an integer indicating the order in which it was processed by the nested loop (1, 2, 3, 4, 5, 6). 2) Display the elements in tabular format. Use the column indices as headings across the top, and the row indices to the left of each row.

  • Write a python nested for loop that prints out the following pattern 100 99 98 97...

    Write a python nested for loop that prints out the following pattern 100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33...

  • create a new Java application called "Scorer" (without the quotation marks) that declares a two-dimensional array...

    create a new Java application called "Scorer" (without the quotation marks) that declares a two-dimensional array of doubles (call it scores) with three rows and three columns and that uses methods and loops as follows. Use a method containing a nested while loop to get the nine (3 x 3) doubles from the user at the command line. Use a method containing a nested for loop to compute the average of the doubles in each row. Use a method to...

  • Use C++ (2D Array) Write a program which: 1. Assigns data given below into the 2D...

    Use C++ (2D Array) Write a program which: 1. Assigns data given below into the 2D array of integers which is 10x10. 2. Prints out the contents of the 2D array after assigning the data to make sure correct data was assigned. 3. Figures out and prints out the square root of the sum of ALL the elements in the 2D array. 4. Figures out and prints out the average of ALL THE ELEMENTS in the 2D array. 5. Figures...

  • Magic Square question for Python

    You have this solution in Java, I am interested in this same solution for Python.One interesting application of two-dimensional arrays is magic squares. A magic square is a square matrix in which the sum of every row, every column, and bothdiagonals is the same. Magic squares have been studied for many years, and there are some particularly famous magic squares. In this exercise you will write code todetermine whether a square is magic.You should find that the first, second, and...

  • Write a Java program that uses nested for loops to print a multiplication table as shown...

    Write a Java program that uses nested for loops to print a multiplication table as shown below. Make sure to include the table headings and separators as shown. The values in the body of the table should be computed using the values in the heading e.g. row 1 column 3 is 1 times 3. * | 1 2 3 4 5 6 7 8 9 4 | 1| 1 2 3 456 7 8 9 2 | 2 4 6.8...

  • PA 9-1 (25 points) Use nested loops to create a 11x11 grid like the one below....

    PA 9-1 (25 points) Use nested loops to create a 11x11 grid like the one below. Tab in to start each row. Each cell is one row tall and three spaces wide. Dash (-) characters (45) were used to construct the horizontal lines. Using the rand function (hint: rand()%15) fill in all the cells in the grid with characters from ASCII number 33 (!) to 47(/). For full credit, use nested loops and center the digits in cells. Hint: for...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT