expected expression in C++? - c++

#include <iostream>
using namespace std;
void num7()
{
int numRows;
cin >> numRows;
for (int x = 0; x < numRows/2; x++) {
for (int y = 0; y <= x; y++) {
cout << "*";
}
cout << "\n";
}
float numRowsfloat = numRows;
double cos = numRowsfloat / 2;
int tan = numRowsfloat / 2;
double sin = tan;
if (cos == sin)
cout << "\n";
for (int x = 0; x < numRows/2; x++) {
for (int y = numRows/2; y >0; y--) {
cout << "*";
}
}
else
for (int x = 0; x < numRows/2+1; x++) {
for (int y = x; y >0; y--) {
cout << "*";
}
cout << "\n";
}
}
In the else column, it says expected expression.
This is trying to make a triangular shape. like
*
**
***
***
**
*
for inputed 6
or
*
**
***
**
*
for inputed 5

You problem is this:
if (cos == sin)
cout << "\n";
for (int x = 0; x < numRows/2; x++) {
for (int y = numRows/2; y >0; y--) {
cout << "*";
}
}
Here only the cout is part of the if statement. The loop is not. You need to add braces around the whole block.

You forgot the braces for the if statement. try this:
if (cos == sin) {
cout << "\n";
for (int x = 0; x < numRows/2; x++) {
for (int y = numRows/2; y >0; y--) {
cout << "*";
}
}
} else
for (int x = 0; x < numRows/2+1; x++) {
for (int y = x; y >0; y--) {
cout << "*";
}
cout << "\n";
}

Related

Don't know what is wrong with my gaussian filter

I'm trying to implement a program to detect borders of an image and the first step I'm taking is applying a gaussian filter to remove noise. The problem is that the filter that I'm applying only makes the image darker and doesn't blur it as it is supposed to.
This is the code that I haVe at the moment:
void gaussFilter(C_Image &imagen, float desviation) {
float kernel[5][5];
float sumaKernel = 0;
float s = desviation * desviation;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
float xaxis = (1 / sqrt(2 * M_PI * s)) * pow(exp(-pow(i, 2)) / (2 * s), 1);
float yaxis = (1 / sqrt(2 * M_PI * s)) * pow(exp(-pow(j, 2)) / (2 * s), 1);
float gaussianFilter = xaxis * yaxis;
kernel[i][j] = gaussianFilter;
sumaKernel += kernel[i][j];
}
}
cout << "Suma kernel: " << sumaKernel << endl;
//Normaizamos el kernel;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
kernel[i][j] /= sumaKernel;
}
}
C_Image::IndexT rowN, colN, colorsN;
C_Image::BMPFileInfo("barbara_gray.bmp", rowN, colN, colorsN);
//filterCreation(kernel, 10);
for (int x = 0; x < 5; x++)
{
for (int y = 0; y < 5; y++)
{
if (y != 4) {
printf("%f|", kernel[x][y]);
}
else
{
printf("%f|\n", kernel[x][y]);
}
}
}
cout << "Numero de columnas: " << colN << "\nNumero de filas: " << rowN << "\nColores de la paleta: " << colorsN << endl;
float suma;
for (int x = 1; x <= imagen.LastRow(); x++)
{
for (int y = 1; y <= imagen.LastCol(); y++)
{
suma = 0;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
suma += imagen(x, y) * kernel[i][j];
}
}
imagen(x, y) = suma;
}
}
}
And this is my main:
int main(int argc, char** argv)
{
char const* path = "barbara_gray.bmp";
C_Image imagen;
imagen.ReadBMP(path);
gaussFilter(imagen, 10);
char const* destinationPath = "result_image.bmp";
imagen.WriteBMP(destinationPath);
}
C_Image is a class given.

How to print a hollow square?

I used a two dimensional for loop to make a shape producing function for a class assignment but I couldn't get it to make a hollow square; only a filled one. I pass in two arguments the width and the symbol being used.
I've played around with it several times but I eventually just turned it in with one incompletion.
It should print something like this
++++
+ +
+ +
++++
#include <iostream>
#include "shapemaker.h"
using namespace std;
void shapemaker::Initialize(int w, int h, char sym)
{
width = w;
height = h;
symbol = sym;
}
int shapemaker::getcanWidth() {return width;}
int shapemaker::getcanHeight() {return height;}
char shapemaker::getSymbol() {return symbol;}
void shapemaker::setSymbol(char s) { symbol = s; }
void shapemaker::setcanWidth(int w) { width = w; }
void shapemaker::setcanHeight(int h) { height = h; }
void shapemaker::drawmidHorline()
{
symbol = getSymbol();
int drawingheight = getcanHeight();
int drawingwidth = getcanWidth();
double midpoint = getcanHeight()/2;
for(int x = 0; x < drawingwidth; x++)
{
for(int y = 0; y < drawingwidth; y++)
{
if(x == midpoint)
cout << getSymbol();
}
cout << endl;
}
}
void shapemaker::drawmidVertline()
{
symbol = getSymbol();
int drawingheight = getcanHeight();
int drawingwidth = getcanWidth();
double midpoint = getcanWidth()/2;
for(int x = 0; x < drawingwidth;x++)
{
for(int y = 0; y < drawingheight; y++)
{
if(y == midpoint)
{
cout << getSymbol();
}
cout << " ";
}
cout << endl;
}
}
void shapemaker::drawWidthsizedFullSquare()
{
symbol = getSymbol();
int drawingwidth = getcanWidth();
for(int x = 0; x < drawingwidth;x++)
{
cout << symbol;
for(int y = 0; y < drawingwidth; y++)
{
cout << symbol;
}
cout << endl;
}
}
void shapemaker::drawWidthsizedOpenSquare()
{
symbol = getSymbol();
int drawingwidth = getcanWidth();
for(int x = 0; x < drawingwidth;x++)
{
cout << getSymbol();
for(int y = 0; y < drawingwidth; y++)
{
if(x == 0 || x == drawingwidth-1)
cout << getSymbol();
}
cout << getSymbol();
cout << endl;
}
}
If you want drawWidthsizedOpenSquare to print this
++++
+ +
+ +
++++
you should write this method like:
void shapemaker::drawWidthsizedOpenSquare()
{
symbol = getSymbol();
int drawingwidth = getcanWidth();
for(int x = 0; x < drawingwidth;x++)
{
for(int y = 0; y < drawingwidth; y++)
{
if(x == 0 || x == drawingwidth-1 || y == 0 || y == drawingwidth-1) // border cell case
{
cout << getSymbol();
}
else
{
cout << " ";
}
}
cout << endl;
}
}

To break nested loop [duplicate]

This question already has answers here:
Can I use break to exit multiple nested 'for' loops?
(23 answers)
Closed 4 years ago.
I want to break the inner loop and increase the value of x if I find something at Matrix[x][y] but if I break in the y for loop it will just increase the value at y loop and not in the x loop.
int matrix[5][5]; //let's assume there are values inside
for(int i = 0; i < 5; i++)
{
for(int j = 0; j<5;j++)
{
int radius = 1;
for(int x = radius - 1; x <= radius + i; x ++)
{
for(int y = radius -1; y <= radius +j; y++)
{
if((x+i) >= 0 && (y+j)>=0)
{
//for example I find the number 45 in the matrix, then the search radius should be increased by 1 and start it over again.
if(matrix[x][y] == 45)
{
radius++;
break; // but if i break here the x value wont be changed
}
}
}
}
}
Of non-local exit mechanisms C++ provides throwing:
try {
for(int x = 0; x < 10; ++x) {
for(int y = 0; y < 3; ++y) {
std::cout << x << ", " << y << std::endl;
if(y == 1 && x == 3) throw true;
}
}
}
catch(bool) {}
std::cout << std::endl;
Additionally you can modify the outer loop's variable so the final condition holds:
for(int x = 0; x < 10; ++x) {
for(int y = 0; y < 3; ++y) {
std::cout << x << ", " << y << std::endl;
if(y == 1 && x == 3) {
x = 10;
break;
}
}
}
std::cout << std::endl;
(Or, if you don't want to modify the variable itself, say, it's not local to the loop and you'll need its value afterwards, add one more flag to the loop condtion:
bool continueOuter{true};
for(int x = 0; continueOuter && x < 10; ++x) {
for(int y = 0; y < 3; ++y) {
std::cout << x << ", " << y << std::endl;
if(y == 1 && x == 3) {
continueOuter = false;
break;
}
}
}
)

change x,y of board by user input

i have a board of 10x10 and here his code:
for(int x = 0; x < 10; x++) // X
{
cout << 0;
for(int y = 0; y < 10; y++) // Y
{
cout << " " << 0;
}
cout << endl;
}
now I want to change 0 to 1 in the x,y location by user input.
how can I do that?
here is simply what I want to do ( in pictures ):
the user input is x = 2, y = 2 and the table changing from Table 1 Example to Table 2 Example ( as a new table ):
TABLE 1 Example | TABLE 2 Example
its just a curiosity question that I've been trying to make.
Get x0 and y0 before the loop using cin>> x0 and cin>> y0.
Inside the loop
if(x == x0 && y==y0)
cout << " " << 1;
else
cout << " " << 0;
The best way would be to keep all values in a two dimensional array. Then, play around with the
values of that array. After than, write the whole array once to the screen.
See the example below :
int matrix[10][10];
for(int i = 0; i < 10; i++)
for(int j = 0; j < 10; j++) matrix[i][j]=0;
matrix[2][3]=1;
matrix[3][4]=1;
matrix[4][5]=1;
for(int i = 0; i < 10; i++){
for(int j = 0; j < 10; j++){
cout<<matrix[i][j];
}
cout << endl;
}
you can print again the whole matrix and check the x,y pair before writing the 0 or the 1
for(int x = 0; x < 10; x++) // X
{
cout << 0;
for(int y = 0; y < 10; y++) // Y
{
//here verify the x,y against the user input
if(x == xUser && y == yUser)
{
cout << " " << 1;
} else{
cout << " " << 0;
}
}
cout << endl;
}

Move elements in a multidimensional array

I'm keeping track of a character's position on a game board through the use of a multidimensional array (board[10][20]). To allow for user movement, I have created a method, movePlayer(), that modifies the value of the index of where 'G' is located.
Whenever I do this, the character 'G' does move, but the previous location of 'G' remains on the gameboard, so there are two 'G's. My question is: How can I move an element (G) in a multidimensional array?
Main Function:
char userInput;
int main()
{
Game obj1;
cout << "New Game (y/n)" << endl;
cin >> userInput;
if(userInput == 'y')
{
obj1.gameBoard();
obj2.movePlayer();
}
}
Game(Class).cpp:
Game::Game()
{
for(int x = 0; x < 10 ; x++)
{
for(int y = 0; y < 20 ; y++)
{
board[x][y]= '.';
}
}
player = 'G';
treasure = 'X';
srand(time(0));
p_Pos1X = rand()%10;
p_Pos1Y = rand()%20;
t_Pos1X = rand()%10;
t_Pos1Y = rand()%20;
endSwitch = 0;
}
void Game::gameBoard()
{
printBoard(p_Pos1X,p_Pos1Y);
}
void Game::printBoard(int px, int py)
{
for(int x = 0; x < 10; x++)
{
for(int y = 0; y < 20 ; y++)
{
board[px][py] = player;
board[t_Pos1X][t_Pos1Y] = treasure;
cout << board[x][y] ;
}
cout << endl;
}
}
void Game:: movePlayer()
{
cin >> playerM;
switch(playerM)
{
case 'W':
case 'w':
movePlayerUp(p_Pos1X);
}
}
void Game::movePlayerUp(int m)
{
m = m - 1;
printBoard(m,p_Pos1Y);
}
If the objetive of the project is not more than a dots matrix and a G reaching a X you dont neds to store a matrix, of course following your approach the code below I hope to be the solution the change is in the printBoard function
Game::Game()
{
for(int x = 0; x < 10 ; x++)
{
for(int y = 0; y < 20 ; y++)
{
board[x][y]= '.';
}
}
player = 'G';
treasure = 'X';
srand(time(0));
p_Pos1X = rand()%10;
p_Pos1Y = rand()%20;
t_Pos1X = rand()%10;
t_Pos1Y = rand()%20;
endSwitch = 0;
}
void Game::gameBoard()
{
printBoard(p_Pos1X,p_Pos1Y);
}
void Game::printBoard(int px, int py)
{
for(int x = 0; x < 10; x++)
{
for(int y = 0; y < 20 ; y++)
{
if(x==px && y==py)
{
cout << player ;
}else if(x== t_Pos1X && y== t_Pos1Y ){
cout << treasure;
}else{
cout << board[x][y] ;
}
}
cout << endl;
}
}
void Game:: movePlayer()
{
cin >> playerM;
switch(playerM)
{
case 'W':
case 'w':
movePlayerUp(p_Pos1X);
}
}
void Game::movePlayerUp(int m)
{
m = m - 1;
printBoard(m,p_Pos1Y);
}
Why not just put a '.' in the player's position just before moving him to the new one?