UVA Online Judge: Non-reproducible Runtime Error [closed] - c++

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 2 years ago.
Improve this question
My code compiled(g++) without errors and ran without errors on my system(Macos) for sample input from the website, but is showing a Runtime Error in the online judge. Please help, I'm a new programmer. This is the problem statement.
My program returns correct results for the test input data.
They haven't given the exact reason for the runtime error.
The website has this information about runtime error:
Runtime Error (RE): Your program failed during the execution (segmentation fault, floating point exception...). The exact cause is not reported to the user to avoid hacking. Be sure that your program returns a 0 code to the shell.
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>
using namespace std;
int main(){
string line;
while(getline( cin,line) && line != "" ){
istringstream split(line);
string kt,jt;
getline(split, kt, ' ');
getline(split, jt);
int k = stoi(kt);
int j = stoi(jt);
vector<int> all_cycles;
for(int i = k; i<= j; i++){
vector<int> cycle ;
int temp = i;
while(temp != 1){
cycle.push_back(temp);
if(temp %2 == 0) temp = temp /2;
else temp = (temp*3) + 1;
}
all_cycles.push_back(cycle.size() + 1 );
cycle.clear();
}
cout << k << " " << j << " " << *max_element(all_cycles.begin(), all_cycles.end()) << "\n";
all_cycles.clear();
}
return 0;
}
Code Update 1:
Replaced vectors with counters.
Fixed undefined behavior if j < k.
Submitted the updated code, still Run time error.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main(){
string line;
while(getline( cin,line) && line != "" ){
istringstream split(line);
string kt,jt;
getline(split, kt, ' ');
getline(split, jt);
int k = stoi(kt);
int j = stoi(jt);
if(k > j) {
int t = k;
k = j;
j = t;
}
int max_cycle = 0;
for(int i = k; i<= j; i++){
int cycle = 1 ;
int temp = i;
while(temp != 1){
cycle++;
if(temp %2 == 0) temp = temp /2;
else temp = (temp*3) + 1;
}
if(cycle > max_cycle) max_cycle = cycle;
}
cout << k << " " << j << " " << max_cycle << "\n";
}
return 0;
}
Code Update 2:
I replaceed string parsing the input by using formatted input and solved the newline detection problem by copying the next character of input from the streambuffer to a variable and checked if it is a newline.
This updated code seems to get rid of the Runtime Error and Now it is an output error.
Which I should be able to solve through formatting my output.
So it seems that stoi in my earlier code might have been throwing an exception during runtime.Thank you #walnut for pointing that out.
#include <iostream>
using namespace std;
int main(){
int k, j;
int eofile = 0;
while(cin >> k >> j && !eofile ){
streambuf * pbuf = cin.rdbuf();
char ch = pbuf->snextc();
if(ch == '\n') eofile = 1;
if(k > j) {
int t = k;
k = j;
j = t;
}
int max_cycle = 0;
for(int i = k; i<= j; i++){
int cycle = 1 ;
int temp = i;
while(temp != 1){
cycle++;
if(temp %2 == 0) temp = temp /2;
else temp = (temp*3) + 1;
}
if(cycle > max_cycle) max_cycle = cycle;
}
cout << k << " " << j << " " << max_cycle << "\n";
}
return 0;
}
If there is a better way to get formatted input and also detect newline, please tell me how to do it.

Solved the problem :
Program run without any errors with runtime : 0.330s
The runtime error was solved when I replaced the stoi() function with input formatting.
Finally there was an output error because I was flipping k and j to prevent undefined behavior when j < k, which I solved by formatting the output accordingly.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main(){
string line;
while(getline( cin,line) && line != "" ){
istringstream split(line);
int k,j;
split >> k >> j;
int flip = 0;
if(k > j) {
flip = 1;
int t = k;
k = j;
j = t;
}
int max_cycle = 0;
for(int i = k; i<= j; i++){
int cycle = 1 ;
int temp = i;
while(temp != 1){
cycle++;
if(temp %2 == 0) temp = temp /2;
else temp = (temp*3) + 1;
}
if(cycle > max_cycle) max_cycle = cycle;
}
if(!flip) cout << k << " " << j << " " << max_cycle << "\n";
else cout << j << " " << k << " " << max_cycle << "\n";
}
return 0;
}
Thanks to everyone for all the help.

Related

Error in finding the right elements from an array in fibonacci sequence c++

So i am changed the code a little bit. It still doesn't work but i think i am little bit closer. The program should check if the number you put in (inside bar[100]) match the fibonacci numbers(stored in fib[30]) and then print the ones that match.
#include <iostream>
#include <math.h>
using namespace std;
int main() {
long x, bar[100], fib[30];
cout << "Cate numere sunt in array? = ";
cin >> x;
for (int i = 0; i < x; i++) {
cout << "bar[" << i << "]=";
cin >> bar[i];
}
fib[0] = 1;
fib[1] = 1;
fib[2] = 2;
for (int i = 3; i <= 30; i++) {
fib[i] = fib[i - 2] + fib[i - 1];
// cout << fib[i] << " ";
}
for (int i = 0; i < x; i++) {
bool fibonacci = false;
int j = 0;
while (j < 30 && !fib) {
// the mistake is here ( ' || ' instead of ' && ')
if (fib[j] == bar[i]) {
fibonacci = true;
}
j++;
}
if (fibonacci) {
cout << bar[i] << " ";
}
}
return 0;
}```
The code above works pretty well for me. It prints out every right number of the Fibonacci sequence you introduce in the bar[100] array. Special thanks to #PaulMcKenzie and everyone who helped!
Move the bar[1] bar[2] and bar[3] initialization outside of the for loop. They are not defined outside the scope of it.

How can I compare the elements in a vector?

I took a look online and none of the answers solves the problem I have comparing the elements from a vector.
I tried implementing a bool function but the problem is the same.
I am pretty new in c++ so please be patient!
PART2: First of all thank you.
So I changed my programm and created a bool function, the problem is now that it doesn get recognised before 5-6 tries.
#include <iostream>
#include <vector>
#include <time.h>
#include <stdlib.h>
#include <string>
using namespace std;
vector<int> input, compareMe, randomNumbers;
const unsigned int MAX_VEKTORSTELLEN = 5;
const unsigned int UPPER_GRENZE = 49;
const unsigned int LOWER_GRENZE = 1;
unsigned int i, j;
string output;
int random, anzahlRichtige, eingabe;
bool isEqual = false;
string lotto(vector<int>)
{
if (input[i] < LOWER_GRENZE || input[i] > UPPER_GRENZE)
{
output = "Die Zahlen muessen zwischen 1 und 49 liegen! \n";
input.pop_back();
}
else if (input.size() != MAX_VEKTORSTELLEN)
output = "Es muessen 6 Zahlen uebergeben werde! \n";
else if (isEqual == true)
output = "Es duerfen keine doppelten Zahlen vorkommen! \n";
else
for (i = 0; i <= MAX_VEKTORSTELLEN; i++)
srand((unsigned)time(NULL) <= UPPER_GRENZE && (unsigned)time(NULL) > 0);
random = rand();
randomNumbers.push_back(random);
return output;
}
bool compare()
{
compareMe = input;
for (i = 0; i < input.size(); i++)
for (j = 0; j < compareMe.size(); j++)
if (compareMe[j] == input[i])
isEqual = true;
return isEqual;
}
int main()
{
cout << "insert 6 numbers: ";
while (cin >> eingabe)
{
input.push_back(eingabe);
lotto(input);
compare();
cout << output;
for (i = 0; i < input.size(); i++) //Debug
cout << input[i] << ", ";
continue;
}
for (i = 0; i < input.size(); i++)
cout << input[i];
system("pause");
return 0;
}
From line 34 to line I didn´t finish to code but doesn´t really matter because I got stuck before.
All your loops in lotto are wrong. You go one past the end of your containers.
for (i = 0; i <= input.size(); i++)
// ^ !!!
It should be <.
You got this right in main.

How to remove blank from code

I have coded this program and it works fine. I get the result I want but because we are using an old system to submit it, my code is rejected because it saying that the 3 last lines generate a blank of my code. Can someone please tell me where is the problem and how I can fix it? Thank you!
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int i, row_nr;
cin >> row_nr;
if(row_nr > 1 && row_nr <= 30)
for(i = 1; i <= row_nr; i++)
{
for(int j = 0; j < row_nr; j++)
{
cout << i + j * (row_nr);
{
cout << " ";
}
}
cout << endl;
}
return 0;
}
You're outputting a space after every value, so there is going to be a space at the end of each line. You should add a check so that you don't output a space after the last value of each line. It seems like you might have intended to do this, but forgot to write the if statement.
#include <iostream>
//#include <iomanip> why?
using namespace std;
int main()
{
int row_nr;
cin >> row_nr;
if(row_nr > 1 && row_nr <= 30)
for(int i = 1; i <= row_nr; i++) //declare iterator variable in for loop statement
{
for(int j = 0; j < row_nr; j++)
{
cout << i + j * (row_nr);
if(j < row_nr - 1) //you forgot this line
{
cout << " ";
}
}
cout << '\n'; //endl flushes the buffer, unnecessary here
}
return 0;
}

Square Root Code C++ without sqrt()

I have to create a code where the user inputs a number which is a perfect square, and I have to show its root. I've made this code, but I'm getting Segmentation Fault 11 , in this piece: int j = squareRootVector[i];
squareRoot.push_back(j);.
I can't change the code too much, so is there a way that I can do that?
#include <iostream>
#include <vector>
using namespace std;
int main() {
cout <<
"Enter the number:\n";
int input;
int number = input;
int divider = 2;
vector<int> squareRootVector;
vector<int> squareRoot;
cin >> number;
for(int divider = 2; number > 1; divider++) {
while((number % divider) == 0) {
number /= divider;
cout << number << endl;
squareRootVector.push_back(divider);
}
}
for(int i = 0; i < squareRootVector.size(); i++) {
cout << squareRootVector[i] << " ";
/*******PROBLEM*******/
if(squareRootVector[i] == squareRootVector[i+1]) {
int j = squareRootVector[i];
squareRoot.push_back(j);
}
/*********************/
}
int root;
for (int i = 0; squareRoot.size(); i++) {
root = root * squareRoot[i];
}
cout << "Square Root of " << input << " is: " << root << endl;
return 0;
}
The behaviour on accessing squareRootVector[i+1] with i just one below size (which your loop constaint allows) is undefined.
Consider writing
for (std::size_t i = 1; i < squareRootVector.size(); i++) {
instead, and rebasing the for loop body accordingly. I've also slipped in a change of type for i.
Shortly, the problem is that the last cycle in the last "for":
for(int i = 0; i < squareRootVector.size(); i++)
has the following line in it:
squareRootVector[i] == squareRootVector[i+1];
This is an "out of limits" error: squareRootVector only has squareRootVector.size() elements (let's say n), and the elements are indexed from 0 to n-1.
squareRootVector[i+1] in the last cycle points one element after the last one of squareRootVector, which is undefined behavior.
Using vector::iterator is proper way.
for(vector<int>::iterator it = squareRootVector.begin(); it != squareRootVector.end(); ++it)
{
if( (it+1) == squareRootVector.end() )
{
//what to do if there's no next member???
break;
}
if( *it == *(it+1) )
{
squareRoot.push_back(*it);
}
}
Thanks for the answers, guys. I've ended up with this code:
#include <iostream>
#include <vector>
using namespace std;
int main() {
cout << "Enter the number:\n";
int input = 0;
int number = 0;
cin >> input;
number = input;
int divider = 2;
vector<int> squareRootVector;
vector<int> squareRoot;
for(int divider = 2; number > 1; divider++) {
while((number % divider) == 0) {
number /= divider;
squareRootVector.push_back(divider);
}
}
int vectorSize = squareRootVector.size() - 1;
for(int i = 0; i < vectorSize; i++) {
if(squareRootVector[i] == squareRootVector[i+1]) {
int j = squareRootVector[i];
squareRoot.push_back(j);
}
}
int root = 1;
for (int i = 0; i < squareRoot.size(); i++) {
root = root * squareRoot[i];
}
cout << "Square Root of " << input << " is " << root << endl;
return 0;
}

Error reading string with cin

I have a silly mistake , but managed not find it is. On line 17 I try to read two integers and a string, but when I input (or similar):
2 3 (
I keep being asked entries. When I input (or similar):
2 3 F
reads smoothly. Could it be " ( " a special character ?
#include <iostream>
using namespace std;
int ocurs(string cad, string subcad) {
int con = -1;
size_t i = 0;
while(i != string::npos) {
i = cad.find(subcad, i);
con++;
}
return con;
}
int main() {
int n, m, con = 0;
string cad, subcad;
cin >> n >> m >> subcad;
//cout << subcad;
for(int i = 0; i < n / 2; i++)
cad.push_back('(');
for(int i = 0; i < n / 2; i++)
cad.push_back(')');
//cout << cad;
con += ocurs(cad, subcad);
cad.clear();
for(int i = 0; i < n; i++)
if(i % 2 == 0) cad.push_back('(');
else cad.push_back(')');
con += ocurs(cad, subcad);
cout << con;
return 0;
}
You end up in an endless loop inside ocurs(), because when cad.find() finds the sought substring, you feed it with the same index it returns, and it keeps finding the same substring. You need to fix your ocurs() routine, for example by adding
if (i != string::npos) ++i;
after the find statement.
The broader answer is that you should learn to use a debugger – or, at least, use some more cout statements where they might be useful.