Need Help in Code Blocks [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 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).

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.

C++ malloc(): memory corruption [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
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.
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.
Improve this question
I am currently going through a fibonacci practice problem on hackerrank and am having a malloc memory corruption error. This is the link to the problem I am doing:
https://www.hackerrank.com/contests/programming-interview-questions/challenges/fibonacci-returns/
Input is 0-10, each number separated by a new line.
For each input, the value at that point in the sequence is printed. It works for small inputs, but after 6 it gets the malloc error. It doesn't seem that the size of the sequence is an issue either, just how many are done in succession.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
vector<int> bigFib(1);
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int x;
while(cin >> x){
if(bigFib.size()-1 >= x){
cout << bigFib[x] << endl;
}
else{
vector<int> fib(x);
fib[0] = 0;
fib[1] = 1;
for(int j = 2; j <= x; j++){
fib[j] = fib[j-1] + fib[j-2];
}
bigFib = fib;
cout << fib[x] << endl;
}
}
return 0;
}
I am pretty new to C++ and can't find the problem. Thanks for your time.
When you create std::vector of size N, you can access elements with index [0, N-1] - which is N elements. You create vector of size x and in your loop:
for(int j = 2; j <= x; j++){
fib[j] = fib[j-1] + fib[j-2];
}
and in this statement
cout << fib[x] << endl;
you try to access element with index equal to x, which is UB. If you do need to access index x create vector with at least x+1 size
In vector<int> fib(x); you declare a vector<int> that has x elements. Those elements are fib[0] through to fib[x - 1]. However, in for(int j = 2; j <= x; j++){ fib[j] = ... you assign to an element out of bounds.
Imagine if x is 1, then you'd expect your fib vector to contain only one element: fib[0]... yet your loop is assigning to fib[1]. Problem? Yup.
I reckon for(int j = 2; j <= x; j++){ should probably be for(int j = 2; j < x; j++){...
... and cout << fib[x] << endl; should be cout << fib[x - 1] << endl;

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

Array of Structs (expected expression error) 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 8 years ago.
Improve this question
I have the following code. I am trying to populate an array with a deck of cards, and I keep encountering the same error "expected expression error" no matter how I code the loops to populate the array.
Can anybody see where I'm going wrong. I think its something painfully simple, that I, who am new to C++, am just missing.
Thanks!!
#include <iostream>
using namespace std;
struct playingCard{
char suit; // heart (1), club (2), spade (3), diamond (4)
int value; // 1 to 13 (ace is LOW)
};
void printArray(playingCard playingCardArray[], int size){
for (int i = 0; i < size; i ++){
cout << playingCardArray[i].suit << ":\t" << playingCardArray[i].value << endl;
}
}
int main()
{
const int ARRAY_SIZE = 52;
playingCard playingCardArray[ARRAY_SIZE];
int i = 1;
int suitLoop = 1;
while (suitLoop == 1){
for (int valueLoop = 1; valueLoop <= 13; valueLoop++){
playingCardArray[i] = {suitLoop, valueLoop},
}
}
printArray(playingCardArray, ARRAY_SIZE);
return 0;
}
To resolve your compilation issue change you inner for loop like this:
for (int valueLoop = 1; valueLoop <= 13; valueLoop++){
playingCardArray[i].suit = suitLoop;
playingCardArray[i].value = valueLoop;
}
Other than compilation your code also has Infinite loop , to resolve this you need to change your main somewhat like this:
int main()
{
const int ARRAY_SIZE = 52;
playingCard playingCardArray[ARRAY_SIZE];
int i = 1;
int suitLoop = 0;
while (suitLoop < ARRAY_SIZE){
for (int valueLoop = 1; valueLoop <= 13; valueLoop++){
playingCardArray[suitLoop].suit = (suitLoop/13 + 1);
playingCardArray[suitLoop++].value = valueLoop;
}
}
printArray(playingCardArray, ARRAY_SIZE);
return 0;
}
Exchanging the comma with a semicolon at the end of playingCardArray[i] = {suitLoop, valueLoop}, solves the problem.

passing two dimensional array to function [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have a problem with passing my created table to fillMatrix function.
Another thing is how to refer to this table in my function. I really appreciate every kind of help. I didn't get any console problem. Program is freezing.
int **createTable(int n)
{
int **table = new int*[n];
for(int i = 0; i<n; i++)
{
table[i] = new int[n];
}
return table;
}
void fillMatrix(int n, int **tab)
{
for(int x = 0; x<=n; x++)
{
for(int y= 0; y<=n; y++)
{
tab[x][y] = 0;
}
}
}
int main()
{
int n;
cout <<"Add size of table";
cin >> n;
int **table = createTable(n);
srand(time(NULL));
fillMatrix(n, table);
return 0;
}
I see only one problem in your code:
for(int x = 1; x <= n; x++)
{
for(int y = 1; y <= n; y++)
{
tab[x][y] = 0;
}
}
Arrays in C++ are zero-based, but you tried to assign tab[n][n] which causes undefined behavior.
Also, you should delete your array once it is no longer needed.
As another solution I suggest you use std::vector instead of dynamically allocated arrays.
it is what they told you about. Your for statement should be like this
for( int i = 0 ; i < n ; i++ )
instead of
for( int i = 0 ; i <= n ; i++ )
The compiler want warn you if you accidentally go beyond the size of an array thats why you didnt have any warnings or errors.
Think about using a vector instead of an array as suggested above.