Process terminated with status -1073741510? [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have the following code that compiles without any errors but does not run. Can someone tell me the problem ?
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int kenken[4][4];
kenken[2][2] = 3;
for(int i = 0; i < 5; i++)
for(int j = 0; j < 5; j++)
for(int k = 0; k < 5; k++){
if(i + j + k == 9){
kenken[0][0] = i;
kenken[0][1] = j;
kenken[0][2] = k;
}
if(i * j * k == 6){
kenken[1][0] = i;
kenken[2][0] = j;
kenken[3][0] = k;
}
if(abs(i - j)== 3){
kenken[1][1] = i;
kenken[1][2] = j;
}
if(abs(i-j) == 2){
kenken[3][1] = i;
kenken[3][2] = j;
}
if(i/j == 2){
kenken[0][3] = i;
kenken[1][3] = j;
}
if(i * j * k == 12){
kenken[2][2] = i;
kenken[2][3] = j;
kenken[3][3] = k;
}
cout << kenken[0][0] << " " << kenken[1][0]
<< kenken[2][0] << " " << kenken[3][0]
<< "\n\n";
}
return 0;
}

When j==0 you're going to get a division by zero error here:
if(i/j == 2){
As Ben suggested, a fix would be:
if( i == 2*j ) {

Related

Represent spiral of numbers in a matrix [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
How to dynamically fill the following output in a two-dimensional array with the same number of rows and columns.
Edit: Here is the solution in c++ using #TheGeneral code from c#.
#include <iostream>
using namespace std;
int main(){
int size = 10;
int half = size/2;
int matrix[size][size];
int number1 = 0;
int number2 = 0;
for(int i = 1; i<=size; i++){
for(int j = 1; j<= size; j++){
if(i > half){
number1 = size + 1 - i;
}else{
number1 = i;
}
if(j > half){
number2 = size + 1 - j;
}else{
number2 = j;
}
if(number1 < number2){
matrix[i-1][j-1] = number1;
}else{
matrix[i-1][j-1] = number2;
}
}
}
cout<<"MATRIX:"<<endl;
for(int i=0; i<size; i++){
for(int j=0; j<size; j++){
cout<<"["<<matrix[i][j]<<"] \t";
}
cout<<endl;
}
return 0;
}
For a bit of fun
The only note worthy things going on, are
Starting the index from 1 (makes things easier)
Conditional Operator to say if an index is greater than 5 reverse the count
Math.Min to make it syemtrical
Exmaple
private static int[, ] CreateArray()
{
var ary = new int[10, 10];
for (var i = 1; i <= 10; i++)
for (var j = 1; j <= 10; j++)
ary[i - 1, j - 1] = Math.Min(i > 5 ? 11 - i : i, j > 5 ? 11 - j : j);
return ary;
}
Demo here

C++:,when i use string array, it comes string subscript out of range [closed]

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
I just want to know why it come the question that string subscript out of range,I have already initialized the string array.Thank you.
#include<bits/stdc++.h>
#define N 100000
using namespace std;
string s[N + 5] = {}, ss[N + 5] = { " " }, fs[N + 5] = { " " };
int main()
{
int n, x; char c;
cin >> n;
for (int i = 0; i<n; ++i)
{
for (int j = 0; j<6; ++j)
{
scanf("%c", &c);//the error comes here.
if (c == ' ') continue;
s[i] += c;
}
ss[i] = s[i] + s[i];
for (int j = 5; j >= 0; --j) fs[i][j] = s[i][5 - j];
}
int flag = 0;
for (int i = 0; i<n; ++i)
{
for (int j = 0; j<n; ++j)
{
if (i == j) continue;
if (find(s[i], fs[j]) || find(s[i], ss[j]))
{
flag = 1; break;
}
}
}
}
1) Why should I not #include <bits/stdc++.h>?
2) Avoid global variables and use std::vector when size is unknown.
3) Why is using namespace std considered bad practice?
You're getting an out of range error because of fs[i][j] accessing empty strings f[i].

Finding the maximum in given outputs [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have written a code to find the largest palindrome formed by multiplication of two 3-digit numbers. However instead of just getting the desired answer i.e. the largest palindrome, I am getting the list of all possible palindromes. How do I program it to find the largest.
Code:
#include <iostream>
using namespace std;
int revfunc(int x) {
int rev = 0, num, d;
num = x;
while(num != 0) {
d = num % 10;
rev = (rev * 10) + d;
num = num / 10;
}
long int maxi = 0;
if(x == rev && maxi < x) {
maxi=x;
cout<<maxi<<endl;
}
}
int main() {
long int ans;
for(int i = 100; i <= 999; i++) {
for(int j = 100; j <= 999; j++) {
ans = i * j;
revfunc(ans);
}
}
cin.get();
return 0;
}
In your program you don't actually select the maximum palindrome, you just dump them all. Here is minimal correction for code to work:
bool revfunc(int x){
int rev = 0, num, d;
num = x;
while (num != 0){
d = num % 10;
rev = (rev * 10) + d;
num = num / 10;
}
long int maxi = 0;
return x == rev&&maxi < x;
}
int main()
{
int max_palindrome = 0;
long int ans;
for (int i = 100; i <= 999; i++){
for (int j = 100; j <= 999; j++){
ans = i*j;
if (ans > max_palindrome && revfunc(ans))
{
max_palindrome = ans;
}
}
}
cout << max_palindrome;
cin.get();
return 0;
}

C++ average of negative elements in array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I need to find the average of the negative elements in the two-dimensional array. This is what I've got so far:
#include <iostream>
using namespace std;
int main() {
int A[3][3];
int i, j;
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++) {
cout << "\n A[" << i + 1 << "][" << j + 1 <<"]=";
cin >> A[i][j];
}
for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++)
cout << A[i][j] << "\t";
cout << "\n";
}
}
You could do it for example:
int negNumber = 0;
int sum = 0;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
if (A[i][j] < 0) {
++negNumber; // increase negatives numbers found
sum += A[i][j]; // add to our result the number
}
}
}
sum = negNumber > 0 ? sum / negNumber : sum; // we need to check if we found at least one negative number
I hope it will help you but be careful! It will return a truncated value (an int).
the ternary operation could be difficult to undertand so you can do it:
if (negNumber > 0) {
sum /= negNumber;
}

Why is my code showing Segmentation Fault? [closed]

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