How to avoid array out of range error in mql4 indicator - mql4

How to deal with or handle/avoid the array out of range error here :
for(int i=0; i<=ObjectsTotal()-1; i++)
{
string objName = ObjectName(0,i);
if(StringFind(objName,"Last#")>=0)
{
string tempval= objName;
string temtext= ObjectGetString(0,objName,OBJPROP_TEXT);
StringReplace(tempval,"Last#","");
int tempnum = (int)(tempval);
for(int k=tempnum; k>=tempnum-20; k--)
{
if(
RBuffer1[k] != EMPTY_VALUE)
{
ObjectDelete(0,objName);
DrawText("Last#"+tempnum,Time[tempnum],Low[tempnum],temtext+"_0","Arial",9,clrWhite);
break;
}
else
if(RBuffer2[k] != EMPTY_VALUE)
{
ObjectDelete(0,objName);
DrawText("Last#"+tempnum,Time[tempnum],Low[tempnum],temtext+"_1","Arial",9,clrWhite);
break;
}
}
}
}
I am sure there is something wrong with my logic but cant figure out what exactly , and the indicator works fine on some timeframes but on 2 ,3 timeframes it throws the error array out of range
This is for(int k=tempnum; k>=tempnum-20; k--) where I am receiving the error.

First of all, check the size of "RBuffer1" using "ArraySize":
int iArrSize = ArraySize(RBuffer1);
if (iArrSize >= 20)
{
...
}
more importantly, the array index starts from 0 up to the predefined size (in your case 20 or 21). But you are trying to access the index 20 up to 41. Obviously, you will receive an "Array out of range" error message. Just change your code to something like:
if (RBuffer1[k - 21] != EMPTY_VALUE)
{
...
}

Related

Cs50 speller: not recognising any incorrect words

I'm currently working on the CS50 Speller function. I have managed to compile my code and have finished a prototype of the full program, however it does not work (it doesn't recognise any mispelled words). I am looking through my functions one at a time and printing out their output to have a look at what's going on inside.
// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
char word[LENGTH + 1];
int counter = 0;
FILE *dicptr = fopen(dictionary, "r");
if (dicptr == NULL)
{
printf("Could not open file\n");
return 1;
}
while (fscanf(dicptr, "%s", word) != EOF)
{
printf("%s", word);
node *n = malloc(sizeof(node));
if (n == NULL)
{
unload();
printf("Memory Error\n");
return false;
}
strcpy(n->word, word);
int h = hash(n->word);
n->next = table[h];
table[h] = n;
amount++;
}
fclose(dicptr);
return true;
}
From what I can see this works fine. Which makes me wonder if the issue is with my check function as shown here:
bool check(const char *word)
{
int n = strlen(word);
char copy[n + 1];
copy[n] = '\0';
for(int i = 0; i < n; i++)
{
copy[i] = tolower(word[i]);
printf("%c", copy[i]);
}
printf("\n");
node *cursor = table[hash(copy)];
while(cursor != NULL)
{
if(strcasecmp(cursor->word, word))
{
return true;
}
cursor = cursor->next;
}
return false;
}
If someone with a keener eye can spy what is the issue I'd be very grateful as I'm stumped. The first function is used to load a the words from a dictionary into a hash table\linked list. The second function is supposed to check the words of a txt file to see if they match with any of the terms in the linked list. If not then they should be counted as incorrect.
This if(strcasecmp(cursor->word, word)) is a problem. From man strcasecmp:
Return Value
The strcasecmp() and strncasecmp() functions return an
integer less than, equal to, or greater than zero if s1 (or the first
n bytes thereof) is found, respectively, to be less than, to match, or
be greater than s2.
If the words match, it returns 0, which evaluates to false.

How can I find the number of elements of a multidimentional dynamic array

My pointer is declared in the header file:
int (*array)[10];
I pass a argument to a function that initializes the array:
void __fastcall TForm1::Initarray(const int cnt)
{
try
{
Form1->array = new int[cnt][53];
}
catch(bad_alloc xa)
{
Application->MessageBoxA("Memory allocation error SEL. ", MB_OK);
}
Form1->Zeroarray();
}
//---------------------------------------------------------------------------
I set all element of the array to "0":
void __fastcall TForm1::Zeroarray()
{
__int16 cnt = SIZEOF_ARRAY(array);
// Here is where I notice the problem. cnt is not correct for the size of the first level of the array.
if(cnt)
{
for(int n = 0; n < cnt; n++)
{
for(int x = 0; x < 53; x++)
{
Form1->array[n][x] = 0;
}
}
}
}
//---------------------------------------------------------------------------
This is my defined size of array macro:
#define SIZEOF_ARRAY(a) (sizeof((a))/sizeof((a[0])));
When array is created with 10 elements of 53 elements array[10][53]
I get a return from SIZEOF_ARRAY == 0. It should equal 10.
I have tried several variation of this macro and just doing straight math with the sizeof() but I cannot get the correct output.
What am I not doing?

-[__NSCFNumber value1]: unrecognized selector sent to instance

-(FileGame *)objectAtXandY:(int) posX secondPos:(int)posY
{
if (posX < 0 || posX >= kBoxWidth || posY < 0 || posY >= kBoxHeight)
return OutBorderStart;
return (FileGame*)[[content objectAtIndex:posY]objectAtIndex:posX];
}
-(void) checkWith:(BOOL)orien
{
int iMax = (orien == isLandscape) ? size1.width : size1.height;
int jMax = (orien == isPortrait) ? size1.height : size1.width;
for (int i=0; i<iMax; i++)
{
int count = 0;
int balValue = -1;
for (int j=0; j<jMax; j++)
{
FileGame* tile =[self objectAtXandY:(orien == isLandscape) ?i :j secondPos:(orien == isPortrait) ?j :i];
[readyToRemove addObject:tile];
}
}
FileGame* square = [readyToRemove objectAtIndex:2];
square.value1 = 0;
if(square.value1==balVal)
{
//some thing to do
}
else{
//some thing to do
}
}
Here readyToRemove is NSMutableArray,balVal is int,FileGame is CCNode.While i run this i'm getting
-[__NSCFNumber value1]: unrecognized selector sent to instance error help me.
The object at index 2 of that array is not a FileGame object. It's a NSNumber. Either you've populated the array in a way you didn't intend or, more likely, the object you expected to find there has been released and the address re-used for a different object.
Turn on zombies in your scheme and you will probably get a better error message.
Also, try logging the value of square when you assign it and verify the data type.
Becareful objective-c isn't really type safe until runtime. I would check your code to make sure you're not putting NSNumber's into that array. Put a break point after you pull it out of the array you'll see.

IOS have a report builder generate in PDF

I have data to generate report that have header footer and content.
In the content have many group when the data in each group over the page area it add new page and write header of group and continued content data
Where can I have lib. or something to do this Task ??
Thanks For Advance.
Add for request code
NSArray *headObject = [uniqueStates allObjects];////Store head of each group
NSArray *detail; //Store all of data to present in table
int allData = [detail count]; // Tel amount of all data to show
/*
int headGroupline = 1; //Tel amount of head group line
int footGroupline = 1; //Tel amount of foot group line
*/
int detailIndex = 0; ///Tel what line we are now
int detailHeadIndex = 0; ///Tel what group we are now
int subdetail = 0; // Tel what line on group now
int aviableLineInpage = 24; // Line avilable in page for data to show
int allPage = 0; //Sum of all page
for (; detailIndex < allData; ) {
allPage++;
for (int i=0; i < aviableLineInpage;) {
if (allData - detailIndex == 0) {
///ShowgrandTotal
........
detailIndex++;
i+=25;
}else{
if (i == 0) {
//Show Head Group
......
}else{
if (subdetail == [[detail filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"(buy_date == %#)", headObject[detailHeadIndex]]] count]+1) {
detailHeadIndex++;
//Show Head Group
..........
}else{
if (subdetail == [[detail filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"(buy_date == %#)", headObject[detailHeadIndex]]] count]) {
//Show sum of each group
..........
i++;
detailIndex++;
subdetail++;
detailHeadIndex++;
}else{
//Show data of detail
........................
detailIndex++;
subdetail++;
}
}
}
i++;
}
}//for all line in page
}//for allData
It format is:
date 27/01/2014
data1
data2
data3
sum date 27/01/2014
date 28/01/2014
data1
data2
<============if page brake add new page and
date 28/01/2014
data3
sum date 28/01/2014
grand total........
But it goes
date 27/01/2014
data1
data2
data3
sum date 27/01/2014
data1
data2
<============if page brake add new page and
date 28/01/2014
data3
grand total........
Thanks very much
Use CGPDFDocumentRef and the associated functions to create the PDF document and each page it contains. When you create a page (UIGraphicsBeginPDFPageWithInfo) you can get the associated drawing context (UIGraphicsGetCurrentContext). Once you have that you can draw in a number of ways:
You could create a view with your header and footer, add content and render it into the view (using its layer)
You could draw images and text direct into the context (using CoreText (CTFrameDraw))
You need to create the logic to set how much content can fit on any one page and when you need to create a new page.
Also, depending on how you choose to draw content into the context you may need to scale and transform (CGContextScaleCTM + CGContextTranslateCTM) the context so that your content isn't upside down.
See this Apple guide.
int allData = [detail count]; // Tel amount of all data to show
/*
int headGroupline = 1; //Tel amount of head group line
int footGroupline = 1; //Tel amount of foot group line
*/
int detailIndex = 0; ///Tel what line we are now
int detailHeadIndex = 0; ///Tel what group we are now
int subdetail = 0; // Tel what line on group now
int aviableLineInpage = 24; // Line avilable in page for data to show
int allPage = 0; //Sum of all page
for (; detailIndex < allData; ) {
allPage++;
for (int i=0; i < aviableLineInpage;) {
if (allData - detailIndex == 0) {
detailIndex++;
i+=25;
}else{
if (i == 0) {
if (subdetail == -1) {
subdetail = 0;
}
}else{
if (subdetail == -1) {
subdetail = 0;
}else{
if (subdetail == [[detail filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"(buy_date == %#)", headObject[detailHeadIndex]]] count]) {
i++;
subdetail = -1;
detailHeadIndex++;
if (detailHeadIndex == [headObject count] ) {
detailIndex++;
}
}else{
detailIndex++;
subdetail++;
}
}
}
i++;
}
}//for all line in page
}//for allData

iOS, sorting through two arrays with different counts simultaneously

I am getting an NSRangeException error when test an array's contents (arrayTwo) inside another array enumeration (arrayOne), if arrayTwo does not have a count equal or greater to arrayOne. What is the most efficient way to do this? Basically I want to grab the object from arrayTwo if it exists, else if there is no object there, just ignore the operation.
int i = 0;
for (Class *arrayOneObject in arrayOne) {
if (arrayTwo[i]!= NULL) {
NSLog(#"array two object found");
}
i++;
}
Edit: According to Hot Lick's suggestion, I did the following, works fine.
int i = 0;
for (Class *arrayOneObject in arrayOne) {
if (arrayTwo.count > i) {
NSLog(#"array two object found");
}
i++;
}

Resources