Stack Overflow-Matrix Multiplication [duplicate] - c++

This question already has answers here:
Declare large array on Stack
(4 answers)
Closed 9 months ago.
I am programming for simple matrices multiplication. However, for large values of matrix size, I faced with matrix overflow error. Could someone help me with this.
here the code!
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int r,c;
cout<<"Rows: ";
cin>>r; // 5000
cout<<"Clumns: ";
cin>>c; // 5000
int m[r][c];
for (int i=0;i<r;i++)
{
for (int j=0;j<c;j++)
{
m[i][j]=i+j;
cout<<setw(4)<<m[i][j];
}
cout<<endl;
}
}

I ran your program with different sizes. And the problem is simply that the array is too big. It works with smaller array sizes, but you can only put so much onto the stack.
So I did this:
#include <iostream>
#include <iomanip>
using namespace std;
class Matrix {
public:
Matrix(int r, int c)
: rows(r), cols(c)
{
m = new int *[rows];
for (int index = 0; index < rows; ++index) {
m[index] = new int[cols];
}
}
int & at(int r, int c) {
return m[r][c];
};
int rows = 0;
int cols = 0;
int ** m = nullptr;
};
int main(int argc, char **argv)
{
int r = atoi(argv[1]);
int c = atoi(argv[2]);
Matrix m(r, c);
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
m.at(i, j) = i+j;
cout << setw(4) << m.at(i, j);
}
cout<<endl;
}
}
And that appears to work. Now, there are some things in here that are bad. I didn't write a destructor, so there's a memory leak. And I didn't do any range checking in the at() method. I was only showing what you could do for very large arrays.
Now, I'm going to beg you... PLEASE put white space in your code. You're going to have no end of errors when you shove everything together the way you do. Notice my for-loops have a lot more space than you do. I didn't fix everywhere, but the coding policy where I work is to include white space for readability. Walls of numbers and operators can be very, very hard to read.
Also, name your variables something longer than a single character.
Both these changes will dramatically reduce future bugs.

Related

C++ - Passing 2D array to function [duplicate]

This question already has answers here:
Passing a 2D array to a C++ function
(17 answers)
Variable length arrays (VLA) in C and C++
(5 answers)
Closed 3 months ago.
I am trying to pass a 2D array to a function but I am failing to do so. There is no problem with passing 1D array. How can I do this?
#include <iostream>
using namespace std;
void DisplayBoard(int matrix[],int n) // Prints out the given array.
{
for (int j = 0; j < n; j++)
{
cout << matrix[j];
}
}
int main()
{
int n,m;
cin>>n;
//int matrix[n]={};
DisplayBoard(matrix,n);
return 0;
}
i think you want to input index number (n) from user and then input array object from user too
i complete your code like this :
#include <iostream>
using namespace std;
void DisplayBoard(int matrix[],int n) // Prints out the given array.
{
cout<<endl ;
for (int j = 0; j < n; j++)
{
cout << matrix[j]<<" ";
}
}
int main() {
int n,m;
cin>>n ;
int matrix[n];
for(int i=0;i<n;i++) {
cin>>matrix[i];
}
DisplayBoard(matrix,n);
return 0;
}
remember to declare array in main function too ,
that was one of your code error !
and this is incorrect to declare array :
int matrix[n]={} // incorrect !
Well. int matrix[n]={}; fills it with zeros and that's what I wanted to do. And my code with 1D array works fine but if I do it like so it does not.
#include <iostream>
using namespace std;
void DisplayMatrix(int matrix[][],int n,int m)
{
for(int i=0;i<n;i++)
{
for (int j = 0; j < m; j++)
{
cout << matrix[j];
}
}
}
int main()
{
int n,m;
cin>>n>>m;
int matrix[n][m]={};
DisplayMatrix(matrix,n,m);
return 0;
}

How to use the same multidimensional array in many functions?

I'm a beginner at C++ and to be honest, I've got no idea how to solve one task.
I have to create a matrix using a two dimensional array. It's size should be dependent on user's input (it should be like...int matrix[m][n], where m and n are the numbers entered by user). Then I'm supposed to fill it with random numbers from 0 to 100 and print it. Well, I can manage it.
The problem starts when I have to create a function finding the highest number from this array's row. The only parameter of this function can be the number of row entered by user (eg. int function(int i)).
The question is-how can I use the same array in multiple functions? Is there any way to do this, considering the fact that I'm a newbie?
Or maybe the task is formed incorrectly?
Sorry for the long post and thanks in advance
PS Someone asked for code, so here it is:
#include <iostream>
#include <cstdlib>
using namespace std;
int function1(int i)
{
//this is one of the functions I'm supposed to create-I described it earlier
}
int main()
{
int m,n;
cout<<"Matrix's size will be m rows and n columns. Please write m and n"<<endl;
cin>>m>>n;
int A[m][n];
int a,b;
for (a=0;a<m;a++)
{
for (b=0;b<n;b++)
{
A[a][b]=rand()%(100+1);
cout<<A[a][b]<<" ";
}
cout<<"\n";
}
}
EDIT: I'd like to thank you all for help guys. I asked my teacher about that and he finally responded. If you're curious, he told us (I hadn't heard it) to define an array like int[100][100] or higher and not allow user to input any higher numbers ;) That's not an optimal solution but surely a practical one. Thank you again!
The correct way to do this in C++ is to use a std::vector or std::array.
If you cannot do this because of artificial requirements, then there is simply no way you can declare a 2D array in C++ based on user input.
cin >> m >> n;
...
int array [m][n]; // not possible
int** wannabe; // not an array
int array [m * n]; // not possible
What you can do is a "mangled" 2D array:
int* mangled = new int[m * n];
Example of use:
class int_matrix
{
private:
int* mangled;
size_t rows;
size_t cols;
public:
int_matrix(size_t row, size_t col)
:rows(row),
cols(col)
{
mangled = new int[row * col];
}
int highest_in_row (size_t row)
{
...
}
};
Please note that this code requires that you follow the rule of three.
In C you would just have elegantly solved this by writing int array[m][n], but you are using C++ so you can't do that.
You can wrap your function into a class. In that class, you can have your array as member variable.
class A {
int **matrix;
public:
A(int rows, int columns) {
matrix = new int*[rows];
for(int i = 0; i < rows; ++i)
matrix[i] = new int[columns];
}
int function(int i); //you can use your matrix in this function
}
If you can't use classes, you can use global variables.
In a file.cpp
int **matrix;
int function(int i) {
//Do Something
}
//With rows the number of rows and columns the number of columns
//You can take these as parameters
int main() {
matrix = new int*[rows];
for(int i = 0; i < rows; ++i)
matrix[i] = new int[columns];
function(42);
}
If you declare a matrix like int int A[m][n]; where m and n aren't const, you can't pass it to a function. There are two ways to fix it:
1) Declare matrix with const size like int A[10][10];. In this case function which finds max will look like this:
int max_in_row(int matr[10][10], int row) {
int max = 0;
for (int col = 0; col < 10; ++col)
if (matr[row][col] > max)
max = matr[row][col];
return max;
}
and you can find max simple as int max = max_in_row(A, <row you want>);
2) (If you don't know size) Declare matrix as array of arrays:
int **A = new int*[n];
for (int i = 0; i < n; ++i)
A[i] = new int[m];
// fill A like you did
Then the function will look like
int max_in_row(int **matr, int row, int m) {
int max = 0;
for (int col = 0; col < m; ++col)
if (matr[row][col] > max)
max = matr[row][col];
return max;
}
and you can find max by int max = max_in_row(A, <row you want>, m);
The following is not standard C++ because it will only work if the compiler supports Variable Length Arrays. VLA were introduced in C99 and made optional in C11 but were never introduced in C++ standard - but some compilers support it even in C++ mode.
The hack will be to store the matrix address as a global void * and cast it to the proper pointer to VLA inside the function. This hack is required because at the moment of the global declaration you cannot know the number of columns of the matrix.
#include <iostream>
#include <cstdlib>
void *Matrix;
int Columns;
using namespace std;
int function1(int i)
{
typedef int MAT[Columns]; // BEWARE!!! VLA is not standard C++
MAT *mat = static_cast<MAT *>(Matrix);
int mx = mat[i][0];
for(int j=0; j<Columns; j++) {
cout << " " << mat[i][j];
if (mat[i][j] > mx) mx = mat[i][j];
}
std::cout << endl;
return mx;
}
int main()
{
int m,n;
cout<<"Matrix's size will be m rows and n columns. Please write m and n"<<endl;
cin>>m>>n;
int A[m][n]; // BEWARE!!! VLA is not standard C++
int a,b;
for (a=0;a<m;a++)
{
for (b=0;b<n;b++)
{
A[a][b]=rand()%(100+1); // Note that I now use a and b here !
cout<<A[a][b]<<" ";
}
cout<<"\n";
}
Matrix = static_cast<void *>(A);
Columns = n;
cout << "Enter row number to process: ";
cin >> a;
b = function1(a);
cout << "Max of row " << a << " is " << b << endl;
return 0;
}
Not really C++-ish, but at least it compiles and give expected results with clang version 3.4.1

Whats wrong with this one? C++ Array Pointer

It says:
[Error] invalid conversion from 'int*' to 'int' [-fpermissive] on line 9 col 5.
What was asked of me to do:
Make a program that would accept array of 10 integers and determine the highest and the lowest integers from the set of integers. Use pointer variables for the highest and lowest integer.
what i did:
#include<iostream>
using namespace std;
int main()
{
int kre_arr[10];
int *kre_p;
for(int k = 0; k<=10; k++)
{
kre_p[k] = &kre_arr[k];
}
int j,temp;
cout<<"Enter 10 Integers: ";
for (*kre_p=0; *kre_p < 10; *kre_p++)
{
cin>>kre_arr[*kre_p];
}
for(*kre_p=0;*kre_p<=10;*kre_p++)
{
for(j=*kre_p+1;j<=10;j++)
{
if(kre_arr[*kre_p] > kre_arr[j])
{
temp = kre_arr[*kre_p];
kre_arr[*kre_p] = kre_arr[j];
kre_arr[j] = temp;
}
}
}
for(*kre_p=0;*kre_p<=9;*kre_p++)
{
cout<<endl<<kre_arr[*kre_p];
}
}
code i did before adding pointer i dont seem to understand pointer that much.
#include<iostream>
using namespace std;
int main()
{
int kre_arr[10];
int *kre_p;
int i,j,temp;
cout<<"Enter 10 Integers: ";
for (int i=0; i < 10; i++)
{
cin>>kre_arr[i];
}
for(i=0;i<=10;i++)
{
for(j=i+1;j<=10;j++)
{
if(kre_arr[i] > kre_arr[j])
{
temp = kre_arr[i];
kre_arr[i] = kre_arr[j];
kre_arr[j] = temp;
}
}
}
for(i=0;i<=9;i++)
{
cout<<endl<<kre_arr[i];
}
}
Looking at what you are asked to do I think you just have to determine the highest and lowest int in the array and point to. You sort the array thats slower.
I think it should look like that:
#include<iostream>
using namespace std;
int main()
{
int kre_arr[10];
int *low;
int *high;
cout<<"Enter 10 Integers: ";
for (int i=0; i < 10; i++)
{
cin>>kre_arr[i];
}
//determine the lowest
low=&kre_arr[0];
for(int i=1;i<10;i++)
{
if(kre_arr[i] < *low)
{
low=&kre_arr[i];
}
}
//determine the highest
high=&kre_arr[0];
for(int i=1;i<10;i++)
{
if(kre_arr[i] > *high)
{
high=&kre_arr[i];
}
}
cout<<"lowest: "<<*low<<"\nhighest: "<<*high;
}
kre_p[k] = &kre_arr[k];
kre_arr is array.
kre_arr[k] is integer.
&kre_arr[k] is integer address ( similar int*)
kre_p is pointer.
kre_p[k] is integer.
So, as a result, you cannot pass directly int* to int.
I guess you want kre_p+k = &kre_arr[k]
Given the state of your code, I fear for your life... So, for your overal survival, and of course in the hopes that you will learn something:
Never use 'using namespace std'. It's bad form.
You are not allocating memory for your array of pointers (kre_p). That will cause your program to crash for sure.
You don't actually need an array of pointers. Your array elements can be conveniently referred to by their offset in the array.
You are doing what appears to be a bubblesort to find the lowest and highest value. That's incredibly inefficient, and completely unnecessary.
C++ can be such a nice language. It bothers me when teachers seem to think they should be teaching it in a form that's as ugly as possible. Consider:
#include <algorithm>
#include <array>
#include <iostream>
int main () {
std::cout << "Enter 10 Integers: ";
std::array<int, 10> kre_arr;
for (auto &Val : kre_arr)
std::cin >> Val;
const int Low = *std::min_element (kre_arr.begin (), kre_arr.end ());
const int High = *std::max_element (kre_arr.begin (), kre_arr.end ());
// The assignment calls for pointers, so let's not disappoint.
const int *LowPtr = &Low;
const int *HighPtr = &High;
}

Filling 2-D arrays from user input

I have a bit of a problem, I am writing a program to ask the user to enter numbers for a Sudoku grid, and then store them in a 2-d array. I know how to print out the array to show the Sudoku grid, But I am having trouble getting the array elements set to the numbers that the user enters, can anyone help?
This is all that I have, which I know is not much but I have only ever done this with 1-d arrays before.
Code:
#include <iostream>
using namespace std;
void fillGrid1(int grid1, int sizeOfArray) {
for(int x = 0; x < sizeOfArray; x++) {
grid1[x][9] = x;
}
}
int main()
{
int grid1[9][9];
fillGrid1(grid1, 9);
for(int row = 0; row < 9; row++) {
for(int column = 0; column < 9; column++) {
cout << grid1[row][column] << " ";
}
cout << endl;
}
}
Here you have two functions, one to interactively fill the hole sudoku by getting the user input. The other for printing the sudoku. With the little information you gave it's what I think you seek:
#include <iostream>
#include <stdio.h>
#include<stdlib.h>
using namespace std;
void interactiveSudokuFill(int grid1[9][9]){
for(int y=0;y<9;y++){
for(int x=0;x<9;x++){
string theString;
cout<<"Write the value to prace in Sudoku["<<y<<"]["<<x<<"] :"<<endl;
std::getline(cin,theString);
int nr=atoi(theString.c_str());
grid1[y][x]=nr;
}
}
}
void printSudoku(int grid[9][9]){
for(int y=0;y<9;y++){
for(int x=0;x<9;x++){
cout<<"["<<grid[y][x]<<"]";
}
cout<<endl;
}
}
int main()
{
int grid1[9][9];
interactiveSudokuFill(grid1);
printSudoku(grid1);
}
There are other more safe/elegant ways of doing this(for example user input should have been checked before delievering it to atoi()), but this way is the simpler I can think of.
Firstly, you're taking in an int where you expect an array:
void fillGrid1(int grid1, int sizeOfArray)
// ^^^^^^^^^
This should be something of the form,
void fillGrid1(int grid1[9][9], int sizeOfArray)
Next is that you should use a nested loop to access the elements of the multidimensional array:
void fillGrid1(int grid1[9][9], int sizeOfArray)
{
for (int i = 0; i < sizeOfArray; ++i)
{
for (int k = 0; k < sizeOfArray; ++k)
{
grid1[i][k] = x; // shouldn't x be the number the user entered?
}
}
}
You should also zero-fill your array:
int grid1[9][9] = {0};

Constructing Identity Matrix [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I have tried to solve an algorithm problem, I'm newbie and I'm trying to practice a lot in programming problems. So I wanted to construct an Identity matrix n*n. I came up with a stupid solution, that worked for a 4*4 matrix, but it didn't work with 5*5. I know that its weird solution and the solution to the problem is really easy when I looked at it. I need to know what did I do wrong so that I can learn, and is my solution is really stupid and I will be better in the future after working much in solving those kind of problems ?
#include <iostream>
#include <vector>
#include <sstream>
#include <iomanip> // for setw, setfill
using namespace std;
int binary(int number);
int main()
{
vector<vector<int> > matrix;
cout<<"Please enter the size of the identity matrix"<<endl;
int n;
cin>>n;
matrix.resize(n);
for (int i=0; i<n;i++)
{
matrix[i].resize(n);
}
int steps = 1<<n-1;
int bin = binary(steps);
ostringstream binString;
binString <<bin;
if(binString.str().size()<n)
{
std::string dest = binString.str();
int nPaddings = n-binString.str().size();
if (nPaddings==0) nPaddings=1;
dest = std::string( nPaddings, '0').append( binString.str());
binString.str("");
binString<<dest;
}
for (int col = 0; col<n; col++)
{
if(col>=1)
{
steps= (int)steps/2;
int bin = binary(steps);
binString.str("");
binString << bin;
if(binString.str().size()<n)
{
std::string dest = binString.str();
int nPaddings = n-steps;
if (nPaddings==0) nPaddings=1;
dest = std::string( nPaddings, '0').append( binString.str());
binString.str("");
binString<<dest;
}
}
for (int row=0; row<n; row++)
{
matrix[col][row] =binString.str().at(row)-'0';
}
}
return 0;
}
int binary(int number) {
long rem,i=1,sum=0;
do
{
rem=number%2;
sum=sum + (i*rem);
number=number/2;
i=i*10;
}while(number>0);
return sum;
}
There is a much simpler way to do it.
First, you should allocate your matrix with the specified size. Then, you know that only the diagonal is 1s:
vector<vector<int> > matrix;
int n;
cout << "Please enter the size of the identity matrix" << endl;
cin >> n;
// Initialize the matrix as a n x n array of 0.
matrix = vector<vector<int> >(n, vector<int>(n,0));
// Set the diagonal to be 1s
for(unsigned int t = 0; t < n; t++)
matrix[t][t] = 1;
You can see a live example here.
Edit:
Your error comes from this line:
int nPaddings = n-steps;
In fact, you're not using the size of dest to compute the padding, which is not correct. See here, I added some debug printfs to see the state of the variables. You can see that nPaddings == -3, hence the errors.
The idea you have:
for each column
get the representation of the column as a string
set the i-th value of the column as the i-th character of the string
So, here is a simpler program using your idea. Separating the code in several functions helps a lot. Also, std::ostringstream and std::string is just pure overkill here.
#include <iostream>
#include <vector>
#include <iomanip> // for setw, setfill
using namespace std;
std::string binStr(unsigned int exponent, unsigned int size);
int main()
{
vector<vector<int> > matrix;
cout<<"Please enter the size of the identity matrix"<<endl;
int n;
cin>>n;
// Initialize the matrix
matrix.resize(n);
for (int i=0; i<n;i++)
matrix[i].resize(n);
// Fill the matrix
for (int col = 0; col<n; col++)
{
std::string bin = binStr(n-col,n);
for (int row=0; row<n; row++)
matrix[col][row] = bin[row]-'0';
}
// Print the matrix and return
for(unsigned int y = 0; y < n; y++)
{
for(unsigned int x = 0; x < n; x++)
cout << "\t" << matrix[y][x];
cout << "\n";
}
return 0;
}
std::string binStr(unsigned int exponent, unsigned int size)
{
// You do not need a string stream (which is like using a bazooka to kill a fly...)
// Instead, just create a string of the required length
// 'str' will contain the binary representation of 2^exponent
std::string str(size,'0');
if(exponent <= size && exponent > 0)
str[size - exponent] = '1';
return str;
}
You can see it in action here.
#include <iostream>
#include <vector>
using namespace std;
vector<vector<int> > make_idty_matrix( int n )
{
vector<vector<int> > idty( n, vector<int>( n, 0 ));
for( int i = 0; i < n; ++i )
idty[i][i] = 1;
return idty;
}
int main()
{
vector<vector<int> > matrix = make_idty_matrix( 5 );
// your code here
// ...
return 0;
}