Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 months ago.
The community reviewed whether to reopen this question 9 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I was expecting 1 2 3 as output, but when I try to run this code:
#include <iostream>
using namespace std;
int fun(int x){
if (x>0){
return fun(x-1);
cout<<x<<endl;
}
}
int main()
{
int x=3;
fun(x);
return 0;
}
I get this warning:
warning: control reaches end of non-void function
Why doesn't it return the value and call fun(x-1)?
But the below code works perfectly. I get 3 2 1 as output.
#include <iostream>
using namespace std;
int fun(int x){
if (x>0){
cout<<x<<endl;
return fun(x-1);
}
}
int main()
{
int x=3;
fun(x);
return 0;
}
Once a function has return'ed, it can't execute any more code:
if (x>0){
return fun(x-1);
cout<<x<<endl; // <-- NEVER EXECUTED
}
The warning is because your function has a non-void return type, but is not return'ing any value when x is <= 0, thus causing undefined behavior.
Try this instead:
#include <iostream>
using namespace std;
int fun(int x){
if (x>0){
int ret = fun(x-1);
cout << x << endl;
return ret;
}
return 0;
}
int main()
{
fun(3);
return 0;
}
Online Demo
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 5 years ago.
Improve this question
#include <iostream>
using namespace std;
//defining 9X9 grid.
int a[9][9] ={{0,0,3,0,9,2,6,0,0},
{1,0,0,3,0,0,8,0,0},
{0,0,5,0,1,0,0,4,0},
{0,3,0,0,0,0,2,5,8},
{2,4,0,0,5,0,0,0,0},
{0,0,0,6,2,0,0,0,3},
{0,1,4,0,0,9,0,3,0},
{6,0,0,7,0,0,1,0,0},
{3,0,0,0,0,4,0,0,2} };
// class sudoku.
class sudoku{
public:
int row,col,i,j,num;
//to check presence of element in particular row.
bool rowCheck(int a[9][9],int &row,int num)
{
for(j=0;j<9;j++)
{
if(a[row][j]==num)
return true;
}
return false;
}
//to check presence of element in particular column.
bool colCheck(int a[9][9], int &col, int num)
{
for(j=0;j<9;j++)
{
if(a[j][col]==num)
return true;
}
return false;
}
//to check presence of element in particular 3X3 grid.
bool boxCheck(int a[9][9],int &row ,int &col ,int num)
{
int x,y;
if(row<3)
x=0;
else if(row>=3 && row<6)
x=3;
else
x=6;
if(col<3)
int y=0;
else if(col>=3 && col<6)
y=3;
else
y=6;
for(i=x;i<x+3;i++)
{
for(j=y;j<y+3;j++)
{
if(a[i][j]==num)
return true;
}
}
return false;
}
//to check index which is unassigned.
bool unAssigned(int a[9][9],int &row,int &col)
{
for(row=0;row<9;row++)
{
for(col=0;col<9;col++)
{
if(a[row][col]==0){
return true;}
}
}
return false;
}
//to return true if position is suitable to insert .
bool isSafe(int a[9][9],int &row,int &col,int num)
{
if(!rowCheck(a,row,num) && !colCheck(a,col,num) &&
!boxCheck(a,row,col,num))
return true;
else
return false;
}
//function to solve sudoku.
bool sudokuSolver(int a[9][9])
{
if(!unAssigned(a,row,col))
return true;
for(i=1;i<=9;i++)
{
if(isSafe(a,row,col,i))
{
a[row][col]=i;
cout<<a[row][col];
if(sudokuSolver(a))
return true;
a[row][col]=0;
}
}
return false;
}
void display(int a[9][9])
{
for(i=0;i<9;i++)
{
for(j=0;j<9;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
}
//class ends
};
//main method
int main()
{
sudoku s;
s.sudokuSolver(a);
s.display(a);
return 0;
}
After calling: unAssigned(a,row,col) the value of row is 9 and the value of colis 9 when unAssigned() returns false. This is a consequence of using references to row and col.
bool unAssigned(int a[9][9],int &row,int &col)
{
for(row=0;row<9;row++)
{
for(col=0;col<9;col++)
{
if(a[row][col]==0){
return true;}
}
}
// here: row is 9 and col is 9
return false;
}
This means that you can return from sudokuSolver() with row and col out of bounds. This will trigger a segmentation fault in the following line:
if(sudokuSolver(a))
return true;
// here row or col are equal to 9 which is out of bounds
a[row][col]=0; // seg-fault here
You never initialize row and col which leads to undefined behaviour once you use their values.
Apart from that, I would suggest you to avoid hard coded array bounds and raw loops. If you use containers and iterators instead you can avoid out of bounds errors completely (not the problem here, but a line for(i=1;i<=9;i++) looks very suspicious and makes me think twice to realize that it is ok).
Moreover, dont pass by reference if the parameter is actually not modified by the method. E.g. bool colCheck(int a[9][9], int &col, int num) does not modify col, thus it is rather confusing why it takes col as reference. Also it is confusing that both row and col are members of the class but at the same time you pass them between the methods. I would suggest to rename the members to max_row and max_col, respectively.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
When i try to compile these errors shows up
line 5: [Error] expected initializer before 'void'
line 27: [Error] expected unqualified-id before '{' token
help me i need to send it to my doctor by 3 hours
#include<iostream>
using namespace std;
void ConvertToCelsuis (int temp)
void ConvertToFahernheit (int temp)
int main()
{
char ch;
int temp;
cout<<"enter a temprature followed by a charcter"<<endl;
cin>>temp>>ch>>endl;
if (ch=='f'|ch=='F')
ConvertToFahernheit (temp);
else if (ch=='c'|ch=='C')
ConvertToCelsuis (temp);
Return 0;
}
void ConvertToFahernheit (int temp)
{
float F;
}
{
F=(temp*1.8)+32;
cout<<F;
cout<<endl;
}
void ConvertToCelsuis (int temp)
{
float C;
{
C=((temp-32)*0.5);
cout<<C;
cout<<endl;
}
}
on these lines:
void ConvertToCelsuis (int temp)
void ConvertToFahernheit (int temp)
you are missing semicolons. After you fix this you will have to delve into other compilation problems, for eg:
cin>>temp>>ch>>endl;
That line is not going to work.
You need a semicolon at the end of your declaration.
void ConvertToCelsuis (int temp);
void ConvertToFahernheit (int temp);
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I keep getting errors when trying to compile the following code. The error is
expected ',' or ';' before '{' token.
It says there's an error on the parentheses after the bool check_row(x)
If I comment it out the same happens for bool check_col(x).
I kept looking back at my books if I didn't define my functions properly but they seem correct, logically.
This is the beginning of an nQueens game on a 4x4 board.
The Queen is represented by the number 1.
The two boolean functions are to check if the row and columns are free.
startGame() assigns 0 to all boxes, and showBoard() shows results of the board.
#include <cstdlib>
#include <iostream>
using namespace std;
int x=0, y=0;
int square[4][4];
void startGame()
{
for(x=0;x<4;x++)
{
for(y=0;y<4;y++)
{
square[x][y]=0;
}
}
}
void showBoard()
{
for(int x=0;x<4;x++)
{
if(x!=0)
{
cout<<endl;
}
for(int y=0;y<4;y++)
{
cout<<square[x][y];
}
}
cout<<endl;
}
bool check_row(x)
{
for(y=0;y<4;y++)
{
if(square[x][y]==1)
{
return false;
}
else if(square[x][y]==0)
{
if(y==3)
{
return true;
}
continue;
}
}
}
bool check_col(y)
{
for(int x=0;x<4;x++)
{
if(square[x][y]==1)
{
return false;
}
else if(square[x][y]==0)
{
if(x==3)
{
return true;
}
continue;
}
}
}
int main(){
startGame();
showBoard();
return 0;
}
bool check_col(y) isn't a valid prototype. You need to provide a type for y - for example bool check_col(int y). The same applies to bool check_row(x).
you have to specify the datatype which you are passing as a parameter in your function..considering x and y are of int type and are local, your function prototype should be
bool check_row(int x)
bool check_col(int y)
If x and y are global then there is no need to pass them..simply
bool check_row()
bool check_col()
will work as the visibility of global variables will be throughout the program, unless shadowed
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am writing this simple code of linear searching using recursion, but still getting some problem.
Always getting false as return value.
The code is as follows:
main()
{
int arr[10] = {12,13,75,23,6,45,90,41,61,56};
int i, j, num;
cout<<" Find number :- ";
cin>>num;
bool check = find(arr, num, 0, 10);
cout<<" check = "<<check<<endl;
system("PAUSE");
return 0;
}
bool find(int arr[], int key, int first, int size)
{
if(first > size)
return false;
else
{
if(arr[first] == key)
return true;
else
find(arr, key, first+1, size);
}
}
Try to add return before to launch the function
bool find(int arr[], int key, int first, int size)
{
if(first > size)
return false;
else
{
if(arr[first] == key)
return true;
else
return find(arr, key, first+1, size);
}
}
Two things:
if(first > size)
return false;
This should be >= not >, or else you'll end up trying to access past the bounds of the array, below.
if(arr[first] == key)
return true;
else
find(arr, key, first+1, size);
Here, you need to remember to explicitly return the value of find(). Otherwise you'll just get a garbage return value instead (which coincidentally happens to be false).
I think it's simple enough
first of all you have to check till 9th index only because the index starts from 0 and ends at 9.
otherwise it may cause run-time error.
take care of declaration of functions and and header files.