Question

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 out and prints out the square root of the sum of the values of the elements of EACH ROW in the 2D array. 6. Figures out and prints the average of EACH COLUMN in the 2D array. 7. Figures out and prints out the square root of the sum of the values on the diagonal from upper left hand corner to bottom right hand corner. 8. Figures out and prints out the average of the values on the diagonal from upper right hand corner to bottom left hand corner.To make assigning the 100 values easy we will test with the following data:

use loops please!

1 2 3 4 5 6 7 8 9 10

11 12 13 14 15 16 17 18 19 20

21 22 23 24 25 26 27 28 29 30

31 32 33 34 35 36 37 38 39 40

41 42 43 44 45 46 47 48 49 50

51 52 53 54 55 56 57 58 59 60

61 62 63 64 65 66 67 68 69 70

71 72 73 74 75 76 77 78 79 80

81 82 83 84 85 86 87 88 89 90

91 92 93 94 95 96 97 98 99 100

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

// C++ program to perform operations of 2D array
#include <iostream>
#include <cmath>
using namespace std;

#define SIZE 10 // size of 2D array

// function declaration
void initialize(int[][SIZE]);
void display(int[][SIZE]);
void calSquareRootTotal(int[][SIZE]);
void calAverageAll(int[][SIZE]);
void calSquareRootRows(int[][SIZE]);
void calAverageColumns(int[][SIZE]);
void calSquareRootMainDiagonal(int[][SIZE]);
void calAverageOtherDiagonal(int[][SIZE]);

int main() {

   int data[SIZE][SIZE];
   initialize(data);
   cout<<"Array elements: ";
   display(data);
   calSquareRootTotal(data);
   calAverageAll(data);
   calSquareRootRows(data);
   calAverageColumns(data);
   calSquareRootMainDiagonal(data);
   calAverageOtherDiagonal(data);

   return 0;
}

//function to assign values to the 2D array as given
void initialize(int data[][SIZE])
{
   int num=1,i,j;
   for(i=0;i<SIZE;i++)
   {
       for(j=0;j<SIZE;j++)
       {
           data[i][j] = num;
           num++;
       }
   }
}

// function to display the data elements
void display(int data[][SIZE])
{
   int i,j;
   for(i=0;i<SIZE;i++)
   {
       cout<<endl;
       for(j=0;j<SIZE;j++)
       {
           cout<<data[i][j]<<" ";
       }
   }
   cout<<endl;
}

// function to calculate and display square root of sum of all values in data
void calSquareRootTotal(int data[][SIZE])
{
   int total = 0;
   int i,j;
   // loop to calculate the total of array elements
   for(i=0;i<SIZE;i++)
   {
       for(j=0;j<SIZE;j++)
       {
           total += data[i][j];
       }
   }

   double result = sqrt(double(total)); // calculate square root of total
   cout<<"Square root of the sum of ALL the elements : "<<result<<endl;
}

// function to calculate and display the average of all values in data
void calAverageAll(int data[][SIZE])
{
   int total = 0;
   int i,j;
   // loop to calculate the total of array elements
   for(i=0;i<SIZE;i++)
   {
       for(j=0;j<SIZE;j++)
           total += data[i][j];
   }

   double avg = ((double)total)/(SIZE*SIZE); // calculate average
   cout<<"Average of ALL THE ELEMENTS : "<<avg<<endl;
}

// function to calculate and display the square root of sum of all values in each row
void calSquareRootRows(int data[][SIZE])
{
   int i,j;
   int total;
   double result;
   // loop to calculate sum of each row
   for(i=0;i<SIZE;i++)
   {
       total = 0;
       for(j=0;j<SIZE;j++)
           total += data[i][j];

       result = sqrt((double)total); // calculate the square root of sum of each row
       cout<<"Square root of sum of values in row "<<(i+1)<<" : "<<result<<endl;
   }
}

// function to calculate and display the average of sum of all values in each column
void calAverageColumns(int data[][SIZE])
{
   int total, i,j;
   double avg;
   // loop to calculate the sum of values in each column
   for(i=0;i<SIZE;i++)
   {
       total = 0;
       for(j=0;j<SIZE;j++)
           total += data[j][i];
       avg = ((double)total)/SIZE; // calculate average of each column values
       cout<<"Average of values in column "<<(i+1)<<" : "<<avg<<endl;
   }
}

// function to calculate and display the square root of the sum of the values on the diagonal from upper left hand corner to bottom right hand corner
void calSquareRootMainDiagonal(int data[][SIZE])
{
   int total=0, i;
   // loop to calculate the total of values in the main diagonal
   for(i=0;i<SIZE;i++)
   {
       total += data[i][i];
   }

   double result = sqrt((double)total); // calculate square root of sum of values in main diagonal
   cout<<"Square root of the sum of the values on the diagonal from upper left hand corner to bottom right hand corner : "<<result<<endl;
}

// function to calculate and display the average of the values on the diagonal from upper right hand corner to bottom left hand corner
void calAverageOtherDiagonal(int data[][SIZE])
{
   int i, total=0;
   // loop to calculate the total of values in other diagonal
   for(i=0;i<SIZE;i++)
       total += data[i][SIZE-1-i];
   double avg = ((double)total)/SIZE; // calculate the average of values in the other diagonal
   cout<<"Average of the values on the diagonal from upper right hand corner to bottom left hand corner : "<<avg<<endl;
}
//end of program

Output:

Array elements: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 4

Add a comment
Know the answer?
Add Answer to:
Use C++ (2D Array) Write a program which: 1. Assigns data given below into the 2D...
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
  • C++ program Write a program which: 1. Enters data into the 2D array of integers which...

    C++ program Write a program which: 1. Enters data into the 2D array of integers which is 5x5 The data should be entered with an assignment statement using nested for loops that is do not scan in the data. The data should be: 1 2 4 8 16 1 3 9 27 81 1 4 16 64 256 1 5 25 125 625 1 6 36 216 1296 2. Print out the following using a nested for loop Row 0:...

  • Write c++ program In this assignment, you need to complete following tasks on 2D array of...

    Write c++ program In this assignment, you need to complete following tasks on 2D array of randomly sales figures. Assume that you are the manager of coffee shop chain, and oversee 5 branches. ID of branches are 1 to 5. You already collected sales data for yesterday from each branch, in three categories: morning, afternoon, and evening. First, you need to create a 2D array (5x3) with random numbers between 300 and 1000 (including 300 and 1000), i.e., from $300...

  • Write c++ program In this assignment, you need to complete following tasks on 2D array of...

    Write c++ program In this assignment, you need to complete following tasks on 2D array of randomly sales figures. Assume that you are the manager of coffee shop chain, and oversee 5 branches. ID of branches are to 5. You already collected sales data for yesterday from each branch, in three categories: morning, afternoon, and evening. First, you need to create a 2D array (5x3) with random numbers between 300 and 1000 (including 300 and 1000), i.e., from $300 to...

  • Write a C++ program - create a 1-d array. Its data type is integer. It has...

    Write a C++ program - create a 1-d array. Its data type is integer. It has 10 elements. - initialize this array by 10 integer numbers. Five elements of the array are positive, and others are negative. - use a while loop to manipulate the array as below. - If the value of an element is positive, the value is doubled. Otherwise, the value is incremented by 3. - print the new value of each element. - use a for...

  • Write a program that, given a starting location in a 2D grid of characters, will count...

    Write a program that, given a starting location in a 2D grid of characters, will count how many contiguously connected @ symbols there are. Cells have to be direct neighbors either horizontally or vertically to count as contiguous. However, the right/left and top/bottom edges ARE considered to be connected – you can “wrap around” from one side of the matrix to the opposite one. The grid of chars will always be 10x10. Your program should load the grid of chars...

  • I am using C++ Write a program to determine if a gird follows the rules to...

    I am using C++ Write a program to determine if a gird follows the rules to be classified as a Lo Shu Magic Square. The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown in figure below. The Lo Shu Magic Square has the following properties: The grid contains the numbers 1 through 9 exactly. The sum of each row, each column, and each diagonal all add up to the same number. This is shown...

  • This is the contents of Lab11.java import java.util.Scanner; import java.io.*; public class Lab11 { public static...

    This is the contents of Lab11.java import java.util.Scanner; import java.io.*; public class Lab11 { public static void main(String args[]) throws IOException { Scanner inFile = new Scanner(new File(args[0])); Scanner keyboard = new Scanner(System.in);    TwoDArray array = new TwoDArray(inFile); inFile.close(); int numRows = array.getNumRows(); int numCols = array.getNumCols(); int choice;    do { System.out.println(); System.out.println("\t1. Find the number of rows in the 2D array"); System.out.println("\t2. Find the number of columns in the 2D array"); System.out.println("\t3. Find the sum of elements...

  • Make a program using Java that asks the user to input an integer "size". That integer...

    Make a program using Java that asks the user to input an integer "size". That integer makes and prints out an evenly spaced, size by size 2D array (ex: 7 should make an index of 0-6 for col and rows). The array must be filled with random positive integers less than 100. Then, using recursion, find a "peak" and print out its number and location. (A peak is basically a number that is bigger than all of its "neighbors" (above,...

  • write a small C program including the following C functions/subroutines. Your main routine should use these...

    write a small C program including the following C functions/subroutines. Your main routine should use these functions to generate a small-ish array of random values and show that the standard deviation lowers as the array is smoothed. • void random_array(double* array, int size, double scale); – load an array with random double values scaled by scale (random number generators generate double values between 0 and 1). Note: array should point to memory already allocated. • double sum(double* array, int size);...

  • write a complete Java program with comments in main and in each method. Data: The input data for this program is given as two columns of numbers. All data will be entered from a fle named input.t...

    write a complete Java program with comments in main and in each method. Data: The input data for this program is given as two columns of numbers. All data will be entered from a fle named input.txt and all output will go to the screen Assume there will not be more than 100 7 23.56 16 88.12 10 75.1 Design a Java class with a main method that does the following 1) Reads the data into two arrays of doubles,...

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