My program skips the if statement [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 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.

Related

SelectionSorting in c++ (Error : EXC_BAD_ACCESS) [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 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.

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++) {...}

Generating a random out of 2 dice roll [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 fixed the typos that I had in my code, and now it runs perfect
int rollDice(int diceRoll[], int numberRolling) // Random dice rolls
{
int values = 0;
for (int i = 0; i < numberRolling; i++)
{
diceRoll[i] = 1 + rand() % 6;
}
for (int i = 0; i < numberRolling; i++)
{
values = values + diceRoll[i];
}
return values;
}
You have a typo in your first for
for (int i = 0; i < numberRolling, i++)
Should be
for (int i = 0; i < numberRolling; i++)

Accesing an element in vector of pair of integer and vector [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
In the following code, I receive segfault at the last line:
int MAX_ITER = 4, n = 5;
vector< pair<int, vector<int> > > InputVector(MAX_ITER);
srand((unsigned)time(NULL));
for (int i = 0; i < MAX_ITER; i++)
InputVector[i].second.resize(n);
for (int i = 0; i < MAX_ITER; i++) {
InputVector[i].first = i+1;
for (int j = 0; j < InputVector[i].second.size(); i++)
InputVector[i].second[j] = rand()%2;
How to access jth element of InputVector[i].second?
for (int j = 0; j < InputVector[i].second.size(); i++)
^^ should be j++

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