Bad Access C++ Error - c++

Can you help me fix a bad access error please?
Here is the code:
#include <iostream>
using namespace std;
int main() {
int t,tr=0;
cin>>t;
while (tr<t) {
int n;
cin>>n;
int distance=n;
int number;
number=n*n;
int spiral[n][n];
for (int i=0;i<n;i++) {
for (int j=0; j<n; j++) {
spiral[i][j]=0;
}
}
for (int i=0; i<n;) {
for (int j=0; j<n;) {
spiral[i][j]=number;
number=number-1;
//cout<<"ij"<<endl;
for (int k=0; k<distance; k++) {
i++;
spiral[i][j]=number;
number--;
//cout<<"k"<<endl;
}
}
}
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
cout<<spiral[i][j];
}
cout<<endl;
}
tr++;
}
return 0;
}
Bad access is on
spiral[i][j]=number;
Here is the link for the problem but this is not important at the moment. I tried nszmobies but it didn't work so I'm asking you.
This is c++.
Here is the problem.

It seems that you have errors in your loops.
Loop
for (int j=0; j<n;)
looks as it is infinite because j variable isn't changing. Moreover variable i in
spiral[i][j]=number;
in your program can be greater or equal to n.

Related

Adding values of 2 columns from a matrice and putting the sum in a vector

My first question about CPP in Stackoverflow. I hope i get an answer.
#include <fstream>
using namespace std;
ifstream fin("Matr.dat");
ofstream fout("out.dat");
int a[1000][1000];
int b[10000];
int n, m;
void read()
{
fin>>n>>m;
for(int i=1; i<n; i++)
{
for(int j=1; j<m; j++)
fin>>a[i][j];
}
}
int vect()
{
int c=1;
for(int i=2; i<=n; i++)
{
for(int j=2; j<=m; j++)
{
b[c]=a[j]+a[j+1];
c++;
}
}
return c;
}
int main()
{
read();
int c=vect();
for(int i=0; i<c; i++)
fout<<b[i]<<' ';
return 0;
}
I and a colleague are trying to add the values of 2 columns into a vector.
For the line `b[c]=a[j]+a[j+1]; i receive an error saying that i use 2 incompatible types together. It is not working...
Can someone please help me add the values of 2 columns into a vector?

the program is stuck while giving output

I dont know why the program is failed to give output, This is the code to find number of zero's
in a given array
#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int arr[n];
for(int i=0; i<n; i++)
{
cin>>arr[i];
}
int b=0 , a;
for(int j=0; j<n; j++)
{
a=arr[j];
while(a==0)
{
b=b++;
}
}
cout<<b;
}
Try changing it to
if (a==0) //you had a while here
{
b=b++;
}
Since you are not changing the value of 'a' inside the loop, it gets stuck inside the while and this leads to an infinite loop!
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
int b = 0, a = 0;
for (int j = 0; j < n; j++)
{
a = arr[j];
if (a == 0) //here you used while use if as you are checking the condition every iteration of loop.
{
b++; //here instead of using b=b++ use this.
}
}
cout << b;
return 0; //return is optional.
}
just do this in the j for loop. you don't need any a variable or anything
and also runtime variable arrays are not allowed in C++ use dynamic arrays or vectors
for(int j=0; j<n; j++)
{
if(arr[j]==0){
b++;
}
}

Why is my program getting terminated when i enter user input?

I was practicing some questions from hacker rank and got stuck in this question called sparse-arrays. (https://www.hackerrank.com/challenges/sparse-arrays/problem) I am a beginner and i am not able to find out the error in my code. Please help. Thank you
I think I am not comparing the strings correctly. I tried using compare function but still it did not work.
#include <iostream>
#include <string>
using namespace std;
int main(){
int n;
cin>>n;
string strings[n];
for(int i=0; i<n; i++){
cin>>strings[i];
}
int m;
cin>>m;
string queries[m];
for(int i=0; i<m; i++){
cin>>queries[i];
}
//comparing
for(int i=0; i<m; i++)
{
int count=0;
for(int j=0; j<n; j++)
{
if(queries[i]==strings[j])
count++;
}
cout<<count<<endl;
}
}
The output should be the number of times the strings in (query) that have appeared in the (strings) but my program is getting terminated please help.
for(int j=0; i<n; j++)
should be
for(int j=0; j<n; j++)

Finding maximus array

I don't understand how this code find the rectangle or square where the sum of all integers are maximum, specially how the adding and subtracting working inside the for loop
if this approach or algorithm has a name, plz let me know
the code is-
#include <iostream>
#include <limits.h>
using namespace std;
int main()
{
int n=3,a[3][3]={-1,1,2,0,3,4,0,-1,0};
int i,j,k,l;
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
if(i>0) a[i][j]+=a[i-1][j];
if(j>0) a[i][j]+=a[i][j-1];
if(i>0&&j>0) a[i][j]-=a[i-1][j-1];
}
}
int ans=INT_MIN;
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
for(k=i; k<n; k++)
{
for(l=j; l<n; l++)
{
int temp=a[k][l];
if(i>0) temp-=a[i-1][j];
if(j>0) temp-=a[i][j-1];
if(i>0 && j>0) temp+=a[i-1][j-1];
if(temp>ans) ans=temp;
}
}
}
}
cout<<ans;
return 0;
}
Thanks

Why is my program pausing after a selection sort function?

I have a simple sorting program being compiled by Dev-C++ 4.9.8.0. I ran the program (yes this compiles) and it simply stops after displaying the line where the vector is displayed for the first time. Note - it does not freeze, it seems to just be taking a pause. In the code, the selection sort comes next so I assume that the error happens there, but there is no error message for me to even figure out what to do!
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <cmath>
#include <ctime>
using namespace std;
void bubbleSort (vector<int>& data)
{
if(data.size() <= 1)
return;
int flag=1;
int temp;
for(int i=1; (i<=data.size()) && flag; i++)
{
flag=0;
for(int j=0; (j<data.size()-1); j++)
{
if(data[j+1] > data[j])
{
temp = data[j];
data[j] = data[j+1];
data[j+1] = temp;
flag=1;
}
}
}
}
void selectionSort(vector<int>& data)
{
int min, temp, n=data.size();
for (int i=0; i<n; i++)
{
min = i;
for (int j=i+1; j<n; j++)
{
if (j<min)
{
temp=i;
i=min;
min=temp;
}
}
}
}
int main()
{
int n;
vector<int> data;
cout<<"Vector length?: "<<endl;
cin>>n;
srand(time(0));
for (int i=0; i<n; i++)
{
data.push_back(rand()%20+1);
}
cout<<"Vector: ";
for (int i=0; i<data.size(); i++)
{
cout<<data[i]<<", ";
}
selectionSort(data);
cout<<"Sorted Vector: ";
for (int i=0; i<data.size(); i++)
{
cout<<data[i]<<", ";
}
system("Pause");
return 0;
}
selectionSort() method has variable 'n' that is completely a random value that happens to be on the stack at that location. You haven't initialized it!
You have a nested loop, which is O(n^2). Say n is 1982734 or some such arbitrarily large number. You are simply looping over 1982734 * 1982734 times. EVENTUALLY it will complete. Why don't you print the value of 'n' inside selectionSort(). Just initialize it with the size of the vector.
As others commented this whole work is in progress.