What is definition of %DMNORMIP and $ in SAS? - sas

I'm an R user and I'm trying to test the SAS code below in R. I think the code is pretty straightforward, except these two topics that are troubling me:
1. What does %DMNORMIP means?
2. What does $ do?
****************************************************************;
****** DECISION TREE SCORING CODE ******;
****************************************************************;
****** LENGTHS OF NEW CHARACTER VARIABLES ******;
LENGTH I_BAD $ 12;
LENGTH _WARN_ $ 4;
****** LABELS FOR NEW VARIABLES ******;
label _NODE_ = 'Node' ;
label _LEAF_ = 'Leaf' ;
label P_BAD1 = 'Predicted: BAD=1' ;
label P_BAD0 = 'Predicted: BAD=0' ;
label Q_BAD1 = 'Unadjusted P: BAD=1' ;
label Q_BAD0 = 'Unadjusted P: BAD=0' ;
label I_BAD = 'Into: BAD' ;
label U_BAD = 'Unnormalized Into: BAD' ;
label _WARN_ = 'Warnings' ;
****** TEMPORARY VARIABLES FOR FORMATTED VALUES ******;
LENGTH _ARBFMT_12 $ 12; DROP _ARBFMT_12;
_ARBFMT_12 = ' '; /* Initialize to avoid warning. */
LENGTH _ARBFMT_7 $ 7; DROP _ARBFMT_7;
_ARBFMT_7 = ' '; /* Initialize to avoid warning. */
****** ASSIGN OBSERVATION TO NODE ******;
IF NOT MISSING(DEBTINC ) AND
DEBTINC < 45.1549076293408 THEN DO;
IF NOT MISSING(VALUE ) AND
VALUE < 491495.5 THEN DO;
_ARBFMT_12 = PUT( DELINQ , BEST12.);
%DMNORMIP( _ARBFMT_12);
IF _ARBFMT_12 IN ('2' ,'4' ,'3' ,'5' ,'7' ) THEN DO;
_ARBFMT_12 = PUT( DELINQ , BEST12.);
%DMNORMIP( _ARBFMT_12);
IF _ARBFMT_12 IN ('7' ) THEN DO;
_NODE_ = 16;
_LEAF_ = 1;
P_BAD1 = 1;
P_BAD0 = 0;
Q_BAD1 = 1;
Q_BAD0 = 0;
I_BAD = '1' ;
U_BAD = 1;
END;
ELSE DO;
IF NOT MISSING(DEBTINC ) AND
DEBTINC < 19.590042161704 THEN DO;
_NODE_ = 26;
_LEAF_ = 2;
P_BAD1 = 1;
P_BAD0 = 0;
Q_BAD1 = 1;
Q_BAD0 = 0;
I_BAD = '1' ;
U_BAD = 1;
END;
ELSE DO;
_NODE_ = 27;
_LEAF_ = 3;
P_BAD1 = 0.18145161290322;
P_BAD0 = 0.81854838709677;
Q_BAD1 = 0.18145161290322;
Q_BAD0 = 0.81854838709677;
I_BAD = '0' ;
U_BAD = 0;
END;
END;
END;
ELSE DO;
_ARBFMT_12 = PUT( DEROG , BEST12.);
%DMNORMIP( _ARBFMT_12);
IF _ARBFMT_12 IN ('3' ,'2' ,'4' ,'6' ) THEN DO;
IF NOT MISSING(CLNO ) AND
34.5 <= CLNO THEN DO;
_NODE_ = 29;
_LEAF_ = 5;
P_BAD1 = 1;
P_BAD0 = 0;
Q_BAD1 = 1;
Q_BAD0 = 0;
I_BAD = '1' ;
U_BAD = 1;
END;
ELSE DO;
_NODE_ = 28;
_LEAF_ = 4;
P_BAD1 = 0.21;
P_BAD0 = 0.79;
Q_BAD1 = 0.21;
Q_BAD0 = 0.79;
I_BAD = '0' ;
U_BAD = 0;
END;
END;
ELSE DO;
IF NOT MISSING(CLNO ) AND
CLNO < 2.5 THEN DO;
IF NOT MISSING(YOJ ) AND
2 <= YOJ THEN DO;
_NODE_ = 39;
_LEAF_ = 7;
P_BAD1 = 1;
P_BAD0 = 0;
Q_BAD1 = 1;
Q_BAD0 = 0;
I_BAD = '1' ;
U_BAD = 1;
END;
ELSE DO;
_NODE_ = 38;
_LEAF_ = 6;
P_BAD1 = 0.1025641025641;
P_BAD0 = 0.89743589743589;
Q_BAD1 = 0.1025641025641;
Q_BAD0 = 0.89743589743589;
I_BAD = '0' ;
U_BAD = 0;
END;
END;
ELSE DO;
_NODE_ = 31;
_LEAF_ = 8;
P_BAD1 = 0.04780206581791;
P_BAD0 = 0.95219793418208;
Q_BAD1 = 0.04780206581791;
Q_BAD0 = 0.95219793418208;
I_BAD = '0' ;
U_BAD = 0;
END;
END;
END;
END;
ELSE DO;
_ARBFMT_12 = PUT( NINQ , BEST12.);
%DMNORMIP( _ARBFMT_12);
IF _ARBFMT_12 IN ('1' ,'0' ) THEN DO;
_NODE_ = 10;
_LEAF_ = 9;
P_BAD1 = 1;
P_BAD0 = 0;
Q_BAD1 = 1;
Q_BAD0 = 0;
I_BAD = '1' ;
U_BAD = 1;
END;
ELSE DO;
_NODE_ = 11;
_LEAF_ = 10;
P_BAD1 = 0.4;
P_BAD0 = 0.6;
Q_BAD1 = 0.4;
Q_BAD0 = 0.6;
I_BAD = '0' ;
U_BAD = 0;
END;
END;
END;
ELSE DO;
_ARBFMT_12 = PUT( DELINQ , BEST12.);
%DMNORMIP( _ARBFMT_12);
IF _ARBFMT_12 IN ('2' ,'1' ,'6' ,'4' ,'3' ,'5' ,'7' ) THEN DO;
IF NOT MISSING(CLAGE ) AND
345.933333333333 <= CLAGE THEN DO;
_NODE_ = 13;
_LEAF_ = 12;
P_BAD1 = 0.27272727272727;
P_BAD0 = 0.72727272727272;
Q_BAD1 = 0.27272727272727;
Q_BAD0 = 0.72727272727272;
I_BAD = '0' ;
U_BAD = 0;
END;
ELSE DO;
_NODE_ = 12;
_LEAF_ = 11;
P_BAD1 = 0.83755274261603;
P_BAD0 = 0.16244725738396;
Q_BAD1 = 0.83755274261603;
Q_BAD0 = 0.16244725738396;
I_BAD = '1' ;
U_BAD = 1;
END;
END;
ELSE DO;
IF NOT MISSING(CLAGE ) AND
178.100666666666 <= CLAGE THEN DO;
IF NOT MISSING(YOJ ) AND
YOJ < 5.4 THEN DO;
_NODE_ = 24;
_LEAF_ = 16;
P_BAD1 = 0.5045045045045;
P_BAD0 = 0.49549549549549;
Q_BAD1 = 0.5045045045045;
Q_BAD0 = 0.49549549549549;
I_BAD = '1' ;
U_BAD = 1;
END;
ELSE DO;
IF NOT MISSING(VALUE ) AND
34050 <= VALUE THEN DO;
_NODE_ = 37;
_LEAF_ = 18;
P_BAD1 = 0.1988950276243;
P_BAD0 = 0.80110497237569;
Q_BAD1 = 0.1988950276243;
Q_BAD0 = 0.80110497237569;
I_BAD = '0' ;
U_BAD = 0;
END;
ELSE DO;
_NODE_ = 36;
_LEAF_ = 17;
P_BAD1 = 0.75;
P_BAD0 = 0.25;
Q_BAD1 = 0.75;
Q_BAD0 = 0.25;
I_BAD = '1' ;
U_BAD = 1;
END;
END;
END;
ELSE DO;
IF NOT MISSING(VALUE ) AND
20665.5 <= VALUE THEN DO;
_ARBFMT_7 = PUT( JOB , $7.);
%DMNORMIP( _ARBFMT_7);
IF _ARBFMT_7 IN ('OTHER' ,'OFFICE' ,'SALES' ,'MGR' ,'PROFEXE' ,
'SELF' ) THEN DO;
_NODE_ = 34;
_LEAF_ = 14;
P_BAD1 = 0.64634146341463;
P_BAD0 = 0.35365853658536;
Q_BAD1 = 0.64634146341463;
Q_BAD0 = 0.35365853658536;
I_BAD = '1' ;
U_BAD = 1;
END;
ELSE DO;
_NODE_ = 35;
_LEAF_ = 15;
P_BAD1 = 0.19230769230769;
P_BAD0 = 0.8076923076923;
Q_BAD1 = 0.19230769230769;
Q_BAD0 = 0.8076923076923;
I_BAD = '0' ;
U_BAD = 0;
END;
END;
ELSE DO;
_NODE_ = 22;
_LEAF_ = 13;
P_BAD1 = 0.95652173913043;
P_BAD0 = 0.04347826086956;
Q_BAD1 = 0.95652173913043;
Q_BAD0 = 0.04347826086956;
I_BAD = '1' ;
U_BAD = 1;
END;
END;
END;
END;
****************************************************************;
****** END OF DECISION TREE SCORING CODE ******;
****************************************************************;
I couldn't find the answer in the SAS website.

Related

How to make atmega 128 electronic watch and stopwatch?

`#include <mega128.h>
unsigned int KEYs_NUMs = 0, FLAG_RUNs = 0, TIME_DAYs = 1, TIME_HOUR = 0, TIME_MINs = 0, TIME_SECs = 0, TIME_10ms = 0, TIME_CNTs = 0 ;
unsigned int TIME_TMR0 = 0, TIME_IMSI, SEGs_NUMs = 0 ;
unsigned int TIME_S0, TIME_S1, TIME_M0, TIME_M1, TIME_H0, TIME_H1 ;
unsigned int IMSI = 0, DAT5 = 0, DAT4 = 0, DAT3 = 0, DAT2 = 0, DAT1 = 0, DAT0 = 0 ;
unsigned int mode = 0 ;
unsigned char SEG7[10] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7C, 0x07, 0x7F, 0x67} ;
unsigned char FNDs[10] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7C, 0x07, 0x7F, 0x67} ;
interrupt [EXT_INT4] void ext_int4_isr(void)
{
KEYs_NUMs += 1 ;
if(KEYs_NUMs == 1) { mode = 1 ; }
else { mode = 0 ; KEYs_NUMs = 0 ; }
}
interrupt [EXT_INT5] void ext_int5_isr(void)
{
if(mode == 1)
{
KEYs_NUMs += 1 ;
if(KEYs_NUMs == 1) { FLAG_RUNs = 1 ; }
else if(KEYs_NUMs == 2) { FLAG_RUNs = 0 ; }
else { FLAG_RUNs = 1 ; KEYs_NUMs = 1 ; }
}
}
interrupt [EXT_INT6] void ext_int6_isr(void)
{
if(mode == 1)
{
KEYs_NUMs -= 1 ;
if(KEYs_NUMs == 1) { TIME_MINs = 0, TIME_SECs = 0, TIME_10ms = 0, TIME_CNTs = 0 ; }
}
}
interrupt [EXT_INT7] void ext_int7_isr(void)
{
}
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
TCNT0 = 0x06 ;
// Place your code here
TIME_TMR0 += 1 ; // 1ms
if(TIME_TMR0 >= 1000)
{ TIME_TMR0 = 0 ; TIME_SECs += 1 ; }
if(TIME_SECs > 59)
{ TIME_SECs = 0 ; TIME_MINs += 1 ; }
if(TIME_MINs > 59)
{ TIME_MINs = 0 ; TIME_HOUR += 1 ; }
if(TIME_HOUR > 23)
{ TIME_HOUR = 0 ; TIME_DAYs += 1 ; }
if(TIME_DAYs > 31)
{ TIME_DAYs = 1 ; }
TIME_IMSI = TIME_HOUR ;
TIME_H1 = TIME_IMSI/10 ;
TIME_H0 = TIME_IMSI - TIME_H1*10 ;
TIME_IMSI = TIME_MINs ;
TIME_M1 = TIME_IMSI/10 ;
TIME_M0 = TIME_IMSI - TIME_M1*10 ;
TIME_IMSI = TIME_SECs ;
TIME_S1 = TIME_IMSI/10 ;
TIME_S0 = TIME_IMSI - TIME_S1*10 ;
if(SEGs_NUMs > 6) { SEGs_NUMs = 0 ; }
SEGs_NUMs += 1 ;
if(SEGs_NUMs == 1)
{
PORTA.5 = 1 ; PORTA.4 = 1 ; PORTA.3 = 1 ; PORTA.2 = 1 ; PORTA.1 = 1 ;
PORTD = SEG7[TIME_S0] ;
PORTA.0 = 0 ;
}
else if(SEGs_NUMs == 2)
{
PORTA.5 = 1 ; PORTA.4 = 1 ; PORTA.3 = 1 ; PORTA.2 = 1 ; PORTA.0 = 1 ;
PORTD = SEG7[TIME_S1] ;
PORTA.1 = 0 ;
}
else if(SEGs_NUMs == 3)
{
PORTA.5 = 1 ; PORTA.4 = 1 ; PORTA.3 = 1 ; PORTA.1 = 1 ; PORTA.0 = 1 ;
PORTD = SEG7[TIME_M0] ;
PORTA.2 = 0 ;
}
else if(SEGs_NUMs == 4)
{
PORTA.5 = 1 ; PORTA.4 = 1 ; PORTA.2 = 1 ; PORTA.1 = 1 ; PORTA.0 = 1 ;
PORTD = SEG7[TIME_M1] ;
PORTA.3 = 0 ;
}
else if(SEGs_NUMs == 5)
{
PORTA.5 = 1 ; PORTA.3 = 1 ; PORTA.2 = 1 ; PORTA.1 = 1 ; PORTA.0 = 1 ;
PORTD = SEG7[TIME_H0] ;
PORTA.4 = 0 ;
}
else
{
PORTA.4 = 1 ; PORTA.3 = 1 ; PORTA.2 = 1 ; PORTA.1 = 1 ; PORTA.0 = 1 ;
PORTD = SEG7[TIME_H1] ;
PORTA.5 = 0 ;
}
if(TIME_TMR0 < 500) { PORTA.7 = 1 ; } // LED off
else { PORTA.7 = 0 ; } // LED on
}
void main(void)
{
DDRA=(1<<DDA7) | (0<<DDA6) | (1<<DDA5) | (1<<DDA4) | (1<<DDA3) | (1<<DDA2) | (1<<DDA1) | (1<<DDA0);
PORTA=(0<<PORTA7) | (0<<PORTA6) | (0<<PORTA5) | (0<<PORTA4) | (0<<PORTA3) | (0<<PORTA2) | (0<<PORTA1) | (0<<PORTA0);
DDRD=(1<<DDD7) | (1<<DDD6) | (1<<DDD5) | (1<<DDD4) | (1<<DDD3) | (1<<DDD2) | (1<<DDD1) | (1<<DDD0);
PORTD=(0<<PORTD7) | (0<<PORTD6) | (0<<PORTD5) | (0<<PORTD4) | (0<<PORTD3) | (0<<PORTD2) | (0<<PORTD1) | (0<<PORTD0);
// Timer/Counter 0 initialization
ASSR=0<<AS0;
TCCR0=(0<<WGM00) | (0<<COM01) | (0<<COM00) | (0<<WGM01) | (1<<CS02) | (0<<CS01) | (0<<CS00);
TCNT0=0x06; OCR0=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=(0<<OCIE2) | (0<<TOIE2) | (0<<TICIE1) | (0<<OCIE1A) | (0<<OCIE1B) | (0<<TOIE1) | (0<<OCIE0) | (1<<TOIE0);
ETIMSK=(0<<TICIE3) | (0<<OCIE3A) | (0<<OCIE3B) | (0<<TOIE3) | (0<<OCIE3C) | (0<<OCIE1C);
// External Interrupt(s) initialization
EICRA=(0<<ISC31) | (0<<ISC30) | (0<<ISC21) | (0<<ISC20) | (0<<ISC11) | (0<<ISC10) | (0<<ISC01) | (0<<ISC00);
EICRB=(1<<ISC71) | (0<<ISC70) | (1<<ISC61) | (0<<ISC60) | (1<<ISC51) | (0<<ISC50) | (1<<ISC41) | (0<<ISC40);
EIMSK=(1<<INT7) | (1<<INT6) | (1<<INT5) | (1<<INT4) | (0<<INT3) | (0<<INT2) | (0<<INT1) | (0<<INT0);
EIFR=(1<<INTF7) | (1<<INTF6) | (1<<INTF5) | (1<<INTF4) | (0<<INTF3) | (0<<INTF2) | (0<<INTF1) | (0<<INTF0);
// Globally enable interrupts
#asm("sei")
while (1)
{
if(mode == 1)
{
if(FLAG_RUNs) { TIME_CNTs += 1 ; } // 1ms
if(TIME_CNTs > 99 ) { TIME_CNTs = 0 ; TIME_SECs += 1 ; }
if(TIME_SECs > 59 ) { TIME_SECs = 0 ; TIME_MINs += 1 ; }
if(TIME_MINs > 59 ) { TIME_MINs = 0 ; }
TIME_10ms = TIME_CNTs/10 ;
SEGs_NUMs ++ ;
if(SEGs_NUMs > 5) { SEGs_NUMs = 0 ; }
switch(SEGs_NUMs)
{
case 0 : PORTA.0 = 0 ; PORTA.1 = 1 ; PORTA.2 = 1 ; PORTA.3 = 1 ; PORTA.4 = 1 ;
PORTA.5 = 1 ;
PORTD = FNDs[DAT0] ; break;
case 1 : PORTA.0 = 1 ; PORTA.1 = 0 ; PORTA.2 = 1 ; PORTA.3 = 1 ; PORTA.4 = 1 ;
PORTA.5 = 1 ;
PORTD = FNDs[DAT1] ; break;
case 2 : PORTA.0 = 1 ; PORTA.1 = 1 ; PORTA.2 = 0 ; PORTA.3 = 1 ; PORTA.4 = 1 ;
PORTA.5 = 1 ;
PORTD = FNDs[DAT2] ; break;
case 3 : PORTA.0 = 1 ; PORTA.1 = 1 ; PORTA.2 = 1 ; PORTA.3 = 0 ; PORTA.4 = 1 ;
PORTA.5 = 1 ;
PORTD = FNDs[DAT3] ; break;
case 4 : PORTA.0 = 1 ; PORTA.1 = 1 ; PORTA.2 = 1 ; PORTA.3 = 1 ; PORTA.4 = 0 ;
PORTA.5 = 1 ;
PORTD = FNDs[DAT4] ; break;
case 5 : PORTA.0 = 1 ; PORTA.1 = 1 ; PORTA.2 = 1 ; PORTA.3 = 1 ; PORTA.4 = 1 ;
PORTA.5 = 0 ;
PORTD = FNDs[DAT5] ; break;
}
IMSI = TIME_MINs ;
DAT5 = IMSI/10 ;
DAT4 = IMSI - DAT5*10 ;
IMSI = TIME_SECs ;
DAT3 = IMSI/10 ;
DAT2 = IMSI - DAT3*10 ;
IMSI = TIME_10ms ;
DAT1 = IMSI/10 ;
DAT0 = IMSI - DAT1*10 ;
}
}
}
` I wrote a code to change the mode by pressing the mode button by putting the clock code in the timer interrupt and the stopwatch code in 'while'. If you change the mode, it moves to the stopwatch as much as the elapsed time, and if you start the stopwatch, the counter starts at the minute digit. And when you reset and return to the clock, the clock counts from the beginning. How can I keep the timer running even if I change the mode? And how do I prevent the timer's time from being transferred to the stopwatch?
According to the comments to the question:
interrupt [EXT_INT4] void ext_int4_isr(void)
{
// just toggle the mode, no need for counting any occurrences...
mode = !mode; // alternatively mode = 1 - mode
if(mode)
{
// reset the stop watch on having been enabled:
flagRuns = 0;
// reset the counter as well!
// TODO: common function for!
// should work calling ext_int6_isr directly, too
}
}
interrupt [EXT_INT5] void ext_int5_isr(void)
{
// can toggle unconditionally, as the flag is not evaluated if not
// in stop watch mode and is reset upon ENTERING the mode (this changed
// compared to your original version!)
flagRuns = !flagRuns;
}
interrupt [EXT_INT6] void ext_int6_isr(void)
{
if(mode && !flagRuns)
{
// reset, common function, see above
}
}
Most likely you need to declare the variables that are written to and read from across ISR and main routine or across different ISR volatile to avoid caching effects.

C++ - Slot Machine Output - I need a simpler way to pass random string arrays into a function

I'm working on a slot machine project for school and I've come up with this code for spitting out the images randomly. I feel like the entire randomCheck() function is a huge waste of space. Can anyone enlighten me how to do this more efficiently?
It's a lot of code... sorry about that. Please let me know if you need any more information from me.
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
#include<random>
#include<vector>
#include <windows.h>
#include <ctime>
using namespace std;
int nuM;
int getNum(int min, int max)
{
nuM = min + (rand() % (max - min) + 1);
return nuM;
}
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
COORD CursorPosition;
void gotoXY(int x, int y, string text)
{
CursorPosition.X = x;
CursorPosition.Y = y;
SetConsoleCursorPosition(console, CursorPosition);
cout << text;
}
vector<int>v(9);
string Clover[] =
{ " .-.-. "
," ( ) "
," .-.\\ : /.-. "
,"( . : . ) "
," ( /|\\ ) "
," `\"` | `\"` "
," ~ "
};
string Seven[] =
{ " _________ "
," |____ / "
," / / "
," / / "
," / / "
," / / "
," /__/ "
};
string Money[] =
{ " ___ "
," _\\ /_ "
," / \\ "
," | | "
," | $$$ | "
," | | "
," \\_____/ "
};
string Diamond[] =
{ " _ "
," / \\ "
," / \\ "
," / \\ "
," \\ / "
," \\ / "
," \\_/ "
};
string Bolt[] =
{ " __ "
," _/ / "
," /__/ "
," _// "
," /_/ "
," // "
,"/' "
};
string Cherry[] =
{ "-._.-(`-.__`-. "
," \\ `~~`"
," .--./ \\ "
,"/# \\ \\.--. "
,"\\ / /# \\"
," '--' \\ / "
," '--' "
};
string wtf1[7];
string wtf2[7];
string wtf3[7];
string wtf4[7];
string wtf5[7];
string wtf6[7];
string wtf7[7];
string wtf8[7];
string wtf9[7];
void slotMachine(string a[], string b[], string c[], string d[], string e[], string f[], string g[], string h[], string i[])
{
for (int y = 0; y<7; y++)
{
gotoXY(20, 1 + y, a[y]);
}
for (int y = 0; y<7; y++)
{
gotoXY(45, 1 + y, b[y]);
}
for (int y = 0; y<7; y++)
{
gotoXY(70, 1 + y, c[y]);
}
//second line
for (int y = 0; y<7; y++)
{
gotoXY(20, 10 + y, d[y]);
}
for (int y = 0; y<7; y++)
{
gotoXY(45, 10 + y, e[y]);
}
for (int y = 0; y<7; y++)
{
gotoXY(70, 10 + y, f[y]);
}
// third line
for (int y = 0; y<7; y++)
{
gotoXY(20, 20 + y, g[y]);
}
for (int y = 0; y<7; y++)
{
gotoXY(45, 20 + y, h[y]);
}
for (int y = 0; y<7; y++)
{
gotoXY(70, 20 + y, i[y]);
}
}
void randomCheck()
{
//a1
getNum(0, 6);
if (nuM == 1)
{
v[0] = 1;
wtf1[0] = Seven[0];
wtf1[1] = Seven[1];
wtf1[2] = Seven[2];
wtf1[3] = Seven[3];
wtf1[4] = Seven[4];
wtf1[5] = Seven[5];
wtf1[6] = Seven[6];
}
else if (nuM == 2)
{
v[0] = 2;
wtf1[0] = Money[0];
wtf1[1] = Money[1];
wtf1[2] = Money[2];
wtf1[3] = Money[3];
wtf1[4] = Money[4];
wtf1[5] = Money[5];
wtf1[6] = Money[6];
}
else if (nuM == 3)
{
v[0] = 3;
wtf1[0] = Diamond[0];
wtf1[1] = Diamond[1];
wtf1[2] = Diamond[2];
wtf1[3] = Diamond[3];
wtf1[4] = Diamond[4];
wtf1[5] = Diamond[5];
wtf1[6] = Diamond[6];
}
else if (nuM == 4)
{
v[0] = 4;
wtf1[0] = Bolt[0];
wtf1[1] = Bolt[1];
wtf1[2] = Bolt[2];
wtf1[3] = Bolt[3];
wtf1[4] = Bolt[4];
wtf1[5] = Bolt[5];
wtf1[6] = Bolt[6];
}
else if (nuM == 5)
{
v[0] = 5;
wtf1[0] = Cherry[0];
wtf1[1] = Cherry[1];
wtf1[2] = Cherry[2];
wtf1[3] = Cherry[3];
wtf1[4] = Cherry[4];
wtf1[5] = Cherry[5];
wtf1[6] = Cherry[6];
}
else
{
v[0] = 6;
wtf1[0] = Clover[0];
wtf1[1] = Clover[1];
wtf1[2] = Clover[2];
wtf1[3] = Clover[3];
wtf1[4] = Clover[4];
wtf1[5] = Clover[5];
wtf1[6] = Clover[6];
}
//a2
getNum(0, 6);
if (nuM == 1)
{
v[1] = 1;
wtf2[0] = Seven[0];
wtf2[1] = Seven[1];
wtf2[2] = Seven[2];
wtf2[3] = Seven[3];
wtf2[4] = Seven[4];
wtf2[5] = Seven[5];
wtf2[6] = Seven[6];
}
else if (nuM == 2)
{
v[1] = 2;
wtf2[0] = Money[0];
wtf2[1] = Money[1];
wtf2[2] = Money[2];
wtf2[3] = Money[3];
wtf2[4] = Money[4];
wtf2[5] = Money[5];
wtf2[6] = Money[6];
}
else if (nuM == 3)
{
v[1] = 3;
wtf2[0] = Diamond[0];
wtf2[1] = Diamond[1];
wtf2[2] = Diamond[2];
wtf2[3] = Diamond[3];
wtf2[4] = Diamond[4];
wtf2[5] = Diamond[5];
wtf2[6] = Diamond[6];
}
else if (nuM == 4)
{
v[1] = 4;
wtf2[0] = Bolt[0];
wtf2[1] = Bolt[1];
wtf2[2] = Bolt[2];
wtf2[3] = Bolt[3];
wtf2[4] = Bolt[4];
wtf2[5] = Bolt[5];
wtf2[6] = Bolt[6];
}
else if (nuM == 5)
{
v[1] = 5;
wtf2[0] = Cherry[0];
wtf2[1] = Cherry[1];
wtf2[2] = Cherry[2];
wtf2[3] = Cherry[3];
wtf2[4] = Cherry[4];
wtf2[5] = Cherry[5];
wtf2[6] = Cherry[6];
}
else
{
v[1] = 6;
wtf2[0] = Clover[0];
wtf2[1] = Clover[1];
wtf2[2] = Clover[2];
wtf2[3] = Clover[3];
wtf2[4] = Clover[4];
wtf2[5] = Clover[5];
wtf2[6] = Clover[6];
}
//a3
getNum(0, 6);
if (nuM == 1)
{
v[2] = 1;
wtf3[0] = Seven[0];
wtf3[1] = Seven[1];
wtf3[2] = Seven[2];
wtf3[3] = Seven[3];
wtf3[4] = Seven[4];
wtf3[5] = Seven[5];
wtf3[6] = Seven[6];
}
else if (nuM == 2)
{
v[2] = 2;
wtf3[0] = Money[0];
wtf3[1] = Money[1];
wtf3[2] = Money[2];
wtf3[3] = Money[3];
wtf3[4] = Money[4];
wtf3[5] = Money[5];
wtf3[6] = Money[6];
}
else if (nuM == 3)
{
v[2] = 3;
wtf3[0] = Diamond[0];
wtf3[1] = Diamond[1];
wtf3[2] = Diamond[2];
wtf3[3] = Diamond[3];
wtf3[4] = Diamond[4];
wtf3[5] = Diamond[5];
wtf3[6] = Diamond[6];
}
else if (nuM == 4)
{
v[2] = 4;
wtf3[0] = Bolt[0];
wtf3[1] = Bolt[1];
wtf3[2] = Bolt[2];
wtf3[3] = Bolt[3];
wtf3[4] = Bolt[4];
wtf3[5] = Bolt[5];
wtf3[6] = Bolt[6];
}
else if (nuM == 5)
{
v[2] = 5;
wtf3[0] = Cherry[0];
wtf3[1] = Cherry[1];
wtf3[2] = Cherry[2];
wtf3[3] = Cherry[3];
wtf3[4] = Cherry[4];
wtf3[5] = Cherry[5];
wtf3[6] = Cherry[6];
}
else
{
v[2] = 6;
wtf3[0] = Clover[0];
wtf3[1] = Clover[1];
wtf3[2] = Clover[2];
wtf3[3] = Clover[3];
wtf3[4] = Clover[4];
wtf3[5] = Clover[5];
wtf3[6] = Clover[6];
}
//b1
getNum(0, 6);
if (nuM == 1)
{
v[3] = 1;
wtf4[0] = Seven[0];
wtf4[1] = Seven[1];
wtf4[2] = Seven[2];
wtf4[3] = Seven[3];
wtf4[4] = Seven[4];
wtf4[5] = Seven[5];
wtf4[6] = Seven[6];
}
else if (nuM == 2)
{
v[3] = 2;
wtf4[0] = Money[0];
wtf4[1] = Money[1];
wtf4[2] = Money[2];
wtf4[3] = Money[3];
wtf4[4] = Money[4];
wtf4[5] = Money[5];
wtf4[6] = Money[6];
}
else if (nuM == 3)
{
v[3] = 3;
wtf4[0] = Diamond[0];
wtf4[1] = Diamond[1];
wtf4[2] = Diamond[2];
wtf4[3] = Diamond[3];
wtf4[4] = Diamond[4];
wtf4[5] = Diamond[5];
wtf4[6] = Diamond[6];
}
else if (nuM == 4)
{
v[3] = 4;
wtf4[0] = Bolt[0];
wtf4[1] = Bolt[1];
wtf4[2] = Bolt[2];
wtf4[3] = Bolt[3];
wtf4[4] = Bolt[4];
wtf4[5] = Bolt[5];
wtf4[6] = Bolt[6];
}
else if (nuM == 5)
{
v[3] = 5;
wtf4[0] = Cherry[0];
wtf4[1] = Cherry[1];
wtf4[2] = Cherry[2];
wtf4[3] = Cherry[3];
wtf4[4] = Cherry[4];
wtf4[5] = Cherry[5];
wtf4[6] = Cherry[6];
}
else
{
v[3] = 6;
wtf4[0] = Clover[0];
wtf4[1] = Clover[1];
wtf4[2] = Clover[2];
wtf4[3] = Clover[3];
wtf4[4] = Clover[4];
wtf4[5] = Clover[5];
wtf4[6] = Clover[6];
}
//b2
getNum(0, 6);
if (nuM == 1)
{
v[4] = 1;
wtf5[0] = Seven[0];
wtf5[1] = Seven[1];
wtf5[2] = Seven[2];
wtf5[3] = Seven[3];
wtf5[4] = Seven[4];
wtf5[5] = Seven[5];
wtf5[6] = Seven[6];
}
else if (nuM == 2)
{
v[4] = 2;
wtf5[0] = Money[0];
wtf5[1] = Money[1];
wtf5[2] = Money[2];
wtf5[3] = Money[3];
wtf5[4] = Money[4];
wtf5[5] = Money[5];
wtf5[6] = Money[6];
}
else if (nuM == 3)
{
v[4] = 3;
wtf5[0] = Diamond[0];
wtf5[1] = Diamond[1];
wtf5[2] = Diamond[2];
wtf5[3] = Diamond[3];
wtf5[4] = Diamond[4];
wtf5[5] = Diamond[5];
wtf5[6] = Diamond[6];
}
else if (nuM == 4)
{
v[4] = 4;
wtf5[0] = Bolt[0];
wtf5[1] = Bolt[1];
wtf5[2] = Bolt[2];
wtf5[3] = Bolt[3];
wtf5[4] = Bolt[4];
wtf5[5] = Bolt[5];
wtf5[6] = Bolt[6];
}
else if (nuM == 5)
{
v[4] = 5;
wtf5[0] = Cherry[0];
wtf5[1] = Cherry[1];
wtf5[2] = Cherry[2];
wtf5[3] = Cherry[3];
wtf5[4] = Cherry[4];
wtf5[5] = Cherry[5];
wtf5[6] = Cherry[6];
}
else
{
v[4] = 6;
wtf5[0] = Clover[0];
wtf5[1] = Clover[1];
wtf5[2] = Clover[2];
wtf5[3] = Clover[3];
wtf5[4] = Clover[4];
wtf5[5] = Clover[5];
wtf5[6] = Clover[6];
}
//b3
getNum(0, 6);
if (nuM == 1)
{
v[5] = 1;
wtf6[0] = Seven[0];
wtf6[1] = Seven[1];
wtf6[2] = Seven[2];
wtf6[3] = Seven[3];
wtf6[4] = Seven[4];
wtf6[5] = Seven[5];
wtf6[6] = Seven[6];
}
else if (nuM == 2)
{
v[5] = 2;
wtf6[0] = Money[0];
wtf6[1] = Money[1];
wtf6[2] = Money[2];
wtf6[3] = Money[3];
wtf6[4] = Money[4];
wtf6[5] = Money[5];
wtf6[6] = Money[6];
}
else if (nuM == 3)
{
v[5] = 3;
wtf6[0] = Diamond[0];
wtf6[1] = Diamond[1];
wtf6[2] = Diamond[2];
wtf6[3] = Diamond[3];
wtf6[4] = Diamond[4];
wtf6[5] = Diamond[5];
wtf6[6] = Diamond[6];
}
else if (nuM == 4)
{
v[5] = 4;
wtf6[0] = Bolt[0];
wtf6[1] = Bolt[1];
wtf6[2] = Bolt[2];
wtf6[3] = Bolt[3];
wtf6[4] = Bolt[4];
wtf6[5] = Bolt[5];
wtf6[6] = Bolt[6];
}
else if (nuM == 5)
{
v[5] = 5;
wtf6[0] = Cherry[0];
wtf6[1] = Cherry[1];
wtf6[2] = Cherry[2];
wtf6[3] = Cherry[3];
wtf6[4] = Cherry[4];
wtf6[5] = Cherry[5];
wtf6[6] = Cherry[6];
}
else
{
v[5] = 6;
wtf6[0] = Clover[0];
wtf6[1] = Clover[1];
wtf6[2] = Clover[2];
wtf6[3] = Clover[3];
wtf6[4] = Clover[4];
wtf6[5] = Clover[5];
wtf6[6] = Clover[6];
}
//c1
getNum(0, 6);
if (nuM == 1)
{
v[6] = 1;
wtf7[0] = Seven[0];
wtf7[1] = Seven[1];
wtf7[2] = Seven[2];
wtf7[3] = Seven[3];
wtf7[4] = Seven[4];
wtf7[5] = Seven[5];
wtf7[6] = Seven[6];
}
else if (nuM == 2)
{
v[6] = 2;
wtf7[0] = Money[0];
wtf7[1] = Money[1];
wtf7[2] = Money[2];
wtf7[3] = Money[3];
wtf7[4] = Money[4];
wtf7[5] = Money[5];
wtf7[6] = Money[6];
}
else if (nuM == 3)
{
v[6] = 3;
wtf7[0] = Diamond[0];
wtf7[1] = Diamond[1];
wtf7[2] = Diamond[2];
wtf7[3] = Diamond[3];
wtf7[4] = Diamond[4];
wtf7[5] = Diamond[5];
wtf7[6] = Diamond[6];
}
else if (nuM == 4)
{
v[6] = 4;
wtf7[0] = Bolt[0];
wtf7[1] = Bolt[1];
wtf7[2] = Bolt[2];
wtf7[3] = Bolt[3];
wtf7[4] = Bolt[4];
wtf7[5] = Bolt[5];
wtf7[6] = Bolt[6];
}
else if (nuM == 5)
{
v[6] = 5;
wtf7[0] = Cherry[0];
wtf7[1] = Cherry[1];
wtf7[2] = Cherry[2];
wtf7[3] = Cherry[3];
wtf7[4] = Cherry[4];
wtf7[5] = Cherry[5];
wtf7[6] = Cherry[6];
}
else
{
v[6] = 6;
wtf7[0] = Clover[0];
wtf7[1] = Clover[1];
wtf7[2] = Clover[2];
wtf7[3] = Clover[3];
wtf7[4] = Clover[4];
wtf7[5] = Clover[5];
wtf7[6] = Clover[6];
}
//c2
getNum(0, 6);
if (nuM == 1)
{
v[7] = 1;
wtf8[0] = Seven[0];
wtf8[1] = Seven[1];
wtf8[2] = Seven[2];
wtf8[3] = Seven[3];
wtf8[4] = Seven[4];
wtf8[5] = Seven[5];
wtf8[6] = Seven[6];
}
else if (nuM == 2)
{
v[7] = 2;
wtf8[0] = Money[0];
wtf8[1] = Money[1];
wtf8[2] = Money[2];
wtf8[3] = Money[3];
wtf8[4] = Money[4];
wtf8[5] = Money[5];
wtf8[6] = Money[6];
}
else if (nuM == 3)
{
v[7] = 3;
wtf8[0] = Diamond[0];
wtf8[1] = Diamond[1];
wtf8[2] = Diamond[2];
wtf8[3] = Diamond[3];
wtf8[4] = Diamond[4];
wtf8[5] = Diamond[5];
wtf8[6] = Diamond[6];
}
else if (nuM == 4)
{
v[7] = 4;
wtf8[0] = Bolt[0];
wtf8[1] = Bolt[1];
wtf8[2] = Bolt[2];
wtf8[3] = Bolt[3];
wtf8[4] = Bolt[4];
wtf8[5] = Bolt[5];
wtf8[6] = Bolt[6];
}
else if (nuM == 5)
{
v[7] = 5;
wtf8[0] = Cherry[0];
wtf8[1] = Cherry[1];
wtf8[2] = Cherry[2];
wtf8[3] = Cherry[3];
wtf8[4] = Cherry[4];
wtf8[5] = Cherry[5];
wtf8[6] = Cherry[6];
}
else
{
v[7] = 6;
wtf8[0] = Clover[0];
wtf8[1] = Clover[1];
wtf8[2] = Clover[2];
wtf8[3] = Clover[3];
wtf8[4] = Clover[4];
wtf8[5] = Clover[5];
wtf8[6] = Clover[6];
}
//c3
getNum(0, 6);
if (nuM == 1)
{
v[8] = 1;
wtf9[0] = Seven[0];
wtf9[1] = Seven[1];
wtf9[2] = Seven[2];
wtf9[3] = Seven[3];
wtf9[4] = Seven[4];
wtf9[5] = Seven[5];
wtf9[6] = Seven[6];
}
else if (nuM == 2)
{
v[8] = 2;
wtf9[0] = Money[0];
wtf9[1] = Money[1];
wtf9[2] = Money[2];
wtf9[3] = Money[3];
wtf9[4] = Money[4];
wtf9[5] = Money[5];
wtf9[6] = Money[6];
}
else if (nuM == 3)
{
v[8] = 3;
wtf9[0] = Diamond[0];
wtf9[1] = Diamond[1];
wtf9[2] = Diamond[2];
wtf9[3] = Diamond[3];
wtf9[4] = Diamond[4];
wtf9[5] = Diamond[5];
wtf9[6] = Diamond[6];
}
else if (nuM == 4)
{
v[8] = 4;
wtf9[0] = Bolt[0];
wtf9[1] = Bolt[1];
wtf9[2] = Bolt[2];
wtf9[3] = Bolt[3];
wtf9[4] = Bolt[4];
wtf9[5] = Bolt[5];
wtf9[6] = Bolt[6];
}
else if (nuM == 5)
{
v[8] = 5;
wtf9[0] = Cherry[0];
wtf9[1] = Cherry[1];
wtf9[2] = Cherry[2];
wtf9[3] = Cherry[3];
wtf9[4] = Cherry[4];
wtf9[5] = Cherry[5];
wtf9[6] = Cherry[6];
}
else
{
v[8] = 6;
wtf9[0] = Clover[0];
wtf9[1] = Clover[1];
wtf9[2] = Clover[2];
wtf9[3] = Clover[3];
wtf9[4] = Clover[4];
wtf9[5] = Clover[5];
wtf9[6] = Clover[6];
}
}
int main()
{
srand(time(0));
bool on = true;
while (on)
{
for (int i = 0; i < 10; i++)
{
system("cls");
randomCheck();
slotMachine(wtf1, wtf2, wtf3, wtf4, wtf5, wtf6, wtf7, wtf8, wtf9);
}
system("pause");
}
return 0;
}
Suggestion for randomCheck (I didn't check if it compiles, but hopefully there shouldn't be any problem):
void randomCheck()
{
static string* images[] = {
Seven, Money, Diamond, Bolt, Cherry, Clover
};
static string* wtfs[] = {
wtf1, wtf2, wtf3, wtf4, wtf5, wtf6, wtf7, wtf8, wtf9
};
for (int i = 0; i < 9; ++i) {
v[i] = getNum(0, 6);
string* image = images[v[i]];
string* wtf = wtfs[i];
for (int j = 0; j < 7; ++j)
wtf[j] = image[j];
}
}
Use loops and arrays to reduce code size.

Check if the next move is check-mate

I am trying to check if the next move of the knight will threaten both kind and queen at the same time, if there's such position it will output YES and the position, otherwise it will be NO.
The input will contain only K for king, Q for queen, and N for knight, and they won't be repeated more than once.
Sample input :
........
........
........
...K....
....Q...
........
N.......
........
this input for example indicates that the knight is 2A , Queen is in 4E ,King is in 5D .
here's my code :
#include <cmath>
#include <stdio.h>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <ctype.h>
#include <fstream>
#include <cstddef>
#include <sstream>
#include<string.h>
#include<cstring>
#include<map>
#include<algorithm>
using namespace std;
int main ()
{
string temp ;
bool flag1 = false , flag2 = false ;
int row1 = 0 , col1 = 0 , row2 = 0 , col2 = 0 ;
int ik=0 , jk=0 , iq=0 , jq=0 , in=0 , jn=0 , i = 8 ;
while ( std::getline (std::cin,temp) )
{
for (int j = 0 ; j<=7 ; j++)
{
if(temp[j] == 'K')
{ ik = i ; jk = j+1 ; }
else if(temp[j] == 'Q')
{ iq = i ; jq = j+1 ; }
else if(temp[j] == 'N')
{ in = i ; jn = j+1 ; }
}
i-- ;
}
// j for columns , i for rows
// if jk = 1 means A , =2 means B , and so on
int threatk[8][2] = {0} , threatq[8][2]= {0} , expn[8][2] = {0} ;
// columns first ( position 0 )
// rows second ( position 1 )
threatk[0][0] = jk+1 ;
threatk[0][1] = ik+2 ;
threatk[1][0] = jk+1 ;
threatk[1][1] = ik-2 ;
threatk[2][0] = jk+2 ;
threatk[2][1] = ik+1 ;
threatk[3][0] = jk+2 ;
threatk[3][1] = ik-1 ;
threatk[4][0] = jk-1 ;
threatk[4][1] = ik+2 ;
threatk[5][0] = jk-1 ;
threatk[5][1] = ik-2 ;
threatk[6][0] = jk-2 ;
threatk[6][1] = ik+1 ;
threatk[7][0] = jk-2 ;
threatk[7][1] = ik-1 ;
threatq[0][0] = jq+1 ;
threatq[0][1] = iq+2 ;
threatq[1][0] = jq+1 ;
threatq[1][1] = iq-2 ;
threatq[2][0] = jq+2 ;
threatq[2][1] = iq+1 ;
threatq[3][0] = jq+2 ;
threatq[3][1] = iq-1 ;
threatq[4][0] = jq-1 ;
threatq[4][1] = iq+2 ;
threatq[5][0] = jq-1 ;
threatq[5][1] = iq-2 ;
threatq[6][0] = jq-2 ;
threatq[6][1] = iq+1 ;
threatq[7][0] = jq-2 ;
threatq[7][1] = iq-1 ;
expn[0][0] = jn+1 ;
expn[0][1] = in+2 ;
expn[1][0] = jn+1 ;
expn[1][1] = in-2 ;
expn[2][0] = jn+2 ;
expn[2][1] = in+1 ;
expn[3][0] = jn+2 ;
expn[3][1] = in-1 ;
expn[4][0] = jn-1 ;
expn[4][1] = in+2 ;
expn[5][0] = jn-1 ;
expn[5][1] = in-2 ;
expn[6][0] = jn-2 ;
expn[6][1] = in+1 ;
expn[7][0] = jn-2 ;
expn[7][1] = in-1 ;
for ( int a = 0 ; a<=7 ; a++)
{
for ( int b=0 ; b<=7 ; b++)
{
if ( ( expn[a][0] == threatk[b][0] && expn[a][1] == threatk[b][1] ) )
{ flag1 = true ; col1 = expn[a][0] ; row1 = expn[a][1] ; }
}
}
for ( int a = 0 ; a<=7 ; a++)
{
for ( int b=0 ; b<=7 ; b++)
{
if ( ( expn[a][0] == threatq[b][0] && expn[a][1] == threatq[b][1] ) )
{ flag2 = true ; col2 = expn[a][0] ; row2 = expn[a][1] ; }
}
}
if ( ( flag1 && flag2 ) && ( col1 >= 1 && col1 <= 8 && row1 >= 1 && row1 <= 8)
&& ( col2 >= 1 && col2 <= 8 && row2 >= 1 && row2 <= 8)
&& ( col1 = col2 && row1 = row2) )
{ string out = "" ;
if ( col1 == 1)out = "A" ;
else if ( col1 == 2) out = "B" ;
else if ( col1 == 3) out = "C" ;
else if ( col1 == 4) out = "D" ;
else if ( col1 == 5) out = "E" ;
else if ( col1 == 6) out = "F" ;
else if ( col1 == 7) out = "G" ;
else if ( col1 == 8) out = "H" ;
cout<<"YES"<<" "<<row1<<out ;
}
else cout<<"NO" ;} '
my approach is to get the threat positions for the king and the queen from a knight , and compare with it with the next possible move for the knight
it's working fine but it fails at some tests that am not aware of i just get to know if it passed all the tests or not .
what do you think is wrong ?
When you are designing code, make sure that all parts are easily testable. Design functions that do one clear thing and can be reused easily. Afterwards test them well so you can figure out what part of your code is wrong.
It is very hard to review your code and figure out what could potentially be wrong. I wrote a solution in Python that should pass all possible edge cases and I will share it here. Parsing input and output is not part of it.
N = 8
def generateThreat(y, x):
threats = []
candidate = (y+2, x+1)
if (candidate[0] < N-1 and candidate[1] < N-1):
threats.append(candidate)
candidate = (y+2, x-1)
if (candidate[0] < N-1 and candidate[1] >= 0):
threats.append(candidate)
candidate = (y-2, x+1)
if (candidate[0] >= 0 and candidate[1] < N-1):
threats.append(candidate)
candidate = (y-2, x-1)
if (candidate[0] >= 0 and candidate[1] >= 0):
threats.append(candidate)
candidate = (y+1, x+2)
if (candidate[0] < N-1 and candidate[1] < N-1):
threats.append(candidate)
candidate = (y+1, x-2)
if (candidate[0] < N-1 and candidate[1] >= 0):
threats.append(candidate)
candidate = (y-1, x+2)
if (candidate[0] >= 0 and candidate[1] < N-1):
threats.append(candidate)
candidate = (y-1, x-2)
if (candidate[0] >= 0 and candidate[1] >= 0):
threats.append(candidate)
return threats
def generateAllThreatsFromCurrent(y, x):
all_threats = set()
for next_step in generateThreat(y, x):
all_threats.update(generateThreat(next_step[0], next_step[1]))
return all_threats
def isMatePossible(king, queen, knight):
y, x = knight
all_threats = generateAllThreatsFromCurrent(y, x)
if king in all_threats and queen in all_threads:
return True
return False

Consistent monotonic subsequence

I have problem with this algorithm. It should search for longest consistent and monotonic subsequence and sum of it. If there are few subsequense with the same length it should return the first one.
It should work as monotonic function - http://en.wikipedia.org/wiki/Monotonic_function
For input : 1 1 7 3 2 0 0 4 5 5 6 2 1
the result is : 6 20 - so it works.
But for input : 23 34 11 5 23 90 11 10 15 12 28 49
the result is : 3 113 - but should be 3 50
I feel that the problem is in switching between increasing and decreasing case. Any idea?
code :
#include <stdio.h>
#define gc getchar
void scan_integer(unsigned long long int* o)
{
register unsigned long long int c = gc();
int x = 0;
for (; ((c<48 || c>57)); c = gc());
for (; c>47 && c<58; c = gc()) {
x = (x << 1) + (x << 3) + c - 48;
}
*o = x;
}
int main(){
unsigned long long int current_value, last_value, sum_increasing, sum_decreasing, length_increasing, length_decreasing, max_length, max_sum, is_increasing;
bool equal = false;
scan_integer(&current_value);
last_value = 0;
sum_increasing = current_value;
sum_decreasing = current_value;
length_increasing = 1;
length_decreasing = 1;
max_length = 1;
max_sum = current_value;
is_increasing = 0;
while (!feof(stdin))
{
last_value = current_value;
scan_integer(&current_value);
if (current_value == last_value){
sum_increasing += current_value;
sum_decreasing += current_value;
length_increasing += 1;
length_decreasing += 1;
equal = true;
}
else {
if (current_value > last_value){
sum_increasing += current_value;
length_increasing += 1;
if (equal == true){
length_decreasing = 1;
sum_decreasing = 0;
equal = false;
}
if (is_increasing < 0){
sum_increasing += last_value;
if (length_decreasing > max_length){
max_length = length_decreasing;
max_sum = sum_decreasing;
}
sum_decreasing = 0;
length_decreasing = 1;
}
is_increasing = 1;
}
else {
sum_decreasing += current_value;
length_decreasing += 1;
if (equal == true){
length_increasing = 1;
sum_increasing = 0;
equal = false;
}
if (is_increasing == 1){
sum_decreasing += last_value;
if (length_increasing > max_length){
max_length = length_increasing;
max_sum = sum_increasing;
}
sum_increasing = 0;
length_increasing = 1;
}
is_increasing = -1;
}
}
}
printf("%llu %llu", max_length, max_sum);
return 0;
}
I see a problem in the code, in this part:
is_increasing = 1; // here
// Did you mean to write a continue here?
}
else {
sum_decreasing += current_value;
length_decreasing += 1;
if (equal == true){
length_increasing = 1;
sum_increasing = 0;
equal = false;
}
if (is_increasing == 1){
sum_decreasing += last_value;
if (length_increasing > max_length){
max_length = length_increasing;
max_sum = sum_increasing;
}
sum_increasing = 0;
length_increasing = 1;
}
is_increasing = -1; // Or you might want to put this inside of the else above
If I understand correctly, 'is_increasing = -1' at the bottom completely invalidates the setting of it in the true condition of the if statemen (at the top of the code above). That's why, In the "else" that handles decreasing sequences 'is_increasing' always has the value of '-1' and such sequences never get saved as good sequences.
I put some codes in the code I copied and pasted. I think that handling the codition at which two numbers in sequence might need a little more care than those comments, but this should set you in the right direction.
Let me know if this helps.

summarize variables from current row and its previous

This is my input:
data a;
policy_no = 1;
X_NET_PREMIUM_AMT = 1;
X_NET_PREMIUM_V_AMT = 8;
output;
policy_no = 1;
X_NET_PREMIUM_AMT = 2;
X_NET_PREMIUM_V_AMT = 6;
output;
policy_no = 1;
X_NET_PREMIUM_AMT = 9;
X_NET_PREMIUM_V_AMT = 4;
output;
policy_no = 1;
X_NET_PREMIUM_AMT = 12;
X_NET_PREMIUM_V_AMT = 5;
output;
policy_no = 1;
X_NET_PREMIUM_AMT = 6;
X_NET_PREMIUM_V_AMT = 3;
output;
policy_no = 2;
X_NET_PREMIUM_AMT = 18;
X_NET_PREMIUM_V_AMT = 78;
output;
policy_no = 2;
X_NET_PREMIUM_AMT = 25;
X_NET_PREMIUM_V_AMT = 16;
output;
policy_no = 2;
X_NET_PREMIUM_AMT = 91;
X_NET_PREMIUM_V_AMT = 42;
output;
policy_no = 3;
X_NET_PREMIUM_AMT = 123;
X_NET_PREMIUM_V_AMT = 52;
output;
policy_no = 3;
X_NET_PREMIUM_AMT = 4;
X_NET_PREMIUM_V_AMT = 1;
output;
run;
What I need to do is for every policy_no:
X_NET_PREMIUM_AMT = X_NET_PREMIUM_AMT OF PREVIOUS ROW + X_NET_PREMIUM_V_AMT OF CURRENT ROW.
So far - no problems.
What makes it difficult for me to do is,
I need the X_NET_PREMIUM_AMT to be updated for the next calculation,
I will explain:
row #1 (policy_no = 1):
X_NET_PREMIUM_AMT = 0(no previouse row) + 8 = 8;
row #2 (policy_no = 1):
X_NET_PREMIUM_AMT = 8 + 6 = 14;
row #3 (policy_no = 1):
X_NET_PREMIUM_AMT = 14 + 4 = 18;
And so on....
Unfortunately,
I have no idea how to do this...
Would aprreciate your help
What you are looking for is a cumulative sum of X_NET_PREMIUM_AMT per policy_no. Because overwriting your original variable is not a good idea I'll name the new variable cumsum.
data b;
set a;
by policy_no;
retain cumsum;
if first.policy_no then cumsum=X_NET_PREMIUM_V_AMT; else cumsum=X_NET_PREMIUM_V_AMT+cumsum;
run;
So when a different policy_no is encountered cumsum will be the same as X_NET_PREMIUM_V_AMT, for the other rows of this policy_no it will be X_NET_PREMIUM_V_AMT+cumsum. Your data has to be sorted by policy_no for this code which is the case here.
data out;
set a;
by policy_no;
OUT_X_NET_PREMIUM_AMT = sum(X_NET_PREMIUM_AMT, lag1(X_NET_PREMIUM_AMT));
OUT_X_NET_PREMIUM_V_AMT = sum(X_NET_PREMIUM_V_AMT, lag1(X_NET_PREMIUM_V_AMT));
if last.policy_no then output;
run;
Use lag1() function for that - returns previous value of variable.
Maybe you mean last 3 values:
OUT_X_NET_PREMIUM_AMT = sum(X_NET_PREMIUM_AMT, lag1(X_NET_PREMIUM_AMT), lag2(X_NET_PREMIUM_AMT));
OUT_X_NET_PREMIUM_V_AMT = sum(X_NET_PREMIUM_V_AMT, lag1(X_NET_PREMIUM_V_AMT), lag2(X_NET_PREMIUM_V_AMT));