Question

In: Computer Science

in C++ In the main function, declare a two-dimensional matrix array 5 by 5, read data...

in C++

In the main function, declare a two-dimensional matrix array 5 by 5, read data from a file named “data.txt”. Suppose the data in the file are integers between 0 and 9. Write your main function to print out the left bottom below the diagonal of the matrix and keep the triangle shape. Note the numbers on the diagonal are not included in the output. An example is given as follows. (25 points)

Suppose the original matrix is

1 2 3 4 5

1 2 3 4 5

1 2 3 4 5

1 2 3 4 5

1 2 3 4 5

The output should be like as follows.

1

1 2

1 2 3

1 2 3 4

#include <iostream>

#include <iomanip>

#include <fstream>

using namespace std;

const int NUM = 5;

int main()

{

      // I - Declaring a five by five array

      /* II - Read data from data.txt and use them to create

             the matrix in the previous step*/

/* III – print out the left bottom below the diagonal of the matrix and keep the triangle shape. Note the numbers on the diagonal are not included in the output.

       read.close();

       return 0;

}

Solutions

Expert Solution

//Working Code

//Remember while printing the left bottom triangle you need to exclude diagonal elements thats the reason we start from i=1 while printing


#include <fstream>
#include<iostream>
using namespace std;
int main() {
   //initialie the array size
   int arr[5][5];
   ifstream is("a.txt");
   int cnt= 0;
   int x;
   
//Construct 5*5 matrix
   for(int i=0;i<5;i++){
   for(int j=0;j<5;j++){
       is>>x;
   arr[i][j]=x;}}
 // Print Left bottom triangle without including diagonal 
  for(int i=1;i<5;i++){
  for(int j=0;j<i;j++){
  cout<<arr[i][j];}
  cout<<endl;
}
  
   is.close();
}

//attached pic of working code and its output as expected


Related Solutions

In the main function, declare a two-dimensional matrix array 5 by 5, read data from a...
In the main function, declare a two-dimensional matrix array 5 by 5, read data from a file named “data.txt”. Suppose the data in the file are integers between 0 and 9. Write your main function to print out the left bottom below the diagonal of the matrix and keep the triangle shape. Note the numbers on the diagonal are not included in the output. An example is given as follows. (25 points) Suppose the original matrix is 1 2 3...
Write a C++ function that takes a two dimensional dynamic array (matrix) and its sizes and...
Write a C++ function that takes a two dimensional dynamic array (matrix) and its sizes and returns true if the matrix is an upper matrix and returns false otherwise. Remark: A matrix M is an upper matrix if it is a square matrix (row size = column size) and every element M[i][j] = 0 for all i > j. That is all the elements of the matrix below the main diagonal are zero.
1.Declare a two-dimensional array of Strings namedchessboard.
1. Declare a two-dimensional array of Strings named chessboard.2. Declare a two-dimensional array of integers named tictactoe.3. Declare and create a two-dimensional array of chars,tictactoe, with 3 rows, each with 3 elements.4. Create a two-dimensional array of ints, plan, with 2 rows, and and 3 columns and initialize the first row to 8, 20, 50 and the second row to 12, 30, 75. Use member initializer syntax.
Provide code samples for the following in C#a. Declare a two-dimensional array of integers names...
Provide code samples for the following in C#a. Declare a two-dimensional array of integers names intArray17.b. Create a loop to calculate the sum of every element in the first column.c. Create a loop to calculate the sum of every element in the array.
In C# - Provide code samples for the following: Declare a two-dimensional array of integers names...
In C# - Provide code samples for the following: Declare a two-dimensional array of integers names intArray17. Create a loop to calculate the sum of every element in the first column. Create a loop to calculate the sum of every element in the array.
Write Matrix Addition 2 D (dimensional) Array program in c++.
Write Matrix Addition 2 D (dimensional) Array program in c++.
Write a C function that finds and displays the maximum value ina two-dimensional array of...
Write a C function that finds and displays the maximum value in a two-dimensional array of integers. The array should be declared as a 10-row-by-20-column array of integers in main (), and the starting the address of the array should be passed to the function. Modify the function so that it also displays the rows and columns number of the element with the maximum value
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test...
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test data. The program should have the following functions: getTotal - This function should accept two-dimensional array as its argument and return the total of all the values in the array. getAverage - This function should accept a two-dimensional array as its argument and return the average of values in the array. getRowTotal - This function should accept a two-dimensional array as its first argument...
Lab 7 - 2D Arrays (C++) In main, declare and fill a 2D array with one...
Lab 7 - 2D Arrays (C++) In main, declare and fill a 2D array with one hundred rows and fifty columns. Iterate through each element and assign it a random value between -72 and 75 inclusive. Have your random number seed be 25. Create functions that do the following: 1. A function called “sum” that returns the sum of all the elements in your 2D Array. 2. A function called “average” that return the average value of the elements in...
c++ please Write and testa C++ main program that: declare an array arrof 6 integers Prompt...
c++ please Write and testa C++ main program that: declare an array arrof 6 integers Prompt the user for 6 integer values and store them in arr. Prompt the user for a target integer target. Search for targetin arr. If targetis found to match an element in the arr, then the program prints out a message which contains the address of the found element, otherwise, if no element found then the message “the element target not found” will be printed...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT