Hi im having a problem with my program and i honestly dont know where or how to solve it. Here is the code
#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <iomanip>
#include <stdio.h>
#include <string.h>
using namespace std;
int main()
{
bool ok;
int testcases;
scanf("%d", &testcases);
vector<int> days(testcases);
vector<int> parties(testcases);
vector<vector<int>> hartals(2);
for (int i = 0; i < testcases; i++)
{
scanf("%d", &(days[i]));
scanf("%d", &parties[i]);
hartals[i].resize(parties[i]);
for (int j = 0; j < parties[i]; j++)
{
scanf("%d", &hartals[i][j]);
}
}
vector<vector<int>> initialHartalsValues = hartals;
for (int i = 0; i < testcases; i++)
{
int week = 0, nonWorkingDays = 0;
for (int j = 1; j < days[i] + 1; j++)
{
if (j != 1 && j - 7 * (week + 1) == 1)
{
week++;
}
ok = true;
for (int k = 0; k < hartals[i].size(); k++)
{
if (hartals[i][k] == j)
{
hartals[i][k] = hartals[i][k] + initialHartalsValues[i][k];
if (ok && j - 7 * week != 6 && j - 7 * week != 7)
{
ok = false;
nonWorkingDays++;
}
}
}
}
printf("%d\n", nonWorkingDays);
}
return 0;
}
The program works fine and it does its job but when i submit it to the judges i throws a runtime error and i cant find where it is.
vector<vector<int>> hartals(2);`
Constructs two entries in the outer vector and no entries in the inner vectors`.
But then we have
for (int i = 0; i < testcases; i++)
{
...
hartals[i].resize(parties[i]);
testcases probably isn't 2, so i can easily be out of bounds and invoke the dreaded Undefined Behaviour. On your machine the program manifests behaviour that appears to "work". On the judging system, you have better luck and the program crashes. Strange as it sounds, crashes are good luck. With a crash you know you have a bug.
Give
vector<vector<int>> hartals(testcases);
a test and see if it helps out. Could be more bugs in there, but that's probably the one you're tripping over right now.
Related
What is the problem with this code? It's nth prime number generator. It gives me the right answer but it gives a run time error in the second test case in Timus online judge. Can anyone please help me? Here is my code.
#include <iostream>
#include <cmath>
using namespace std;
#define MAX 16000
bool prime[MAX];
void sieve() {
prime[0]=prime[1]=true;
int r=sqrt(MAX);
for(int i = 3; i <= r; i++) {
for(int j = i*i; j <= MAX; j+=(2*i)) {
prime[j]=true;
}
}
}
int main()
{
int t, n;
sieve();
cin >> t;
for(int i = 0; i < t; i++) {
cin >> n;
int c = 1, k, tk=2;
for(k = 3; c < n; k+=2) {
if(k%2!=0 && prime[k]==false) {
c++;
tk = k;
}
}
cout << tk << endl;
}
return 0;
}
One glaring error is this one:
#define MAX 16000
bool prime[MAX];
// ...
for(int j = i*i; j <= MAX; j+=(2*i)) {
prime[j]=true; // <-- Buffer overrun when j == MAX
When j == MAX, you are writing to prime[MAX], when the highest index is MAX - 1. This is a buffer overrun, thus leading to undefined behavior.
Any loop that has <= as the condition to continue is considered suspect. This erroneous use of <= in a for loop condition is one tell-tale sign that an off-by-one access may occur.
The condition probably should be:
for(int j = i*i; j < MAX; j+=(2*i)) {
prime[j]=true;
As to the safer coding that C++ gives you, using std::array<bool, MAX>, and then the at() function would flag this:
#include <array>
#define MAX 16000
std::array<bool, MAX> prime;
// ...
for(int j = i*i; j <= MAX; j+=(2*i)) {
prime.at(j) = true; // <-- std::out_of_range exception thrown
This would have thrown a std::out_of_range exception as soon as j == MAX, stopping your program from going forward with (hopefully) a description of why the exception was raised.
I'm creating this program just for fun but i'm havin a problem
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int j = 1;
std::vector<int> n_bin(j);
int n=0;
int a;
cin>>n;
do
{
n_bin[j] = n%2;
n= n/2;
a++;
j++;
}while(n>0);
for(j=a; j>0; j--)
{
cout<<n_bin[j];
}
return 0;
}
the only problem is the output, infact before the real binary number i get a lot of more number for example:
if i assign 104 to n i get: 246978862916874543020108190880108137760108190884026797651687454302000108137760108191524026797651687454302183600828414839627280108137760146978862716874543020001321208800108136960-10108137760108364961101000 where only the last 7 are useful.
n_bin is initialized to have exactly one element, at n_bin[0]. n_bin[j] exhibits undefined behavior for any value of j other than 0, by way of accessing an index out of bounds.
void decToBinary(int n)
{
int binaryNum[1000];
int i = 0;
while(n > 0)
{
binaryNum[i] = n % 2;
n = n / 2;
i++;
}
for(int j = i - 1; j >= 0; j--)
{
cout << binaryNum[j];
}
}
void selectionSort (vector<string>& dictionary)
{
string min;
for (int i = 0; i < dictionary.size(); ++i)
{
for (int j = i + 1; j < dictionary.size(); ++j)
{
if (dictionary.at(j) < dictionary.at(i))
{
min = dictionary.at(j);
}
}
swap(dictionary.at(i), min);
}
return;
}
I entered 5 inputs: hey, ok, so, no, okay
Outputs: no, okay, so, no
Could someone explain where I went wrong with my sorting function? Thanks!
Try this instead:
void selectionSort(vector<string>& dictionary)
{
for (int i = 0; i < dictionary.size(); ++i)
{
int m = i;
for (int j = i + 1; j < dictionary.size(); ++j)
{
if (dictionary[j] < dictionary[m])
m = j;
}
if (m != i)
swap(dictionary[i], dictionary[m]);
}
}
Basically when your i-loop starts you assume that m-th element is smallest and then inside j-loop you check remaining elements if there is a smaller element. If you find smaller element you update m (instead of copying actual strings). At the end of the loop you check if m is changed from i, and if so you swap elements at i and m.
Here's full example with your input, when asking question it's recommended to provide similar minimal example that can easily reproduce your problem:
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
#include <assert.h>
using namespace std;
void selectionSort(vector<string>& dictionary)
{
for (int i = 0; i < dictionary.size(); ++i)
{
int m = i;
for (int j = i + 1; j < dictionary.size(); ++j)
{
if (dictionary[j] < dictionary[m])
m = j;
}
if (m != i)
swap(dictionary[i], dictionary[m]);
}
}
int main()
{
vector<string> data{ "hey", "ok", "so", "no", "okay" };
vector<string> data2 = data;
selectionSort(data);
std::sort(data2.begin(), data2.end());
assert(equal(data.begin(), data.end(), data2.begin())); // verify that your sort matches what std::sort does
for (const auto s : data)
cout << s << ' ';
cout << endl;
}
and the output is:
hey no ok okay so
I have the following program (this is it in its entirety, except comments):
#include <iostream>
#include <vector>
void sieve(uint32_t n) {
for(uint32_t i = 0; i < n; i++) {
for(uint32_t j = i * i; j < n; j += i) {
}
}
}
int main(int argv, char * argc[]) {
sieve(10);
return 0;
}
It compiles just fine. When I run it, it just hangs forever. Even if I put std::cout << "Test" as the first line in main, it never prints.
Am I missing something obvious here?
for(uint32_t j = i * i; j < n; j += i) {
First time through i is 0 and so j doesn't actually change (j += 0) ==> infinite loop
I started learning C++ recently, and thought I would test my mettle with Project Euler problems. I solved the first two, but I am stuck on the third. It is compiling correctly without any errors, but it is crashing as soon as it is executed. I tried removing the nested for loops to isolate the problem, and it still crashed.
#include<iostream>
#include<math.h>
int main()
{
float quot;
int num = 0;
int array[100];
float next;
for(int i = 0; i < 100; i++)
{
for (int j = 0; j < 100; j++)
{
if((i % j) == 0)
{
quot=j/i;
num=num+1;
}
if (num=2)
{
array[i]=i;
}
}
}
for (int i = 0; i < 100; i++)
{
if((13195 % i) == 0)
{
std::cout << i;
}
}
}
In if((i%j)==0) if i and j are zero, your next line is dividing i and j. This is division by zero.