Create random matrix with size passed as arguments
up vote
0
down vote
favorite
I have a very simple question. The following code (with the purpose of creating a random matrix of ints) does not compile. Specifically the line
int m[nrow * ncol];
in the randomMatrix function causes the problem. It says the expression nrow*ncol has to be a constant value. I am new to C++ hence my attempts have been uncreative (e.g. putting 'const' in front of the arguments).
Can someone give me a hint?
Thank you.
#include "pch.h"
#include <iostream>
#include <ctime>
using namespace std;
int* randomMatrix(int nrow, int ncol) {
int m[nrow * ncol];
int index;
for (int col = 0; col < ncol; col++) {
for (int row = 0; row < nrow; row++) {
index = nrow * col + row;
m[index] = rand() % 2;
}
}
return m;
}
int main() {
const unsigned int nrow = 10;
const unsigned int ncol = 10;
int m[nrow * ncol];
m = randomMatrix(nrow, ncol);
print(m, nrow, ncol);
system("PAUSE");
}
c++ arrays
add a comment |
up vote
0
down vote
favorite
I have a very simple question. The following code (with the purpose of creating a random matrix of ints) does not compile. Specifically the line
int m[nrow * ncol];
in the randomMatrix function causes the problem. It says the expression nrow*ncol has to be a constant value. I am new to C++ hence my attempts have been uncreative (e.g. putting 'const' in front of the arguments).
Can someone give me a hint?
Thank you.
#include "pch.h"
#include <iostream>
#include <ctime>
using namespace std;
int* randomMatrix(int nrow, int ncol) {
int m[nrow * ncol];
int index;
for (int col = 0; col < ncol; col++) {
for (int row = 0; row < nrow; row++) {
index = nrow * col + row;
m[index] = rand() % 2;
}
}
return m;
}
int main() {
const unsigned int nrow = 10;
const unsigned int ncol = 10;
int m[nrow * ncol];
m = randomMatrix(nrow, ncol);
print(m, nrow, ncol);
system("PAUSE");
}
c++ arrays
Use astd::array, astd::vector, or putconstexprinstead ofconst. Don't return a temporary from a function (minrandomMatrix). Better yet, use iterators. Even better, use a standard algorithm with a lambda (for_eachin this case). But I guess you don't know about all of that yet.
– Nelfeal
Nov 8 at 10:32
I used vector. And you are right. I don't know nothing from what you mentioned. But I promise the next silly question will follow. But: Thanks.
– Mika Prouk
Nov 8 at 10:45
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a very simple question. The following code (with the purpose of creating a random matrix of ints) does not compile. Specifically the line
int m[nrow * ncol];
in the randomMatrix function causes the problem. It says the expression nrow*ncol has to be a constant value. I am new to C++ hence my attempts have been uncreative (e.g. putting 'const' in front of the arguments).
Can someone give me a hint?
Thank you.
#include "pch.h"
#include <iostream>
#include <ctime>
using namespace std;
int* randomMatrix(int nrow, int ncol) {
int m[nrow * ncol];
int index;
for (int col = 0; col < ncol; col++) {
for (int row = 0; row < nrow; row++) {
index = nrow * col + row;
m[index] = rand() % 2;
}
}
return m;
}
int main() {
const unsigned int nrow = 10;
const unsigned int ncol = 10;
int m[nrow * ncol];
m = randomMatrix(nrow, ncol);
print(m, nrow, ncol);
system("PAUSE");
}
c++ arrays
I have a very simple question. The following code (with the purpose of creating a random matrix of ints) does not compile. Specifically the line
int m[nrow * ncol];
in the randomMatrix function causes the problem. It says the expression nrow*ncol has to be a constant value. I am new to C++ hence my attempts have been uncreative (e.g. putting 'const' in front of the arguments).
Can someone give me a hint?
Thank you.
#include "pch.h"
#include <iostream>
#include <ctime>
using namespace std;
int* randomMatrix(int nrow, int ncol) {
int m[nrow * ncol];
int index;
for (int col = 0; col < ncol; col++) {
for (int row = 0; row < nrow; row++) {
index = nrow * col + row;
m[index] = rand() % 2;
}
}
return m;
}
int main() {
const unsigned int nrow = 10;
const unsigned int ncol = 10;
int m[nrow * ncol];
m = randomMatrix(nrow, ncol);
print(m, nrow, ncol);
system("PAUSE");
}
c++ arrays
c++ arrays
asked Nov 8 at 10:28
Mika Prouk
248213
248213
Use astd::array, astd::vector, or putconstexprinstead ofconst. Don't return a temporary from a function (minrandomMatrix). Better yet, use iterators. Even better, use a standard algorithm with a lambda (for_eachin this case). But I guess you don't know about all of that yet.
– Nelfeal
Nov 8 at 10:32
I used vector. And you are right. I don't know nothing from what you mentioned. But I promise the next silly question will follow. But: Thanks.
– Mika Prouk
Nov 8 at 10:45
add a comment |
Use astd::array, astd::vector, or putconstexprinstead ofconst. Don't return a temporary from a function (minrandomMatrix). Better yet, use iterators. Even better, use a standard algorithm with a lambda (for_eachin this case). But I guess you don't know about all of that yet.
– Nelfeal
Nov 8 at 10:32
I used vector. And you are right. I don't know nothing from what you mentioned. But I promise the next silly question will follow. But: Thanks.
– Mika Prouk
Nov 8 at 10:45
Use a
std::array, a std::vector, or put constexpr instead of const. Don't return a temporary from a function (m in randomMatrix). Better yet, use iterators. Even better, use a standard algorithm with a lambda (for_each in this case). But I guess you don't know about all of that yet.– Nelfeal
Nov 8 at 10:32
Use a
std::array, a std::vector, or put constexpr instead of const. Don't return a temporary from a function (m in randomMatrix). Better yet, use iterators. Even better, use a standard algorithm with a lambda (for_each in this case). But I guess you don't know about all of that yet.– Nelfeal
Nov 8 at 10:32
I used vector. And you are right. I don't know nothing from what you mentioned. But I promise the next silly question will follow. But: Thanks.
– Mika Prouk
Nov 8 at 10:45
I used vector. And you are right. I don't know nothing from what you mentioned. But I promise the next silly question will follow. But: Thanks.
– Mika Prouk
Nov 8 at 10:45
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
You are getting an error because arrays lengths must be known at compile time. You are writing code that creates an array, but the length is only known at runtime... This means the compiler doesn't know how much space to allocate and you have to do it yourself. You could use malloc when you create the array - but I wouldn't recommend it.
The best bet is to use std::vector instead of an array.
add a comment |
up vote
0
down vote
The size of an array in C++ must be fixed at compile time. You can not have it depend on non-const parameters.
You should not use C-style arrays in C++ unless there is a good reason for it. The idiomatic way if the length is unknown at compile-time is to use a std::vector:
#include<vector> // <- At top
std::vector<int> m(nrow*ncol); // Instead of int m[nrow*ncol]
The return type of your function should then just be std::vector<int>.
New contributor
eukaryota is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
You are getting an error because arrays lengths must be known at compile time. You are writing code that creates an array, but the length is only known at runtime... This means the compiler doesn't know how much space to allocate and you have to do it yourself. You could use malloc when you create the array - but I wouldn't recommend it.
The best bet is to use std::vector instead of an array.
add a comment |
up vote
0
down vote
accepted
You are getting an error because arrays lengths must be known at compile time. You are writing code that creates an array, but the length is only known at runtime... This means the compiler doesn't know how much space to allocate and you have to do it yourself. You could use malloc when you create the array - but I wouldn't recommend it.
The best bet is to use std::vector instead of an array.
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
You are getting an error because arrays lengths must be known at compile time. You are writing code that creates an array, but the length is only known at runtime... This means the compiler doesn't know how much space to allocate and you have to do it yourself. You could use malloc when you create the array - but I wouldn't recommend it.
The best bet is to use std::vector instead of an array.
You are getting an error because arrays lengths must be known at compile time. You are writing code that creates an array, but the length is only known at runtime... This means the compiler doesn't know how much space to allocate and you have to do it yourself. You could use malloc when you create the array - but I wouldn't recommend it.
The best bet is to use std::vector instead of an array.
edited Nov 8 at 11:57
answered Nov 8 at 10:37
Theo Emms
1475
1475
add a comment |
add a comment |
up vote
0
down vote
The size of an array in C++ must be fixed at compile time. You can not have it depend on non-const parameters.
You should not use C-style arrays in C++ unless there is a good reason for it. The idiomatic way if the length is unknown at compile-time is to use a std::vector:
#include<vector> // <- At top
std::vector<int> m(nrow*ncol); // Instead of int m[nrow*ncol]
The return type of your function should then just be std::vector<int>.
New contributor
eukaryota is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
up vote
0
down vote
The size of an array in C++ must be fixed at compile time. You can not have it depend on non-const parameters.
You should not use C-style arrays in C++ unless there is a good reason for it. The idiomatic way if the length is unknown at compile-time is to use a std::vector:
#include<vector> // <- At top
std::vector<int> m(nrow*ncol); // Instead of int m[nrow*ncol]
The return type of your function should then just be std::vector<int>.
New contributor
eukaryota is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
up vote
0
down vote
up vote
0
down vote
The size of an array in C++ must be fixed at compile time. You can not have it depend on non-const parameters.
You should not use C-style arrays in C++ unless there is a good reason for it. The idiomatic way if the length is unknown at compile-time is to use a std::vector:
#include<vector> // <- At top
std::vector<int> m(nrow*ncol); // Instead of int m[nrow*ncol]
The return type of your function should then just be std::vector<int>.
New contributor
eukaryota is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
The size of an array in C++ must be fixed at compile time. You can not have it depend on non-const parameters.
You should not use C-style arrays in C++ unless there is a good reason for it. The idiomatic way if the length is unknown at compile-time is to use a std::vector:
#include<vector> // <- At top
std::vector<int> m(nrow*ncol); // Instead of int m[nrow*ncol]
The return type of your function should then just be std::vector<int>.
New contributor
eukaryota is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
eukaryota is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered Nov 8 at 10:37
eukaryota
3919
3919
New contributor
eukaryota is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
eukaryota is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
eukaryota is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53205843%2fcreate-random-matrix-with-size-passed-as-arguments%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Use a
std::array, astd::vector, or putconstexprinstead ofconst. Don't return a temporary from a function (minrandomMatrix). Better yet, use iterators. Even better, use a standard algorithm with a lambda (for_eachin this case). But I guess you don't know about all of that yet.– Nelfeal
Nov 8 at 10:32
I used vector. And you are right. I don't know nothing from what you mentioned. But I promise the next silly question will follow. But: Thanks.
– Mika Prouk
Nov 8 at 10:45