I am trying to compile the following problem (downloads.cpp) in C++ using Cygwin:
#include <fstream>
#include <algorithm>
#include <cstdio>
#include <climits>
using namespace std;
int main() {
int N, ft = 0, bw = 0;
ifstream input;
input.open("downloads.in");
input >> N;
pair <int, int> S_T[N+1];
S_T[N+1].first = INT_MAX;
S_T[N+1].second = INT_MAX;
int FT[N];
fill(FT, FT + sizeof(FT), 0);
for (int i = 0; i < N; ++i) {
input >> S_T[i].first >> S_T[i].second;
}
input.close();
sort(S_T, S_T + N);
for (int i = 0; i < N; ++i)
{
if (S_T[i+1].second != 0) {
for (int j = i+1; j < N; ++j) {
S_T[j].second -= S_T[i].second;
}
S_T[i+1].first += S_T[i].first;
S_T[i+1].second *= S_T[i-1].second / S_T[i].second;
} else if (S_T[i+1].first != INT_MAX && S_T[i+1].second != INT_MAX) {
S_T[i+1].first += S_T[i].first;
S_T[i+1].second *= S_T[i-1].second / S_T[i].second;
}
FT[i] = S_T[i].second;
}
for (int i = 0; i < N; ++i)
ft += FT[i];
ofstream output;
output.open("downloads.out");
output << ft << endl;
output.close();
return 0;
}
And I get the following error (with varying 5-digit numbers after "downloads"):
0 [main] downloads 76188 cygwin_exception::open_stackdumpfile: Dumping stack trace to downloads.exe.stackdump
[Finished in 6.5s]
Additionally, the program takes significantly longer than the usual one second to compile, and Cygwin seems to create a .exe.stackdump file with the same name as my program in the same file.
I have already checked for Cygwin opening background processes, and have found none. Anyone know why this could be happening?
Related
Hello guys can anyone tell me how do i fix this problem?When i debug in codeblocks and press the next line button it says "Cannot find bounds of current function".How do i fix this?here in the while loop i wanted to test out the debugger.
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define pb push_back
#define vint vector<int>
#define all(v) v.begin(), v.end()
int main()
{
int n, i, in, x, sum1 = 0, sum2, t = 0;
cin >> n;
vint a;
a.pb(0);
for (i = 0; i < n; i++) {
cin >> in;
a.pb(in);
sum1 = sum1 + a[i];
}
in = 1;
n = n + 1;
while (in != 5) { // i want to debug from here.
sum2 = sum1 + in;
for (i = 0; i < n + 1; i++) {
if (i == n + 1) {
i = -1;
continue;
}
sum2--;
if (sum2 == 0) {
break;
}
}
if (i != 0) {
t++;
}
in++;
}
cout << t << endl;
return 0;
}
That usually means that there is no debug info found. Do you try to debug a release project maybe? For code/symbols to be present during debugging, you need to use a debug build.
I am working on a brute force task, but when I run my program, it gives an empty output file. Can anybody please help me fix this? The problem statement is below along with my code after that.
PROBLEM STATEMENT:
Write a program that reads two numbers (expressed in base 10):
N (1 <= N <= 15)
S (0 < S < 10000)
and then finds and prints (in base 10) the first N numbers strictly greater than S that are palindromic when written in two or more number bases (2 <= base <= 10).
Solutions to this problem do not require manipulating integers larger than the standard 32 bits.
CODE
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <fstream>
using namespace std;
string convert(int num, int base)
{
int quo = 100000;
int rem = 0;
string to_reverse;
while (quo > 0)
{
quo = num / base;
rem = num % base;
to_reverse += to_string(rem);
num /= base;
}
reverse(to_reverse.begin(), to_reverse.end());
return to_reverse;
}
bool is_pal(string conv_num)
{
string reversed_conv_num = conv_num;
reverse(reversed_conv_num.begin(), reversed_conv_num.end());
if (reversed_conv_num == conv_num)
{
return true;
}
return false;
}
int main()
{
ofstream fout("dualpal.out");
ifstream fin("dualpal.in");
int n, start;
fin >> n >> start;
vector<int> finals;
int times = 0;
for (int i = start + 1; i <= 10000; i++)
{
if (times == n)
{
for (auto x : finals)
{
fout << x << "\n";
}
break;
}
else
{
for (int j = 2; j <= 10; j++)
{
if(is_pal(convert(i, j)) == true)
{
times++;
finals.push_back(i);
}
}
}
}
return 0;
}
Try this code. I made some changes.
#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <fstream>
using namespace std;
string convert(int num, int base)
{
int quo = 100000;
int rem = 0;
string to_reverse;
ostringstream str1; /*this and the next commented lines are added because "to_string" didnt work in my compiler*/
while (quo > 0)
{
quo = num / base;
rem = num % base;
str1 << rem; //this
to_reverse += str1.str(); //and this
num /= base;
}
reverse(to_reverse.begin(), to_reverse.end());
return to_reverse;
}
bool is_pal(string conv_num)
{
string reversed_conv_num = conv_num;
reverse(reversed_conv_num.begin(), reversed_conv_num.end());
if (reversed_conv_num == conv_num)
{
return true;
}
return false;
}
int main()
{
ofstream fout;
fout.open("dualpal.out", ios::out); //open the file in binary mode
//ifstream fin("dualpal.in");
int n, start;
cin >> n >> start;
vector<int> finals;
int times = 0;
for (int i = start + 1; i <= 10000; i++)
{
if (times == n)
{
//just a simple iterator for vector
for (vector<int>::iterator it = finals.begin(); it != finals.end(); ++it)
{
fout << *it << "\n";
}
break;
}
else
{
for (int j = 2; j <= 10; j++)
{
if(is_pal(convert(i, j)) == true)
{
times++;
finals.push_back(i);
}
}
}
}
fout.close(); //close the file
return 0;
}
USE OF STRING STREAMS
int number = 1000;
ostringstream s;
s << number;
string str = s.str();
This method can be used to convert number to strings.
This code requires <sstream> header file.
I am trying to make a C++ program of the FourSum problem i.e. Does there exist a sum of four different numbers that sum to zero.
"i + j + k + l = 0" "-2 + -1 + 3 + 0 = 0"
I can get it to work, but the running time is around 10-15 times slower than the java implementation (both are naive approaches, so basically just four for loops). I know it is possible to use BinarySearch for the last loop, provided you sort the array. But it was my understanding that generally C++ should perform better and run faster than Java and certainly also Python. So how do I do that?
Any or all suggestions are welcome. Thanks.
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
int main () {
string line;
int N;
ifstream myfile ("ints/ints-400-1.txt");
getline(myfile,line);
stringstream into(line);
into >> N;
std::vector<long long> vals (N);
if (myfile.is_open())
{
int i = 0;
while ( getline(myfile,line) ) {
vals[i] = stoll( line );
++i;
}
myfile.close();
}
else cout << "Unable to open file";
for (int i = 0; i != vals.size(); i++) {
for (int j = i + 1; j != vals.size(); j++) {
for (int k = j + 1; k != vals.size(); k++) {
for (int l = k + 1; l != vals.size(); l++) {
if (vals[i] + vals[j] + vals[k] +vals[l] == 0) {
cout << "true";
}
}
}
}
}
return 0;
}
I have a task that by given line of weights of cages and I have to split them into 2 trucks. The split should be done like this that |a - b| to have least value where 'a' is the common weight of the cages in the first truck and 'b' is the common weight of the cages of second truck. My program seems to work but when I upload it to hackerrank abort function is called. So where is my fault? Here is the code:
#include <iostream>
#include <vector>
#include <sstream>
#include <cstring>
using namespace std;
int main()
{
string input;
int k;
while (getline(cin, input))
{
/* splitting the input into integers */
vector<int> v;
istringstream iss(input);
while (iss >> k) v.push_back(k);
/* --- II --- */
unsigned long sum = 0;
unsigned i, j;
for (i = 0; i < v.size(); i++)
sum += v[i];
vector<char> can;
can.push_back(1);
for (i = 1; i <= sum; i++)
can[i] = 0;
for (i = 0; i < v.size(); i++)
{
for (j = sum; j+1 > 0; j--)
{
if (can[j])
{
can[j + v[i]] = 1;
}
}
}
for (i = sum / 2; i > 1; i--)
{
if (can[i])
{
if (i <= sum - i)
{
cout << i << " " << sum - i << endl;
break;
}
else
{
cout << "a should be <= b";
break;
}
}
}
}
return 0;
}
How can this work?
You create an empty vector of char, push one single value into it and that try to assign value passed the first:
...
vector<char> can;
can.push_back(1); // can contains one single value
for (i = 1; i <= sum; i++)
can[i] = 0; // Error "vector subscript out of range" in debug mode
If you do not ask the control of vector subscript you will just invoke undefined behaviour.
But if you just want to expand the vector, you can repeatedly can push_back:
for (i = 1; i <= sum; i++)
can.push_back(0);
EDIT: solved! I was treating negative numbers test case as 0, instead of having the output be negative as well. thanks for the help!
Here is the challenge description: https://www.codeeval.com/open_challenges/17/
I keep getting a partially solved score. I want to know why. As in my eyes, this code works. And I believe that it is O(N) time. Thanks for looking!
Here is my code:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
int max(int a, int b)
{
if (a > b)
return a;
else return b;
}
int maxSubArray(vector<int> values)
{
int max_so_far = values[0];
int curr_max = values[0];
for(int i = 1; i < values.size(); ++i)
{
curr_max = max(values[i], curr_max + values[i]);
max_so_far = max(max_so_far, curr_max);
}
return max_so_far;
}
int main(int argc, char *argv[])
{
std::vector<vector<int> > Values; //to hold the values of the stock price change
ifstream file(argv[1]);
std::string line; //for the txt file input
int value = 0; //for holding the value of stock change
while (std::getline(file, line))
{
int pos = 0;
if(line.length() == 0)
continue;
else
{
std::istringstream iss(line);
std::vector<int> list; // temporary list of values to be pushed back into the 2-d vector
while (iss >> value)
{
list.push_back(value);
}
Values.push_back(list);
}
}
for(int i = 0; i < Values.size(); ++i)
{
cout << maxSubArray(Values[i]);
cout << endl;
}
/*
cout << " Printing the values : " << endl;
for (int j = 0; j < Values.size(); ++j)
{
for (int k = 0; k < Values[j].size(); ++k)
cout << Values[j][k] << " ";
cout << endl;
}
*/
return 0;
}
so I swapped out some code now. I get better score but I it's still a partial.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
using namespace std;
int max(int a, int b)
{
if (a > b)
return a;
else return b;
}
int maxSubArray(vector<int> values)
{
int max_so_far = values[0];
int curr_max = values[0];
if (curr_max < 0)
{
curr_max = 0;
max_so_far = 0;
}
for(int i = 1; i < values.size(); ++i)
{
curr_max = max(values[i], curr_max + values[i]);
curr_max = max(curr_max, 0);
max_so_far = max(max_so_far, curr_max);
}
return max_so_far;
}
int main(int argc, char *argv[])
{
std::vector<vector<int> > Values; //to hold the values of the stock price change
ifstream file(argv[1]);
std::string line; //for the txt file input
std::string token; //for the subtring that will be converted from char to int
int value = 0; //for holding the value of stock change
int count = 0;// for holding how many total cases
while (!file.eof())
{
int pos = 0;
getline(file, line);
if(line.length() == 0)
continue;
else
{
std::vector<int> list; // temporary list of values to be pushed back into the 2-d vector
while ((pos = line.find(",")) != std::string::npos )
{
token = line.substr(0,pos);
value = atoi(token.c_str());
line.erase(0, pos + 1);
list.push_back(value);
}
value = atoi(line.c_str());
list.push_back(value);
Values.push_back(list);
++count;
}
}
for(int i = 0; i < Values.size(); ++i)
{
cout << maxSubArray(Values[i]);
cout << endl;
}
cout << " Printing the values : " << endl;
for (int j = 0; j < Values.size(); ++j)
{
for (int k = 0; k < Values[j].size(); ++k)
cout << Values[j][k] << " ";
cout << endl;
}
return 0;
}
Why are you passing the vector by value here?
int maxSubArray(vector<int> values)
That looks like a significant optimization opportunity.
I think you don't read the problem exactly right. When they say 'all contiguous sub ararys', they mean you have to take the max over all i andj of for(idx = i; i < j; ++i) { total += vec[idx]; }. Right now your code basically assumes i = 0 which isn't what you are supposed to do.
Just from looking at the output examples they provide, I can see that your code isn't going to give the answer that they expect.
it seems right, the only thing I can think of is that when the list gets long, your result can overflow, so change int to long long.
Besides technical optimizations suggested in other answers, concerning the algorithm, i think a little fix can make your algorithm work. When curr_max drops to a negative value, due to encountering a negative integer that exceeds curr_max, you can simply drop all the previous integers including the current value and start over. This fix is simple, you can add one line to your loop like this:
for(int i = 1; i < values.size(); ++i)
{
curr_max = max(values[i], curr_max + values[i]);
curr_max = max(curr_max, 0); // <---------------- add this line
max_so_far = max(max_so_far, curr_max);
}