Hey guys please help me on this I have tried calling my bool function in my main func but it wont even show the first cout of program and the compiler terminates the program here is my code
#include <iostream>
using namespace std;
bool puzzle(int size, int array[], int start)
{
cout <<"how many blocks you want for the puzzle? \n";
cin >> size;
for (int i = 0; i < size; i++)
{
cout << "enter your numbers in order for the blocks:\n";
cin >> array[i];
if (array[0] > size) { return false; };
if (array[0] == size) { return true; };
}
}
int main()
{
puzzle;
return 0;
}
Your function has parameters so you need to call them to make it work. In this case (an example):
int size = 5;
int array[5];
int start = 0;
puzzle(size, array, start);
Related
The Function won't initiate can someone help? When I run it in the debugger program skips over function and I don't know why?
#include <iostream>
using namespace std;
int size_array= 0;
int *data_array;
void sorting(int *[], int);
int main()
{
cout<<"enter in array size \n";
cin>>size_array;
int *data_array=new int(size_array);
for(int i=0;i<size_array;i++)
{
cout<<"enter number "<<i+1<<endl;
cin>>data_array[i];
}
**int sorting(int data_array, int size_array);**
for (int i=0; i<size_array;i++)
{
cout<<data_array[i]<<endl;
}
return 0;
}
The marked code is simply declaring the function, not calling it.
Also, your data_array is a pointer to a single int whose value is initialized as size_array. But you want an array of size_array number of ints instead.
Try this:
#include <iostream>
using namespace std;
void sorting(int[], int);
int main()
{
int size_array = 0;
cout << "enter in array size \n";
cin >> size_array;
int *data_array = new int[size_array];
for(int i = 0; i < size_array; i++)
{
cout << "enter number " << i+1 << endl;
cin >> data_array[i];
}
sorting(data_array, size_array);
for (int i = 0; i < size_array; i++)
{
cout << data_array[i] << endl;
}
delete[] data_array;
return 0;
}
void sorting(int data_array[], int size_array)
{
// sort data_array as needed...
}
I am a C++ noob and I have just started learning it, and one of my assignments is to print a solution to the N-Queens problem, where the board would be N*N depending on user input. My IDE keeps showing me errors I don't understand in my code, even though to me it looks good and fine.
#include <iostream>
#include <array>
#include <stdexcept>
using namespace std;
int N;
bool safe(char board[N][N], int row, int col)
{
//checks if it's safe to place a queen
//doesn't give me any errors
}
bool placeQueen(char board[N][N], int col)
{
for (int i = 0; i < N; i++)
{
if ( safe(board, i, col) )
// says there is no matching function to call safe
{
board[i][col] = 1;
if ( placeQueen(board, col + 1) ){
//says cannot initialize parameter of type char(*)[*]
//with an Ivalue of type char(*)[N]
return true;
}
board[i][col] = 0;
}
}
return false;
}
void printAnswer(char board[N][N]){
//prints the final answer
}
int main()
{
int i, j;
try{
cout << "Enter the number of queens: ";
cin >> N;
char board[N][N];
for (int i = 0; i < N; i++){
for (int j = 0; i < N; i++){
board[i][j] = '.';
}
}
if ( placeQueen(board, 0) == false )
//no matching function to call placeQueen
{
throw runtime_error("Solution does not exist.");
return 0;
}
printAnswer(board);
//no matching function to call printAnswer
}
catch (runtime_error& excpt){
cout << excpt.what();
}
return 0;
}
It's probably me just being stupid but help would be appreciated, thanks!
char board[N][N] is not C++ when N is not a compile time constant. It's an extension by gcc that really shouldn't be on by default.
You are not defining functions that take (C style) arrays of arrays of char, but instead they take something that isn't defined in standard C++, and behaves differently to how it would in C.
You should instead define some other type as your board, e.g. using Board = std::vector<std::vector<char>>;. Then you can pass (references to) this type around.
#include <iostream>
#include <vector>
using Board = std::vector<std::vector<char>>;
bool safe(const Board & board, int row, int col)
{
//checks if it's safe to place a queen
//doesn't give me any errors
}
bool placeQueen(Board & board, int col)
{
for (int i = 0; i < N; i++)
{
if (safe(board, i, col) )
{
board[i][col] = 1;
if ( placeQueen(board, col + 1) ){
return true;
}
board[i][col] = 0;
}
}
return false;
}
void printAnswer(const Board & board){
//prints the final answer
}
int main()
{
std::cout << "Enter the number of queens: ";
int N;
std::cin >> N;
Board board{N, {N, '.'}}; // Initialise N vectors of N '.'
if (!placeQueen(board, 0))
{
std::cout << "Solution does not exist.";
return 0;
}
printAnswer(board);
return 0;
}
I am trying to convert a decimal number to binary number using stacks and I have to use structs.
Now from my understanding of structs, we can have member functions in them. And stack follows a LIFO strategy.
We can create a stack by making a struct, declaring some members, and have some functions to initialize and work on these members.
So, I tried declaring a struct with above mentioned things and my understanding, but I still do not seem to get the concept right. I believe it is an error on my part. But reading forums still does not help as we have not been taught classes yet and on every forum, structs and classes are intermixed.
Here is my code so far, any help and guidance regarding the concept and logic would be much appreciated.
#include<iostream>
using namespace std;
struct bin{
int num[15];
int ci;
void init()
{
ci = 0;
for (int i = 0;i < 15;i++)
num[i] = -1;
}
void push(int n)
{
num[ci] = n;
ci++;
}
int pop()
{
int temp = num[--ci];
num[ci] = -1;
return temp;
}
};
int main()
{
int inp, count = 0;
bin var;
cout << "Enter a decimal number to convert into binary: ";
cin >> inp;
while (inp != 0)
{
int rem = inp % 2;
cout << "rem= " << rem << endl;
inp /= 2;
cout << "inp= " << inp << endl;
var.push(rem);
count++;
}
cout << "\nYour binary is: ";
while (count != 0)
{
cout << var.pop();
count--;
}
return 0;
}
I tried my best to find the mistakes but couldn't. So,in the end I simply used arrays and implemented the code as follows
#include<iostream>
using namespace std;
void push(int bin[], int n, int &ci);
void init(int bin[], int &count, int &ci);
int pop(int bin[], int &ci);
void display(int bin[], int &count, int &ci);
void findBinary(int bin[], int &count, int&ci, int &inp);
int main()
{
int inp, count, ci;
int bin[20];
char c = '\0';
init(bin,count,ci);
cout << "Enter a decimal number to convert into binary: ";
cin >> inp;
findBinary(bin, count, ci, inp);
display(bin, count, ci);
return 0;
}
void init(int bin[], int &count, int &ci)
{
count = 0;
ci = 0;
for (int i = 0;i < 20;i++)
bin[i] = -1;
}
int pop(int bin[], int &ci)
{
int temp = bin[--ci];
return temp;
}
void push(int bin[], int n, int &ci)
{
bin[ci] = n;
ci++;
}
void display(int bin[], int &count, int &ci)
{
cout << "\nYour binary is: ";
while (count != 0)
{
cout << pop(bin, ci);
count--;
}
cout << endl;
}
void findBinary(int bin[], int &count, int&ci, int &inp)
{
while (inp != 0)
{
int rem = inp % 2;
inp /= 2;
push(bin, rem, ci);
count++;
}
}
So my questions are:
1. When we write a function within a struct, does the function runs when we create a struct type object?
2. Is the method I used to implement stack using struct in the first example correct?
When you first create an object from a struct or class, the constructor runs - but no other function (so not your init function). If you want that to run, you need to call it explicitly:
bin var;
var.init();
In terms of the implementation of your functions: you should use bounds-checking to ensure that you don't push more items than you can handle, or pop more than are in the queue. Besides that I can't see much wrong with them. Consider using a std::vector instead of an array, but as you're just starting to learn about C++, you'll likely cover that later in your course.
By the time you're adding constructors to a struct though, you may as well switch to using a class. In C++, structs and classes are equivalent (with the minor tweak that the default access modifier in a struct is public, whereas in classes it's private), so don't let that put you off.
why don't you do the problem in this way ,I fixed it just like this .
#include
using namespace std;
#define MAX_STACK 10
int top=-1;
struct bin
{
int num[MAX_STACK];
bool isEmpty()
{
return (top < 0);
}
void push(int newItem)
{
// if stack has no more room for
// another item
if (top >= MAX_STACK-1)
cout<<"Stack Overflow!";
else{
++top;
num[top] = newItem;
}
}
int pop()
{
if(top < 0)
{
//top points to nothing i.e stack is empty
cout << "Stack Underflow \n";
return 0;
}
else
{
/*decrement the top pointer and it
points to the next top element in the stack*/
int d = num[top--];
return d;
}
}
};
int main()
{
int value, count = 0;
bin var;
int rem;
cout << "Enter a decimal number to convert into binary: ";
cin >> value;
while (value != 0)
{
rem = value % 2;
cout << "rem= " << rem << endl;
var.push(rem);
count++;
value /= 2;
}
cout << "\nYour binary is: ";
while (count != 0)
{
cout << var.pop();
count--;
}
return 0;
}
I am trying to solve the 'Copy-Paste' programming problem (https://www.codechef.com/problems/RRCOPY ) of codechef
Here is what I have tried
#include <iostream>
using namespace std;
bool isNumberInArray(int array[], int A, int size)
{
bool isFound = false;
for(int i = 0; i < size; i++)
{
if(array[i] == A)
{
isFound = true;
break;
}
}
return isFound;
}
int main()
{
int T, i = 0, A, size = 0, count;
int array[100000];
cin >> T;
while(T--)
{
cin >> size;
count = 0;
i = 0;
while(size--)
{
cin >> A;
if(isNumberInArray(array, A, count) == false)
{
array[i] = A;
count++;
}
i++;
}
cout << count << endl;
}
return 0;
}
Can anyone please tell what I am doing wrong.
Thanks in advance.
There are three mistakes in your program.
First: Why is the variable i outside the if block. You are incrementing the index even if there are duplicates in your array. (You can just use count instead of i).
array[count]=A;
count++;
Second: You shouldn't print the result immediately after evaluating a testcase. You need to store all the results of all testcases in an array, and print them after you solve all of them.
Third: Your algorithm runs on O(n^2) which will definitely result in a TLE.
You need to improve your algorithm to O(n). My solution:https://www.codechef.com/viewsolution/9011078
Next time please be more concise about what you want to ask.
The error is this:
cannot convert int*' toint*' for argument 1' tobool permition(int*, int, int)'
Here in code i have a int board[n], and the user gives the 'n'...
i want to give my permition function this array so i had to give it by pointers because the length of it is not specified...So how can i solve this problem
Here is my code:
#include <cstdlib>
#include <iostream>
#include <cmath>
using namespace std;
bool permition(int* board[],int place,int n_){
int m=place;
while(m!=0){
m--;
if(abs(abs(board[m]-board[place])-abs(m-place))==1
&& abs(m-place)<3 && abs(board[m]-board[place]))
return false;
}
return true;
}
void printBoard(int* board[],int n){
for(int i=0;i<n;i++)
cout << board[i]<< " ";
cout << endl;
}
int main()
{
int p=0;
int n;
cout << "plz: ";
cin >> n;
int board[n];
for(int i=0;i<n;i++)
board[i]=0;
while(p<n){
while((board[p]<n) && permition(board,p,n)==false)
board[p]+=1;
if(board[p]<n)
p++;
else{
p--;
board[p]+=1;
}
if(p==n && board[0]<n-1)
//it means the first number is not max so we should
//print and continue from fist again
{
printBoard(board,n);
p=0;
board[0]+=1;
}
}
system("PAUSE");
return 0;
}
Get rid of [] in the function definition of permition:
bool permition(int* board,int place,int n_)
See if that helps!
Change
bool permition(int* board[],int place,int n_)
to
bool permition(int const board[],int place,int n_)
The current argument declaration (first above) says that board is an array of pointers to int, which it isn't.
The [] ends up as a pointer, so you can alternatively write
bool permition(int const* board,int place,int n_)
This form has the advantage that you can use const also on the pointer, while with [] you have a pointer that can be changed but that looks like an array.
The disadvantage is that the declaration no longer communicates "array" to the reader.
As others have noted, the original code passes an array of int (which is treated as an int *) to a function that's declared to take a pointer to an array of int. Here's a corrected version, with some formatting changes that I hope will be helpful.
#include <cstdlib>
#include <iostream>
#include <cmath>
using namespace std;
// Removed unused parameter n_.
bool permission(int board[], int place) {
int m = place;
// The original code could return false only if (place - m) < 3,
// so no need to test when (place - m) >= 3.
while (m-- > max(0, place - 3) {
int board_diff = abs(board[m] - board[place];
int index_diff = place - m; // Always >= 0
if (abs(board_diff - index_diff) == 1 && board_diff != 0) {
return false;
}
}
return true;
}
void printBoard(int board[], int n) {
for (int i = 0; i < n; i++) {
cout << board[i] << " ";
}
cout << endl;
}
int main() {
int p = 0;
int n;
cout << "plz: ";
cin >> n;
int board[n] = { 0 }; // Zeroes entire array.
while (p < n) {
// Never compare bool to true or false; just use !bool_var.
while ((board[p] < n) && !permission(board, p)) {
board[p]++;
}
if (board[p] < n) {
p++;
} else {
board[--p]++;
}
if (p == n && board[0] < (n - 1)) {
// The first number is not max so we should
// print and continue from first again
printBoard(board, n);
board[p = 0]++; // Assign and increment.
}
}
system("PAUSE");
return 0;
}
int board[];
is the same as
int *board;
so the solution in my guess is to write that
bool permition(int board[],int place,int n_)
void printBoard(int board[],int n)
You need to understand and to work more on pointers !