answersLogoWhite

0


Best Answer

#include<iostream>

int main()

{

int matrix[3][3];

for (size_t row=0; row<3; ++row)

{

for (size_t col=0; col<3; ++col)

{

std::cout << "Enter a value for element [" << row << "][" << col << "]: ";

std::cin >> matrix[row][col];

}

}

std::cout << std::endl;

for (size_t row=0; row<3; ++row)

{

for (size_t col=0; col<3; ++col)

{

std::cout << matrix[row][col] << '\t';

}

std::cout << std::endl;

}

std::cout << std::endl;

}

User Avatar

Wiki User

9y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

9y ago

#include<iostream>

#include<array>

#include<limits>

#include<random>

using namespace std;

const size_t max_row = 3;

const size_t max_col = 3;

using array3x3 = std::array<std::array<int, max_col>, max_row>;

int get_min (const array3x3& arr)

{

int min = INT_MAX;

for (size_t row=0; row!=max_row; ++row)

for (size_t col=0; col!=max_col; ++col)

if (arr[row][col] < min)

min = arr[row][col];

return min;

}

int get_max (const array3x3& arr)

{

int max = INT_MIN;

for (size_t row=0; row!=max_row; ++row)

for (size_t col=0; col!=max_col; ++col)

if (max < arr[row][col])

max = arr[row][col];

return max;

}

int main()

{

std::default_random_engine generator;

std::uniform_int_distribution<int> distribution (-10, 10);

cout << "Array\n" << endl;

array3x3 arr;

for (size_t row=0; row!=max_row; ++row)

{

for (size_t col=0; col!=max_col; ++col)

{

arr[row][col] = distribution (generator);

cout << arr[row][col] << '\t';

}

cout << endl;

}

cout << endl;

cout << "Minimum = " << get_min (arr) << endl;

cout << "Maximum = " << get_max (arr) << endl;

}

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

#include<iostream>

#include<array>

#include<climits>

#include<cassert>

#include<time.h>

// 3x3 matrix typedef

using Row = std::array<int, 3>;

using Matrix = std::array<Row, 3>;

int smallest (Matrix& matrix)

{

int small = INT_MAX;

for (size_t r=0; r<matrix.size(); ++r)

{

const Row& row = matrix[r];

for (size_t col=0; col<row.size(); ++col)

{

if (row[col]<small)

small=row[col];

}

}

return small;

}

int main()

{

srand((unsigned)time(nullptr));

std::cout << "Matrix:\n" << std::endl;

Matrix matrix;

for (size_t r=0; r<matrix.size(); ++r)

{

Row& row = matrix[r];

for (size_t col=0; col<row.size(); ++col)

{

row[col] = rand();

std::cout << row[col] << '\t';

}

std::cout << std::endl;

}

std::cout << std::endl;

std::cout << "Smallest value in matrix: " << smallest (matrix) << std::endl;

std::cout << std::endl;

}

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

#include<iostream>

#include<iomanip>

#include<fstream>

#include<random>

#include<time.h>

std::ostream& write_value (std::ostream& os, unsigned value)

{

for (unsigned size = sizeof(unsigned); size; --size, value >>= 8)

os.put (static_cast<char>(value & 0xFF));

return os;

}

std::istream& read_value (std::istream& is, unsigned& value)

{

value = 0;

for (unsigned size = 0; size < sizeof(unsigned); ++size)

{

unsigned read = static_cast<unsigned>(is.get());

read <<= (8 * size);

value |= read;

}

return is;

}

int main()

{

// The filename.

const char * filename = "test.dat";

// pseudo-random number generator.

std::default_random_engine generator;

generator.seed ((unsigned) time (NULL));

std::uniform_int_distribution<unsigned> distribution (0, 1000);

// Instantiate a 3x3 matrix.

unsigned matrix[3][3];

// Fill matrix with random numbers (print as we go).

std::cout << "Matrix:\n" << std::endl;

for (unsigned dim1=0; dim1<3; ++dim1)

{

for (unsigned dim2=0; dim2<3; ++dim2)

{

matrix[dim1][dim2] = distribution (generator);

std::cout << std::setw (4) << matrix[dim1][dim2] << " ";

}

std::cout << std::endl;

}

std::cout << std::endl;

// Instantiate an output file.

std::ofstream outfile;

outfile.open (filename, std::ios::out | std::ios::binary);

// Write the matrix to the file.

for (unsigned dim1=0; dim1<3; ++dim1)

for (unsigned dim2=0; dim2<3; ++dim2)

write_value (outfile, matrix[dim1][dim2]);

outfile.close();

// Instantiate an input file.

std::ifstream infile;

infile.open (filename, std::ios::in | std::ios::binary);

// Read first value (initial lowest value).

unsigned lowest = 0;

if (read_value (infile, lowest))

{

// Read remainder of file.

unsigned current;

while (read_value (infile, current))

{

// Update the lowest value if the

// current value is lower.

if (current < lowest)

lowest = current;

}

}

infile.close();

std::cout << "Lowest value: " << lowest << std::endl;

}

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

#include<iostream>

#include<array>

#include<limits>

#include<random>

using namespace std;

const size_t max_row = 3;

const size_t max_col = 3;

using array3x3 = std::array<std::array<int, max_col>, max_row>;

int get_min (const array3x3& arr)

{

int min = INT_MAX;

for (size_t row=0; row!=max_row; ++row)

for (size_t col=0; col!=max_col; ++col)

if (arr[row][col] < min)

min = arr[row][col];

return min;

}

int get_max (const array3x3& arr)

{

int max = INT_MIN;

for (size_t row=0; row!=max_row; ++row)

for (size_t col=0; col!=max_col; ++col)

if (max < arr[row][col])

max = arr[row][col];

return max;

}

int main()

{

std::default_random_engine generator;

std::uniform_int_distribution<int> distribution (-10, 10);

cout << "Array\n" << endl;

array3x3 arr;

for (size_t row=0; row!=max_row; ++row)

{

for (size_t col=0; col!=max_col; ++col)

{

arr[row][col] = distribution (generator);

cout << arr[row][col] << '\t';

}

cout << endl;

}

cout << endl;

cout << "Minimum = " << get_min (arr) << endl;

cout << "Maximum = " << get_max (arr) << endl;

}

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

int main()

{

int a[3][3] = {{1,2,3},{4,-5,6},{7,8,9}};

int smallest = INT_MAX;

for (size_t row=0; row<3; ++row)

{

for (size_t col=0; col<3; ++col)

{

if (a[row][col]<smallest) smallest = a[row][col];

}

}

assert (smallest==-5);

}

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

int largest(int a[],int n)

{

int i;

int l=a[0];

for (i=0;i<n;i++)

if (a[i]>l)

l=a[i];

cout<<l;

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you write a c plus plus program which prompts the user to enter elements of a 3x3 matrix and then displays it?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you write a java program to find the transpose of the matrix for the given elements?

You basically write a nested for loop (one for within another one), to copy the elements of the matrix to a new matrix.


Write a C program using dynamic memory allocation to find the sum of elements of a matrix?

Did you know that memory allocation is not needed to display the matrix? However, the C program is to find the sum of all the elements.


What is a reduced matrix?

Reduced matrix is a matrix where the elements of the matrix is reduced by eliminating the elements in the row which its aim is to make an identity matrix.


How do you write c program to perform sum of elements of matrix using pointers with functions?

i cant write


What are elements of a matrix?

They are the number in the matrix.


How do you write a program that calculates the sum of the matrix elements given numbers?

ring me and ill explain - 086 22222222222222227 ring me


What is a sparse matrix?

A sparse matrix is a matrix in which most of the elements are zero.


Is the scalar matrix is always a identity matrix?

No. A scalar matrix is a diagonal matrix whose main diagonal elements are the same. Only if the diagonal elements are all 1 is it an identity matrix.


2x2 matrix multiplication program in 8085 processor?

how to write a program for matrix multiplication in microprocesspr


Write a program to multiply 33 matrix.?

write a program to multily 3*3 matrix.


Cannon matrix multiplication program?

maltiplication of matrix for algorithme


What are the key elements of skills matrix?

what is the key element of skill matrix