Convert julian to date c++ - c++

I am trying to get the date from a julian date using this two functions that I wrote
int dayepoch(int amin, int acur,int mcur, int dcur){
int daycount = 0;
for(int a=amin;a<acur;a++){
for(int m=mcur;m<=12;m++){
for(int d=1;d<=daymo(a,m);d++) daycount ++;
}
}
for(int m=1;m<mcur-1;m++){
for(int d=1;d<=daymo(acur,m);d++) daycount ++;
}
for(int d=1;d<=dcur;d++) daycount ++;
return daycount;
}
int *dayepochrev(int amin, int e){
int a = amin;
int m = 1;
int d = 1;
while(dayepoch(amin,a,m,d) <= e) a++;
a--;
while(dayepoch(amin,a,m+1,d) <= e) m++;
m--;
while(dayepoch(amin,a,m+1,d) <= e) d++;
d--;
int *epochrev = new int[3];
epochrev[0] = a;
epochrev[1] = m;
epochrev[2] = d;
return epochrev;
delete[] epochrev;
}
where
int daymo(int a, int m){
int dmmax = 0;
if((a % 4 == 0 && a % 100 != 0)||(a % 400 == 0) ) {
if(m==0)dmmax = 0;
if(m==1)dmmax = 31;
if(m==2)dmmax = 29;
if(m==3)dmmax = 31;
if(m==4)dmmax = 30;
if(m==5)dmmax = 31;
if(m==6)dmmax = 30;
if(m==7)dmmax = 31;
if(m==8)dmmax = 31;
if(m==9)dmmax = 30;
if(m==10)dmmax = 31;
if(m==11)dmmax = 30;
if(m==12)dmmax = 31;
}
else {
if(m==0)dmmax = 0;
if(m==1)dmmax = 31;
if(m==2)dmmax = 28;
if(m==3)dmmax = 31;
if(m==4)dmmax = 30;
if(m==5)dmmax = 31;
if(m==6)dmmax = 30;
if(m==7)dmmax = 31;
if(m==8)dmmax = 31;
if(m==9)dmmax = 30;
if(m==10)dmmax = 31;
if(m==11)dmmax = 30;
if(m==12)dmmax = 31;
}
return dmmax;
}
If the starting month is MOSTART = 4 and the starting year = 1945 and the number of months considered are 12, than the total number of days are
EPOCH = 365
(from 1/4/1945 to 31/3/1946) which I can get using
int EPOCH = dayepoch(1945, 1946, MOSTART,31);
which is the TOTAL NUMBER OF DAYS IN THE GIVEN PERIOD (DAYS SINCE YEAR).
Then I would like to come back to the date using the sequence 1-365 applied to the given period
for(int e=1;e<EPOCH+1;e++){
epochrev = dayepochrev(amin,e);
}
but I get the dates 1-1-1945 -> 31-12-1945. Please can you help me to do this?
Many thanks

Related

How to generate nested tree of offsets based on index permutation C++

I got the following offsets which reset and go to the next offset if offset value hits 0xC. The last offset is always 0xC.
How do I generate the nested tree of offsets based on index.
The pattern goes like this
0,C = index 0
4,C = index 1
8,C = index 2
0,0,C = index 3
0,4,C = index 4
0,8,C = index 5
4,0,C = index 6
4,4,C = index 7
4,8,C = index 8
8,0,C = index 9
8,4,C = index 10
8,8,C = index 11
0,0,0,C = index 12
and so on and so on.
#include <stdio.h>
int main() {
int offset_0 = 0;
int offset_1 = 0;
int offset_2 = 0;
int offset_3 = 0;
int offset_4 = 0;
bool offset_0_activate = true;
bool offset_1_activate = false;
bool offset_2_activate = false;
bool offset_3_activate = false;
int index = 100;
for (int i = 0; i < index; i++) {
offset_0 += 4;
if (offset_0 == 0xC) {
offset_0 = 0;
offset_1 += 4;
}
if (offset_1 == 0xC) {
offset_1 = 0;
offset_2 += 4;
}
if (offset_2 == 0xC) {
offset_2 = 0;
offset_3 += 4;
}
if (offset_3 == 0xC) {
break;
}
printf("index = %d offset0 = %d offset1 = %d offset2 = %d offset3 = %d\n", i, offset_0, offset_1, offset_2, offset_3);
}
}

Compression in c++ using bitwise operators

We have been given an assignment in which we have to compress 4 bytes into 3 bytes. It wants us to have a compression of 25%, by packing chars into 6 bits instead of 8 bits. It should be 25% exact compression, but my program is doing approx 50%. Code Book is my own "ASCII" sort of table, The bitwise operators are used to perform compression. Does anyone have any idea why it is compressing it by 50% rather than 25%? I know namespace std isn't a good practice, but we are asked to use it. Thanks!`
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
unsigned char CodeBook[53][2];
unsigned char FindCharacterCode(unsigned char C)
{
for (int j = 0; j < 53; j++)
if (CodeBook[j][0] == C)
return CodeBook[j][1];
return 0;
}
void MyCodeBook()
{
CodeBook[0][0] = 'a'; CodeBook[0][1] = 1;
CodeBook[1][0] = 'b'; CodeBook[1][1] = 2;
CodeBook[2][0] = 'c'; CodeBook[2][1] = 3;
CodeBook[3][0] = 'd'; CodeBook[3][1] = 4;
CodeBook[4][0] = 'e'; CodeBook[4][1] = 5;
CodeBook[5][0] = 'f'; CodeBook[5][1] = 6;
CodeBook[6][0] = 'g'; CodeBook[6][1] = 7;
CodeBook[7][0] = 'h'; CodeBook[7][1] = 8;
CodeBook[8][0] = 'i'; CodeBook[8][1] = 9;
CodeBook[9][0] = 'j'; CodeBook[9][1] = 10;
CodeBook[10][0] = 'k'; CodeBook[10][1] = 11;
CodeBook[11][0] = 'l'; CodeBook[11][1] = 12;
CodeBook[12][0] = 'm'; CodeBook[12][1] = 13;
CodeBook[13][0] = 'n'; CodeBook[13][1] = 14;
CodeBook[14][0] = 'o'; CodeBook[14][1] = 15;
CodeBook[15][0] = 'p'; CodeBook[15][1] = 16;
CodeBook[16][0] = 'q'; CodeBook[16][1] = 17;
CodeBook[17][0] = 'r'; CodeBook[17][1] = 18;
CodeBook[18][0] = 's'; CodeBook[18][1] = 19;
CodeBook[19][0] = 't'; CodeBook[19][1] = 20;
CodeBook[20][0] = 'u'; CodeBook[20][1] = 21;
CodeBook[21][0] = 'v'; CodeBook[21][1] = 22;
CodeBook[2][0] = 'w'; CodeBook[22][1] = 23;
CodeBook[23][0] = 'x'; CodeBook[23][1] = 24;
CodeBook[24][0] = 'y'; CodeBook[24][1] = 25;
CodeBook[25][0] = 'z'; CodeBook[25][1] = 26;
CodeBook[26][0] = '0'; CodeBook[26][1] = 27;
CodeBook[27][0] = '1'; CodeBook[27][1] = 28;
CodeBook[28][0] = '2'; CodeBook[28][1] = 29;
CodeBook[29][0] = '3'; CodeBook[29][1] = 30;
CodeBook[30][0] = '4'; CodeBook[30][1] = 31;
CodeBook[31][0] = '5'; CodeBook[31][1] = 32;
CodeBook[32][0] = '6'; CodeBook[32][1] = 33;
CodeBook[33][0] = '7'; CodeBook[33][1] = 34;
CodeBook[34][0] = '8'; CodeBook[34][1] = 35;
CodeBook[35][0] = '9'; CodeBook[35][1] = 36;
CodeBook[36][0] = '!'; CodeBook[36][1] = 37;
CodeBook[37][0] = '$'; CodeBook[37][1] = 38;
CodeBook[38][0] = '('; CodeBook[38][1] = 39;
CodeBook[39][0] = ')'; CodeBook[39][1] = 40;
CodeBook[40][0] = '#'; CodeBook[40][1] = 41;
CodeBook[41][0] = '&'; CodeBook[41][1] = 42;
CodeBook[42][0] = '%'; CodeBook[42][1] = 43;
CodeBook[43][0] = '-'; CodeBook[43][1] = 44;
CodeBook[44][0] = '.'; CodeBook[44][1] = 45;
CodeBook[45][0] = ','; CodeBook[45][1] = 46;
CodeBook[46][0] = '\''; CodeBook[46][1] = 47;
CodeBook[47][0] = ';'; CodeBook[47][1] = 48;
CodeBook[48][0] = ':'; CodeBook[48][1] = 49;
CodeBook[49][0] = '?'; CodeBook[49][1] = 50;
CodeBook[50][0] = ' '; CodeBook[50][1] = 51;
CodeBook[51][0] = ' '; CodeBook[51][1] = 52;
CodeBook[52][0] = 'ΒΆ'; CodeBook[52][1] = 53;
}
int main()
{
MyCodeBook();
ifstream In;
ofstream Out;
In.open("C://Users//osama//Desktop//code.txt");
Out.open("C://Users//osama//Desktop//compressed.txt");
unsigned char Data[4] = { 0 },
Compressed[3] = { 0 }, Code[4] = { 0 };
int i;
while (!In.eof())
{
if (In.is_open())
{
for (i = 0; i < 4; i++)
{
In >> Data[i];
Code[i] = FindCharacterCode(Data[i]);
}
}
else
{
cout << "Not open!" << endl << endl;
return -1;
}
Compressed[0] = Code[0] << 2;
Compressed[0] = (Compressed[0] | Code[1] >> 4);
Compressed[1] = (Code[1] << 4);
Compressed[1] = (Compressed[1] | Code[2] >> 2);
Compressed[2] = (Code[2] << 6);
Compressed[2] = Compressed[2] | Code[3];
for (i = 0; i < 3; i++)
{
Out << Compressed[i];
}
}
In.close();
Out.close();
return 0;
}
Hint:
For any given input of your compression algorithm you will get a number between 1 and 53 (let's call it 0 and 53).
How many bits do we need to fit a number this large?
1? in one bit we can store between 0 and 1 - too small.
2? in 2 bits we can store between 0 and 3 - too small.
...
5? in 5 bits we can store between 0 and 31 - too small.
6? in 6 bits we can store between 0 and 63 - big enough.
the inputs are chars, or bytes - 8 bits.
6 / 8 * 100 = 75%
Here's your 25% compression.
Now you need to figure out how to encode 4 lots of 6 bits into 3 bytes of 8 bits.
That's where your bitwise operators (and some bit-shifting) will come in.

Programming Homework: Robot Genetic Algorithm [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
This program is built to run 10000 generations of 200 robots, allowing evolution to shape the digits in the robot's "DNA". The problem I'm having is that, even though there is nothing but numbers going into the 'robotGenes' array, the resulting DNA that prints is a random collection of letters, numbers and symbols. I have absolutly no idea why. I have tried switching up where I declare and assign my variables, unfortunatly its all been in vane. Plz halp.
P.s. I'm posting the entire program code here, as I am unsure what is causing the problem, though I am positive that I am simply being daft. Any help is greatly appreciated.
#include <iostream>
#include <string>
#include <stdlib.h>
#include <cmath>
using namespace std;
int randBattery = 0,
randRobot = 0,
randDirect = 0,
board[144] = { },
testDirect = 0,
randTurn = 0,
randTurnDirect = 0,
genAdd = 0,
masterFit = 0,
abc = 0;
double fitness[10000] = { },
winners[5] = { };
std::string startCond;
unsigned char robotGenes[200][12] = { },
test[12] = { };
class Display {
public:
int results() {
cout << "\n\n\n-Evolution Winning Avg. Fitness Ratings-\n\n";
cout << "1. " << winners[1];
cout << "\n DNA: ";
for (int G = 1; G <= 12; G++)
{
cout << robotGenes[1][G];
}
cout << "\n\n2. " << winners[2];
cout << "\n DNA: ";
for (int G = 1; G <= 12; G++)
{
cout << robotGenes[41][G];
}
cout << "\n\n3. " << winners[3];
cout << "\n DNA: ";
for (int G = 1; G <= 12; G++)
{
cout << robotGenes[81][G];
}
cout << "\n\n4. " << winners[4];
cout << "\n DNA: ";
for (int G = 1; G <= 12; G++)
{
cout << robotGenes[121][G];
}
cout << "\n\n5. " << winners[5];
cout << "\n DNA: ";
for (int G = 1; G <= 12; G++)
{
cout << robotGenes[161][G];
}
return 0;
};
};
class RobotSex {
public:
int bang() {
int newGen, newGen2, B;
newGen = 101;
newGen2 = 102;
B = 2;
for (int C = 1; C <= 100; C += 2)
{
robotGenes[newGen][6] = robotGenes[C][6];
robotGenes[newGen][7] = robotGenes[C][7];
robotGenes[newGen][8] = robotGenes[C][8];
robotGenes[newGen][9] = robotGenes[C][9];
robotGenes[newGen][10] = robotGenes[B][10];
robotGenes[newGen][11] = robotGenes[B][11];
robotGenes[newGen2][6] = robotGenes[B][6];
robotGenes[newGen2][7] = robotGenes[B][7];
robotGenes[newGen2][8] = robotGenes[B][8];
robotGenes[newGen2][9] = robotGenes[B][9];
robotGenes[newGen2][10] = robotGenes[C][10];
robotGenes[newGen2][11] = robotGenes[C][11];
newGen += 2;
newGen2 += 2;
B += 2;
}
};
};
class Genocide {
public:
int ethnicCleansing() {
for (int x = 101; x <= 200; x++)
{
for (int y = 6; y <= 12; y++)
{
robotGenes[x][y] = 0;
}
}
};
};
class Sorting {
public:
int sortPower() {
for (int P = 0; P <= 200; P++)
{
for (int x = 1; x <= 200; x++)
{
int y;
y = x + 1;
if (robotGenes[x][12] < robotGenes[y][12])
for (int Q = 1; Q <= 12; Q++)
swap (robotGenes[x][Q], robotGenes[y][Q]);
}
}
};
int addGenFit() {
for (int o = 1; o <= 200; o++)
{
genAdd += robotGenes[o][12];
}
fitness[masterFit] = genAdd / 200;
::masterFit += 1;
};
int sortGenFit() {
for (int P = 0; P <= 10000; P++)
{
for (int x = 1; x <= 9999; x++)
{
int y;
y = x + 1;
if (fitness[x] < fitness[y])
swap (fitness[x], fitness[y]);
}
}
for (int o = 1; o <= 2000; o++)
{
genAdd += fitness[o];
}
winners[1] = genAdd / 2000;
for (int o = 2001; o <= 4000; o++)
{
genAdd += fitness[o];
}
winners[2] = genAdd / 2000;
for (int o = 4001; o <= 6000; o++)
{
genAdd += fitness[o];
}
winners[3] = genAdd / 2000;
for (int o = 6001; o <= 8000; o++)
{
genAdd += fitness[o];
}
winners[4] = genAdd / 2000;
for (int o = 8001; o <= 10000; o++)
{
genAdd += fitness[o];
}
winners[5] = genAdd / 2000;
};
};
class Establishing {
public:
int clearBoard() {
for (int c = 0; c <= 144; c++) //Clearing board
{
board[c] = 0;
}
for (int x = 0; x <= 12; x++) //Establishing walls
{
board[x] = 9;
}
for (int x = 132; x <= 144; x++)
{
board[x] = 9;
}
for (int x = 13; x <= 121; x += 12)
{
board[x] = 9;
}
for (int x = 24; x <= 132; x += 12)
{
board[x] = 9;
}
};
};
class Randomizer {
public:
int randBattery() {
for (int t = 1; t <= 4; t++)
{
srand(time(NULL)); //Random battery placement R1
::randBattery = (rand() % 14+23);
board[::randBattery] = 1;
}
for (int t = 1; t <= 4; t++)
{
srand(time(NULL)); //Random battery placement R2
::randBattery = (rand() % 26+35);
board[::randBattery] = 1;
}
for (int t = 1; t <= 4; t++)
{
srand(time(NULL)); //Random battery placement R3
::randBattery = (rand() % 38+47);
board[::randBattery] = 1;
}
for (int t = 1; t <= 4; t++)
{
srand(time(NULL)); //Random battery placement R4
::randBattery = (rand() % 50+59);
board[::randBattery] = 1;
}
for (int t = 1; t <= 4; t++)
{
srand(time(NULL)); //Random battery placement R5
::randBattery = (rand() % 62+71);
board[::randBattery] = 1;
}
for (int t = 1; t <= 4; t++)
{
srand(time(NULL)); //Random battery placement R6
::randBattery = (rand() % 74+82);
board[::randBattery] = 1;
}
for (int t = 1; t <= 4; t++)
{
srand(time(NULL)); //Random battery placement R7
::randBattery = (rand() % 85+95);
board[::randBattery] = 1;
}
for (int t = 1; t <= 4; t++)
{
srand(time(NULL)); //Random battery placement R8
::randBattery = (rand() % 98+107);
board[::randBattery] = 1;
}
for (int t = 1; t <= 4; t++)
{
srand(time(NULL)); //Random battery placement R9
::randBattery = (rand() % 110+119);
board[::randBattery] = 1;
}
for (int t = 1; t <= 4; t++)
{
srand(time(NULL)); //Random battery placement R10
::randBattery = (rand() % 122+131);
board[::randBattery] = 1;
}
};
int randPlacement() {
for (int t = 1; t <= 200; t++)
{
srand(time(NULL)); //Random robot placement & resets power level
randRobot = (rand() % 62+71);
robotGenes[t][1] = randRobot;
robotGenes[t][12] = 5;
}
};
char randDirect() {
for (int d = 1; d <= 200; d++)
{
srand(time(NULL)); //Random robot direction
::randDirect = (rand() % 62+71);
robotGenes[d][5] = ::randDirect;
}
};
};
class Reader {
public:
int readDNA() {
do {
if (board[test[1]] == 1) //Pickup battery
test[12] += 5;
if (test[9] == 1) //Check for wall
{
if (board[test[2]] == 9)
{
if (test[5] == 1)
test[5] = 4;
if (test[5] == 2)
test[5] = 1;
if (test[5] == 3)
test[5] = 2;
if (test[5] == 4)
test[5] = 3;
}
if (board[test[2]] == 9 && board[test[3]] == 9)
{
if (test[5] == 1)
test[5] = 2;
if (test[5] == 2)
test[5] = 3;
if (test[5] == 3)
test[5] = 4;
if (test[5] == 4)
test[5] = 1;
}
}
if (test[7] == test[6]) //Changing direction after # of moves
if (test[6] % 2 == 0) //Turning left
{
if (test[5] == 1)
test[5] = 4;
if (test[5] == 2)
test[5] = 1;
if (test[5] == 3)
test[5] = 2;
if (test[5] == 4)
test[5] = 3;
}
else //Turning right
{
if (test[5] == 1)
test[5] = 2;
if (test[5] == 2)
test[5] = 3;
if (test[5] == 3)
test[5] = 4;
if (test[5] == 4)
test[5] = 1;
}
if (test[10] == 1) //Left sensor checking for battery, TURN LEFT
if (test[5] == 1)
test[5] = 4;
if (test[5] == 2)
test[5] = 1;
if (test[5] == 3)
test[5] = 2;
if (test[5] == 4)
test[5] = 3;
if (test[11] == 1) //Right sensor checking for battery, TURN RIGHT
if (test[5] == 1)
test[5] = 2;
if (test[5] == 2)
test[5] = 3;
if (test[5] == 3)
test[5] = 4;
if (test[5] == 4)
test[5] = 1;
if (test[5] == 1) //Face north
{
test[2] = test[1] + 12;
::test[3] = test[1] - 1;
test[4] = test[1] + 1;
test[1] += 12; //Move north
test[2] += 12;
::test[3] += 12;
test[4] += 12;
}
if (test[5] == 2) //Face east
{
test[2] = test[1] + 1;
::test[3] = test[1] + 12;
test[4] = test[1] - 12;
test[1] += 1; //Move east
test[2] += 1;
::test[3] += 1;
test[4] += 1;
}
if (test[5] == 3) //Face south
{
test[2] = test[1] - 12;
::test[3] = test[1] + 1;
test[4] = test[1] - 1;
test[1] -= 12; //Move south
test[2] -= 12;
::test[3] -= 12;
test[4] -= 12;
}
if (test[5] == 4) //Face west
{
test[2] = test[1] - 1;
::test[3] = test[1] - 12;
test[4] = test[1] + 12;
test[1] -= 1; //Move west
test[2] -= 1;
::test[3] -= 1;
test[4] -= 1;
}
test[7] += 1;
test[12] -= 1;
} while (test[12] > 1 && test[7] < 24);
};
};
class Running {
public:
void runRobot() {
for (int q = 1; q <= 200; q++)
{
for (int z = 1; z <= 12; z++)
{
::test[z] = robotGenes[q][z];
}
Reader objectDNA;
objectDNA.readDNA();
for (int z = 1; z <= 12; z++)
{
::robotGenes[q][z] = test[z];
}
}
};
};
int main()
{
//System Greeting
cout << "Enter anything to start simulation.\n\n";
cin >> startCond;
masterFit = 1;
//Setting starting genes
for (int t = 1; t <= 200; t++)
{
srand(time(NULL));
randTurn = (rand() % 10+1);
robotGenes[t][6] = randTurn;
}
for (int o = 1; o <= 40; o++)
{
robotGenes[o][8] = 1;
robotGenes[o][9] = 0;
robotGenes[o][10] = 0;
robotGenes[o][11] = 0;
}
for (int o = 41; o <= 80; o++)
{
robotGenes[o][8] = 0;
robotGenes[o][9] = 1;
robotGenes[o][10] = 1;
robotGenes[o][11] = 1;
}
for (int o = 81; o <= 120; o++)
{
robotGenes[o][8] = 1;
robotGenes[o][9] = 1;
robotGenes[o][10] = 0;
robotGenes[o][11] = 0;
}
for (int o = 121; o <= 160; o++)
{
robotGenes[o][8] = 0;
robotGenes[o][9] = 0;
robotGenes[o][10] = 0;
robotGenes[o][11] = 0;
}
for (int o = 161; o <= 200; o++)
{
robotGenes[o][8] = 1;
robotGenes[o][9] = 0;
robotGenes[o][10] = 1;
robotGenes[o][11] = 1;
}
//Running sims (not the game)
Randomizer objectRaDirect;
objectRaDirect.randDirect();
Randomizer objectRaPlace;
objectRaPlace.randPlacement();
for (int sims = 1; sims <= 10000; sims++)
{
Establishing objectClear;
objectClear.clearBoard();
Randomizer objectRaBattery;
objectRaBattery.randBattery();
Running objectRun;
objectRun.runRobot();
abc += 1;
cout << "\n\nGeneration " << abc << " is complete.\n";
Sorting objectPower;
objectPower.sortPower();
Sorting objectGenFit;
objectGenFit.addGenFit();
Genocide objectHitler;
objectHitler.ethnicCleansing();
RobotSex objectSex;
objectSex.bang();
}
Sorting objectWin;
objectWin.sortGenFit();
for (int B = 1; B <= 5; B++)
{
winners[B] = abs (winners[B]);
}
Display objectEnd;
objectEnd.results();
};
You have two major issues with your code:
1) You have an out-of-bounds array access here:
unsigned char robotGenes[200][12] = { }, test[12] = { };
//...
for (int G = 1; G <= 12; G++)
{
cout << robotGenes[1][G];
}
On the last iteration, you are printing a garbage value. The program actually invokes undefined behavior, since robotGenes[1][G] is out-of-bounds when G is 12.
You are also making the same mistake in the ethnicCleansing function, but probably even worse since you are not just reading an invalid location, you are writing to it:
for (int y = 6; y <= 12; y++)
{
robotGenes[x][y] = 0;
}
When y is 12, you are writing to an invalid index.
Array indices start from 0 up to n-1, where n is the total number of entries in the array. Thus G ranges from 0 to 11, inclusive. Going to index 12 is an out-of-bounds access.
2) You are calling functions that are supposed to return a value, but fail to do so. For example,
int Genocide::ethnicCleansing
This function is supposed to return an int, but doesn't return anything. Thus your program invokes undefined behavior. Not returning a value from a function that is supposed to return a value is undefined behavior. Note that you have several other functions with the same issue.
Please see your full code here.
Fix the warnings, fix the out-of-bounds accesses first. I don't know if these will be the only problems, but they are the two that stick out like sore thumbs.
You are most likely printing the data as a char array instead of an unsigned char array. This causes ASCII characters represented by the numbers to be printed instead of the actual numbers.

C++ Functions in loop/struct

Here is my unaltered working code:
#include <iostream>
using namespace std;
const int MAXACCOUNTS = 8;
int interest(int Balance, int MAXACCOUNTS);
struct Account
{
int Number;
double Balance;
int DaysSinceDebited;
};
int main()
{
int Accountnumber;
double Balance;
int DaysSinceDebited;
double Total[MAXACCOUNTS] = {};
Account accounts[MAXACCOUNTS];
accounts[0].Number = 1001;
accounts[0].Balance = 4254.40;
accounts[0].DaysSinceDebited = 20;
accounts[1].Number = 7940;
accounts[1].Balance = 270006.25;
accounts[1].DaysSinceDebited = 35;
accounts[2].Number = 4382;
accounts[2].Balance = 123.50;
accounts[2].DaysSinceDebited = 2;
accounts[3].Number = 2651;
accounts[3].Balance = 85326.92;
accounts[3].DaysSinceDebited = 14;
accounts[4].Number = 3020;
accounts[4].Balance = 657.0;
accounts[4].DaysSinceDebited = 5;
accounts[5].Number = 7168;
accounts[5].Balance = 7423.34;
accounts[5].DaysSinceDebited = 360;
accounts[6].Number = 6285;
accounts[6].Balance = 4.99;
accounts[6].DaysSinceDebited = 1;
accounts[7].Number = 9342;
accounts[7].Balance = 107964.44;
accounts[7].DaysSinceDebited = 45;
for (int i = 0; i < MAXACCOUNTS; i++)
{
if ((accounts[i].Balance > 10000) || (accounts[i].DaysSinceDebited>30))
Total[i] = accounts[i].Balance * 1.06; //6% interest added
else Total[i] = accounts[i].Balance * 1.03; //3% interest added
cout << accounts[i].Number << " has a balance of " << accounts[i].Balance << ". The amount with interest is: " << Total[i] << endl;
system("pause");
}
}
Here is what i need to do: You must add a function to your program called CalcInterest. This function will take as its ONLY parameter an Account, and return the Interest calculated as shown in Part 1. Your main program should now use this function instead, to generate the display as in Part 1.
This is what i tried:
#include <iostream>
using namespace std;
const int MAXACCOUNTS = 8;
int CalcInterest(Account);
struct Account { //declare struct outside of main
int Number;
double Balance;
int DaysSinceDebited;
};
int main()
{
int AccountNumber[MAXACCOUNTS] = { 1001, 7940, 4382, 2651, 3020, 7168, 6245, 9342 };
double Balance[MAXACCOUNTS] = { 4254.40, 27006.25, 123.50, 85326.92, 657.0, 7423.34, 4.99, 107864.44 };
int DaysSinceDebited[MAXACCOUNTS] = { 20, 35, 2, 14, 5, 360, 1, 45 };
double Total[MAXACCOUNTS] = {};
//add your code here
Account accounts[MAXACCOUNTS];
accounts[0].Number = 1001;
accounts[0].Balance = 4254.40;
accounts[0].DaysSinceDebited = 20;
accounts[1].Number = 7940;
accounts[1].Balance = 270006.25;
accounts[1].DaysSinceDebited = 35;
accounts[2].Number = 4382;
accounts[2].Balance = 123.50;
accounts[2].DaysSinceDebited = 2;
accounts[3].Number = 2651;
accounts[3].Balance = 85326.92;
accounts[3].DaysSinceDebited = 14;
accounts[4].Number = 3020;
accounts[4].Balance = 657.0;
accounts[4].DaysSinceDebited = 5;
accounts[5].Number = 7168;
accounts[5].Balance = 7423.34;
accounts[5].DaysSinceDebited = 360;
accounts[6].Number = 6285;
accounts[6].Balance = 4.99;
accounts[6].DaysSinceDebited = 1;
accounts[7].Number = 9342;
accounts[7].Balance = 107964.44;
accounts[7].DaysSinceDebited = 45;
CalcInterest(Account);
}
int CalcInterest(Account) {
for (int i = 0; i < MAXACCOUNTS; i++)
{
if ((accounts[i].Balance > 10000) || (accounts[i].DaysSinceDebited > 30))
Total[i] = accounts[i].Balance * 1.06;
else Total[i] = accounts[i].Balance * 1.03;
cout << accounts[i].Number << "has a balance of " << accounts[i].Balance << ". The amount with interest is : " << Total[i] << endl;
}
system("pause");
return 0;
}
There were many errors with this, mostly of things becoming undefined, such as .DaysSinceDebited etc PLEASE HELP!
I believe that by "there are many errors", you are talking about compilation errors. A quick look at your code confirms this.
The first mistake you made is that this function only works on a single account. You therefore cannot loop over your accounts array inside that function, neither can you access Total. You are also a little confused about the syntax of passing arguments, as well as the datatype that should be returned. I can help you with that.
Change your function definition to:
double CalcInterest( const Account & account )
{
// Do your interest calculation on 'account' here, then return it from the function.
double interest = 0.0; //<-- For you to do.
return interest;
}
Then you can simplify your loop in main...
for (int i = 0; i < MAXACCOUNTS; i++)
{
// Calculate the interest on the account, then do something with it.
double interest = CalcInterest( accounts[i] );
Total[i] = 0.0; //<-- For you to do.
}
Note that I've only provided the language structure here, since this is obviously an assignment of some sort. I have indicated the parts where you need to do some work.

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.