SelectionSorting in c++ (Error : EXC_BAD_ACCESS) [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I just recently learned to do selection sorting an array. I am also using X-Code on a mac.
I thought I did everything correctly, but I seem to keep getting this error message on the if statement :
Thread 1: EXC_BAD_ACCESS(code=1, address=0x7fff5fc00000).
What am I doing wrong?
using namespace std;
void selectionSorting(int array[], int n)
{
for(int i = 0; i < n-1; n++)
{
int min = i;
for(int j = i + 1; j < n; j++)
{
if(array[j] < array[min]) //Thread 1: EXC_BAD_ACCESS(code=1, address=0x7fff5fc00000)
min = j;
}
int temp = array[i];
array[i] = array[min];
array[min] = temp;
}
}
int main()
{
int n = 10;
int array[]= {10,9,8,7,6,5,4,3,2,1};
selectionSorting(array, n);
for(int x=0; x < n; x++)
{
cout << array[x] << " ";
}
return 0;
}

You have a logical error at for(int i = 0; i < n-1; n++). It should be for(int i = 0; i < n-1; i++) (iterate through the elements of the array).
Also EXC_BAD_ACCESS suggests that your are trying to access a piece of memory that is no longer accessible or it doesn't go well with the intended use.
See that this occurs at if(array[j] < array[min]), which is obvious because j is going beyond the array length as you do n++.
As suggested in the comments try using a debugger.

Related

Visual Studio 2015 IDE breaking for no reason [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Whenever I run this code, my IDE is breaking (no specific error just this Windows messages that says Programm doesn't work anymore).
Could you check if it is due to my code?
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i; int array1[10], array2[10];
for (i = 0; 1 < 10; i++) {
array1[i] = i;
array2[i] = i;
}
array2[9] = 30;
for (i = 0; i < 10; i++) {
if (array1[i] == array2[i]) {
continue;
}
else {
printf("Die Arrays unterscheiden sich an Position %d\n", i);
break;
}
}
return 0;
}
Yes, I assume it is your code.
1 < 10 is always true. So for (i = 0; 1 < 10; i++) {...} runs forever.
I imagine what you meant to do was more like...
for (i = 0; i < 10; i++) {...}

My program skips the if statement [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm writing a program that would make a power of a matrix.
as you see, I'm trying to ask at the for (int n...) loop if n==0,
but when I'm debugging - I see that the program just skips the condition and doesn't even enter it. I mean it doesn't even "ask" the question if n==0...
What is the problem?
void Matrix::pow(int power, Matrix & result)
{
for (int i = 0; i < power-1; i++)
{
for (int j = 0; j < rows; j++)
{
for (int k = 0; k < cols; k++)
{
for (int n = 0; n < cols; n++)
{
if (n==0)
{
(&result)->_array[i][j] == 0; //Reset result's array.
}
(&result)->_array[i][j] += this->_array[i][n] * this->_array[n][j];
}
}
}
}
}
This is a boolean expression, not an assignment.
(&result)->_array[i][j] == 0; //Reset result's array.

Printing 2D arrays? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I have encountered a problem with printing 2D arrays. Here is my code, any help will be greatly appreciated.
#include<iostream>
using namespace std;
int main()
{
int NumRow, NumColumn;
int anArray[2][2] = {{1,2},{3,4}};
for (int N_column = 1; N_column < NumColumn; N_column++)
{
for (int N_row = 1; N_row < NumRow; N_row++)
{
cout << anArray[N_row,N_column];
}
}
return 0;
}
3 problems:
Array indexes start at 0.
NumColumn, NumRow are uninitialized.
wrong syntax [y,j], use [i][j]
Try like this:
...
int NumRow = 2, NumColumn = 2;
int anArray[2][2] = {{1,2},{3,4}};
for (int N_column = 0; N_column < NumColumn; N_column++)
{
for (int N_row = 0; N_row < NumRow; N_row++)
{
cout << anArray[N_row][N_column];
}
}
...
You declare
int NumRow, NumColumn;
but you never assign a value to them. Use
int NumRow = 2, NumColumn = 2;
instead. Also, C-arrays start at 0, not at 1, so you must update your for-loops as well:
for (int N_column = 0; ...
for (int N_row = 0; ...
Last, change the output statement, as multidimensional arrays need to be reached in a different way:
cout << anArray[N_row][N_column];
There are few issues in your code:
1st: You declare NumRow, NumColumn but use them without initializing them before which cause to Undefined Behaviour.
Solution: Initialized them
NumRow = 2;
NumColumn = 2;
2nd: Array syntax in the following line-
cout << anArray[N_row,N_column];
it should be
cout << anArray[N_row][N_column];
3rd: C++ arrays are zero indexed, so you should start initializing the loop control variables like following:
for (int N_column = 0; N_column < NumColumn; N_column++)
{ ^^^
for (int N_row = 0; N_row < NumRow; N_row++)
{ ^^^^
//...

Need Help in Code Blocks [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
This code executed successfully in Visual C++, but showed run time error in Code
Blocks returning "Process terminated with status -1073741819". Also when MAX is
defined as "#define MAX 4" it executed successfully. Can anyone please help?
Thanx!
#include <iostream>
#include <cstdlib>
#define MAX 32
using namespace std;
double **A, **B, **C;
void initialize(double** x) //code to initialize matrix
{
static int n = 0;
for(int i = 0; i < MAX; i++)
*(x+i) = (double*) new double[MAX];
srand(n);
double* ptr = *x;
for(int i = 0; i < MAX; i++)
for(int j = 0; j < MAX; j++)
*(ptr+(i*MAX)+j) = rand() % 100;
n++;
}
void print(double** x)
{
double* ptr = *x;
for(int i = 0; i < MAX; i++){
for(int j = 0; j < MAX; j++)
cout<<*(ptr+(i*MAX)+j)<<" ";
cout<<endl;
}
}
int main(){
A = (double**) new double[MAX];
B = (double**) new double[MAX];
C = (double**) new double[MAX];
initialize(A);
initialize(B);
initialize(C);
print(A);
cout<<endl;
print(B);
cout<<endl;
print(C);
cout<<endl;
system("pause");
return 0;
}
In place of "*(ptr+(i*MAX)+j)" to access x[i,j] use " * ( *(x+i)+j) " .
This will resolve the segmentation fault.
"*(ptr+(i*MAX)+j)" is same as ptr[i*MAX+j] which is an out-of-bounds access for some values of i and j.
your variables should be double *, not double ** (pointers to pointers).

Nested For Loop C++ [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
Can some one please point out what is causing the nested for loop to not be executed in the below code sample. The "for (int j = 40; j < =0; j-=5)" loop is not being executed.
void printTable(int windS, int windL)
{
for (int i = windS; i <= windL; i+=5)
{
for (int j = 40; j <=0; j-=5)
{
cout << " " << windChill(j, i);
}
}
}
Thanks in advance.
Because:
for (int j = 40; j <= 0; j -= 5)
will never execute. The j <= 0 will start as 40 <= 0 which results in false.
What you probably meant was:
for (int j = 40; j >= 0; j -= 5)
// ^^
int j=40 is an initializer
then follows condition j <=0 which is never true. You probably meant j>=0
And the action to perform on each iteration j-=5