PLEASE CODE EVERYTHING IN C++
Quick Thinking: Arrays
Objective: Test your knowledge in providing efficient solutions for operations on arrays. What you can use only:
----------------------------------------------------------------------------------------------------------------------------------------------------------
1) Populate the array
using loops and a variable on
an even/odd pattern by row
Master Array
0 | 2 | 4 | 6 |
7 | 9 | 11 | 13 |
14 | 16 | 18 | 20 |
21 | 23 | 25 | 27 |
2) Compute the average value of each column in the master array and store the values in a parallel array.
3) Compute the average of the entire array
0 | 2 | 4 | 6 |
7 | 9 | 11 | 13 |
14 | 16 | 18 | 20 |
21 | 23 | 25 | 27 |
4) Compute the average of the corners.
0 | 2 | 4 | 6 |
7 | 9 | 11 | 13 |
14 | 16 | 18 | 20 |
21 | 23 | 25 | 27 |
5) Compute the average of the left-to-right diagonal
0 | 2 | 4 | 6 |
7 | 9 | 11 | 13 |
14 | 16 | 18 | 20 |
21 | 23 | 25 |
27 |
6) Compute the average of inside only
0 | 2 | 4 | 6 |
7 | 9 | 11 | 13 |
14 | 16 | 18 | 20 |
21 | 23 | 25 | 27 |
7) Compute the average of the right-to-left diagonal
0 | 2 | 4 | 6 |
7 | 9 | 11 | 13 |
14 | 16 | 18 | 20 |
21 | 23 | 25 | 27 |
8) Compute the average value of each row in the master array and store the values in a parallel array.
9) Compute the average of the boundaries
0 | 2 | 4 | 6 |
7 | 9 | 11 | 13 |
14 | 16 | 18 | 20 |
21 | 23 | 25 | 27 |
PLEASE CODE EVERYTHING IN C++
Note: Solved Questions 1 to 7.
#include<stdio.h>
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
const int SIZE = 4;
int variable = 0;
int master_array[SIZE][SIZE];
double average = 0.0;
double parallel_array[SIZE];
// populate the master array
for (int row = 0; row < SIZE; row ++){
for (int col = 0 ; col < SIZE;col++ ){
master_array[row][col] = 2*col + 7*row; //main pattern
}
}
// printing the array for checking (commment out if not required)
for (int row = 0; row < SIZE; row ++){
for (int col = 0 ; col < SIZE;col ++ ){
cout << master_array[row][col]<<" ";
}
cout << "\n";
}
//calculating the average for each column
for (int col = 0; col < SIZE;col++){
average = 0; // resetting the average variable to 0
for (int row = 0; row < SIZE; row ++){
average += master_array[row][col];
}
parallel_array[col] = average/SIZE; // saving the value of each column's average
}
cout <<"The value of the parallel array : "<<"\n";
for (int i = 0; i < SIZE; i ++){
cout << parallel_array[i] << " ";
}
//computing average of the whole array
average = 0;
for (int i = 0; i < SIZE; i++){
for (int j = 0 ; j < SIZE; j++){
average += master_array[i][j];
}
}
average = average/(SIZE*SIZE);
// printing the result
cout <<"\n";
cout << "The average of the whole array is: ";
cout << average << "\n";
//computing the average of the corners
average = 0;
for (int i = 0 ; i < SIZE; i++){
for (int j = 0; j < SIZE; j++){
if ((i == 0 && j == 0)||(i == 0 && j == SIZE-1)||(i == SIZE-1 && j == 0 )||(i == SIZE-1 && j == SIZE-1)){
average += master_array[i][j];
//cout << master_array[i][j] << " "
}
}
}
//printing the average of the corners
cout << "The average of the corners: " << average/4 << "\n ";
//computing the average of the left to right diagonal
average = 0;
for (int i = 0; i < SIZE; i++){
for (int j = 0; j < SIZE; j++){
if (i==j){
average += master_array[i][j];
}
}
}
average = average/SIZE;
cout << "The average of the left to right diagonal "<< " "<<average;
//computing the average of the inside only
average = 0;
for (int i = 0; i < SIZE; i++){
for (int j = 0; j < SIZE; j++){
if ((i>0 && i < SIZE-1)&&(j>0 && j < SIZE-1)){
average += master_array[i][j];
// cout << master_array[i][j];
}
}
}
cout <<"\n";
cout << "The average of the insides only : " << average/((SIZE-2)*(SIZE-2));
//computing the average of the right-to-left diagonal
average = 0;
for (int i = 0; i < SIZE; i++){
for (int j = 0; j < SIZE; j++){
if (i+j == SIZE-1){
average += master_array[i][j];
}
}
}
cout <<" \n";
cout <<"The average of the right to left diagonal is: "<<average/SIZE<<"\n";
return 0;
}
Code Output:
Get Answers For Free
Most questions answered within 1 hours.