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++)
{ ^^^^
//...
Related
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.
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 6 years ago.
Improve this question
I'm still new to c++ so this is a learning process for me. Also i know that i should initially use a vector to do this but i have an exercise that specifies an array so i'm trying to write a function that removes all duplicate elements in an array but i receive the error
C2100: illegal indirection
if someone could point me in the right direction
int main()
{
int *t;
int removel[9] = { 1, 1, 1, 2, 3, 4, 5, 6, 6, };
t = removeAll(removel, 9, 1);
for (int i = 0; i < 8; i++)
cout << t[i] << " ";
}
int* removeAll(int list[], int listlength, int removeitem)
{
int count = 0;
int* list2;
int removeindex;
int length;
int tempindex;
for (int i = 0; i < listlength; i++)
{
if (removeitem == list[i])
count++;
}
length = listlength - (count + 1);
list2 = new int[length];
int j;
while (j<=length)
{
remove_if(list[0], list[listlength - 1], removeitem);
for (j = 0; j < length; j++)
if (list[j] == NULL)// not sure what the remove_if func puts inplace of the removed element
continue;
else
list2[j] = list[j];
}
return list2;
}
Firstable you should calculate length like listlength - count, not listlength - (count + 1).
Then, after list2 = new int[length]; you should copy elements which are different with removeitem and skip other ones. You can do it like this
int j = 0;
for (int i = 0; i < listlength; i++) {
if (removeitem == list[i])
continue;
list2[j] = list[i];
j++;
}
and return successfully created list2. But you should also know its size. You can do it by creating int tSize in main and pass it to removeAll by link. removeAll will change its value to length. So add int & list2size to the parameters list of removeAll and write list2size = length; before returning list2. Finally, when printing t, change i < 8 to i < tSize.
If you do all this program will work right but don't forget about formatting.
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 couldn't figure out why but this is my code :
char expression[256] = {};
cin >> expression;
cout << endl;
int** variableTable = NULL;
int numOfVals = getNumberOfVariables(expression);
variableTable = static_cast<int**>(calloc(numOfVals, sizeof(int*)));
for (int i = 0; i < numOfVals; i++)
{
variableTable[i] = static_cast<int*>(calloc(2, sizeof(int)));
}
fillPromoterTable(expression, variableTable, numOfVals);
this is the fillPromoterTable
void fillPromoterTable(const char* expression, int** variableTable, int numOfVals)
{
char promoter[15] = {};
char *token;
char* expCpy = pcstrdup(expression);
for (int i = 0; numOfVals; i++)
{
token = strtok(expCpy, "+-*/");
int nLen = istrlen(token);
for (int j = 0; j < nLen; j++)
{
if (isdigit(token[j]))
promoter[j] = token[j];
if (isalpha(token[j]))
break;
}
variableTable[i][0] = atoi(promoter);
memset(promoter, '\0', 15);
token = strtok(NULL, "+-*/");
}
free(token);
free(expCpy);
}
in this line :
variableTable[i][0] = atoi(promoter);
I get an error saying I'm trying to write to address 0xFDFDFDFD
I couldn't figure out why it happens I could use some help.
Change the for loop condition in the function fillPromoterTable
for (int i = 0; numOfVals; i++)
To:
for (int i = 0; i < numOfVals; i++)
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.
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).