how to reduce if else condition in code? - ios

I am new beginner in iOS, i want to remove if else condition and use proper code for this following code ..
can anyone help me for this ?
I have to Many condition
if (B == 99)
{
B = 11;
[Search_color addObject:Z];
}else if(B == 4)
{
B = 1;
[Search_color addObject:D];
}else if (B == 5)
{
B = 2;
[Search_color addObject:E];
}else if (A == 14)
{
B = 11; A = 10;
[Search_color addObject:N];
[Search_color addObject:Z];
}
else if (B == 26)
{
B = 11;
[Search_color addObject:Z];
}

can use switch with same result :
switch (B) {
case 99:
B = 11;
//[Search_color addObject:Z];
break;
case 4:
B = 1;
//[Search_color addObject:D];
break;
case 5:
B = 2;
//[Search_color addObject:E];
break;
case 26:
if (A != 14) {
B = 11;
//[Search_color addObject:Z];
break;
}
default:
if (A == 14) {
B = 11; A = 10;
//[Search_color addObject:N];
//[Search_color addObject:Z];
}
break;
}
Code use switch in function
- (void)yourFunction
{
NSInteger B, A;
B = 99;
switch (B) {
case 99:
B = 11;
//[Search_color addObject:Z];
break;
case 4:
B = 1;
//[Search_color addObject:D];
break;
case 5:
B = 2;
//[Search_color addObject:E];
break;
case 26:
if (A != 14) {
B = 11;
//[Search_color addObject:Z];
break;
}
default:
if (A == 14) {
B = 11; A = 10;
//[Search_color addObject:N];
//[Search_color addObject:Z];
}
break;
}
NSLog(#"B = %ld", B);
}
output: B = 11.

Related

MQL4 increase the width of the COG

Here is the code:
#property indicator_chart_window
#property indicator_buffers 5
#property indicator_color1 RoyalBlue
#property indicator_color2 LimeGreen
#property indicator_color3 LimeGreen
#property indicator_color4 Goldenrod
#property indicator_color5 Goldenrod
//-----------------------------------
extern int bars_back = 100;
extern int m = 2;
extern int i = 0;
extern double kstd = 2.0;
extern int sName = 1102;
//----------------------
double fx[], sqh[], sql[], stdh[], stdl[];
double ai[10,10], b[10], x[10], sx[20];
double sum;
int ip, p, n, f;
double qq, mm, tt;
int ii, jj, kk, ll, nn;
double sq, std;
//*******************************************
int init()
{
IndicatorShortName("Center of Gravity");
SetIndexStyle(0, DRAW_LINE, 2 );
SetIndexBuffer(0, fx);
SetIndexBuffer(1, sqh);
SetIndexBuffer(2, sql);
SetIndexBuffer(3, stdh);
SetIndexBuffer(4, stdl);
p = MathRound(bars_back);
nn = m + 1;
ObjectCreate("pr" + sName, 22, 0, Time[p], fx[p]);
ObjectSet("pr" + sName, 14, 159);
return(0);
}
//----------------------------------------------------------
int deinit()
{
ObjectDelete("pr" + sName);
}
//**********************************************************************************************
int start()
{
int mi;
//-------------------------------------------------------------------------------------------
ip = iBarShift(Symbol(), Period(), ObjectGet("pr" + sName, OBJPROP_TIME1));
p = bars_back;
sx[1] = p + 1;
SetIndexDrawBegin(0, Bars - p - 1);
SetIndexDrawBegin(1, Bars - p - 1);
SetIndexDrawBegin(2, Bars - p - 1);
SetIndexDrawBegin(3, Bars - p - 1);
SetIndexDrawBegin(4, Bars - p - 1);
//----------------------sx-------------------------------------------------------------------
for(mi = 1; mi <= nn * 2 - 2; mi++)
{
sum = 0;
for(n = i; n <= i + p; n++)
{
sum += MathPow(n, mi);
}
sx[mi + 1] = sum;
}
//----------------------syx-----------
for(mi = 1; mi <= nn; mi++)
{
sum = 0.00000;
for(n = i; n <= i + p; n++)
{
if(mi == 1)
sum += Close[n];
else
sum += Close[n] * MathPow(n, mi - 1);
}
b[mi] = sum;
}
//===============Matrix=======================================================================================================
for(jj = 1; jj <= nn; jj++)
{
for(ii = 1; ii <= nn; ii++)
{
kk = ii + jj - 1;
ai[ii, jj] = sx[kk];
}
}
//===============Gauss========================================================================================================
for(kk = 1; kk <= nn - 1; kk++)
{
ll = 0; mm = 0;
for(ii = kk; ii <= nn; ii++)
{
if(MathAbs(ai[ii, kk]) > mm)
{
mm = MathAbs(ai[ii, kk]);
ll = ii;
}
}
if(ll == 0)
return(0);
if(ll != kk)
{
for(jj = 1; jj <= nn; jj++)
{
tt = ai[kk, jj];
ai[kk, jj] = ai[ll, jj];
ai[ll, jj] = tt;
}
tt = b[kk]; b[kk] = b[ll]; b[ll] = tt;
}
for(ii = kk + 1; ii <= nn; ii++)
{
qq = ai[ii, kk] / ai[kk, kk];
for(jj = 1; jj <= nn; jj++)
{
if(jj == kk)
ai[ii, jj] = 0;
else
ai[ii, jj] = ai[ii, jj] - qq * ai[kk, jj];
}
b[ii] = b[ii] - qq * b[kk];
}
}
x[nn] = b[nn] / ai[nn, nn];
for(ii = nn - 1; ii >= 1; ii--)
{
tt = 0;
for(jj = 1; jj <= nn - ii; jj++)
{
tt = tt + ai[ii, ii + jj] * x[ii + jj];
x[ii] = (1 / ai[ii, ii]) * (b[ii] - tt);
}
}
//===========================================================================================================================
for(n = i; n <= i + p; n++)
{
sum = 0;
for(kk = 1; kk <= m; kk++)
{
sum += x[kk + 1] * MathPow(n, kk);
}
fx[n] = x[1] + sum;
}
//-----------------------------------Std-----------------------------------------------------------------------------------
sq = 0.0;
for(n = i; n <= i + p; n++)
{
sq += MathPow(Close[n] - fx[n], 2);
}
sq = MathSqrt(sq / (p + 1)) * kstd;
std = iStdDev(NULL, 0, p, MODE_SMA, 0, PRICE_CLOSE, i) * kstd;
for(n = i; n <= i + p; n++)
{
sqh[n] = fx[n] + sq;
sql[n] = fx[n] - sq;
stdh[n] = fx[n] + std;
stdl[n] = fx[n] - std;
}
//-------------------------------------------------------------------------------
ObjectMove("pr" + sName, 0, Time[p], fx[p]);
//----------------------------------------------------------------------------------------------------------------------------
return(0);
}
//==========================================================================================================================
I would like the increase the lines width to 2.
I solved with
SetIndexStyle(0, DRAW_LINE, EMPTY,2, indicator_color1);
SetIndexStyle(1, DRAW_LINE, EMPTY,2, indicator_color2);
SetIndexStyle(2, DRAW_LINE, EMPTY,2, indicator_color3);
SetIndexStyle(3, DRAW_LINE, EMPTY,2, indicator_color4);
SetIndexStyle(4, DRAW_LINE, EMPTY,2, indicator_color5);

How to insert an alert into an MQL4 indicator-code?

I have a free MQL4 indicator that displays the most important resistance a support based on the timeframe.
It's a fantastic indicator, but I need an implementation with a message and a sound alert when a support or resistance is touched.
How can I insert alert only when H1, H4, D1, W1 and MN1 are touched?
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Black
extern int LB = 3;
extern int maxBarsForPeriod = 1000;
extern bool showM01 = TRUE;
extern bool showM05 = TRUE;
extern bool showM15 = TRUE;
extern bool showM30 = TRUE;
extern bool showH01 = TRUE;
extern bool showH04 = TRUE;
extern bool showD01 = TRUE;
extern bool showW01 = TRUE;
extern bool showMN1 = TRUE;
int gi_120 = 0;
int gi_124 = 0;
int gi_128 = 0;
int gi_132 = 0;
int gi_136 = 0;
int gi_140 = 0;
int gi_144 = 0;
int gi_148 = 0;
int gi_152 = 0;
int gi_156 = 0;
int gi_160 = 0;
int gi_164 = 0;
int gi_168 = 0;
int gi_172 = 0;
int gi_176 = 0;
int gi_180 = 0;
int gi_184 = 0;
int gi_188 = 0;
double gda_192[];
double gda_196[];
double gda_200[];
double gda_204[];
double gda_208[];
double gda_212[];
double gda_216[];
double gda_220[];
double gda_224[];
double gda_228[];
double gda_232[];
double gda_236[];
double gda_240[];
double gda_244[];
double gda_248[];
double gda_252[];
double gda_256[];
double gda_260[];
int init() {
IndicatorShortName("3 Line Break On Chart +levels");
set_prevBarTime(1, 0);
set_prevBarTime(5, 0);
set_prevBarTime(15, 0);
set_prevBarTime(30, 0);
set_prevBarTime(60, 0);
set_prevBarTime(240, 0);
set_prevBarTime(1440, 0);
set_prevBarTime(10080, 0);
set_prevBarTime(43200, 0);
return (0);
}
int deinit() {
DeleteHLineObject(StringConcatenate(getPeriodAsString(1), " Sup"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(1), " Res"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(1), " Sup C"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(1), " Res C"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(5), " Sup"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(5), " Res"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(5), " Sup C"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(5), " Res C"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(15), " Sup"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(15), " Res"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(15), " Sup C"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(15), " Res C"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(30), " Sup"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(30), " Res"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(30), " Sup C"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(30), " Res C"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(60), " Sup"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(60), " Res"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(60), " Sup C"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(60), " Res C"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(240), " Sup"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(240), " Res"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(240), " Sup C"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(240), " Res C"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(1440), " Sup"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(1440), " Res"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(1440), " Sup C"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(1440), " Res C"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(10080), " Sup"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(10080), " Res"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(10080), " Sup C"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(10080), " Res C"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(43200), " Sup"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(43200), " Res"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(43200), " Sup C"));
DeleteHLineObject(StringConcatenate(getPeriodAsString(43200), " Res C"));
return (0);
}
double Diap(int ai_0, bool ai_4, int ai_8, int ai_12) {
double ld_ret_20;
if (ai_4) {
ld_ret_20 = get_max(ai_0, ai_12);
for (int li_16 = 1; li_16 < ai_8; li_16++)
if (get_max(ai_0, ai_12 - li_16) > ld_ret_20) ld_ret_20 = get_max(ai_0, ai_12 - li_16);
}
if (!ai_4) {
ld_ret_20 = get_min(ai_0, ai_12);
for (li_16 = 1; li_16 < ai_8; li_16++)
if (get_min(ai_0, ai_12 - li_16) < ld_ret_20) ld_ret_20 = get_min(ai_0, ai_12 - li_16);
}
return (ld_ret_20);
}
void EmulateDoubleBuffer(double ada_0[], int ai_4) {
if (ArraySize(ada_0) < ai_4) {
ArraySetAsSeries(ada_0, FALSE);
ArrayResize(ada_0, ai_4);
ArraySetAsSeries(ada_0, TRUE);
}
}
void DeleteHLineObject(string a_name_0) {
ObjectDelete(a_name_0);
ObjectDelete(a_name_0 + "_Label");
}
void ShowHLineObject(string a_name_0, int ai_8, int a_style_12, double ad_16, int ai_24) {
if (ObjectFind(a_name_0) != 0) CreateHLineObject(a_name_0, ai_8, a_style_12, ad_16, ai_24);
ObjectSet(a_name_0 + "_Label", OBJPROP_PRICE1, ad_16);
ObjectSet(a_name_0 + "_Label", OBJPROP_TIME1, Time[0] + Period() * ai_24);
ObjectSet(a_name_0 + "_Label", OBJPROP_STYLE, a_style_12);
ObjectSet(a_name_0, OBJPROP_PRICE1, ad_16);
}
void CreateHLineObject(string a_text_0, color a_color_8, int a_style_12, double a_price_16, int ai_24) {
ObjectCreate(a_text_0 + "_Label", OBJ_TEXT, 0, Time[0] + Period() * ai_24, a_price_16);
ObjectSetText(a_text_0 + "_Label", a_text_0, 7, "Verdana", a_color_8);
ObjectCreate(a_text_0, OBJ_HLINE, 0, Time[0], a_price_16);
ObjectSet(a_text_0, OBJPROP_STYLE, a_style_12);
ObjectSet(a_text_0, OBJPROP_COLOR, a_color_8);
ObjectSet(a_text_0, OBJPROP_WIDTH, 1);
}
string getPeriodAsString(int ai_0) {
string ls_ret_4 = 0;
switch (ai_0) {
case 1:
ls_ret_4 = "M1";
break;
case 5:
ls_ret_4 = "M5";
break;
case 15:
ls_ret_4 = "M15";
break;
case 30:
ls_ret_4 = "M30";
break;
case 60:
ls_ret_4 = "H1";
break;
case 240:
ls_ret_4 = "H4";
break;
case 1440:
ls_ret_4 = "D1";
break;
case 10080:
ls_ret_4 = "W1";
break;
case 43200:
ls_ret_4 = "MN1";
}
return (ls_ret_4);
}
void set_prevBarTime(int ai_0, int ai_4) {
switch (ai_0) {
case 1:
gi_120 = ai_4;
return;
case 5:
gi_124 = ai_4;
return;
case 15:
gi_128 = ai_4;
return;
case 30:
gi_132 = ai_4;
return;
case 60:
gi_136 = ai_4;
return;
case 240:
gi_140 = ai_4;
return;
case 1440:
gi_144 = ai_4;
return;
case 10080:
gi_148 = ai_4;
return;
case 43200:
gi_152 = ai_4;
return;
return;
}
}
int get_prevBarTime(int ai_0) {
switch (ai_0) {
case 1:
return (gi_120);
break;
case 5:
return (gi_124);
break;
case 15:
return (gi_128);
break;
case 30:
return (gi_132);
break;
case 60:
return (gi_136);
break;
case 240:
return (gi_140);
break;
case 1440:
return (gi_144);
break;
case 10080:
return (gi_148);
break;
case 43200:
return (gi_152);
}
return (0);
}
void set_prevBarCount(int ai_0, int ai_4) {
switch (ai_0) {
case 1:
gi_156 = ai_4;
return;
case 5:
gi_160 = ai_4;
return;
case 15:
gi_164 = ai_4;
return;
case 30:
gi_168 = ai_4;
return;
case 60:
gi_172 = ai_4;
return;
case 240:
gi_176 = ai_4;
return;
case 1440:
gi_180 = ai_4;
return;
case 10080:
gi_184 = ai_4;
return;
case 43200:
gi_188 = ai_4;
return;
return;
}
}
int get_prevBarCount(int ai_0) {
switch (ai_0) {
case 1:
return (gi_156);
break;
case 5:
return (gi_160);
break;
case 15:
return (gi_164);
break;
case 30:
return (gi_168);
break;
case 60:
return (gi_172);
break;
case 240:
return (gi_176);
break;
case 1440:
return (gi_180);
break;
case 10080:
return (gi_184);
break;
case 43200:
return (gi_188);
}
return (0);
}
void set_max(int ai_0, int ai_4, double ad_8) {
switch (ai_0) {
case 1:
gda_192[ai_4] = ad_8;
return;
case 5:
gda_196[ai_4] = ad_8;
return;
case 15:
gda_200[ai_4] = ad_8;
return;
case 30:
gda_204[ai_4] = ad_8;
return;
case 60:
gda_208[ai_4] = ad_8;
return;
case 240:
gda_212[ai_4] = ad_8;
return;
case 1440:
gda_216[ai_4] = ad_8;
return;
case 10080:
gda_220[ai_4] = ad_8;
return;
case 43200:
gda_224[ai_4] = ad_8;
return;
return;
}
}
double get_max(int ai_0, int ai_4) {
switch (ai_0) {
case 1:
return (gda_192[ai_4]);
break;
case 5:
return (gda_196[ai_4]);
break;
case 15:
return (gda_200[ai_4]);
break;
case 30:
return (gda_204[ai_4]);
break;
case 60:
return (gda_208[ai_4]);
break;
case 240:
return (gda_212[ai_4]);
break;
case 1440:
return (gda_216[ai_4]);
break;
case 10080:
return (gda_220[ai_4]);
break;
case 43200:
return (gda_224[ai_4]);
}
return (0.0);
}
void set_min(int ai_0, int ai_4, double ad_8) {
switch (ai_0) {
case 1:
gda_228[ai_4] = ad_8;
return;
case 5:
gda_232[ai_4] = ad_8;
return;
case 15:
gda_236[ai_4] = ad_8;
return;
case 30:
gda_240[ai_4] = ad_8;
return;
case 60:
gda_244[ai_4] = ad_8;
return;
case 240:
gda_248[ai_4] = ad_8;
return;
case 1440:
gda_252[ai_4] = ad_8;
return;
case 10080:
gda_256[ai_4] = ad_8;
return;
case 43200:
gda_260[ai_4] = ad_8;
return;
return;
}
}
double get_min(int ai_0, int ai_4) {
switch (ai_0) {
case 1:
return (gda_228[ai_4]);
break;
case 5:
return (gda_232[ai_4]);
break;
case 15:
return (gda_236[ai_4]);
break;
case 30:
return (gda_240[ai_4]);
break;
case 60:
return (gda_244[ai_4]);
break;
case 240:
return (gda_248[ai_4]);
break;
case 1440:
return (gda_252[ai_4]);
break;
case 10080:
return (gda_256[ai_4]);
break;
case 43200:
return (gda_260[ai_4]);
}
return (0.0);
}
void emulate_tlbmaxmin(int ai_0, int ai_4) {
switch (ai_0) {
case 1:
EmulateDoubleBuffer(gda_192, ai_4);
EmulateDoubleBuffer(gda_228, ai_4);
return;
case 5:
EmulateDoubleBuffer(gda_196, ai_4);
EmulateDoubleBuffer(gda_232, ai_4);
return;
case 15:
EmulateDoubleBuffer(gda_200, ai_4);
EmulateDoubleBuffer(gda_236, ai_4);
return;
case 30:
EmulateDoubleBuffer(gda_204, ai_4);
EmulateDoubleBuffer(gda_240, ai_4);
return;
case 60:
EmulateDoubleBuffer(gda_208, ai_4);
EmulateDoubleBuffer(gda_244, ai_4);
return;
case 240:
EmulateDoubleBuffer(gda_212, ai_4);
EmulateDoubleBuffer(gda_248, ai_4);
return;
case 1440:
EmulateDoubleBuffer(gda_216, ai_4);
EmulateDoubleBuffer(gda_252, ai_4);
return;
case 10080:
EmulateDoubleBuffer(gda_220, ai_4);
EmulateDoubleBuffer(gda_256, ai_4);
return;
case 43200:
EmulateDoubleBuffer(gda_224, ai_4);
EmulateDoubleBuffer(gda_260, ai_4);
return;
return;
}
}
void displayPeriod(int a_timeframe_0) {
int li_4;
int l_count_8;
int li_12;
int li_20;
double ld_24;
double ld_32;
double ld_40;
double ld_48;
int l_count_56;
int l_count_60;
int li_unused_64;
if (get_prevBarTime(a_timeframe_0) == 0 || get_prevBarTime(a_timeframe_0) != iTime(Symbol(), a_timeframe_0, 0) || get_prevBarCount(a_timeframe_0) == 0 || get_prevBarCount(a_timeframe_0) != iBars(Symbol(), a_timeframe_0)) {
set_prevBarTime(a_timeframe_0, iTime(Symbol(), a_timeframe_0, 0));
set_prevBarCount(a_timeframe_0, iBars(Symbol(), a_timeframe_0));
li_4 = iBars(Symbol(), a_timeframe_0);
if (maxBarsForPeriod > 0 && li_4 > maxBarsForPeriod) li_4 = maxBarsForPeriod;
l_count_8 = 0;
li_12 = li_4;
emulate_tlbmaxmin(a_timeframe_0, li_4);
li_20 = 1;
while (iClose(Symbol(), a_timeframe_0, li_12 - 1) == iClose(Symbol(), a_timeframe_0, li_12 - 1 - li_20)) {
li_20++;
if (li_20 > li_12 - 1) break;
}
if (iClose(Symbol(), a_timeframe_0, li_12 - 1) > iClose(Symbol(), a_timeframe_0, li_12 - 1 - li_20)) {
set_max(a_timeframe_0, 0, iClose(Symbol(), a_timeframe_0, li_12 - 1));
set_min(a_timeframe_0, 0, iClose(Symbol(), a_timeframe_0, li_12 - 1 - li_20));
}
if (iClose(Symbol(), a_timeframe_0, li_12 - 1) < iClose(Symbol(), a_timeframe_0, li_12 - 1 - li_20)) {
set_max(a_timeframe_0, 0, iClose(Symbol(), a_timeframe_0, li_12 - 1 - li_20));
set_min(a_timeframe_0, 0, iClose(Symbol(), a_timeframe_0, li_12 - 1));
}
for (int li_16 = 1; li_16 < LB; li_16++) {
while (iClose(Symbol(), a_timeframe_0, li_12 - li_20) <= Diap(a_timeframe_0, 1, li_16, l_count_8) && iClose(Symbol(), a_timeframe_0, li_12 - li_20) >= Diap(a_timeframe_0, 0, li_16, l_count_8)) {
li_20++;
if (li_20 > li_12 - 1) break;
}
if (li_20 > li_12 - 1) break;
if (iClose(Symbol(), a_timeframe_0, li_12 - li_20) > get_max(a_timeframe_0, li_16 - 1)) {
set_max(a_timeframe_0, li_16, iClose(Symbol(), a_timeframe_0, li_12 - li_20));
set_min(a_timeframe_0, li_16, get_max(a_timeframe_0, li_16 - 1));
l_count_8++;
}
if (iClose(Symbol(), a_timeframe_0, li_12 - li_20) < get_min(a_timeframe_0, li_16 - 1)) {
set_min(a_timeframe_0, li_16, iClose(Symbol(), a_timeframe_0, li_12 - li_20));
set_max(a_timeframe_0, li_16, get_min(a_timeframe_0, li_16 - 1));
l_count_8++;
}
}
for (li_16 = LB; li_16 < li_12; li_16++) {
while (iClose(Symbol(), a_timeframe_0, li_12 - li_20) <= Diap(a_timeframe_0, 1, LB, l_count_8) && iClose(Symbol(), a_timeframe_0, li_12 - li_20) >= Diap(a_timeframe_0, 0, LB, l_count_8)) {
li_20++;
if (li_20 > li_12 - 1) break;
}
if (li_20 > li_12 - 1) break;
if (iClose(Symbol(), a_timeframe_0, li_12 - li_20) > get_max(a_timeframe_0, li_16 - 1)) {
set_max(a_timeframe_0, li_16, iClose(Symbol(), a_timeframe_0, li_12 - li_20));
set_min(a_timeframe_0, li_16, get_max(a_timeframe_0, li_16 - 1));
l_count_8++;
}
if (iClose(Symbol(), a_timeframe_0, li_12 - li_20) < get_min(a_timeframe_0, li_16 - 1)) {
set_min(a_timeframe_0, li_16, iClose(Symbol(), a_timeframe_0, li_12 - li_20));
set_max(a_timeframe_0, li_16, get_min(a_timeframe_0, li_16 - 1));
l_count_8++;
}
}
ld_24 = 0;
ld_32 = 0;
ld_40 = 0;
ld_48 = 0;
l_count_56 = 0;
l_count_60 = 0;
li_unused_64 = 0;
for (li_16 = 1; li_16 <= l_count_8; li_16++) {
if (get_max(a_timeframe_0, li_16) > get_max(a_timeframe_0, li_16 - 1)) {
if (l_count_60 >= LB) ld_24 = get_max(a_timeframe_0, li_16 - LB);
else ld_24 = get_min(a_timeframe_0, li_16 - l_count_60 - 1);
ld_48 = get_max(a_timeframe_0, li_16);
ld_40 = 0;
ld_32 = 0;
l_count_60++;
l_count_56 = 0;
}
if (get_max(a_timeframe_0, li_16) < get_max(a_timeframe_0, li_16 - 1)) {
if (l_count_56 >= LB) ld_32 = get_min(a_timeframe_0, li_16 - LB);
else ld_32 = get_max(a_timeframe_0, li_16 - l_count_56 - 1);
ld_40 = get_min(a_timeframe_0, li_16);
ld_24 = 0;
ld_48 = 0;
l_count_60 = 0;
l_count_56++;
}
}
if (ld_24 > 0.0) ShowHLineObject(StringConcatenate(getPeriodAsString(a_timeframe_0), " Sup"), 16711680, STYLE_SOLID, ld_24, 500);
else DeleteHLineObject(StringConcatenate(getPeriodAsString(a_timeframe_0), " Sup"));
if (ld_32 > 0.0) ShowHLineObject(StringConcatenate(getPeriodAsString(a_timeframe_0), " Res"), 255, STYLE_SOLID, ld_32, 500);
else DeleteHLineObject(StringConcatenate(getPeriodAsString(a_timeframe_0), " Res"));
if (ld_40 > 0.0) ShowHLineObject(StringConcatenate(getPeriodAsString(a_timeframe_0), " Sup C"), 16711680, STYLE_DASHDOTDOT, ld_40, 1200);
else DeleteHLineObject(StringConcatenate(getPeriodAsString(a_timeframe_0), " Sup C"));
if (ld_48 > 0.0) ShowHLineObject(StringConcatenate(getPeriodAsString(a_timeframe_0), " Res C"), 255, STYLE_DASHDOTDOT, ld_48, 1200);
else DeleteHLineObject(StringConcatenate(getPeriodAsString(a_timeframe_0), " Res C"));
}
}
int start() {
if (Period() <= PERIOD_M1 && showM01) displayPeriod(PERIOD_M1);
if (Period() <= PERIOD_M5 && showM05) displayPeriod(PERIOD_M5);
if (Period() <= PERIOD_M15 && showM15) displayPeriod(PERIOD_M15);
if (Period() <= PERIOD_M30 && showM30) displayPeriod(PERIOD_M30);
if (Period() <= PERIOD_H1 && showH01) displayPeriod(PERIOD_H1);
if (Period() <= PERIOD_H4 && showH04) displayPeriod(PERIOD_H4);
if (Period() <= PERIOD_D1 && showD01) displayPeriod(PERIOD_D1);
if (Period() <= PERIOD_W1 && showW01) displayPeriod(PERIOD_W1);
if (Period() <= PERIOD_MN1 && showMN1) displayPeriod(PERIOD_MN1);
return (0);
}
How can I insert alert only when H1, H4, D1, W1 and MN1 are touched?
First, straight forget to use a call to Alert() in CustomIndicator
MQL4/5 architecture strictly forbids one to call any blocking-mode method inside a CustomIndicator-type-of-MQL4/5-code.
This will not be possible, so let's focus on can-do alternative(s):
A ) one may send a signalling message to any block-able MQL4/5 script ( which is executed in another thread or an external process.
B ) one may use pipe-based signalling, if O/S permits to use these rudimentary tools.
C ) one may use an internal GlobalVariableSet() / GlobalVariableCheck() native MQL4/5 support for inter-thread data-based signalling / messaging, allowing a non-CustomIndicator ( Script ) MQL4/5 type of code to "remotely" handle any of the blocking parts of a service ( a call to native Alert() et al )
D ) one may use a file-based signalling, if all of above seems too complicated.
Having started with A ) years ago, I have never had a need to resort to B), the less the others "beyond" A), since then, yet these options still remains also possible.
The code will go as easy as:
if ( ... )
{ int RC = mql4zmq_msg_init_data( aSendMsgOBJ,
aSendMsgBuffUCHAR,
aSendMsgBUFF_SIZE * 4
);
// if RC
// ------------------------------------------------------.SEND()
zmq_send( aZmqPUBLISHER, aSendMsgOBJ );
// ------------------------------------------------------.SEND()
...
}
The remote receivers ( could be many ) then do whatever they want, be it in MQL4/5, python, Go, C++, java, ... chapel, FORTRAN, MATLAB, ...
Cool, isn't it?

Boxing Detected Objects in OpenCV

I am currently implementing a connected components algorithm and the last step of the algorithm requires me to enclose the objects I found in a box. I have attempted to enclose an object in a box and this is the result:
As you can see some of them seem to be enclosed in a box. Some of the lines of the box are not seen unless I stretch the windows of the imshow function's output and also some of them have color when I expected a line with a shade of gray.
My question is: Is the object really getting enclosed since I remember when I ran a similar code of mine into a different OS the lines with color are not see at all but are seen in my computer. Additionally, why are some of the lines in a different color given that I was expecting a shade of gray.
Mat src, src_gray;
Mat dst, detected_edges;
const char* window_name = "THRESHOLDED IMAGE";
/**
* #function connectedComponent
*/
static void connectedComponent(int, void*)
{
Mat test; //dummy
Mat sub;
int newObject = 0;
int zeroTest = 0, nonZero = 0;
int arr[5] = {0,0,0,0,0};
/// Reduce noise with a kernel 3x3
blur( src_gray,detected_edges, Size(3,3) ); //filtering out of noise
namedWindow("INITIAL", WINDOW_NORMAL);
imshow("INITIAL",detected_edges);
resizeWindow("INITIAL", 300, 300);
threshold(detected_edges, detected_edges, 0,255, THRESH_BINARY | THRESH_OTSU);
int** newSub = new int*[detected_edges.rows];
for(int i = 0; i < detected_edges.rows; i++)
newSub[i] = new int[detected_edges.cols];
for(int i = 0; i < detected_edges.rows; i++){
for(int j = 0; j < detected_edges.cols; j++){
newSub[i][j] = 0;
}
}
/*INITIAL MARKING LOOP*/
for(int i = 0; i < detected_edges.rows; i++){
for(int j = 0; j < detected_edges.cols; j++){
if(detected_edges.at<uchar>(i,j) == 0){
if(i-1 < 0 && j-1 < 0){
newObject = newObject + 1; //no values
newSub[i][j] = newObject;
}else if(i-1 >= 0 && j-1 < 0){
if(newSub[i-1][j] != 0){
newSub[i][j] = newSub[i-1][j]; //only up has value
}else{
newObject = newObject + 1; //no values
newSub[i][j] = newObject;
}
}else if(i-1 < 0 && j-1 >= 0){
if(newSub[i-1][j] != 0){
newSub[i][j] = newSub[i-1][j]; //only left has value
}else{
newObject = newObject + 1; //no values
newSub[i][j] = newObject;
}
}else{
if(newSub[i-1][j] == 0 && newSub[i][j-1] == 0){
newObject = newObject + 1; //no values
newSub[i][j] = newObject;
}else if(newSub[i-1][j] == newSub[i][j-1]){ //same value
newSub[i][j] = newSub[i-1][j];
}else if((newSub[i-1][j] != 0 && newSub[i][j-1] == 0)){
newSub[i][j] = newSub[i-1][j]; //only up has value
}else if(newSub[i-1][j] == 0 && newSub[i][j-1] != 0 ){
newSub[i][j] = newSub[i][j-1]; //only left has value
}else if(newSub[i-1][j] != newSub[i][j-1]){
newSub[i][j] = newSub[i-1][j]; //different values follow upper's value
}
}
}
}
}
int a = 1;
int maxRows = detected_edges.rows;
int maxCols = detected_edges.cols;
/*CONNECTING PIXELS RIGHT-BOTTOM*/
while(a < newObject){
int update = 0;
for(int i = 0; i < maxRows; i++){
for(int j = 0; j < maxCols; j++){
if(newSub[i][j] == a){
if(i+1 < maxRows && j+1 < maxCols){
if(newSub[i][j+1] > a){ //both points allowed
int value = newSub[i][j+1]; //get the value I need to replace
for(int h = 0; h < maxRows; h++){
for(int k = 0; k < maxCols; k++){
if(newSub[h][k] == value){ //replace all instances of that value
newSub[h][k] = a;
}
}
}
update = 1;
}
if(newSub[i+1][j] > a){ //both points allowed
int value = newSub[i+1][j]; //get the value I need to replace
for(int h = 0; h < maxRows; h++){
for(int k = 0; k < maxCols; k++){
if(newSub[h][k] == value){
newSub[h][k] = a; //replace all instances of that value
}
}
}
update = 1;
}
}else if(i+1 > maxRows && j+1 < maxCols){
if(newSub[i][j+1] > a){ //bottom is not allowed
int value = newSub[i][j+1]; //get the value I need to replace
for(int h = 0; h < maxRows; h++){
for(int k = 0; k < maxCols; k++){
if(newSub[h][k] == value){
newSub[h][k] = a; //replace all instances of that value
}
}
}
update = 1;
}
}else if(i+1 < maxRows && j+1 > maxCols){
if(newSub[i+1][j] > a){ //right is not allowed
int value = newSub[i+1][j]; //get the value I need to replace
for(int h = 0; h < maxRows; h++){
for(int k = 0; k < maxCols; k++){
if(newSub[h][k] == value){ //replace all instances of that value
newSub[h][k] = a;
}
}
}
update = 1;
}
}
}
}
}
a++;
}
/*CONNECTING PIXELS LEFT-TOP*/
a = newObject;
while(a > 0){
int update = 0;
for(int i = maxRows-1; i > 0; i--){
for(int j = maxCols-1; j > 0 ; j--){
if(newSub[i][j] == a){
if(i-1 >= 0 && j-1 >= 0){
if(newSub[i][j-1] > a){ //both points allowed
int value = newSub[i][j-1]; //get the value I need to replace
for(int h = 0; h < maxRows; h++){
for(int k = 0; k < maxCols; k++){
if(newSub[h][k] == value){
newSub[h][k] = a;
}
}
}
update = 1;
}
if(newSub[i-1][j] > a){
int value = newSub[i-1][j]; //get the value I need to replace
for(int h = 0; h < maxRows; h++){
for(int k = 0; k < maxCols; k++){
if(newSub[h][k] == value){ //replace all instances of that value
newSub[h][k] = a;
}
}
}
update = 1;
}
}else if(i-1 >= 0 && j-1 < 0){
if(newSub[i][j-1] > a){ //left is not allowed
int value = newSub[i][j-1]; //get the value I need to replace
for(int h = 0; h < maxRows; h++){
for(int k = 0; k < maxCols; k++){ //replace all instances of that value
if(newSub[h][k] == value){
newSub[h][k] = a;
}
}
}
update = 1;
}
}else if(i-1 < 0 && j-1 >= 0){
if(newSub[i-1][j] > a){ //top is not allowed
int value = newSub[i-1][j]; //get the value I need to replace
for(int h = 0; h < maxRows; h++){
for(int k = 0; k < maxCols; k++){ //replace all instances of that value
if(newSub[h][k] == value){
newSub[h][k] = a;
}
}
}
update = 1;
}
}
}
}
}
a--;
}
for(int i = 0; i < maxRows; i++){
for(int j = 0; j < maxCols; j++){
int check = 0;
if(newSub[i][j] != 0){
for(int k = 0; k < 5; k++){
if(newSub[i][j] == arr[k]){ //check if there is an instance of the value in the given array of values
check = 1;
break;
}
}
if(check == 0){
for(int r = 0; r < 5; r++){
if(arr[r] == 0){
arr[r] = newSub[i][j]; //if new value is found add to array
break;
}
}
}
}
}
}
/*
I HAVE AN ARRAY CONTAINING ALL VALUES
**/
src.copyTo( sub, detected_edges);
sub = Scalar::all(0);
/*SET AN INTENSITY FOR CORRESPONDING VALUES*/
int intensity = 50;
a = 0;
while(a < 5){
int update = 0;
for(int i = 0; i < maxRows; i++){
for(int j = 0; j < maxCols; j++){
if(newSub[i][j] == arr[a]){
sub.at<uchar>(i,j) = intensity;
}
}
}
a++;
intensity = intensity + 50;
}
a = 250;
/*GETTING MIN-MAX COORDINATES*/
while(a >= 50){
int setter = 0;
int minRow = 0;
int minCol = 0;
int maxRow = 0;
int maxCol = 0;
for(int i = 0; i < maxRows; i++){
for(int j = 0; j < maxCols; j++){
if(sub.at<uchar>(i,j) == a){
if(setter == 0){
minRow = i;
minCol = j;
maxRow = i;
maxCol = j;
setter = 1;
}else{
if(i <= minRow){
minRow = i;
}
else{
if(i > maxRow){
maxRow = i;
}
}
if(j <= minCol){
minCol = j;
}
else{
if(j > maxCol){
maxCol = j;
}
}
}
}
}
}
/*THIS IS WHERE I MAKE MY BOUNDING BOX*/
for(int i = minRow; i < maxRow; i++){
sub.at<uchar>(i,minCol) = 255; //set up the horizontal lines
sub.at<uchar>(i,maxCol) = 255;
}
for(int i = minCol; i < maxCol; i++){
sub.at<uchar>(minRow,i) = 255; //set up the vertical lines
sub.at<uchar>(maxRow,i) = 255;
}
a = a - 50;
}
dst = Scalar::all(0);
src.copyTo( dst, detected_edges);
imshow( window_name, dst );
namedWindow("FINAL", WINDOW_NORMAL);
imshow("FINAL",sub); //final output
resizeWindow("FINAL", 300, 300);
for(int i = 0; i < detected_edges.rows; i++)
delete[] newSub[i];
delete[] newSub;
}
/**
* #function main
*/
int main( int, char** argv )
{
/// Load an image
src = imread( argv[1] );
if( src.empty() )
{ return -1; }
/// Create a matrix of the same type and size as src (for dst)
dst.create( src.size(), src.type() );
/// Convert the image to grayscale
cvtColor( src, src_gray, COLOR_BGR2GRAY ); //grayscale for one channel for easy computation
/// Create a window
namedWindow( window_name, WINDOW_NORMAL );
resizeWindow(window_name, 300,300);
/// Show the image
connectedComponent(0, 0);
/// Wait until user exit program by pressing a key
waitKey(0);
return 0;
}

What's the best way to use IF and SWITCH together for game levels?

I have the following code working correctly in my game project where a ball is randomly shown to the player.
int random = arc4random_uniform(5);
switch (random)
{
case 0:
{
NSLog(#"Case 0 Ball");
}
break;
case 1:
{
NSLog(#"Case 1 Big Ball");
break;
}
case 2:
{
NSLog(#"Case 2 Center Ball");
break;
}
case 3:
{
NSLog(#"Case 3 Red Ball");
break;
}
case 4:
{
NSLog(#"Case 4 Blue Ball");
break;
}
}
What I would like to do is limit the number of potential balls shown to the player until the player achieves a certain level. I think the simplest way to do this is with just changing the number of random cases. So for instance...
int random = arc4random_uniform(1);
int random = arc4random_uniform(2);
int random = arc4random_uniform(3);
int random = arc4random_uniform(4);
int random = arc4random_uniform(5);
Of course the problem is when I try to wrap those options in the if statement below, it separates it from the Switch, which won't work.
int unlockBallLevel = [_levels getCurrentLevel];
if (unlockBallLevel > 5) {
int random = arc4random_uniform(5);
}
I'm wondering what would be the most logical and efficient way to limit the number of balls until a certain level is reached?
Perhaps:
switch (arc4random_uniform([_levels getCurrentLevel] < 5 ?: 5))
{
...
}
I solved the problem. I still don't know if there is a more efficient way to doing this, but the following code works and unlocks more balls as the player levels up through level 75. After level 75, all five balls are unlocked.
int unlockAtLevel = [_levels getCurrentLevel];
if (unlockAtLevel >= 1)
{
ballsForRandom = 1;
NSLog(#"\n\nLevel %i reached! There are now %i main balls unlocked!\n\n",unlockAtLevel,ballsForRandom);
}
if (unlockAtLevel >= 10)
{
ballsForRandom = 2;
NSLog(#"\n\nLevel %i reached! There are now %i main balls unlocked!\n\n",unlockAtLevel,ballsForRandom);
}
if (unlockAtLevel >= 30)
{
ballsForRandom = 3;
NSLog(#"\n\nLevel %i reached! There are now %i main balls unlocked!\n\n",unlockAtLevel,ballsForRandom);
}
if (unlockAtLevel >= 50)
{
ballsForRandom = 4;
NSLog(#"\n\nLevel %i reached! There are now %i main balls unlocked!\n\n",unlockAtLevel,ballsForRandom);
}
if (unlockAtLevel >= 75)
{
ballsForRandom = 5;
NSLog(#"\n\nLevel %i reached! There are now %i main balls unlocked!\n\n",unlockAtLevel,ballsForRandom);
}
int random = arc4random_uniform(ballsForRandom);
switch (random)
{
case 0:
{
NSLog(#"Case 0 Ball");
}
break;
case 1:
{
NSLog(#"Case 1 Big Ball");
break;
}
case 2:
{
NSLog(#"Case 2 Center Ball");
break;
}
case 3:
{
NSLog(#"Case 3 Red Ball");
break;
}
case 4:
{
NSLog(#"Case 4 Blue Ball");
break;
}
}
Based on #CRD's comments, I made the following changes to the code. The code below seems like a much more efficient and smarter implementation over the last answer posted.
int unlockAtLevel = [_levels getCurrentLevel];
if (unlockAtLevel >= 25)
{
ballsForRandom = 6;
NSLog(#"\n\nLevel %i reached! There are now %i main balls unlocked!\n\n",unlockAtLevel,ballsForRandom);
} else if (unlockAtLevel >= 20)
{
ballsForRandom = 5;
NSLog(#"\n\nLevel %i reached! There are now %i main balls unlocked!\n\n",unlockAtLevel,ballsForRandom);
} else if (unlockAtLevel >= 15)
{
ballsForRandom = 4;
NSLog(#"\n\nLevel %i reached! There are now %i main balls unlocked!\n\n",unlockAtLevel,ballsForRandom);
} else if (unlockAtLevel >= 10)
{
ballsForRandom = 3;
NSLog(#"\n\nLevel %i reached! There are now %i main balls unlocked!\n\n",unlockAtLevel,ballsForRandom);
} else if (unlockAtLevel >= 3)
{
ballsForRandom = 2;
NSLog(#"\n\nLevel %i reached! There are now %i main balls unlocked!\n\n",unlockAtLevel,ballsForRandom);
} else if (unlockAtLevel >= 1)
{
ballsForRandom = 1;
[[GCHelper defaultHelper]reportAchievementIdentifier:achievementID1 percentComplete:100];
NSLog(#"\n\nLevel %i reached! There are now %i main balls unlocked!\n\n",unlockAtLevel,ballsForRandom);
}
NSLog(#"\n\n###############################################################\n\nCurrently the player is on level %i so there should be %i main balls unlocked!\n\n###############################################################\n\n",unlockAtLevel,ballsForRandom);
int random = arc4random_uniform(ballsForRandom);
switch (random)

How to convert numbers into text?

I have been learning Objective-C with the Kochan book and I can't figure out how to do this exercise program. Only odd numbered exercises are listed online and this one is even. The exercise is to convert numbers into words. So, if "932" was entered, the program should return: "nine three two"
I used a do, while loop but the words came out backwards, as in "two three nine". Can anyone suggest a technique that works for this?
int number, digit;
NSLog(#"Type in your integer.");
scanf("%i", &number);
do
{
digit = number % 10;
if (digit == 0)
NSLog(#"zero");
if (digit == 1)
NSLog(#"one");
if (digit == 2)
NSLog(#"two");
if (digit == 3)
NSLog(#"three");
if (digit == 4)
NSLog(#"four");
if (digit == 5)
NSLog(#"five");
if (digit == 6)
NSLog(#"six");
if (digit == 7)
NSLog(#"seven");
if (digit == 8)
NSLog(#"eight");
if (digit == 9)
NSLog(#"nine");
number /= 10;
}
while (number != 0);
This isn't exactly what you want, but for your consideration:
NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterSpellOutStyle];
NSString *s = [f stringFromNumber:[NSNumber numberWithInt:932]];
NSLog(#"%#", s);
[f release];
This will log:
nine hundred and thirty-two
Again, it's not the "nine three two" you want, but it's also nice and short. :)
Since you're adding the numbers to a string, and you want to calculate them right to left, prepend the string with each new number. Something like:
numberString = [NSString stringWithFormat:#"%# %#", theNewNumber, numberString];
Where theNewNumber is a string (like #"six") and numberString is the string that you want to output once you're done...
(oh, and don't forget to initialize numberString before you start looping...something like:
NSString *numberString = #"";
=====
Based on the code you just posted, you could either do it mathematically, or just pre-pend a string like this:
Put this variable in your .h file:
NSString *numberString;
Then put this in your .m:
- (void) prependNumber:(NSString *)num {
numberString = [NSString stringWithFormat:#"%# %#", num, numberString];
}
NSLog(#"Type in your integer.");
scanf("%i", &number);
numberString = #"";
do
{
digit = number % 10;
if (digit == 0)
[self prependNumber:#"zero"];
if (digit == 1)
[self prependNumber:#"one"];
if (digit == 2)
[self prependNumber:#"two"];
if (digit == 3)
[self prependNumber:#"three"];
if (digit == 4)
[self prependNumber:#"four"];
if (digit == 5)
[self prependNumber:#"five"];
if (digit == 6)
[self prependNumber:#"six"];
if (digit == 7)
[self prependNumber:#"seven"];
if (digit == 8)
[self prependNumber:#"eight"];
if (digit == 9)
[self prependNumber:#"nine"];
number /= 10;
}
while (number != 0);
NSLog (#"%#", numberString);
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int a, b, number, logNum, nThNum;
NSLog(#"Please enter a valid integer: ");
scanf("%d", &number); // read input as a decimal integer
if (!number) // if zero or something other than a number is entered output zero
NSLog(#"Zero");
else if (number < 0) { // convert negatives to something that can be used
number = -number;
NSLog(#"(negative)"); // but output negative first then continue as usual
}
logNum = (log10(number) + 1); // find how many digits there are in the number
for (int j=0; j < logNum; j++) {// loop based on number of digits
a = pow(10,logNum-j);
b = pow(10,logNum-1-j);
nThNum = (number % a) / b;// find the nth digit in a number, in our case 1st
switch (nThNum) {// output current digit that was found
case 0:
NSLog(#"Zero");
break;
case 1:
NSLog(#"One");
break;
case 2:
NSLog(#"Two");
break;
case 3:
NSLog(#"Three");
break;
case 4:
NSLog(#"Four");
break;
case 5:
NSLog(#"Five");
break;
case 6:
NSLog(#"Six");
break;
case 7:
NSLog(#"Seven");
break;
case 8:
NSLog(#"Eight");
break;
case 9:
NSLog(#"Nine");
break;
default:
break;
}
}
[pool drain];
return 0;
}
Well, now that you've posted your code, your method will work great if you first reverse the number. So, you can just write a short routine to do that, then use your own code.
Well it sounds like you're halfway there if you were able to get the numbers to convert to words already, even if output backwards.
Assuming you're looping through your data, incrementing the index, just start at the character length of the number, decrementing your index backwards, reversing your output.
We can't help you much more without seeing your actual code. ;)
As a learning exercise, I modified Dave's code:
+(NSString*)doIt:(NSString*)inString delimiter:(NSString*)delimiter{
NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterSpellOutStyle];
NSMutableString* outString= [[NSMutableString alloc]init];
for (int i=0; i< [inString length]; i++) {
unsigned char oneChar= [inString characterAtIndex:i];
if (oneChar>47 && oneChar<58) {
NSString* temp=[f stringFromNumber:[NSNumber numberWithUnsignedChar:oneChar-48]];
[outString appendFormat:#"%#",temp];
[outString appendString:delimiter];
}
}
[f release];
[outString autorelease];
return outString;
}
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
#autoreleasepool {
// insert code here...
int number; //store the value the user enter
int lastDigit; //pick of the last digit of the integer
int tempNum; //a temporary storage of the integer the user enter
int count = 0; //used to count how many digits were entered
int count2; //going to be use as a duplicate of count
NSLog(#"Enter an integer");
scanf("%i", &number);
tempNum = number;
//Loop to find out how many digits were entered
for (; number != 0; number /= 10) {
count +=1;
}
//Loop to convert the numbers into words
for (; count != 0; count -= 1) {
count2 = count; //set count2 to count so the for and while loop use them independently
number = tempNum; //restore the value entered by by the user to the number variable
//Loop to reverse the order of the last digit
while (count2 != 0) { //loops to the same number of counts to get the first digit
lastDigit = number % 10; //picks off the last value in the integer
number /= 10; //enables the loop to set the last value of the integer to zero
count2 -=1; //loops one less time to get the numbers from front to back
}
//switch statements
switch (lastDigit) {
case 9:
NSLog(#"nine");
break;
case 8:
NSLog(#"eight");
break;
case 7:
NSLog(#"seven");
break;
case 6:
NSLog(#"six");
break;
case 5:
NSLog(#"five");
break;
case 4:
NSLog(#"four");
break;
case 3:
NSLog(#"three");
break;
case 2:
NSLog(#"two");
break;
case 1:
NSLog(#"one");
break;
case 0:
NSLog(#"zero");
break;
default:
break;
}
}
}
return 0;
}
I use nested under nested loop but believe that this works
int i, j, number, reversenumber = 0;
NSLog(#" Input Number:");
scanf( "%i", &number);
if (number != 0)
// chekcing for zero entry
{
for (;number!= 0; number = number/10)
//for reversing the number entered so that the words doesn't come reversed when printed
{
i = number%10;
reversenumber = reversenumber * 10 + i;
}
NSLog(#"Reverser Number for the input number is %i", reversenumber);
// mid routine check to print the reversed number
while(reversenumber != 0)
{
j = reversenumber % 10;
switch (j)
{
case 9:
NSLog(#"nine");
break;
case 8:
NSLog(#"eight");
break;
case 7:
NSLog(#"seven");
break;
case 6:
NSLog(#"six");
break;
case 5:
NSLog(#"five");
break;
case 4:
NSLog(#"four");
break;
case 3:
NSLog(#"three");
break;
case 2:
NSLog(#"two");
break;
case 1:
NSLog(#"one");
break;
default:
NSLog(#"zero");
}
reversenumber /= 10;
}
}
else
NSLog(#"Zero");
}
return 0;
}
very easy, there are number of approaches but i normally try this :
do
{
digit = number % 10;
switch (digit) {
case 0:
[self prependNumber:#"zero"];
break;
case 1:
[self prependNumber:#"one"];
break;
case 2:
[self prependNumber:#"two"];
break;
case 3:
[self prependNumber:#"three"];
break;
case 4:
[self prependNumber:#"four"];
break;
case 5:
[self prependNumber:#"five"];
break;
case 6:
[self prependNumber:#"six"];
break;
case 7:
[self prependNumber:#"seven"];
break;
case 8:
[self prependNumber:#"eight"];
break;
case 9:
[self prependNumber:#"nine"];
break;
default:
break;
}
number /= 10;
}
while (number != 0);
/************/
-(void) prependNumber:(NSString*)str{
NSLog(str);
}

Resources