Printing rectangle from instance method - printing

I am having trouble with an instance method that uses a nested for loop to print a rectangle of asterisks. When the code is executed, once it hits the print method the output in the console disappears and the program continues running.
public class Rectangle {
private int length;
private int width;
//Constructor for a rectangle with no parameters
public Rectangle() {
//create a square rectangle with length of 1
length = 1;
width = 1;
}//End Rectangle0
//Constructor for a rectangle with 2 parameters
public Rectangle(int len, int wid) {
//create a rectangle with given parameters
length = len;
width = wid;
}//end Rectangle2
//method to print rectangle
public void print() {
//loop to print rectangle
for(int i = 0; i <= length; i++) {
for(int j = 0; j <= width; i++) {
System.out.print("*");//prints row
}//end nested for loop
System.out.println();//begin new line
}//end for loop
}//End method print
}//End class rectangle
If the length = 3 and width = 4, output should be three rows of asterisks all with 4 columns of asterisks
Instead of that result, once it begins to print the rectangle, all output disappears from the console and the program does not stop running.

You are incrementing 'i' in the inner j loop, Change that to j++.
Also since you are using i <= length so start the loop from i=1 instead of i=0. Same for inner j loop.
Hope this helps.

Related

Mql4 I want to plot the data of the previous array

What I want to do is actually very simple. Create an array and pass any data to that array. Then I want to take the previous data of this array and draw it on the screen, but I cannot access the previous data of the array.
int start()
{
double up[];
int limit;
int counted_bars=IndicatorCounted();
if(counted_bars<0) return(-1);
if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;
for(int i=0; i<limit; i++)
{
up[i] = Close[i]; //I am assigning the closing data to this array as an example.
Line[i] = up[i+1]; //Line array is the name of the array to be displayed on the screen. I want to assign the value of the previous up array to this array but it doesn't work.
}
}

How to convert send order from mql4 to mql5

Below is the method I'm using to place an order after three minutes if it doesn't go through. I've converted the larger part of it from mql4 to mql5. It's just the commented part that I'm not sure how I'll change to mql5 since in mql5 send orders return bool's and not int's. I would be glad if I could get help with fixing this remaining part.
void MakeOrders()
{
static datetime lastTime = 0;
datetime currTime = iTime(Symbol(),PERIOD_M3,0);
if (currTime>lastTime)
{
for (int i=ObjectsTotal(0, 0, -1)-1; i>=0; i--)
{
string name = ObjectName(0, i, 0, -1);
if (ObjectGetString(0, name, OBJPROP_NAME, 0)==OBJ_RECTANGLE && ObjectGetString(0,name,OBJPROP_TEXT)=="")
{
double entryPrice=ObjectGetDouble(0,name,OBJPROP_PRICE,1)-3*_Point;
double stopLoss=ObjectGetDouble(0,name,OBJPROP_PRICE,2);
double slDist=fabs(entryPrice-stopLoss);
double dTakeProfit=entryPrice-2*slDist;
MqlTradeRequest request={0};
MqlTradeResult result={0};
//--- parameters of request
request.action =TRADE_ACTION_DEAL; // type of trade operation
request.symbol =Symbol(); // symbol
request.volume =lotSize; // volume of 0.1 lot
request.type =ORDER_TYPE_BUY_LIMIT; // order type
request.price = entryPrice; // price for opening
//request.deviation=5; // allowed deviation from the price
request.magic =magicnumber; // MagicNumber of the order
request.tp = dTakeProfit;
request.sl = stopLoss;
//--- send the request
if(!OrderSend(request,result))
PrintFormat("OrderSend error %d",GetLastError());
/*
int ticketSell = OrderSend(Symbol(),OP_SELLLIMIT,lotSize, entryPrice,0,stopLoss,dTakeProfit,"SellOrder",magicnumber,0,Red);
if (ticketSell>0)
{
ObjectSetText(name,string(ticketSell));
i = ObjectsTotal()-1; // rather than continuing the 'for' loop, we must restart because arrows (ie new objects) were created.
}
*/
}
}
lastTime = currTime;
}
}
if(result.retcode==10009 || result.order>0)
ObjectSetText(name,string(result.order));

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?

Objective C " * " syntax and usage

I am re-writing the particle filter library of iOS in Swift from Objective C which is available on Bitbucket and I have a question regarding a syntax of Objective C which I cannot understand.
The code goes as follows:
- (void)setRssi:(NSInteger)rssi {
_rssi = rssi;
// Ignore zeros in average, StdDev -- we clear the value before setting it to
// prevent old values from hanging around if there's no reading
if (rssi == 0) {
self.meters = 0;
return;
}
self.meters = [self metersFromRssi:rssi];
NSInteger* pidx = self.rssiBuffer;
*(pidx+self.bufferIndex++) = rssi;
if (self.bufferIndex >= RSSIBUFFERSIZE) {
self.bufferIndex %= RSSIBUFFERSIZE;
self.bufferFull = YES;
}
if (self.bufferFull) {
// Only calculate trailing mean and Std Dev when we have enough data
double accumulator = 0;
for (NSInteger i = 0; i < RSSIBUFFERSIZE; i++) {
accumulator += *(pidx+i);
}
self.meanRssi = accumulator / RSSIBUFFERSIZE;
self.meanMeters = [self metersFromRssi:self.meanRssi];
accumulator = 0;
for (NSInteger i = 0; i < RSSIBUFFERSIZE; i++) {
NSInteger difference = *(pidx+i) - self.meanRssi;
accumulator += difference*difference;
}
self.stdDeviationRssi = sqrt( accumulator / RSSIBUFFERSIZE);
self.meanMetersVariance = ABS(
[self metersFromRssi:self.meanRssi]
- [self metersFromRssi:self.meanRssi+self.stdDeviationRssi]
);
}
}
The class continues with more code and functions which are not important and what I do not understand are these two lines
NSInteger* pidx = self.rssiBuffer;
*(pidx+self.bufferIndex++) = rssi;
Variable pidx is initialized to the size of a buffer which was previously defined and then in the next line the size of that buffer and buffer plus one is equal to the RSSI variable which is passed as a parameter in the function.
I assume that * has something to do with reference but I just can't figure out the purpose of this line. Variable pidx is used only in this function for calculating trailing mean and standard deviation.
Let explain those code:
NSInteger* pidx = self.rssiBuffer; means that you are getting pointer of the first value of the buffer.
*(pidx+self.bufferIndex++) = rssi; means that you are setting the value of the buffer at index 0+self.bufferIndex to rssiand then increase bufferIndex by 1. Thanks to #Jakub Vano point it out.
In C++, it will look like that
int self.rssiBuffer[1000]; // I assume we have buffer like that
self.rssiBuffer[self.bufferIndex++] = rssi

Accelerate framework "sign" function

I'm trying to find a super fast way of getting the sign of each value in a vector. I was hoping to find a function in the accelerate framework to do this, but couldn't find one. Here's what it would do:
float *inputVector = .... // some audio vector
int length = ...// length of input vector.
float *outputVector = ....// result
for( int i = 0; i<length; i++ )
{
if( inputVector[i] >= 0 ) outputVector[i] = 1;
else outputVector[i] = -1;
}
Ok, I think I've found a way...
vvcopysignf() "Copies an array, setting the sign of each value based on a second array."
So, one method would be to make an array of 1s, then use this function to change the sign of the 1s based on an input array.
float *ones = ... // a vector filled with 1's
float *input = .... // an input vector
float *output = ... // an output vector
int bufferSize = ... // size of the vectors;
vvcopysignf(output, ones, input, &bufferSize);
//output now is an array of -1s and 1s based the sign of the input.

Resources