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 8 years ago.
Improve this question
My code is as follows. Its is giving segmentation fault. I have been debugging it but stuck! I can't find the problem. Can someone help me?
#include <iostream>
#include <fstream>
using namespace std;
char art[200][200];
char art2[200][200];
int n;
void solve(char a, int x, int y);
int main() {
// ifstream fin("cowart.in");
// ofstream fout("cowart.out");
cin >> n;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
cin >> art[i][j];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
art2[i][j] = art[i][j];
int rh = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (art[i][j] == 'R' || art[i][j] == 'G' || art[i][j] == 'B') {
rh++;
solve(art[i][j], i, j);
}
}
}
int rc = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
art[i][j] = art2[i][j];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (art[i][j] == 'G')
art[i][j] = 'R';
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (art[i][j] == 'R' || art[i][j] == 'B') {
rc++;
solve(art[i][j], i, j);
}
}
}
cout << rh << " " << rc << endl;
// system("PAUSE");
// fin.close();
// fout.close();
return 0;
}
void solve(char a, int x, int y) {
if (x < 0 || y < 0 || x >= n || y >= n)
return;
if (art[x][y] != a)
return;
art[x][y] == '.';
if (x < n - 1)
solve(a, x + 1, y);
if (x > 0)
solve(a, x - 1, y);
if (y < n - 1)
solve(a, x, y + 1);
if (y > 0)
solve(a, x, y - 1);
return;
}
This code is the solution to: USACO Problem 414
The test case i is showing Segmentation Fault is:
5
RRRBB
GGBBB
BBBRR
BBRRR
RRRRR
Output should be: 4 3
In the solve() function, notice this line?
art[x][y] == '.';
The code above tests if art[x][y] is equal to '.', then throws the result away. It's legal C++, but does nothing useful. A smart compiler may give you a warning.
It's obviously meant to be assignment:
art[x][y] = '.';
The function solve() goes in infinite recursion, causing stack overflow.
Implement solve using non-recursive rather iterative method.
Implement 4-neighborhood problem in iterative way with loops and iterations
Related
My screen isn't printing please help me with this.I'm a beginner and i'm trying to make snake game.
void myScreen()
{
char screen[30][50];
int x = 30, y = 50;
for (int i = 1; i <= x; i++)
{
if (i == 1 || i == x)
for (int j = 1; j <= y; j++)
screen[i][j] = '#';
else
for (int j = 1; j <= y; j++)
if (j == 1 || j == y)
screen[i][j] = '#';
else
screen[i][j] = ' ';
}
for (int i = 1; i <= x; i++)
{
for (int j = 1; j <= y; j++)
cout << screen[i][j];
cout << endl;
}
}
the screen isn't printing
C++ arrays are zero-based. That means that the "first" element has actually the index 0. So your code like that:
for(int i=1;i<=x;i++)
shall be reworked like that:
for (int i = 0; i < x; i++)
Now back to your question: why it isn't printing? The fact is that you are accessing the memory outside of the array, and that is an Undefined Behavior in C++. That may be observed as the program that never gets to the code that prints the array.
The reason it becose you don't call the funtion in main()
here how to do it:
int main(){
myScreen();
}
the int main() is when the program is starting.after you can call other functions
They have other reason like you don't import any libraries
#include <iostream>
using namespace std;
add this in the start of your code
You are missing a main function in your code. In C++ (and C), the main function gets called when you start the program. All other functions that you want to be executed you have to call from this function. Additionally, you start counting your array indexes from 1 - but the first element of an array has 0 as index in most programming languages (including C, C++, Python, JavaScript, bash, …, …). This code should work:
#inclue <iostream>
using namespace std;
void myScreen()
{
int x = 30, y = 50;
char screen[x][y];
for (int i = 0; i < x; i++)
{
if (i == 0 || i == x - 1)
{
for (int j = 0; j < y; j++)
{
screen[i][j] = '#';
}
} else
{
for (int j = 0; j < y; j++)
{
if (j == 1 || j == y)
{
screen[i][j] = '#';
} else
{
screen[i][j] = ' ';
}
}
}
}
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
cout << screen[i][j];
}
cout << endl;
}
}
int main(int argc, char* argv[]) {
myScreen();
}
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int n, a[n];
cin >> n;
float b = n / 2;
for (int i = 1; i <= n; i++)
cin >> a[i];
// the code seems to be not working from here till the point marked below
if (n % 2 == 0)
{
for (int k = 1; k <= b; k++)
{
for (int j = (b + 1); j <= n; j++)
{
if (a[k] > a[j])
swap(a[k], a[j]);
}
}
}
// till this point(pls tell me what's wrong with this part of the code)
for (int i = 1; i <= n; i++)
cout << a[i] << " ";
getch();
return 0;
}
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 7 years ago.
Improve this question
void power(int a[][21], int n, int d) {
int e[21][21], k, i, j, l;
if (d == 1) {
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++)
cout << a[i][j] << " ";
cout << endl;
}
} else {
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
e[i][j] = 0;
for (l = 1; l < d; l++)
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
for (k = 1; k <= n; k++)
e[i][j] = e[i][j] + (a[i][k] * a[k][j]);
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++)
cout << e[i][j] << " ";
cout << endl;
}
}
}
I am trying to build a function that calculates the power of a matrix. The program works for the 1st and 2nd power of the matrix but if I want to calculate the 3rd power the function will return incorrect values. I think the problem is at retaining the previous results but I can't figure out on how to fix it.
This block of code
for (l = 1; l < d; l++)
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
for (k = 1; k <= n; k++)
e[i][j] = e[i][j] + (a[i][k] * a[k][j]);
sets e to be a*a no matter what d is.
You need to have a temporary matrix to make things work.
Bootstrap:
e = a;
In the loop:
temp = e;
e = temp * a;
temp = e;
e = temp * a;
... etc.
Something along the lines of:
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
e[i][j] = a[i][j];
}
}
for (l = 1; l < d; l++)
{
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
temp[i][j] = e[i][j];
}
}
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
e[i][j] = 0;
for (k = 0; k < n; k++)
{
e[i][j] += (temp[i][k] * a[k][j]);
}
}
}
}
PS I have used 0-based indexing to access the matrices. I am not sure why you have used 1-based indexing.
#include<iostream>
using namespace std;
void compute_it(int k, int n){
for(int i = 1; i < k; i++){
for(int j = 0; j < n; j++){
for(int z = 0; z < j; z++){
if(i == 1 && j == 0){
cout << "in here\n";
}
}
}
}
}
int main(){
compute_it(2,3);
}
I'm not sure what is happening here. It's probably a really dumb error, but I don't understand why the "in here" is not being printed when I run this code.
Your inner loop iterates on the condition z < j. In that case, j will never be 0 when that loop is running.
I cannot understand/think of a case where my code fails to give correct output.
Link to the problem: http://www.spoj.pl/problems/MKBUDGET/
The problem clearly has a DP solution. I am posting my solution below:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<vector <int> > opt;
void compute_opt(vector<int> A,int n,int hire,int fire,int sal,int max_a)
{
for(int i = A[0]; i <= max_a; i++) //for num workers in 1st month
opt[0][i] = i*(hire + sal);
for(int i = 1; i < n; i++) //num of months
for(int j = A[i]; j <= max_a; j++) //num of workers for ith month >=A[i] and <= max_a
{
opt[i][j] = opt[i-1][A[i-1]] + j*sal + (A[i] > A[i-1] ? (A[i]-A[i-1])*hire : (A[i-1] - A[i])*fire);
for(int k = A[i-1]; k <= max_a; k++)
opt[i][j] = min(opt[i][j], opt[i-1][k] + j*sal + (j>k ? (j-k)*hire : (k-j)*fire));
}
}
int ans(vector<int> A, int n, int max_a)
{
int ret = opt[n-1][A[n-1]];
for(int i = A[n-1]; i <= max_a; i++)
ret = min (ret, opt[n-1][i]);
return ret;
}
int main()
{
vector<int> A;
int n, hire, fire, sal,max_a, c = 1;
while(1)
{
cin >> n;
if(n == 0)
break;
A.clear();
opt.clear();
max_a = 0;
cin >> hire >> sal >> fire;
A.resize(n);
for(int i = 0; i < n; i++)
{cin >> A[i];
max_a = max(max_a,A[i]);
}
opt.resize(n);
for(int i = 0; i < n; i++)
opt[i].resize(max_a + 2);
compute_opt(A,n,hire,fire,sal,max_a);
cout << "Case " << c << ", cost = $" << ans(A,n,max_a) << endl;
c++;
}
return 0;
}
I am getting correct answers for the two sample test cases but I get a WA when I submit. Any help ?
OK, so your problem is that you disallow the case where you hire any number of employees between A[i] and A[i - 1]. Maybe it's a good idea to fire some unneeded employees, but not all. That's why you get WA. I modified your code and got it accepted:
void compute_opt(vector<int> A,int n,int hire,int fire,int sal,int max_a)
{
// Fill all disallowed entries with infinity
for (int i = 0; i < A[0]; ++i)
opt[0][i] = 1000000000;
for(int i = A[0]; i <= max_a; i++) //for num workers in 1st month
opt[0][i] = i*(hire + sal);
for(int i = 1; i < n; i++)
for(int j = 0; j <= max_a; j++)
{
// No need for special case handling,
//just check all previous numbers of employees
opt[i][j] = 1000000000;
if (A[i] > j) continue;
for(int k = 0; k <= max_a; k++)
opt[i][j] = min(opt[i][j],
opt[i-1][k] + j*sal + (j>k ? (j-k)*hire : (k-j)*fire));
}
}
By the way, there's a "greedier" solution than the one you have that does not depend on the number of employees being small (so that the table can be allocated).