Populating a two dimensional vector C++ - c++

I'm working on a project for school to create a minesweeper game.
#include <iostream>
#include <cstdlib>
#include <vector>
using namespace std;
int mines;
int rows;
int columns;
vector<vector<int> > mineField;
int main() {
cout << "Input number of rows: ";
cin >> rows;
cout << "Input number of columns: ";
cin >> columns;
cout << "Input number of mines: ";
cin >> mines;
int mine = 0;
int num_of_mines = 0;
vector<int> temp;
while(num_of_mines < mines){
mine = rand()%(rows * mines);
if(mineField[mine][mine] != 1){
temp.push_back(1);
mineField.push_back(temp);
num_of_mines++;
}
}
return 0;
}
What i'm trying to do here is populate the minefield using a two dimensional vector and then trying to output it to see if the mines were randomly allocated. (1 <=> mines); but this keeps giving me an error.
Thank you so much for any help!

As others have stated, your problem is that you don't properly allocate your mineField matrix. Easiest would be to add
for (int i = 0; i < rows; ++i) {
mineField.push_back(vector<int>(columns, 0)); // init everything to zeroes
}
Now you can populate the mine field with mines. Some pointers on generating mines: at the moment you only randomize one coordinate, but you should randomize two. Also, I'm not entirely sure why you need to multiply by mines. I would do something like:
while (num_of_mines < mines) {
minex = rand() % columns;
miney = rand() % rows;
if (mineField[minex][miney] != 1)
...

Your if statement looks after something that does not exist because of the way mine is computed (you look after the size of the matrix not after the size of a line and a column)
(you do not correctly init the minefield and do not use columns)

Related

Some test cases aren't passing

#include <iostream>
#include <vector>
int i=0; //points at the current stack that we are working with
int box=0; //no. of boxes held by the crane
int64_t H; //max. height of the stacks given in the que.
int main()
{
int n, value; //storing no. of stacks and creating an additional variable value to store operations
std::cin>> n >> H;
int64_t arr[n]; //storing the no. of boxes each stack has in an array
std::vector<int> arr2; //storing the operations we have to perform in a vector
for(int j=0; j<n; j++){std::cin>> arr[j];} //getting arr
while(std::cin>>value) //getting arr2
{
arr2.push_back(value);
}
for(int xy=0; xy<n; xy++){if(arr[xy]>H){return 0;}} //ensuring that all stacks have no.of boxes less than max. height
if(arr2.size()<1 || arr2.size()>10e5 || n<1 || n>10e5 || H<1 || H>10e8){return 0;} //constraints given in the que.
int k=0; //creating a variable to keep count of how many programs we have already executed
while(k<arr2.size()){
if(arr2[k] == 1){MoveLeft();}
else if(arr2[k]==2){MoveRight(n);}
else if(arr2[k]==3){PickBox(arr, i);}
else if(arr2[k]==4){Dropbox(arr, i);}
else if(arr2[k]==0){k=arr2.size();}
k++;
}
for(int j=0; j<n; j++){std::cout<< arr[j] << " ";} //printing the arr after executing the code
return 0;
}
This is a question from a past year ZCO. And the above code is what I wrote to solve the prob.
The four functions Moveleft, MoveRight, Pickbox, Dropbox have been defined in the same file but aren't shown here because I think there's no issue with them.
When I submit the code, all test cases passed except 2. I don't know what is the problem with my code. Pls help me.
I have tried my best to make the code readable. Sorry if the code looks messy.
With the method you're trying to define an array with a user-input length is unfortunately invalid in C++.
But fortunately, there are basically two methods use to allocate arrays dynamically.
Method 1: Using Vectors
Vector is an important part of C++. It has a lot of features (e.g. its size don't need to be defined static unlike a normal array does, can redefine array size, etc.) An example's given:
#include <iostream>
#include <vector>
int main(void) {
std::vector<int> vArray; // vector<> declaration
int size = 0;
int getInput = 0;
std::cout << "Enter an array size: ";
std::cin >> size;
for (int i = 0; i < size; i++) {
std::cout << "Enter a value: ";
std::cin >> getInput;
vArray.push_back(getInput); // inserts one+ container and data in it
}
for (int i = 0; i < vArray.size(); i++) {
// retrieving contained data...
std::cout << vArray[i] << std::endl;
}
return 0;
}
Method 2: Using 'new' Keyword with Pointed Variable
The simple use of new will help you to achieve your requirement. It's less recommended since already there's concept of vectors which actually works efficiently than arrays. Let's take a look into a simple program:
#include <iostream>
int main(void) {
int *pArray;
int size;
std::cout << "Enter an array size: ";
std::cin >> size;
pArray = new int[size]; // initializing array with dynamic size
for (int i = 0; i < size; i++) {
std::cout << "Enter value: ";
std::cin >> pArray[i];
}
for (int i = 0; i < size; i++) {
std::cout << pArray[i] << std::endl;
}
delete[] pArray;
return 0;
}
Both are nice options to work with, but it's recommended by most using vector<>.

C++ - Reading table of numbers from file into 2D Array (only last row stored) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
Starting with an input file that looks like this:
2 3
2 3 4
4 3 2
I am trying to read this data into a 2D array in C++ (the first row specifying number of rows / cols).
My code currently looks like:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream fin;
fin.open ("dataset.in");
// a matrix
int a_numrows;
int a_numcols;
int a[a_numrows][a_numcols];
fin >> a_numrows >> a_numcols;
cout << a_numrows << " " << a_numcols << endl;
for (int i = 0; i<a_numrows; i++)
{
for (int j = 0; j<a_numcols; j++)
{
fin >> a[i][j];
}
}
cout << a[0][0] << endl;
fin.close();
return 0;
}
However it seems as though in each row of the 2D array, the last row is being stored. Thus when a[0][0] is outputted, it returns 4. This behavior is not how I think things should work coming from other languages.
You must permute these lines:
int a[a_numrows][a_numcols];
fin >> a_numrows >> a_numcols;
to
fin >> a_numrows >> a_numcols;
int a[a_numrows][a_numcols];
I guess this is a mistake of inattention.
That's said, there are safer/better ways to declare/use 2D arrays. Here is a possible example:
#include <fstream>
#include <iostream>
#include <iterator>
#include <vector>
int main()
{
std::ifstream fin("dataset.in");
size_t n_rows, n_cols;
fin >> n_rows >> n_cols;
using T = int;
std::vector<T> array(n_rows * n_cols);
array.assign(std::istream_iterator<T>(fin), std::istream_iterator<T>());
fin.close();
//-----
for (size_t i = 0; i < n_rows; i++)
{
for (size_t j = 0; j < n_cols; j++)
{
std::cout << array[i * n_cols + j] << "\t";
}
std::cout << endl;
}
return 0;
}
Output:
g++ reader.cpp; ./a.out
2 3 4
4 3 2
Remember that when doing numerical computations it is generally better to store all the numbers into a contiguous memory chunk (like it is done in std::vector). In this situation it is easier for the compiler to vectorize your code.
To access components use:
[i*n_cols+j]: row-major (C-style) -> the given example,
more efficient to loop in this order: for i { for j ... } }
[j*n_rows+i]: column-major (Fortran-style) -> compatible with Blas & Lapack,
more efficient to loop in this order for j { for i ... } }
To declare an array in C++, the size has to be known at compile time. I.e. you can't pass a_numrows and a_numcols as array-dimensions as these are runtime values. For such an approach I would use a std::vector:
vector<vector<int>> a;
//... read a_numrows and a_numcols
a.resize(a_numrows); //resize creates #a_numrows empty columns
for(int i = 0; i < a_numrows; ++i)
{
for(int j = 0; j < a_numcols; ++j)
{
int value; fin >> value;
a[i].push_back(value); //access the ith row and add a new column with value inside
}
}

Reading the specific data from the file using Vector function C++

I am a new in C++ and have difficulties in importing specific data (numbers) from the file.
My input looks like:
Open High Low Close
1.11476 1.11709 1.10426 1.10533
1.10532 1.11212 1.10321 1.10836
1.10834 1.11177 1.10649 1.11139
1.09946 1.10955 1.09691 1.10556
1.10757 1.11254 1.09914 1.10361
1.10359 1.12162 1.10301 1.11595
1.09995 1.10851 1.09652 1.10097
I use the following code which works fine for me to read the second column entirely, however I need to read specific data only. For example the third row/ third column which is 1.10649How can I read specific data? Do I need to use the string to get the row/column and then convert it to int in order to read it in a vector? I am open for any suggestions and would be greatly appreciated if any could help me with this issue.
// Data import 2nd Column
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <vector>
using namespace std;
int main()
{
const int columns = 4;
vector< vector <double> > data;
ifstream market_data("market_data.txt");
if (market_data.is_open()) {
double num;
vector <double> line;
while (market_data >> num) {
line.push_back(num);
if (line.size() == columns) {
data.push_back(line);
line.clear();
}
}
}
vector <double> column;
double col = 2;
for (double i = 0; i < data.size(); ++i) {
column.push_back(data[i][col - 1]);
cout << column[i] << endl;
}
system ("pause");
return 0;
}
You need to use a integer value for indexing (size_t to be precise), change
for (double i = 0; i < data.size(); ++i) {
to
for( size_t i = 0; i < data.size(); ++i) {
// ^^^^^^
Otherwise everything seems fine from your code sample.
If your numbers will always contain 7 characters (i assume it's not binary file), then you could make this simple.
Use seekg() method of ifstream.
Each number fills 10 characters (7 of number, 3 spaces). So, if you have table ROWS x COLUMNS, then to get specific number, you can do this:
const int ROW_LEN = 4
const int DATA_LEN = 10
...
int row,column;
double num;
std::cin >> row; //assume first row is 0
std::cin >> column //assume first column is 0
marked_data.seekg((column*ROW_LEN + row)*DATA_LEN);
marked_data >> num // here is your number
Thank you for replies.. I have solved the issue. So instead of:
vector <double> column;
double col = 2;
for (double i = 0; i < data.size(); ++i) {
column.push_back(data[i][col - 1]);
cout << column[i] << endl;
}
enough to write:
cout << data[2][2] << endl;

Having Trouble With A Simple C++ Program

I'm creating this very simple C++ program.
the program asks the user to enter a few integers and stores them in an array.but when a specific integer(for example 50)is entered,the input is ended and then,all of the integers are displayed on the screen except for 50.
for example:
input:
1
2
88
50
output:
1
2
88
the error i'm getting is when i use cout to print the array,all of numbers are shown,including 50 and numbers i did'nt even entered.
this is my code so far:
#include<iostream>
int main() {
int num[100];
for(int i=0;i<=100;i++) {
cin >> num[i];
if (num[i]!=50) break;
}
for(int j=0;j<=100;j++) {
cout << num[j] << endl;
}
return 0;
}
Change the program the following way
#include<iostream>
int main()
{
const size_t N = 100;
int num[N];
size_t n = 0;
int value;
while ( n < N && std::cin >> value && value != 50 ) num[n++] = value;
for ( size_t i = 0; i < n; i++ ) std::cout << num[i] << std::endl;
return 0;
}
Here in the first loop variable n is used to count the actual number of entered values. And then this variable is used as the upper bound for the second loop.
As for your program then the valid range of indices for the first loop is 0-99 and you have to output only whose elements of the array that were inputed.
A do while loop is more suitable for your problem. The stop condition will check if the number fit inside the array (if k is not bigger than 100) and if number entered is 50.
#include<iostream>
using namespace std;
int main() {
int num[100];
int k = 0;
// A do while loop will be more suitable
do{
cin >> num[k++];
}while(k<100&&num[k-1]!=50);
for (int j = 0; j < k-1; j++) {
cout << num[j] << endl;
}
return 0;
}
Also, a better solution to get rid of 100 limitation is to use std::vector data structure that automatically adjust it's size, like this:
vector<int> num;
int temp;
do {
cin >> temp;
num.push_back(temp);
} while (temp != 50);
Note, you can use temp.size() to get the number of items stored.
You read up to 101 numbers, but if you enter 50 you break the loop and go for printing it. In the printing loop you go through all 101 numbers, but you actually may have not set all of them.
In the first loop count in a count variable the numbers you read until you meet 50 and in the printing loop just iterate count-1 times.
You have allocated an array of 100 integers on the stack. The values are not initialized to zero by default, so you end up having whatever was on the stack previously appear in your array.
You have also off-by-one in both of your loops, you allocated array of 100 integers so that means index range of 0-99.
As the question is tagged as C++, I would suggest that you leave the C-style array and instead use a std::vector to store the values. This makes it more flexible as you don't have to specify a fixed size (or manage memory) and you don't end up with uninitialized values.
Little example code (requires C++11 compiler):
#include <iostream>
#include <vector>
int main()
{
std::vector<int> numbers; // Store the numbers here
for(int i = 0; i < 100; ++i) // Ask a number 100 times
{
int n;
std::cin >> n;
if( n == 50 ) // Stop if user enters 50
break;
numbers.push_back(n); // Add the number to the numbers vector
}
for (auto n : numbers) // Print all the values in the numbers vector
std::cout << n << std::endl;
return 0;
}
There are just 2 changes in your code check it out :
int main()
{
int num[100],i; //initialize i outside scope to count number of inputs
for(i=0;i<100;i++) {
cin >> num[i];
if (num[i]==50) break; //break if the entered number is 50
}
for(int j=0;j<=i-1;j++)
{
cout << num[j] << endl;
}
return 0;
}
Okay, others already pointed out the two mistakes. You should use i < 100 in the loop conditions instead of i <= 100 and you have to keep track of how many elements you entered.
Now let me add an answer how I think it would be better.
#include <iostream>
#include <vector>
int main() {
std::vector<int> numbers; // Create an empty vector.
for (int temp; // a temp variable in the for loop.
numbers.size() < 100 && // check that we have less than 100 elements.
std::cin >> temp && // read in the temp variable,
// and check if the read was a success.
temp != 50) // lastly check that the value we read isn't 50.
{
numbers.push_back(temp); // Now we just add it to the vector.
}
for (int i = 0; i < numbers.size(); ++i)
std::cout << numbers[i]; // Now we just print all the elements of
// the vector. We only added correct items.
}
The above code doesn't even read anymore numbers after it found 50. And if you want to be able to enter any number of elements you just have to remove the check that we have less than 100 elements.
Now I commented the above code a bit much, if you compress it it'll reduce to just:
#include <iostream>
#include <vector>
int main() {
std::vector<int> numbers; // Create an empty vector.
for (int temp; numbers.size() < 100 && std::cin >> temp && temp != 50)
numbers.push_back(temp);
for (int i = 0; i < numbers.size(); ++i)
std::cout << numbers[i];
}
If you can use the C++11 standard it reduces to:
#include <iostream>
#include <vector>
int main() {
std::vector<int> numbers; // Create an empty vector.
for (int temp; numbers.size() < 100 && std::cin >> temp && temp != 50)
numbers.push_back(temp);
for (int element : numbers)
std::cout << element;
}
for (auto element : numbers) is new, it basically means for every int 'element' in 'numbers'.

Passing 2D array Random Number Generator into main with C++

Hi guys I'm very new to C++ and was wondering if you guys could help me. Right now I'm just going by the book and what the teacher told me to do so some of the stuff might look different.
What I want to do is have my void random generator put numbers into my 2D array and then it goes into main. Then I have it pass through into my display function but for some reason I can't get it work right. Can you guys help me out?
edit: Ok I figured that it has something to do with my random number generator not putting the numbers into the array but not sure why. Since my number generator works find with 1D Array.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;
//Globral Varaibles Must be on top
const int max = 100;
const int min = 1;
const int COL = 4;
const int Rows = 3;
//Functions
void Population(int Array[][COL], int size);
void Show(const int Array[][COL], int max);
int main()
{
int a[3][4];
Population(a, Rows);
Show(a, Rows);
}
void Population( int Array[][COL], int size)
{
for (int index = 0; index < Rows; index++)
{
for (int Count = 0; Count < COL; Count++)
{
unsigned seed = time(0);
Array[index][Count] = (rand() % (max - min + 1)) + min;
}
}
}
void Show(const int a[][COL], int Rows)
{
for (int i = 0; i < Rows; i++)
{
for (int J = 0; J < COL; J++)
{
cout << setw(4) << a[i][J] << endl;
}
}
cout << endl;
cout << endl;
}
You seem to be seeding your random number generator every time you enter your second for loop in population(). You should only seed a random number generator once in a program, near the beginning.
Try removing your seed line from population() and instead using
srand(time(NULL));
at the beginning of your main()
AH, I figured out the problem. I had been mistaking what was being showing as one giant column instead of it being divided into rows and columns.
I had to have a small space after the a[i][J] part so that it could be divided into rows and columns.
cout << setw(4) << a[a][J] << " ";
That_Knight_Guy thanks for the suggestion. Now my generator finally puts out random numbers.