I tested my code on valgrind. It said I lost 1 block in memory leak, which is in this line :
Project *temp = new Project[numProjects + 1]; . Does anyone now how to fix the issue.
Project *temp = new Project[numProjects + 1];
bool Department::addProject(Project &newProject)
{
bool valid = true;
double totalCost = 0;
for (int i = 0; i < numProjects; i++)
{
totalCost += projects[i].m_cost;
}
totalCost += newProject.m_cost;
if (totalCost > budget)
{
valid = false;
}
else
{
Project *temp = new Project[numProjects + 1];
for (int i = 0; i < numProjects; i++)
{
temp[i] = projects[i];
}
temp[numProjects] = newProject;
numProjects++;
delete[] projects;
projects = temp;
}
return valid;
}
I tried many ways including delete[] temp or delete temp inside for if and else and even clear() function after that. Nothing changes the equation.
https://en.wikipedia.org/wiki/ANSI_escape_code#CSI_sequences
I tried to use this site but I couldn't, can you help me?
this cod is running in windows system but i want to this code change linux. What should i do?
tetro is my class vector. I want it to go slowly down the map
for (int i = 0; i < tetro.size(); i++) {
string tetrodemo = tetro[i].print();
tetro[i].Board(board);
for (int j = 0; j < 10; j++) {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos = { 1, (j + 1) };
SetConsoleCursorPosition(hConsole, pos);
WriteConsoleA(hConsole, tetrodemo.c_str(), tetrodemo.size(), NULL, NULL);
Sleep(WAIT_LOOP);
}
My code is not passing check50 and I can't find what's wrong.
// Sort pairs in decreasing order by strength of victory
void sort_pairs(void)
{
// TODO
for (int i = 0; i < pair_count - 1; i++)
{
int c = 0;
int high = preferences[pairs[i].winner][pairs[i].loser] -preferences[pairs[i].loser}[pairs[i].winner];
for (int j = i + 1; j < pair_count; j++)
{
if (preferences[pairs[j].winner][pairs[j].loser] - preferences[pairs[j].loser][pairs[j].winner] > high)
{
high = preferences[pairs[j].winner][pairs[j].loser] - preferences[pairs[j].loser][pairs[j].winner];
c = j;
}
}
pair temp = pairs[i];
pairs[i] = pairs[c];
pairs[c] = temp;
}
return;
}
I mimimized my code to the problem part.
I used to create root signature version 1.0 with no problem. Then I tried to upgrade my code to compatible with root signature version 1.1 if the hardware support.
D3D12_ROOT_DESCRIPTOR1 CBV1rootDescriptor;
CBV1rootDescriptor.ShaderRegister = 0;
CBV1rootDescriptor.RegisterSpace = 0;
CBV1rootDescriptor.Flags = D3D12_ROOT_DESCRIPTOR_FLAG_NONE;
D3D12_ROOT_DESCRIPTOR1 CBV2rootDescriptor;
CBV2rootDescriptor.ShaderRegister = 1;
CBV2rootDescriptor.RegisterSpace = 0;
CBV1rootDescriptor.Flags = D3D12_ROOT_DESCRIPTOR_FLAG_NONE;
D3D12_ROOT_PARAMETER1 rootParam[2];
rootParam[0].ParameterType = D3D12_ROOT_PARAMETER_TYPE_CBV;
rootParam[0].Descriptor = CBV1rootDescriptor;
rootParam[0].ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL;
rootParam[1].ParameterType = D3D12_ROOT_PARAMETER_TYPE_CBV;
rootParam[1].Descriptor = CBV2rootDescriptor;
rootParam[1].ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL;
D3D12_FEATURE_DATA_ROOT_SIGNATURE featureData = {};
featureData.HighestVersion = D3D_ROOT_SIGNATURE_VERSION_1_1;
if (FAILED(device->CheckFeatureSupport(D3D12_FEATURE_ROOT_SIGNATURE, &featureData, sizeof(featureData))))
{
featureData.HighestVersion = D3D_ROOT_SIGNATURE_VERSION_1_0;
}
D3D12_ROOT_SIGNATURE_DESC1 rootSigDesc = {};
rootSigDesc.NumParameters = _countof(rootParam);
rootSigDesc.pParameters = rootParam;
rootSigDesc.NumStaticSamplers = 0;
rootSigDesc.pStaticSamplers = nullptr;
rootSigDesc.Flags = D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT;
D3D12_VERSIONED_ROOT_SIGNATURE_DESC VersionedrootSigDesc = {};
VersionedrootSigDesc.Version = featureData.HighestVersion;
VersionedrootSigDesc.Desc_1_1 = rootSigDesc;
ID3DBlob* serializedRootSig = nullptr;
ID3DBlob* errorBlob = nullptr;
ThrowIfFailed(D3DX12SerializeVersionedRootSignature(&VersionedrootSigDesc, featureData.HighestVersion, &serializedRootSig, &errorBlob));
The code will throw if I run like above, I checked featureData.HighestVersion is 1.1. If I forced featureData.HighestVersion to 1.0, the code will pass. And if I remove the second rootParam[1], only use 1 rootParam, even featureData.HighestVersion is 1.1, the code will pass. Does version 1.1 has some restrictions on CBV on root descriptor? (Windows 10 21H2 OS Build 19044.1706, Visual Studio 2022 community 17.2.2)
D3D12_ROOT_DESCRIPTOR1 CBV2rootDescriptor;
CBV2rootDescriptor.ShaderRegister = 1;
CBV2rootDescriptor.RegisterSpace = 0;
CBV1rootDescriptor.Flags = D3D12_ROOT_DESCRIPTOR_FLAG_NONE;
Since I copied and pasted for CBV2, I didn't change CBV1rootDescriptor.Flags = D3D12_ROOT_DESCRIPTOR_FLAG_NONE; to CBV2rootDescriptor.Flags = D3D12_ROOT_DESCRIPTOR_FLAG_NONE; and I didn't initialize CBV2rootDescriptor, so CBV2rootDescriptor.Flags could be random number which cause the creation failed. I found this when I initialize CBV2rootDescriptor to {}, then the failure gone, which made to to look at each assignment again and found CBV1rootDescriptor.Flags didn't change to CBV2rootDescriptor.Flags.
I'm trying to solve this problem from Google's Code Jam 2008:
The problem is called Train Timetable and you can find the full explanation here:
Code Jam - Train Timetable
Note: I've decided to solve the problem with Node.js.
My code is the next:
function timeToMinutes(time) {
const timeArray = time.split(":");
const hours = parseInt(timeArray[0]);
const minutes = parseInt(timeArray[1]);
const hoursInMinutes = hours * 60;
const total = hoursInMinutes + minutes;
return total;
}
function timetableFiller(NAB, NBA, array) {
let timetable = {
departuresFromA: [],
arrivalsToB: [],
departuresFromB: [],
arrivalsToA: [],
};
for (let i = 0; i < NAB + NBA; i++) {
let tempArr = [];
tempArr = array[i].split(" ");
if (i < NAB) {
timetable.departuresFromA.push(tempArr[0]);
timetable.arrivalsToB.push(tempArr[1]);
} else {
timetable.departuresFromB.push(tempArr[0]);
timetable.arrivalsToA.push(tempArr[1]);
}
}
return timetable;
}
function timetableToMinutes(timetable) {
let timetableMinutes = {
departuresFromA: [],
arrivalsToB: [],
departuresFromB: [],
arrivalsToA: [],
};
for (const property in timetable) {
timetable[property].map((element) =>
timetableMinutes[property].push(timeToMinutes(element))
);
}
return timetableMinutes;
}
function trainsNeededCounter(arrivalsFromDestiny, departuresFromOrigin, tat) {
let trainsNeeded = departuresFromOrigin.length;
for (let i = 0; i < arrivalsFromDestiny.length; i++) {
for (let j = 0; j < departuresFromOrigin.length; j++) {
if (arrivalsFromDestiny[i] + tat <= departuresFromOrigin[j]) {
trainsNeeded = trainsNeeded - 1;
departuresFromOrigin.splice(j, 1);
}
}
}
return trainsNeeded;
}
function responseGenerator(inputA, inputB, caseNumber) {
return `Case #${caseNumber}: ${inputA} ${inputB}`;
}
function problemSolution(input) {
const numberOfCases = parseInt(input[0]);
input.shift();
let response = [];
let caseNumber = 0;
let NAB;
let NBA;
for (let i = 0; i < input.length; i = i + NAB + NBA + 2) {
caseNumber = caseNumber + 1;
const tat = parseInt(input[i]);
const arrayNTrips = input[i + 1].split(" ");
NAB = parseInt(arrayNTrips[0]);
NBA = parseInt(arrayNTrips[1]);
const arraySchedule = input.slice(i + 2, i + 2 + NAB + NBA);
const timetable = timetableFiller(NAB, NBA, arraySchedule);
const timetableMinutes = timetableToMinutes(timetable);
const trainsNeededAB = trainsNeededCounter(
timetableMinutes.arrivalsToA,
timetableMinutes.departuresFromA,
tat
);
const trainsNeededBA = trainsNeededCounter(
timetableMinutes.arrivalsToB,
timetableMinutes.departuresFromB,
tat
);
response.push(
responseGenerator(trainsNeededAB, trainsNeededBA, caseNumber)
);
}
return response;
}
function readInput() {
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false,
});
let problem = [];
rl.on("line", (line) => {
problem.push(line);
}).on("close", () => {
const solution = problemSolution(problem);
solution.map((response) => console.log(response));
});
}
readInput();
How to replicate the issue
You should login into Code Jam with your Google account.
Paste into the code area on the right side and activate the Test run mode.
As input you can copy paste the sample input provided in the problem and you can see that the output is exactly as the sample output.
I've tried with my own variations of the input and the responses seems correct but when I run the real attempt the platform says WA or Wrong Answer.
Thank you so much for your help!
I made a video about this recently. You should check it out.
I think you can understand the logic flow from it. We are both doing the same thing basically.
https://youtu.be/_Cp51vMDZAs
-check this out
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
void solve(int t)
{
int NA, NB;
float T;
cin >> T >> NA >> NB;
cin.ignore();
vector<string> ASchedule, BSchedule;
if (NA > 0)
for (int i = 0; i < NA; i++)
{
string s;
getline(cin, s);
ASchedule.push_back(s);
}
if (NB > 0)
for (int i = 0; i < NB; i++)
{
string s;
getline(cin, s);
BSchedule.push_back(s);
}
int alength, blength;
alength = (int)ASchedule.size();
blength = (int)BSchedule.size();
if (alength == 0 || blength == 0)
{
cout << "Case #" << t << ": " << alength << " " << blength << endl;
return;
}
float TT = T / 10;
string val, value;
int d;
float ADH, ADM, AAH, AAM, BDH, BDM, BAH, BAM;
vector<float> AD, AA, BD, BA;
for (int i = 0; i < alength; i++)
{
val = ASchedule[i];
ADH = stof(val.substr(0, 2));
AAH = stof(val.substr(6, 2));
ADM = stof(val.substr(3, 2));
AAM = stof(val.substr(9, 2));
if (val.at(9) == '0')
{
AAM /= 10;
AAM += TT;
AAM *= 10;
}
else
AAM += T;
if (AAM > 59)
{
d = -1;
while (AAM != 59)
{
AAM -= 1;
d++;
}
AAH++;
AAM = 0;
AAM += d;
}
// if (ADH > 23)
// ADH = 0;
// if (AAH > 23)
// AAH = 0;
ADM /= 100;
ADH += ADM;
AAM /= 100;
AAH += AAM;
AD.push_back(ADH);
AA.push_back(AAH);
}
for (int j = 0; j < blength; j++)
{
value = BSchedule[j];
BDH = stof(value.substr(0, 2));
BDM = stof(value.substr(3, 2));
BAH = stof(value.substr(6, 2));
BAM = stof(value.substr(9, 2));
if (value.at(9) == '0')
{
BAM /= 10;
BAM += TT;
BAM *= 10;
}
else
BAM += T;
if (BAM > 59)
{
d = -1;
while (BAM != 59)
{
BAM -= 1;
d++;
}
BAH++;
BAM = 0;
BAM += d;
}
// if (BDH > 23)
// BDH = 0;
// if (BAH > 23)
// BAH = 0;
BDM /= 100;
BDH += BDM;
BAM /= 100;
BAH += BAM;
BA.push_back(BAH);
BD.push_back(BDH);
}
int no1 = alength, no2 = blength;
sort(BD.begin(), BD.end());
sort(BA.begin(), BA.end());
sort(AA.begin(), AA.end());
sort(AD.begin(), AD.end());
for (int i = 0; i < alength; i++)
for (int j = 0; j < blength; j++)
if (AD[i] >= BA[j])
{
no1--;
BA[j] = 50;
break;
}
for (int i = 0; i < blength; i++)
for (int j = 0; j < alength; j++)
if (AA[j] <= BD[i])
{
no2--;
AA[j] = 50;
break;
}
cout << "Case #" << t << ": " << no1 << " " << no2 << endl;
}
int main()
{
int N;
cin >> N;
cin.ignore();
for (int t = 1; t <= N; t++)
solve(t);
}