Square matrix in c ++ - c++

I did the above code to calculate the matrix of a square matrix but it does not work, can someone explain the error to me please
#include <iostream>
using namespace std;
int main() {
int M[3][3]={{2, 4, -6}, {1, 5, 3}, {1, 3, 2}};
int n, mult;
n=3;
cout << "Matrix: " << endl;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
cout << M[i][j] << "\t";
cout << "\n";
}
for (int i=0; i<n; i++){
for (int j=i+1; j<n; j++){
mult=M[j][i]/M[i][i];
for (int k=i; k<n; k++){
M[j][k]=M[j][k]-mult*M[i][k];
}
}
}
cout << "Echelon matrix: " << endl;
for (int i=0; i<n; i++){
for (int j=i+1; j<n; j++){
for(int k=i; k<n; k++){
cout << M[j][k] << "\t";
cout << "\n";
}
}
}
return 0;
}

In this code, I replaced int by double, even if that, for this particular matrix, it would work with int.
The main problem was that the output of the result was broken, 3 loops when two only are needed.
Note that you don't check that the Gauss pivot M[i][i] is not null. The code should be completed to handle this case.
Output:
Matrix:
2 4 -6
1 5 3
1 3 2
Echelon matrix:
2 4 -6
0 3 6
0 0 3
#include <iostream>
//using namespace std;
int main() {
double M[3][3]={{2, 4, -6}, {1, 5, 3}, {1, 3, 2}};
int n = 3;
std::cout << "Matrix: " << std::endl;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
std::cout << M[i][j] << "\t";
std::cout << "\n";
}
for (int i = 0; i < n; i++){
for (int j = i+1; j < n; j++){
double mult = M[j][i]/M[i][i];
for (int k = i; k < n; k++){
M[j][k] = M[j][k] - mult*M[i][k];
}
}
}
std::cout << "Echelon matrix: " << std::endl;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
std::cout << M[i][j] << "\t";
}
std::cout << std::endl;
}
return 0;
}

Related

Static array and Dynamic array in a function

I have a question as below C++ code.
this is the code(No function) that can successful execute.
#include <iostream>
using namespace std;
int main()
{
int size_3 = 3;
//arr1 is a static array
int arr1[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
//arr2 is a dynamic array
int** arr2 = new int* [size_3];
for (int i = 0; i < size_3; i++) {
arr2[i] = new int[size_3];
}
int val = 9;
for (int i = 0; i < size_3; i++) {
for (int j = 0; j < size_3; j++) {
arr2[i][j] = val--;
}
}
cout << "Elements in matrix 1: " << endl;
for (int i = 0; i < size_3; i++) {
for (int j = 0; j < size_3; j++) {
cout << arr1[i][j] << " ";
}
cout << endl;
}
cout << endl;
cout << "Elements in matrix 2: " << endl;
for (int i = 0; i < size_3; i++) {
for (int j = 0; j < size_3; j++) {
cout << arr2[i][j] << " ";
}
cout << endl;
}
cout << endl;
int** prod_arr = new int* [size_3];
for (int i = 0; i < size_3; i++) {
prod_arr[i] = new int[size_3];
}
for (int i = 0; i < size_3; i++) {
for (int j = 0; j < size_3; j++) {
prod_arr[i][j] = 0;
for (int k = 0; k < size_3; k++) {
prod_arr[i][j] += arr1[i][k] * arr2[k][j];
}
}
}
cout << "Elements in the product of 2 matrices: " << endl;
for (int i = 0; i < size_3; i++) {
for (int j = 0; j < size_3; j++) {
cout << prod_arr[i][j] << " ";
}
cout << endl;
}
}
However, I am gonna to use the function for the multiplication of the 2 matrices.
how could I move it to function, since the first array is static array, the second array is dynamic array.
I have try different way to use pointer , pass-by reference in the function, but still can't work.
My invalid code as below.
#include <iostream>
using namespace std;
int** function(int farr1, int farr2, int size3) {
int** prod_arr = new int* [size3];
for (int i = 0; i < size3; i++) {
prod_arr[i] = new int[size3];
}
for (int i = 0; i < size3; i++) {
for (int j = 0; j < size3; j++) {
prod_arr[i][j] = 0;
for (int k = 0; k < size3; k++) {
prod_arr[i][j] += farr1[i][k] * farr2[k][j];
}
return prod_arr[i][j];
}
}
}
int main()
{
int size_3 = 3;
//arr1 is a static array
int arr1[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
//arr2 is a dynamic array
int** arr2 = new int* [size_3];
for (int i = 0; i < size_3; i++) {
arr2[i] = new int[size_3];
}
int val = 9;
for (int i = 0; i < size_3; i++) {
for (int j = 0; j < size_3; j++) {
arr2[i][j] = val--;
}
}
cout << "Elements in matrix 1: " << endl;
for (int i = 0; i < size_3; i++) {
for (int j = 0; j < size_3; j++) {
cout << arr1[i][j] << " ";
}
cout << endl;
}
cout << endl;
cout << "Elements in matrix 2: " << endl;
for (int i = 0; i < size_3; i++) {
for (int j = 0; j < size_3; j++) {
cout << arr2[i][j] << " ";
}
cout << endl;
}
cout << endl;
int** prod_arr = function(farr1, farr2, size_q3);
cout << "Elements in the product of 2 matrices: " << endl;
for (int i = 0; i < size_3; i++) {
for (int j = 0; j < size_3; j++) {
cout << prod_arr[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
I want to use the function to execute the code.
I got 2 array, one is static and another is dynamic,
How to use pointer and pass to function with this different arrays.
Thanks a lot for your help.
Some C++ array examples to get you started :
A 2D dynamically array can be modeled as std::vector<std::vector<int>>
#include <array> // static array
#include <vector> // dynamic array, can resize at runtime
#include <iostream>
// https://en.cppreference.com/w/cpp/container/array
// accept a modifiable static array (content can be changed
// the & means by reference, the array will not get copied
// https://isocpp.org/wiki/faq/references
void func1(std::array<int, 4>& values)
{
values[2] = 3;
}
// array cannot be modified in body of func2 only used : const
void func2(const std::array<int, 4>& values)
{
std::cout << values[2] << "\n";
}
// https://en.cppreference.com/w/cpp/container/vector
// return a dynamically allocated array of integers
// it will have a size of 5 when returned
std::vector<int> get_values()
{
return std::vector<int>{1, 2, 3, 4, 5};
}
int main()
{
auto values = get_values();
values.push_back(6); // add another value
// https://en.cppreference.com/w/cpp/language/range-for
// prefer to use those if you don't need indices
// (which is not as often as you think)
for (const int value : values)
{
std::cout << value << " ";
}
return 0;
}

How to fill a 2D through input and then read back as output in a C++. Here is the code which I wrote. I gives wrong output.I am a beginner

#include <iostream>
#include<fstream>
using namespace std;
int main() {
int a = 1;
if (a == 1) {
int a[1][1];
for (int i = 0; i <= 1; i++) {
for (int j = 0; j <= 1; j++) {
cin >> a[i][j];
}
}
cout << endl;
for (int k = 0; k <= 1; k++) {
for (int l = 0; l <= 1; l++) {
cout << a[k][l] << " ";
}
cout << endl;
}
}
return 0;
}
In this program if we enter input as :
1
2
3
4
it gives output :
1 3
3 1
it should give output as:
1 2
3 4
Please help, I am a beginner.
I am coding in code blocks.
Try this
int main()
{
int arr[2][2]; // declares a 2x2 array
cout << "Enter integers : " << endl;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
cin >> arr[i][j]; //inserts at the index i and j in the array
}
}
cout << "Display array : " << endl;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
cout << arr[i][j] << endl; /*displays the value at
index i and j in the array*/
}
}
}

finding the largest number in 2d array and its position in row and column

With my code I can find the largest number in the matrix but I cant find the exact position (in rows and column)of the number. I'm a beginner so I don't know any fancy libraries and i prefer not to use them because i have not study them and its a 4X4 matrix
here id my code
#include<iostream>
using namespace std;
int main()
{
int a[4][4], big1, n, m, i, j,loc1,loc2;
cout << "Enter no of rows and columns:";
cin >> m >> n;
cout << "Enter the array:\n";
for (i = 0; i < m; i++)
{
for (j = 0; j < n; ++j)
{
cin >> a[i][j];
}
}
cout << endl << "Entered Matrix: " << endl;
for (i = 0; i < m; ++i)
for (j = 0; j < n; ++j)
{
cout << " " << a[i][j];
if (j == n - 1)
cout << endl << endl;
}
big1 = a[0][0];
loc1 = 0;
loc2 = 0;
for (i = 0; i < m; ++i)
{
for (j = 0; j<n; ++j)
{
for (int i = 0; i<4; i++)
for (int j = 0; j<4; j++)
if (a[i][j]>big1)
big1 = a[i][j];
loc1 = i;
loc2 = j;
}
}
cout << "\nLargest number:" << big1 << endl;
cout << "The position that had the largest number is " << loc1 <<" " << loc2 << endl;
system("pause");
return 0;
}
You are just missing some braces to update the location variables only for the maximum:
for (int i = 0; i<4; i++)
for (int j = 0; j<4; j++)
if (a[i][j]>big1)
{
big1 = a[i][j];
loc1 = i;
loc2 = j;
}
Everything is write with your code. Small things need to update add braces after if condition
Here it he updated code
for (i = 0; i < m; ++i)
{
for (j = 0; j<n; ++j)
{
for (int i = 0; i<4; i++)
for (int j = 0; j<4; j++)
if (a[i][j]>big1){
big1 = a[i][j];
loc1 = i;
loc2 = j;
}
}
}

Sort 2D Array Row's Depend on the last column

I want to Write a Program in c++ that swap the row but my code only sort the element of the array.
For example:
I made a array of 3*5.
I input data as follows
1 2 3 4 10
5 6 7 8 26
9 1 2 3 15
Now I want to swap the rows by ascending order depends on the last column.i.e.
5 6 7 8 26
9 1 2 3 15
1 2 3 4 10
My code Only Sort the Row form left to Right.
int arr[3][5], sum, temp;
for(int i =0; i<3; i++)
{
sum=0;
for(int j =0; j<4; j++)
{
cout << "Enter "<<j+1<<" Number of "<<i+1<<" Student";
cin >> arr[i][j];
sum = sum + arr[i][j];
}
arr[i][4]=sum;
}
for(int i = 0; i<3; i++)
{
for(int j = 0; j<5; j++)
{
cout << arr[i][j]<<"\t";
}
cout << endl;
}
cout<<"----------------"<<endl;
for(int i=0;i<3;i++)
{
for(int j = 0;j<4;j++)
{
for(int k =0;k<4;k++)
{
if(arr[i][j]<arr[i][k])
{
temp=arr[i][j];
arr[i][j]=arr[i][k];
arr[i][k]=temp;
}
}
}
}
for(int i =0;i<3;i++)
{
for(int j =0;j<5;j++)
{
cout<<arr[i][j]<<"\t";
}
cout<<endl;
}
If performance is not of significant concern, just use the same idea as Bubble sort.
Compare last column element between each pair of rows, and if not in order swap the rows. This can be done with a loop and a temporary variable.
if(matrix[i][cols-1] > matrix[j][cols-1])
{
int temp ;
for(int k = 0; k < cols; ++k)
{
temp = matrix[i][k] ;
matrix[i][k] = matrix[j][k] ;
matrix[j][k] = temp ;
}
}
Since you are using C++, you can use std::array if your matrix has fixed size rows and columns. i.e.
std::array<std::array<int, colums>, rows> matrix ;
Here is a working example which uses std::swap to do the swapping of rows. The sorting routine is essentially the bubble sort.
for(int i = 0; i < rows; ++i)
for(int j = 0; j < i; ++j)
if(matrix[j][cols-1] > matrix[i][cols-1])
std::swap(matrix[i], matrix[j]);
Maybe this is your answer:
int arr[3][5], sum, temp;
for (int i = 0; i<3; i++)
{
sum = 0;
for (int j = 0; j<4; j++)
{
cout << "Enter " << j + 1 << " Number of " << i + 1 << " Student";
cin >> arr[i][j];
sum = sum + arr[i][j];
}
arr[i][4] = sum;
}
cout << endl << endl;
for (int i = 0; i<3; i++)
{
for (int j = 0; j<5; j++)
{
cout << arr[i][j] << "\t";
}
cout << endl;
}
cout << "----------------" << endl << endl;
int tmp[5],i,j,k;
for ( i = 0; i<3; i++)
{
for ( j = 0; j<3; j++)
{
if (arr[j][4]<arr[i][4])
{
for ( k = 0; k <= 4; k++){
int t = arr[i][k];
arr[i][k] = arr[j][k];
arr[j][k] = t;
}
}
}
}
for (int i = 0; i<3; i++)
{
for (int j = 0; j<5; j++)
{
cout << arr[i][j] << "\t";
}
cout << endl;
}

Block Matrix Transpose

I wanted to implement transposition of a matrix by dividing the input matrix into blocks and then transposing them. I referred to the corresponding post A Cache Efficient Matrix Transpose Program? and wrote my code like this:
#include<iostream>
#include<stdlib.h>
#define m 4
#include<sys/time.h>
#include<time.h>
#include<malloc.h>
using namespace std;
int **a, **b, **c;
int count = 0;
clock_t t1, t2;
int blocksize = 2;
int main(){
a = (int **)malloc(m*sizeof(int *));
for(int i = 0;i<m;i++){
a[i] = (int *)malloc(m*sizeof(int));
}
b = (int **)malloc(m*sizeof(int *));
for(int i = 0;i<m;i++){
b[i] = (int *)malloc(m*sizeof(int));
}
for(int i=0; i<m; i++){
for(int j =0; j<m; j++){
a[i][j]=(2*i)+(3*j);
}
}
for(int i=0; i<m; i++){
for(int j =0; j<m; j++){
cout << a[i][j] << "\t";
}
cout << "\n";
}
cout << "\n";
t1 = clock();
// MAIN BLOCK TRANSPOSE CODE
for (int i = 0; i < m; i += blocksize) {
for (int j = 0; j < m; j += blocksize) {
for (int k = i; k < i + blocksize; ++k) {
for (int l = j; l < j + blocksize; ++l) {
b[k + l*m] = a[l + k*m];
}
}
}
}
t2 = clock();
for(int i=0; i<m; i++){
for(int j =0; j<m; j++){
cout << b[i][j] << "\t";
}
cout << "\n";
}
free(a);
free(b);
cout << "\n";
cout << (double)(t2-t1)/CLOCKS_PER_SEC << "\n";
return 0;
}
However, the code is not working as expected. I implemented the code that is said to be working in the corresponding post. Please help if possible.
Input Array:
0 3 6 9
2 5 8 11
4 7 10 13
6 9 12 15
Expected Output Array:
0 2 4 6
3 5 7 9
6 8 10 12
9 11 13 15
Obtained Result:
0 3 6 9
Segmentation fault
I think your matrix is supposed to be encoded in a single array, not in an array of arrays (See the Edit 2 of the linked question).
You might want to try that instead:
int *a, *b, *c;
a = (int *)malloc(m*m*sizeof(int));
b = (int *)malloc(m*m*sizeof(int));
for(int i=0; i<m; i++){
for(int j =0; j<m; j++){
a[i*m+j]=(2*i)+(3*j);
}
}
for(int i=0; i<m; i++){
for(int j =0; j<m; j++){
cout << a[i*m+j] << "\t";
}
cout << "\n";
}
cout << "\n";