How can I make variables available to my function? [duplicate] - c++

This question already has answers here:
Passing a 2D array to a C++ function
(18 answers)
Closed 9 years ago.
Code in C++:
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
void check(int i,int j);
int main()
{
int n;
cin >> n;
int a[n][n];
int b[n][n];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
b[i][j] = 0;
}
}
b[0][0] = 1;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; i++)
{
cin >> a[i][j];
}
}
check(0, 0);
if (b[n - 1][n - 1] == 1)
cout << "yesssss";
else
cout << "no!!!!";
}
void check(int i, int j)
{
if((i - 1) >= 0 &&
(i - 1) <= (n - 1) &&
(abs(a[i][j] - a[i - 1][j]) >= 10) &&
b[i - 1][j] == 0)
{
b[i - 1][j] = 1;
check(i - 1, j);
}
if((i + 1) >= 0 &&
(i + 1) <= (n-1) &&
(abs(a[i][j] - a[i + 1][j]) >= 10) &&
b[i + 1][j] == 0)
{
b[i + 1][j] = 1;
check(i + 1, j);
}
if((j + 1) >= 0 &&
(j + 1) <= (n - 1) &&
(abs(a[i][j] - a[i][j + 1]) >= 10) &&
b[i][j + 1] == 0)
{
b[i][j + 1] = 1;
check(i, j + 1);
}
if((j - 1) >= 0 &&
(j-1) <= (n - 1) &&
(abs(a[i][j] - a[i][j - 1]) >= 10) &&
b[i][j - 1] == 0)
{
b[i][j - 1] = 1;
check(i, j - 1);
}
}
My code has a function named check inside which integer variable n and arrays a and b. I want them to be made available to this function. If i declare global variable, then I can't use cin outside the main function.
How can I make these variables available to my check function?

Since you do not know the size of the array before you pass it into check, you need to use a pointer to a pointer (and not a 2D array). Here's the modified code (you also had a typo in your nested for-loop where you should have had a j instead of an i):
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
void check(int i, int j, int** a, int** b, int n);
int main() {
int n;
cin >> n;
int** a;
int** b;
a = new int* [n];
b = new int* [n];
for (int i = 0; i < n; i++)
a[i] = new int[n];
for (int i = 0; i < n; i++)
b[i] = new int[n];
for (int i=0;i<n;i++) {
for (int j=0;j<n;j++) {
b[i][j]=0;
}
}
b[0][0]=1;
for (int i=0;i<n;i++) {
for (int j=0;j<n;j++) {
cin >> a[i][j];
}
}
check(0,0,a,b,n);
if (b[n-1][n-1] == 1)
cout << "yesssss";
else
cout << "no!!!!";
}
void check(int i, int j, int** a, int** b, int n)
{
if((i-1)>=0 && (i-1)<=(n-1) && (abs(a[i][j]-a[i-1][j])>=10) && b[i-1][j]==0)
{
b[i-1][j]=1;
check(i-1,j,a,b,n);
}
if((i+1)>=0 && (i+1)<=(n-1) && (abs(a[i][j]-a[i+1][j])>=10) && b[i+1][j]==0)
{
b[i+1][j]=1;
check(i+1,j,a,b,n);
}
if((j+1)>=0 && (j+1)<=(n-1) && (abs(a[i][j]-a[i][j+1])>=10) && b[i][j+1]==0)
{
b[i][j+1]=1;
check(i,j+1,a,b,n);
}
if((j-1)>=0 && (j-1)<=(n-1) && (abs(a[i][j]-a[i][j-1])>=10) && b[i][j-1]==0)
{
b[i][j-1]=1;
check(i,j-1,a,b,n);
}
}

Related

Is there any way to put the whole code inside of void function to for loop in C++?

I'm curios on why when I put the whole code inside void max_heap to for loop of void build_maxheap, program does not work. The for loop just repeat what is the code inside vode max_heap then why does it not work if it works when I put the void function? Is there anyway I can put the whole code inside the max_heap to the for loop of void build_maxheap and run the program fine?
#include <iostream>
using namespace std;
void max_heap(int *a, int m) {
int j, t;
t = a[m];
j = 2 * m;
while (j <= 10) {
if (j < 10 && a[j+1] > a[j])
j = j + 1;
if (t > a[j])
break;
else if (t <= a[j]) {
a[j / 2] = a[j];
j = 2 * j;
}
}
a[j/2] = t;
return;
}
void build_maxheap(int *a,int ) {
int k;
for(k = 10/2; k >= 1; k--) {
max_heap(a,k);
}
}
int main() {
int i, a[10];
for (i = 1; i <= 10; i++) {
cout << "Enter num:";
cin>>a[i];
}
build_maxheap(a,10);
cout<<"\nMax Heap \n" << endl;
for (i = 1; i <= 10; i++) {
cout<<a[i]<<endl;
}
}
Im having a hard time converting my program from O(N) to O(N log N). Is there any problem in my loop?
#include <iostream>
using namespace std;
void max_heap(int *a, int m) {
int j, t;
t = a[m];
j = 2 * m;
while (j <= 10) {
if (j < 10 && a[j+1] > a[j])
j = j + 1;
if (t > a[j])
break;
else if (t <= a[j]) {
a[j / 2] = a[j];
j = 2 * j;
}
}
a[j/2] = t;
return;
}
void build_maxheap(int *a,int ) {
int k;
for(k = 10/2; k >= 1; k--) {
max_heap(a,k);
}
}
int main() {
int i, a[10];
for (i = 1; i <= 10; i++) {
cin>>a[i];
}
build_maxheap(a,10);
cout<<"Max Heap - O(N log N)\n";
for (i = 1; i <= 10; i++) {
cout<<a[i]<<endl;
}
}

Creates A Number Pattern Using Array

So the numbers pattern going like this :
Input : 2
Output :
0 0 0 1 0 0 0
0 0 2 0 12 0 0
0 3 0 0 0 11 0
4 0 0 0 0 0 10
0 5 0 0 0 9 0
0 0 6 0 8 0 0
0 0 0 7 0 0 0
I have solved it using usual method, but when I tried with array, the output is really messed up. Any suggestion how to make this number pattern with array?
This is my code that creates this number pattern :
int input, n, mid, i, j;
cin >> input;
n = (2*input)+3;
mid = (n+1)/2;
for(i = 1; i <= n; i++) {
for (j = 1; j <= mid; j++) {
if (i <= mid && j == mid-i+1) cout << i << " ";
else if (i > mid && j == mid-n+i) cout << i << " ";
else cout << "0 ";
}
for (j = mid+1; j <= n; j++) {
if (i >= mid && j == n+mid-i) cout << (2*n-i) << " ";
else if (i < mid && j == mid+i-1) cout << (2*n-i) << " ";
else cout << "0 ";
}
cout << endl;
}
Thanks in advance.
I think this code(working) might help you.
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int main() {
int input, n, mid, i, j;
cin >> input;
n = (2*input)+3;
mid = (n+1)/2;
int arr[n][n];
memset(arr, 0, sizeof(arr));
for(i = 1; i <= n; i++) {
for (j = 1; j <= mid; j++) {
if (i <= mid && j == mid-i+1) arr[i - 1][j - 1] = i;
else if (i > mid && j == mid-n+i) arr[i - 1][j - 1] = i;
}
for (j = mid+1; j <= n; j++) {
if (i >= mid && j == n+mid-i) arr[i - 1][j - 1] = 2 * n - i;
else if (i < mid && j == mid+i-1) arr[i - 1][j - 1] = 2 * n - i;
}
}
}
Supplementing baymaxx, if you desire dynamic memory allocation:
#include <iostream>
#include <stdio.h>
#include <string.h>
int input, n, mid, i, j;
cin >> input;
n = (2*input)+3;
mid = (n+1)/2;
// create array of the specified size.
int** arr = (int**) malloc (n * sizeof(int**));
for (int p = 0; p < n; ++p) {
arr[p] = (int*) malloc(n * sizeof(int));
for (int q = 0; q < n; ++q) {
arr[p][q] = 0;
}
}
// fill array (algorithm attr. baymaxx)
for(i = 1; i <= n; i++) {
for (j = 1; j <= mid; j++) {
if (i <= mid && j == mid-i+1) {
arr[i - 1][j - 1] = i;
}
else if (i > mid && j == mid-n+i) {
arr[i - 1][j - 1] = i;
}
}
for (j = mid+1; j <= n; j++) {
if (i >= mid && j == n+mid-i) {
arr[i - 1][j - 1] = 2 * n - i;
}
else if (i < mid && j == mid+i-1) {
arr[i - 1][j - 1] = 2 * n - i;
}
}
}
// print array
for (int p = 0; p < n; ++p) {
for (int q = 0; q < n; ++q) {
int x = arr[p][q];
std::cout << x << " ";
}
std::cout << endl;
}
// delete array
for (int i = 1; i < n; ++i) {
free(arr[i]);
}
free(arr);

Longest Common Subsequence is not showing the result

I have written this code using dynamic programming approach and I think the logic is good but the code is not displaying the result. The code is as follows:
#include <iostream>
using namespace std;
void LCS(int input1[], int input2[], int n, int m) {
int L[n + 1][m + 1]; /*This matrix stores the length of common subsequences*/
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= m; j++) {
if (i == 0 || j == 0)
L[i][j] = 0;
else if (input1[i - 1] == input2[j - 1])
L[i][j] = 1 + L[i - 1][j - 1];
else
L[i][j] = max(L[i - 1][j], L[i][j - 1]);
}
}
int index = L[n][m];
int lcs[index];
int i = n, j = m;
while (i > 0 && j > 0) {
if (input1[i - 1] == input2[j - 1]) {
lcs[index - 1] = input1[i - 1];
i--;
j--;
index--;
} else if (L[i - 1][j] > L[i][j - 1])
i--;
else
j--;
}
for (int i = 0; i < index; i++){
cout << lcs[i];
}
}
int main() {
int n, m;
cin >> n >> m;
int input1[n], input2[m]; /*two arrays from which longest subsequnce is to be found*/
for (int i = 0; i < n; i++)
cin >> input1[i];
for (int i = 0; i < m; i++)
cin >> input2[i];
LCS(input1, input2, n, m);
return 0;
}
The code terminates without showing any result!
I even switched to a different IDE but its the same. What is wrong with this?
You are modifying the index variable. Create a copy of it and modify that. Here I used temp.
int index = L[n][m];
int temp = index;
int lcs[index];
int i = n, j = m;
while (i > 0 && j > 0) {
if (input1[i - 1] == input2[j - 1]) {
lcs[temp - 1] = input1[i - 1];
i--;
j--;
temp--;
} else if (L[i - 1][j] > L[i][j - 1])
i--;
else
j--;
}
for (int i = 0; i < index; i++){
cout << lcs[i];
}
In your version index is decremented to zero when you want to print the result so nothing will be printed.

'this' cannot be used in a constant expression error (C++)

all. I have a class defined as follows:
class Board {
int columns, rows;
bool board[10][10];
public:
Board(int, int);
void nextFrame();
void printFrame();
};
My void nextFrame() keeps giving me errors for [rows][columns] because " 'this' cannot be in a constant expression" for both of them. How can I redefine this so that it works? I understand the error. The definition of the function is below, and the error occurs on the 3rd line of the following code sample.
void Board::nextFrame() {
int numSurrounding = 0;
bool tempBoard[rows][columns];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
if ((i + 1) < rows && board[i + 1][j] == true)
{
numSurrounding++;
}
if ((i - 1) >= 0 && board[i - 1][j] == true)
{
numSurrounding++;
}
if ((j + 1) < columns && board[i][j + 1] == true)
{
numSurrounding++;
}
if ((j - 1) >= 0 && board[i][j - 1] == true)
{
numSurrounding++;
}
if ((i + 1) < rows && (j + 1) < columns && board[i + 1][j + 1] == true)
{
numSurrounding++;
}
if ((i + 1) < rows && (j - 1) >= 0 && board[i + 1][j - 1] == true)
{
numSurrounding++;
}
if ((i - 1) >= 0 && (j + 1) < columns && board[i - 1][j + 1] == true)
{
numSurrounding++;
}
if ((i - 1) >= 0 && (j - 1) >= 0 && board[i - 1][j - 1] == true)
{
numSurrounding++;
}
if (numSurrounding < 2 || numSurrounding > 3)
{
tempBoard[i][j] = false;
}
else if (numSurrounding == 2)
{
tempBoard[i][j] = board[i][j];
}
else if (numSurrounding == 3)
{
tempBoard[i][j] = true;
}
numSurrounding = 0;
}
}
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
board[i][j] = tempBoard[i][j];
}
}
}
You need to use a collection from the STL.
Here's an example that nests vectors to get your board:
#include <vector>
#include <iostream>
using namespace std;
class Board {
int columns, rows;
vector<vector<bool>> board;
public:
Board(int x, int y) : board(vector<vector<bool>>(x, vector<bool>(y))) {
}
void nextFrame() {
// Fill in
}
void printFrame() {
// Fill in
}
size_t xdim() {
return board.size();
}
size_t ydim() {
if (board.size() == 0) {
return 0;
}
return board.at(0).size();
}
};
int main() {
Board b(10, 20);
cout << "Made the board" << endl;
cout << "X: " << b.xdim() << endl;
cout << "Y: " << b.ydim() << endl;
}
You can learn about the member initialization syntax for board(vector<vector<bool>>(x, vector<bool>(y))) here and here.

Shortest distance using BFS

I am trying to solve this SPOJ problem. The question asks to find the shortest path for each black(1) pixel.
Since it is a unweighted graph I used BFS.
for input:
3 3
010
000
000
it's giving:
323
434
343
instead of:
101
212
323
This is my code
#include<iostream>
#include<queue>
#include<string.h>
using namespace std;
typedef pair < int, int >ii;
int R, C, i, j;
queue < ii > myQueue;
int visit[100][100];
int dist[100][100];
void bfs(ii s)
{
int i, j;
int count = 0;
ii node;
memset(visit, 0, sizeof(visit));
memset(dist, 0, sizeof(dist));
myQueue.push(s);
dist[node.first][node.second] = 0;
while (!myQueue.empty()) {
node = myQueue.front();
myQueue.pop();
if (visit[node.first][node.second])
continue;
visit[node.first][node.second] = 1;
//cout << node.first << " " << node.second << "\n";
i = node.first;
j = node.second;
if (j - 1 < R && j - 1 >= 0) {
myQueue.push(make_pair(i, j - 1));
if(dist[i][j - 1] == 0)
dist[i][j - 1] = dist[i][j] + 1;
}
if (j + 1 < R && j + 1 >= 0) {
myQueue.push(make_pair(i, j + 1));
if(dist[i][j+1] == 0)
dist[i][j + 1] = dist[i][j] + 1;
}
if (i - 1 < C && i - 1 >= 0) {
myQueue.push(make_pair(i - 1, j));
if(dist[i-1][j] == 0)
dist[i - 1][j] = dist[i][j] + 1;
}
if (i + 1 < C && i + 1 >= 0) {
myQueue.push(make_pair(i + 1, j));
if(dist[i+1][j] == 0)
dist[i + 1][j] = dist[i][j] + 1;
}
}
}
int main()
{
char input[100][100];
scanf("%d %d", &R, &C);
for (i = 0; i < R; i++)
scanf("%s", &input[i]);
int GRID[R][C];
for (i = 0; i < R; i++)
for (j = 0; j < C; j++)
GRID[i][j] = input[i][j] - '0';
for (i = 0; i < R; i++)
for (j = 0; j < C; j++) {
if (GRID[i][j] == 1)
bfs(make_pair(i, j));
}
for (i = 0; i < R; i++) {
for (j = 0; j < C; j++) {
printf("%d", dist[i][j]);
}
printf("\n");
}
}
ideone
Try this:
if (j - 1 < R && j - 1 >= 0) {
myQueue.push(make_pair(i, j - 1));
if(dist[i][j - 1] == 0)
dist[i][j - 1] = dist[i][j] + 1;
}
do this for all dist[][].
You have doubled result may be because you run your BFS twice between paired vertices.
But I'm not sure.