Question

Write a full C++program to do the following processes on a two dimension array of integers....

Write a full C++program to do the following processes on a two dimension array of integers. Assume the size is 4x4 .

1- Fill the array with the numbers like this pattern:

1 1 1 1

2 2 2 2

3 3 3 3

4 4 4 4

Note: Do not accept the values as input, or hard code it. Find the math to generate these numbers and fill the array with them.   

2- Reset the main diagonal elements of the array. This means all the elements which fall in the main diagonal will be 0. See this example: The result will be like this:

0 1 1 1

2 0 2 2

3 3 0 3

4 4 4 0

Note: Again find the relationship among these items in the main diagonal and reset them.

3- Create a method named Sum to find the summation of each row of the array. So pass the array and the row number that you want to calculate and return the sum of it.  

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

#include<iostream>

#include<conio.h>

using namespace std;

const int arr_size = 4;

int arr[arr_size][arr_size] = {0};

/* Method to print array */

void print(){

for(int i = 0; i< arr_size; i++ ){

for(int j =0; j< arr_size; j++){

cout<<arr[i][j]<<" ";

}

cout<<endl;

}

}

/* Method to find sum corresponding to row numbers.

Param In: array[Integer][Integer], row number[Integer]

Output: sum[Integer] of corresponding row

*/

int sum(int arr[arr_size][arr_size], int row){

int sum_row =0;

for(int i = 0; i< arr_size; i++){

sum_row += arr[row-1][i];

}

return sum_row;

}

int main(){

/* First part: Array is generated based upon the row numbers.

So for row 'i' enteries will be 'i' for all columns

Assumption: row will start from index 1 not 0

*/

for(int i = 1; i< arr_size+1; i++){

for(int j = 0; j< arr_size; j++){

arr[i-1][j] = i;

}

}

//Printing array

print();

cout<<endl;

/* Modifying array with main diagonal as zero

RelationShip: For diagonal element i = j or row number = columm number

*/

for(int i = 0; i< arr_size; i++){

arr[i][i] = 0;

}

//Printing array

print();

cout<<endl;

int row;

cout<<"\n Enter the row number for which you need summation: ";

cin>>row;

cout<<"Sum for row number: "<< row<< " ,is: "<<sum(arr, row);

getch();

}

Output:

Add a comment
Know the answer?
Add Answer to:
Write a full C++program to do the following processes on a two dimension array of integers....
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
  • Write a c program to implement threads. Accept an array of 40 integers, write a thread...

    Write a c program to implement threads. Accept an array of 40 integers, write a thread method named sum of array elements. Divide the array into 4 equal elements and call threads to find the sum of each part. Store the sum in a variable called “arrays'. Print the sum in the main function. (10 points)

  • Done in C++ using visual studio 1. Write a program with one additional function called int[]...

    Done in C++ using visual studio 1. Write a program with one additional function called int[] reverseArray(int array). This function will receive an array from main and then reverse and should return the reversed array to the main to print out. Use a single array and a single loop, you’re are neither allowed to print out just in reverse order nor allowed to use another array variable to store the original array in reverse order. 2. Write a program which...

  • in java Given n >= 0, create an array with length n*n with following the pattern,...

    in java Given n >= 0, create an array with length n*n with following the pattern, shown for n=3 { 0, 1, 2, 1, 0, 3, 2, 3, 0} (Spaces have been added to show the grouping of elements of the array more "obvious.") Note: The values in the array are the sum of the "row" and "column" values, except along the diagonal, where it is zero. diagonal(3) ? [0, 1, 2, 1, 0, 3, 2, 3, 0] diagonal(2) ?...

  • Write in C code: Define an array f=[23,-12,34,-56,-10,12,19], using call-by-reference (use pointers), to pass the array...

    Write in C code: Define an array f=[23,-12,34,-56,-10,12,19], using call-by-reference (use pointers), to pass the array to a function, named Summation. In main: Define array, pass array, print out the array and the result returned from Summation on screen. In function Summation: 1)take array and size from main 2) sum each element according to the following rules 3) if the value of f(i) is positive, add 2*f(i) to the sum 4)if the value of f(i) is negative, add -3*f(i) to...

  • using matlab In Class Exercises 8-Arrays For the following exercises, assume an array is already populated, your job is to write a program that traverses the array. DO NOT HARD CODE the last in...

    using matlab In Class Exercises 8-Arrays For the following exercises, assume an array is already populated, your job is to write a program that traverses the array. DO NOT HARD CODE the last index. In other words, you code for 1-4&6 should be able to handle the following 2 arrays: amp2 10 array1 [52, 63, 99, 71, 3.1] array2 - [99:-3: 45); For 5: wordArray1 wordArray2 ('number, 'arrays', 'indices', 'hello', finish' [number, 'different, 'finish? 1. Determine the sum of all...

  • Using Matlab Write a program which will: a. Accept two numbers from the user m and...

    Using Matlab Write a program which will: a. Accept two numbers from the user m and n b. Define an array with the dimensions a(n,m) and fill it with random numbers in the range of -100 to 100 inclusive. c. Provide the user the following menu from which he can select: 1. Sum of all elements in the array. Print the result. 2. Sum of the element in a row that he’ll specify. Print the result. 3. Sum of the...

  • IN C++ Write a program that uses a 3x3 array and randomly places each integer from...

    IN C++ Write a program that uses a 3x3 array and randomly places each integer from 1 to 9 into the nine squares. The program calculates the "magic number" by adding all the numbers in the array and then dividing the sum by 3. The 3x3 array is said to be a "magic square" if the sum of each row, each column, and each diagonal is equal to the magic number. Your program must use at least the following functions:...

  • C++: Write a program that uses a 3 X 3 array and randomly place each integer...

    C++: Write a program that uses a 3 X 3 array and randomly place each integer from 1 to 9 into the nine squares. The program calculates the magic number by adding all the numbers in the array and then dividing the sum by 3. The 3 X 3 array is a magic square if the sum of each row, each column, and each diagonal is equal to the magic number. Your program must contain at least the following functions:...

  • Three C Code Questions: 5. Write a C program that declares an array of 10 strings,...

    Three C Code Questions: 5. Write a C program that declares an array of 10 strings, each of max length 50 characters, and then reads an entire line of text from the keyboard into each string. 6. Write C code that finds the longest name in this array of strings that you read in Q5. Hint: find the position of the longest string, not the string itself. 7. Write a C program that creates a 4 x 6 two dimensional...

  • 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...

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