My source code is below:
string** field = new string*[m]; //initialise a mxn Matrix=> field.
for (int i = 0; i < m; i++) { // we do this because the compiler does not know the ammount of memory required in advance hence use of poitners.
field[i] = new string[n];
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
cin >> &field[i][j];
}
}
I am having trouble trying to push the input from cin to my 2d matrix field. I have tried double dereferencing ie. &&field, but still get the same error. Any assistance would be greatly appreciated.
I initially wrote cin >> field[i][j] but got confused because intellisense said no operator matches operands of type std::istream and std::string. To solve that problem I had to import string The corrected code is below:
#include <string>;
//...
string** field = new string*[m]; //initialise a mxn Matrix=> field.
for (int i = 0; i < m; i++) { // we do this because the compiler does not know the ammount of memory required in advance hence use of poitners.
field[i] = new string[n];
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
cin >> field[i][j];
}
}
Related
whats the prob
#include <iostream>
using namespace std;
int main() {
int M; //height
int N; //length
cin >> M;
cin >> N;
int list[M][N], smaller[N];
string smaller_str;
for (int i = 0;i < M; ++i){
getline(cin, smaller_str);
for (int j = 0; j < N; i = i++) {
cin >> smaller_str[j];
}
list[i] = smaller;
}
}
i want to put 1D array "smaller" in to 2D array list
however i do get errors in the "list[i] = smaller;" part
i need help guys
Unless you want to check for errors, you won't need to use getline in this case. Just read each elements via >> operator.
Also note that Variable-Length Arrays like int list[M][N]; is not in the standard C++. You should use std::vector instead of that.
Another point is the the i = i++ in the inner loop is wrong. It should be ++j.
#include <iostream>
#include <vector>
using namespace std;
int main() {
int M; //height
int N; //length
cin >> M;
cin >> N;
//int list[M][N];
std::vector<std::vector<int> > list(M, std::vector<int>(N));
for (int i = 0;i < M; ++i){
for (int j = 0; j < N; ++j) {
cin >> list[i][j];
}
}
}
You cannot assign another arrays to an array, what you can do is copy/ move its content. For this, use std::copy,
...
int list[M][N], smaller[N];
string smaller_str;
for (int i = 0; i < M; ++i) {
getline(cin, smaller_str);
for (int j = 0; j < N; i = i++) {
cin >> smaller_str[j];
}
std::copy(smaller, smaller + N, reinterpret_cast<int*>(&list[i]));
}
Note: If you would like to move the content rather than copying, swap the std::copy with std::move.
using namespace std;
int main() {
int t;
cin >> t;
for (int j = 0; j < t; j++) {
int n;
cin >> n;
vector<string> cities;
for (int i = 0; i < n; i++) {
cin >> cities[i];
}
int size = cities.size();
for (int i = 0; i < n; i++) {
for (int k = 1; k < n; k++) {
if (cities[i] == cities[k]) size--;
}
}
cout << size << '\n';
}
}
So, this task is from kattis website(link: https://open.kattis.com/problems/everywhere) and the goal is to get the number of the cities someone has visited, the problem is that some of these cities were visited twice or even more, so the only thing we need is to get the number of cities.
That's how input looks like:
7
saskatoon
toronto
winnipeg
toronto
vancouver
saskatoon
toronto
3
edmonton
edmonton
edmonton
And my code doesn't let me input the whole input, I mean, I input 3 values and then program stops working. Can you help me?
You forgot to initialize your vector with a size. You can do it by passing n to the constructor:
vector<string> cities(n);
I have a 2D array that I want to assign the values of another array. I'm making a game of life simulator and have everything else working but this. My code is this:
for(int i = 0; i < ROWS; i++) {
for (int j = 0; i < COLS; j++) {
current[i][j] = next[i][j];
}
}
current and next are both bool's. I keep getting the error code EXC_BAD_ACCESS in X-Code. I'm unsure what I'm doing wrong
To re-iterate my comment, the innerloop compares i with COL rather than j. So
for(int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
current[i][j] = next[i][j];
}
}
should solve the issue
I need some help. I need to make a modular program. But I have a little trouble. So, I need to read a 2D array but user defines row and column.
After the reading I need to make some calculations in another functions... but i can't write well a function which works...
I have tried with pointers... but I can't use well. I'm beginner.
//main
int a[2500]; //symbolic. n<=50 -in my case
int n;
reading (a*,n);
//reading function
void reading(int* array[], int &n)
{
cout << "n=<<;
cin >> n;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n ;j++) {
cin >> array[i][j];
}
}
}
Please help me.
There are a number of problems with this code. The most glaring one is that C++ != Python, so you have to put the main code inside a function like this:
int main() {
//Main code here, calling other functions etc
reading (&a,n);
return 0; //or return 1 to signal there has been an error
}
From there, you can work on your code. The good thing about a modular design is that you can debug parts of it and be sure each part works. Use a debugger, and ask a question with a single question in on SO if you are still stuck after doing some research too.
i got a right solution ;)
//main
int **matrix, n;
matrixread(matrix,n);
//matrixread
void matrixread(int** &matrix, int &m)
{
cin >> m;
matrix = new int*[m];
for (i = 0; i < m; i++)
{
matrix[i] = new int[m];
}
for (i = 0; i < m; i++)
{
for (j = 0; j < m; j++)
{
cout << "matrix[" << i << "," << j << "]= " ;
cin >> matrix[i][j];
}
}
I keep getting the error Heap Corruption Detected. I have read through several questions on here, but I can't quite find out what is causing this in my code. I am trying to create a 2d array that will hold a matrix that is read from a text file.
// Create a 2d matrix to hold the matrix (i = rows, j = columns)
matrix = new int*[cols];
for(int i = 0; i <= cols; i++) {
matrix[i] = new int[rows];
}
// Populate the matrix from the text file
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
inputFile >> matrix[i][j];
}
}
My destructor is:
for(int i = 0; i <= cols; i++) {
delete[] matrix[i];
}
delete[] matrix;
I've tried debugging, but that does do much help in this case. Any suggestions?
matrix = new int*[cols];
for(int i = 0; i <= cols; i++) {
matrix[i] = new int[rows];
}
For an array with cols elements, the index is from 0 to cols - 1 inclusively.
The heap corruption will be detected when
delete [] matrix;
Since matrix[cols] write a position out of array bound.
UPDATE
As #DanielKO (thank you buddy :p) pointed out in the comment
there is a mismatch, the "Populate the matrix..." loop makes "i"
iterate over "rows" when it should be iterating over "cols".
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
inputFile >> matrix[i][j];
When you allocated you went from 0 to cols in i. Now you're changing i to be rows.
EDIT: Below would honor your commented row/column rules and follow RAII:
std::vector<std::vector<int>> matrix(rows, std::vector<int>(cols));
for( int i=0; i<rows; ++i ) {
for( int j=0; j<cols; ++j ) {
inputFile >> matrix[i][j];
}
}
// no need for delete matrix cleaned up when leaving scope.