Im trying to build a minesweeper game and i keep getting a compiling error: lvalue required as left operand of assignment. only on these two lines:
#include <iostream>
#include <ctime>
using namespace std;
// ------------------------------------------------------
// class Cell
// represents one grid element in the Minesweeper game
// ------------------------------------------------------
class Cell {
public:
Cell();
void print();
void peek();
void unittest();
void setMined(bool);
bool getMined();
void setAdj(int);
private:
bool covered;
bool marked;
bool mined;
int adjcount;
};
// ------------------------
// functions for class Cell
// ------------------------
Cell::Cell(){
covered = true;
marked = false;
mined = false;
adjcount = 0;
// cout << "Creating a Cell" << endl;
}
void Cell::setAdj(int n){
adjcount = n;
}
bool Cell::getMined(){
return mined;
}
void Cell::setMined(bool b){
mined = b;
}
void Cell::print(){
if (marked) cout << " L ";
else {
if (covered) cout << " ? ";
else{
if (mined) cout << " # ";
else if (adjcount == 0) cout << " _ ";
else cout << " " << adjcount << " ";
}
}
}
void Cell::peek(){
if (mined) cout << " # ";
else if (adjcount == 0) cout << " _ ";
else cout << " " << adjcount << " ";
}
void Cell::unittest(){
print(); cout << endl;
covered = false;
print(); cout << endl;
adjcount = 4;
print(); cout << endl;
mined = true;
print(); cout << endl;
covered = true;
print(); cout << endl;
marked = true;
print(); cout << endl;
}
// -------------------------------------
// class Board
// this class represents a 2 dimensional
// array of Cell objects for the game
// of minesweeper
//--------------------------------------
class Board{
public:
Board();
void print();
void peek();
void adjacencycount();
void mixMined();
private:
static const int rows = 5;
static const int cols = 5;
Cell cells [rows][cols];
int mines;
};
// --------------------------
// functions for class Board
// --------------------------
Board::Board(){
mines = 6;
for(int i = 0; i < 1 ; i++){
for(int j = 0; j < mines; j++){
cells[i][j].setMined(true);
}
}
}
void Board::mixMined(){
int shuffle = 1000;
for(int i = 0; i < shuffle; i++){
int r1 = (rand()%rows);
int c1 = (rand()%cols);
int r2 = (rand()%rows);
int c2 = (rand()%cols);
if(r1 && c1 != r2 && c2){
bool temp = cells[r1][c1].getMined();
cells[r1][c1].getMined() = cells[r2][c2].getMined();
cells[r2][c2].getMined() = temp;
}
}
}
void Board::adjacencycount(){
for( int i = 0; i < rows; i++){
for( int j = 0; j < cols; j++){
if(!cells[i][j].getMined()){
int count = 0;
if (i-1 >= 0 && j-1 >= 0 && cells[i-1][j-1].getMined()) count++;
if (i-1 >= 0 && cells[i-1][j].getMined()) count++;
if (i-1 >= 0 && j+1 <= cols-1 && cells[i-1][j+1].getMined()) count++;
if (j-1 >= 0 && cells[i][j-1].getMined()) count++;
if (j+1 <= cols-1 && cells[i][j+1].getMined()) count++;
if (i+1 <= rows-1 && j-1 >= 0 && cells[i+1][j-1].getMined()) count++;
if (i+1 <= rows-1 && cells[i+1][j].getMined()) count++;
if (i+1 <= rows-1 && j+1 <= cols-1 && cells[i+1][j+1].getMined()) count++;
cells[i][j].setAdj(count);
// cout << count; -- for testing purposes
}
}
}
}
void Board::print(){
for (int i = 0; i < rows; i++){
for (int j = 0; j < cols; j++){
cells[i][j].print();
}
cout << endl << endl;
}
}
void Board::peek(){
for (int i = 0; i < rows; i++){
for (int j = 0; j < cols; j++){
cells[i][j].peek();
}
cout << endl << endl;
}
}
// -------------------------
// main function for testing
// -------------------------
int main(void) {
//Cell c;
//c.unittest();
srand(time(0));
Board b;
b.mixMined();
b.adjacencycount();
b.peek();
return 0;
}
I'm trying to get my cells to swap, so that the mines would randomize every new game. Ive searched around and couldn't find a solution to this. I added "==" but that function isn't going to do what i want it to.
++EDIT++ I'm sorry it did state lvalue required, i missed typed that
minesweeper.cpp: In member function ‘void Board::mixMined()’:
minesweeper.cpp:130: error: lvalue required as left operand of assignment
minesweeper.cpp:131: error: lvalue required as left operand of assignment
Thats the error that occurs.
I think getMined() is actually something like this:
bool getMined()
So you are trying to assign to rValue which is not possible
You might want to write some function like:
void setMined(bool m) and the use it like:
cells[r1][c1].setMined( cells[r2][c2].getMined() );
Related
I used a 'bubble-sort' for my C++ program, but it introduces random '0' values in array in a Fractional Greedy Program
int sorteaza()
{
int aux,schimb,i;
do
{
schimb=0;
for (i=0;i<=n;++i)
if (G[i][3]<G[i+1][3])
{
swap(G[i], G[i+1]);
}
}
while (schimb);
}
This is my entire code:
#include<iostream>
using namespace std;
int n; // Numarul de elemente
float G[100][3]; // Obiecte + detalii masa profit potenta
int masa = 0;
int read_data()
{
cout << "Greutatea Rucsac" << endl;
cin >> masa;
cout << "Obiecte: " << endl;
cin >> n;
for(int i = 1; i<=n;i++)
{
for(int j = 1; j<=2;j++)
{
cin >> G[i][j];
if(G[i][1] != 0 && G[i][2] != 0)
{
G[i][3] = G[i][2] / G[i][1];
}
}
}
}
// 2 500
// 4 500
int sorteaza()
{
int aux,schimb,i;
do
{
schimb=0;
for (i=0;i<=n;++i)
if (G[i][3]<G[i+1][3])
{
swap(G[i], G[i+1]);
}
}
while (schimb);
}
int verify()
{
for(int i = 1; i<=n;i++)
{
for(int j = 1; j<=3;j++)
{
cout << G[i][j];
cout << endl;
//G[i][3] = G[i][1] / G[i][2];
}
}
}
int greedy()
{
float profit = 0;
int i = 1;
int aux;
while(i<=n && masa>=0)
{
//cout << "G[i][1]: " << G[i][1] << endl;
if(masa>=G[i][1]) {
//cout << "Am ajuns aici";
profit=profit+G[i][2];
masa=masa-G[i][1];
}
else {
//cout << "Am ajuns dincolo";
aux= (masa*100)/G[i][1];
profit = profit + (aux * G[i][2])/100;
break;
}
i++;
}
cout << profit;
}
int main()
{
read_data();
sorteaza();
verify();
// greedy();
}
Learn to index all your arrays from zero.
float G[100][3];
Legal indexes are 0 to 99 and 0 to 2. So this code should be
for (int i = 0; i < n; i++)
{
for (int j = 0; j < 2; j++)
{
cin >> G[i][j];
}
if (G[i][0] != 0 && G[i][1] != 0)
{
G[i][2] = G[i][1] / G[i][0];
}
}
and this code should be
if (G[i][2] < G[i+1][2])
{
swap(G[i], G[i+1]);
}
All your arrays start at zero. I'm sure you've been told this, but you have to start putting it into practise.
In general, write your for loops like this
for (int i = 0; i < N; ++i)
That's the correct loop for an array of size N.
You probably need <n instead of ≤n (that's where the uninitialized value i.e. 0 comes from). And you miss one loop in the bubble sort. Right now you're only bubbling the smallest element to the end of the list.
Also no idea what you're doing with that schimb and while condition.
Furthermore you're defining G as float[100][3] so you can't use G[i][3], only G[i][2].
int sorteaza()
{
int i,j;
for (i=0; i<n; i++)
{
for (j=i+1; j<n; j++)
{
if (G[i][2] < G[j][2])
{
swap(G[i], G[j]);
}
}
}
}
Ok, so I was doing a tiny project for school and I can't find the answer anywhere to why this small change in code makes it finish in no time when number m gets higher. Look at the variable "k" I change it from int to long.
I'm trying to find the longest sequence in the Collatz sequence between 1 and 1000000
void lengstaRuna() {
cout << "Hæsta tala?:";
int m;
cin >> m;
int lengstaRuna = 0;
int talaLengstuRunu = 0;
int k;
for(int i = 2; i < m; i++) {
int lengd = 1;
k = i;
while(k != 1) {
if(k % 2 == 0) {
k = k/2;
} else {
k = k*3 +1;
}
lengd++;
}
if(lengd > lengstaRuna) {
lengstaRuna = lengd;
talaLengstuRunu = i;
}
}
cout << "Lengsta runa: " << lengstaRuna << endl;
cout << "Tala lengstu runu: " << talaLengstuRunu << endl;
}
void lengstaRuna() {
cout << "Hæsta tala?:";
int m;
cin >> m;
int lengstaRuna = 0;
int talaLengstuRunu = 0;
long k;
for(int i = 2; i < m; i++) {
int lengd = 1;
k = i;
while(k != 1) {
if(k % 2 == 0) {
k = k/2;
} else {
k = k*3 +1;
}
lengd++;
}
if(lengd > lengstaRuna) {
lengstaRuna = lengd;
talaLengstuRunu = i;
}
}
cout << "Lengsta runa: " << lengstaRuna << endl;
cout << "Tala lengstu runu: " << talaLengstuRunu << endl;
}
The question is simple: Why does it run so much faster when input m==1000000?
I see what's happening here. Basically, above certain value for your input, the int is overflowing since you are doing k*3.
I modified your code to check this (see below). Upto input value of around 113000, the max your 'k' has to hold is 1570824735 (close to INT_MAX 2147483647). Anything 114000 or above, 'k' overflows and the code goes into uncharted territory. That problem doesn't happen when you use long of course.
./a.out 113000
j: 1570824735
Lengsta runa: 354
Tala lengstu runu: 106239
#include <iostream>
#include <string>
using namespace std;
void lengstaRuna(int m) {
int lengstaRuna = 0;
int talaLengstuRunu = 0;
int k;
long j = 0;
for(int i = 2; i < m; i++) {
int lengd = 1;
k = i;
while(k != 1) {
if(k % 2 == 0) {
k = k/2;
} else {
if (k*3 > j)
j = k*3;
k = k*3 +1;
}
lengd++;
}
if(lengd > lengstaRuna) {
lengstaRuna = lengd;
talaLengstuRunu = i;
}
}
cout << "j: " << j << endl;
cout << "Lengsta runa: " << lengstaRuna << endl;
cout << "Tala lengstu runu: " << talaLengstuRunu << endl;
}
int main (int ac, char** av) {
std::string::size_type sz;
lengstaRuna(std::stoi(av[1]));
}
I am trying to use the BFS method to search through my graph and then determine if there is a path between my two nodes. I understand it and can implement it with a linked list, but I am just having trouble grasping it with a matrix instead.
I believe I am going wrong in my looping section, I feel like I am iterating on the wrong thing, or maybe comparing the wrong values. Thanks for any help.
Here is the code I have:
#include <iostream>
#include <queue>
#include <string.h>
#include <stack>
#include <list>
using namespace std;
class Graph{
private:
int total_routes;
int total_stations;
public:
int AdjMatrix[100][100];
Graph(int routes, int stations);
void addRoute(int from, int to, int weight);
void printGraph();
bool isRoute(int from, int to);
};
Graph::Graph(int routes, int stations)
{
for(int i = 0; i < stations; i++){
for(int j=0; j < stations; j++){
AdjMatrix[i][j]=0;
}
}
total_routes = routes;
total_stations = stations;
}
void Graph::printGraph(){
cout << "\n" << endl;
for(int i = 0; i < total_stations; i ++){
for(int j = 0; j < total_stations; j++){
cout << " " << AdjMatrix[i][j];
}
cout << endl;
}
}
void Graph::addRoute(int from, int to, int weight){
AdjMatrix[from][to] = weight;
}
bool Graph::isRoute(int from, int to){
bool route = false;
bool visited[total_stations] = {false};
queue<int> verticies;
if (from == to){
cout << "Going into if its the same node statement" << endl;
return true;
}
visited[from] = true;
verticies.push(from);
cout << "Testing if there is a route from " << from << " To " << to << endl;
while(!verticies.empty() && route == false ){
int current;
current = verticies.front();
verticies.pop();
cout << "Going into for Loop, with a current value of " << current << endl;
for ( int i = AdjMatrix[current][0]; i < total_stations ; i++ ){
if (i == to ){
route = true;
break;
}
if ( visited[i] == false){
visited[i] = true;
verticies.push(i);
}
}
}
return route;
}
int main() {
Graph newGraph(2,10); //10 stations(Nodes), 2 routes
newGraph.addRoute(0,1,10); //Route from 1 to 2, with a weight of 10.
newGraph.addRoute(2,9,1); //Route of 2 to 9, with a weight of 1.
newGraph.printGraph();
bool answer = newGraph.isRoute(3,9); //Should say no route...
if (answer){
cout << "There is a route!" << endl;
}
else{
cout << "There is no route!" << endl;
}
return 0;
}
It would be better to initialize AdjMatrix[i][j] with NULL
Then you can do
for ( int i = 0; i < total_stations ; i++ ){
if (AdjMatrix[current][i]!=NULL)
{
if (i == to ){
route = true;
break;
}
if ( visited[i] == false){
visited[i] = true;
verticies.push(i);
}
}
You should iterate over this way
for ( int i = 0; i < total_stations ; i++ )
and check whether
visited[i] == false && AdjMatrix[current][i] != 0
to push new vertices into the queue.
Can someone please help me. I am struggling to find in my code why the last value in column B always gets incremented by one. I have written some code since its an assignment due today. I also cant figure out why the last value in column B is not equal to 196 because in the reset function it sets all the values in the array to 196 . Any suggestion would be appreciated. Thank you in advance
#include <iostream> //includes cin cout
#include <iomanip>
using namespace std; //setting up the environment
const int NUMBER_OF_ROWS = 3;
const int NUMBER_OF_COLUMNS = 3;
void printAllSeats(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]);
void reset(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]);
void askForUsersSeat(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS], int &SeatCountNumber, bool &anyFreeSeats);
bool isFull(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]);
bool isEmpty(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]);
int main() { //main starts
int maxSeats;
int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS];
int SeatCountNumber = 0;
bool anyFreeSeats;
reset(seats);
anyFreeSeats = true;
SeatCountNumber = 0;
while (anyFreeSeats) {
printAllSeats(seats);
askForUsersSeat(seats, SeatCountNumber, anyFreeSeats);
}
system("pause");
return 0;
} //main ends
void printAllSeats(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]) {
cout << endl;
cout << setw(10) << " - = Available R = Reserved\n\n";
for (int i = 0; i <= NUMBER_OF_ROWS; i++) {
cout << setw(15) << i << " ";
for (int j = 0; j < NUMBER_OF_COLUMNS; j++) {
if (i == 0) {
cout << " " << static_cast<char>(j + 65) << " ";
} else {
cout << " " << static_cast<char>(seats[i][j]) << " ";
}
}
cout << endl;
}
cout << endl;
}
void reset(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]) {
//set all values in array to 196
for (int i = 0; i <= NUMBER_OF_ROWS; i++) {
for (int j = 0; j <= NUMBER_OF_COLUMNS; j++) {
seats[i][j] = 196;
}
}
}
void askForUsersSeat(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS], int &SeatCountNumber, bool &anyFreeSeats) {
int seatChoiceNumber;
char seatChoiceLetter;
int letter;
int maxSeats = NUMBER_OF_ROWS * NUMBER_OF_COLUMNS;
cout << "Seat (Row, Column): ";
cin >> seatChoiceNumber >> seatChoiceLetter;
letter = static_cast<int>(toupper(seatChoiceLetter));
if (((letter >= 65) && (letter < (65 + NUMBER_OF_COLUMNS))) && ((seatChoiceNumber > 0) && (seatChoiceNumber <= NUMBER_OF_ROWS))) {
if (seats[(seatChoiceNumber)][(letter - 65)] == 82) {
} else {
seats[(seatChoiceNumber)][(letter - 65)] = 82;
SeatCountNumber++; //this changes last value in column B for some reason
if (SeatCountNumber < maxSeats) {
anyFreeSeats = true;
}
else if (SeatCountNumber > maxSeats) {
printAllSeats(seats);
anyFreeSeats = false;
}
}
} else {
}
}
I kind of cleaned up the code a bit. It seems you found your answer in the comments, so I just did some indentation. Try and eliminate whitespaces in your code (mind you, the one I am putting here is not perfect either, but you get the point). Clean and easy to read code doesn't only make it better for you, but as you get higher up in the industry and other people begin reading and working on your code, having clean and easy to read code really helps :)
#include <iostream> //includes cin cout
#include <iomanip>
using namespace std; //setting up the environment
const int NUMBER_OF_ROWS = 3;
const int NUMBER_OF_COLUMNS = 3;
void printAllSeats(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]);
void reset(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]);
void askForUsersSeat(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS], int &SeatCountNumber, bool &anyFreeSeats);
bool isFull(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]);
bool isEmpty(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]);
int main()
{
int maxSeats;
int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS];
int SeatCountNumber = 0;
bool anyFreeSeats;
reset(seats);
anyFreeSeats = true;
SeatCountNumber = 0;
while (anyFreeSeats)
{
printAllSeats(seats);
askForUsersSeat(seats, SeatCountNumber, anyFreeSeats);
}
system("pause");
return 0;
} //main ends
void printAllSeats(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS])
{
cout << endl;
cout << setw(10) << " - = Available R = Reserved\n\n";
for (int i = 0; i <= NUMBER_OF_ROWS; i++)
{
cout << setw(15) << i << " ";
for (int j = 0; j < NUMBER_OF_COLUMNS; j++)
{
if (i == 0)
{
cout << " " << static_cast<char>(j + 65) << " ";
}
else
{
cout << " " << static_cast<char>(seats[i][j]) << " ";
}
}
cout << endl;
}
cout << endl;
}
void reset(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS])
{
//set all values in array to 196
for (int i = 0; i <= NUMBER_OF_ROWS; i++)
{
for (int j = 0; j <= NUMBER_OF_COLUMNS; j++)
{
seats[i][j] = 196;
}
}
}
void askForUsersSeat(int seats[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS], int &SeatCountNumber, bool &anyFreeSeats)
{
int seatChoiceNumber;
char seatChoiceLetter;
int letter;
int maxSeats = NUMBER_OF_ROWS * NUMBER_OF_COLUMNS;
cout << "Seat (Row, Column): ";
cin >> seatChoiceNumber >> seatChoiceLetter;
letter = static_cast<int>(toupper(seatChoiceLetter));
if (((letter >= 65) && (letter < (65 + NUMBER_OF_COLUMNS))) && ((seatChoiceNumber > 0) && (seatChoiceNumber <= NUMBER_OF_ROWS)))
{
if (seats[(seatChoiceNumber)][(letter - 65)] == 82)
{
}
else
{
seats[(seatChoiceNumber)][(letter - 65)] = 82;
SeatCountNumber++; //this changes last value in column B for some reason
if (SeatCountNumber < maxSeats)
{
anyFreeSeats = true;
}
else if (SeatCountNumber > maxSeats)
{
printAllSeats(seats);
anyFreeSeats = false;
}
}
}
else {
}
}
Note: Some more whitespaces could even come out but I generally like to have spaces after certain statements (personal preference).
i am trying to do simple sets intersection , it works correctly , but when output comes , it shows only memory address or garbage random value , help me ,i applied breakpoints , but it's not working . actually i am new in C++
#include<iostream>
using namespace std;
class set
{
private:
int size;
int *elem;
public:
set()
{
size = 0;
elem = NULL;
}
set(int s);
~set();
set(set &s);
set intersection(set A, int z);
void inputset();
void outputset();
};
set::set(int s)
{
size = s;
elem = new int[s];
}
void set::inputset()
{
int i;
cout << "Enter the set Element" << endl;
for(i = 0 ; i < size ; i++)
{
cin >> elem[i];
}
}
set set::intersection(set A, int z)
{
int i, j, k = 0;
set R(z);
for(i = 0; i < size; i++)
{
for(j = 0; j < A.size; j++)
{
if(elem[i] == A.elem[j])
{
R.elem[k] = A.elem[j];
k++;
break;
}
}
}
return R;
}
set::~set()
{
delete []elem;
}
set::set(set &s)
{
int i;
if(size > 0)
{
delete []elem;
}
size = s.size;
elem = new int[size];
for(i = 0; i < size; i++)
{
elem[i] = s.elem[i];
}
}
void set::outputset()
{
int i;
cout << "The elements of new set is : " << endl;
for(i = 0; i < size; i++)
{
cout << elem[i] << endl;
}
cout << endl;
}
int main()
{
int x, y, z;
char choice;
cout << "Enter sizeof set A" << endl;
cin >> x;
set S1(x);
S1.inputset();
S1.outputset();
cout << "Enter sizeof set B" << endl;
cin >> y;
set S2(y);
S2.inputset();
S2.outputset();
z = x + y;
set S3(z);
cout << "Enter I for intersection" << endl << "Enter U for union" << endl << "Enter D for difference" << endl;
cin >> choice;
switch(choice)
{
case'I':
S3 = S1.intersection(S2, z);
S3.outputset();
break;
default:
cout << "Invalid entry";
}
return 0;
}
There are several ways to check intersections between two sets, if your sets are in arrays, I'd suggest looping one of them and have a check if it's in between the other:
for (int i = 0; i < element1_size; i++) {
for (int u = 0; u < element2_size; u++) {
if (element1[i] == element2[u]) {
cout << "Intersection point : " << element1[i] << endl;
}
}
}
Your copy ctor should be set(const set &s); instead of set(set &s); What you have is a ctor that accepts a reference to set, which is not a copy ctor. So compiler generates default one for you and you have issue returning object by value from method intersection()
Also please fix your code indent, it is not easy to read it.
Also remove following lines from your "copy ctor":
if(size>0)
{
delete []elem;
}
You cannot delete that pointer, your object is not constructed yet.
How about using a map to keep track of the "intersected" elements in much more efficient way. Here is a snippet of what I have in mind:
std::map<int, int> m;
for(int i = 0; i < size; ++i)
m.insert( std::pair<int, int>(elem[i], 1) );
for(int i = 0; i < A.size; ++i)
if(m.count(A[i]) > 0)
R.elem[k++] = A[i]
This way you avoid the nested for-loops, and will be able to compute the intersection with logarithmic time complexity.