trying to solve project euler number 14 [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 7 years ago.
Improve this question
I´m trying to solve project euler problem number 14 and i have the code almost ready, but it keeps me giving the wrong answer.. Why it doesn't count more steps?? Thanks, sorry for the lack of commentary..
#include <iostream>
int collatz_length(int number);
int main() {
using namespace std;
int size_sequence, max_sequence = 0, number_ = 1000000, num;
while (number_>1) {
size_sequence = collatz_length(number_);
if (size_sequence > max_sequence) {
max_sequence = size_sequence;
num = number_;
cout << "size " << size_sequence
<< " starting number " << num << endl;
}
number_--;
}
cout << "The longest sequence has "
<< max_sequence << " steps, starting from the number: " << num << endl;
cin.get();
return 0;
}
int collatz_length(int number) {
using namespace std;
int size_sequence = 0;
while (number > 1) {
if ((number % 2) == 0){
number /= 2;
}
else {
number = (3 * number + 1);
}
size_sequence++;
}
return size_sequence;
}

It is possible that 3*n+1 will overflow an int. Perhaps you should use a uint64_t?

Related

I got a memory error and i dont know whats cauising it in c++ [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 1 year ago.
Improve this question
Hello this code is part of a big code and it causing a lot of problem i dont know why. i tried to use structer and vectors its ggot an error about vector out of range then i switched to class but still got same error im now compiling just problematic and its looks like a memory error. how can i fix this ?
#include <time.h>
#include <locale.h>
#include <vector>
#include <exception>
using namespace std;
class işlem {
string işlemdetay; int işlemtutar; time_t işlemtarih; int hesapno;
public:
vector <işlem> işlemler;
işlem() { işlemdetay = " "; işlemtutar = 0; işlemtarih = time_t(0); hesapno = 0; };
işlem(string İşlemdetay, int İşlemtutar, int Hesapno) {
işlemdetay = İşlemdetay; işlemtutar = İşlemtutar; hesapno = Hesapno;
işlem newişlem(işlemdetay, işlemtutar, hesapno);
işlemler.push_back(newişlem);
}
void listele(int hesapid) {
int size = işlemler.size();
for (; size >= 0; size--)
{
if (işlemler[size].hesapno == hesapno)
{
//cout << "İşlem Tarihi: " << işlemler[size].işlemtarih << endl;
cout << "İşlem Tutar: " << işlemler[size].işlemtutar << endl;
cout << "İşlem Detayı: " << işlemler[size].işlemdetay << endl;
}
}
}
};
int main()
{
try
{
işlem newişlem("Hesap oluşturuldu.", 400, 1);
}
catch (const std::exception e)
{
cout << e.what();
}
}```
işlemler[işlemler.size()] is out-of-range. The initial value of size should be işlemler.size() - 1, not işlemler.size().

C++ "Program.exe has stopped working" [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 6 years ago.
Improve this question
In no way I can solve why I'm getting the error in the screenshot when entering the first value and trying to proceed.
The error happens only with code below.
Any help would be appreciated!
Error screenshot
//Darbas40
#include <io.h>
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
int n,
u,
m,
ms,
v,
vs,
i;
double vidvaikinu,
vidmerginu;
n = 7; m = 0; v = 0; ms = 0; vs = 0;
for (i = 1; i <= n; i++)
{
wcout << "Iveskite mokiniu ugius: "; cin >> u;
if (u > 0)
{
m++;
ms = ms + u;
}
else if (u < 0)
{
v++;
vs = vs + u;
}
vidmerginu = ms / m;
vidvaikinu = fabs(vs / v);
}
wcout << " " << endl;
wcout << "vidvaikinu = " << vidvaikinu << ", vidmerginu = " << vidmerginu << "." << endl;
return 0;
}
You'll get a divide by zero error if u <= 0 in:
vidmerginu = ms / m;
because m is set to 0 and not incremented.
And a divide by zero error if u >= 0 in:
vidvaikinu = fabs(vs / v);
because v is set to 0 and not incremented.

Facing error in c++ code [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am writing code of ACM problem in which we have to check possibilities of different items. It's some minor error in the code.
#include<iostream>
using namespace std;
void CheckPossibilities( int numItems, int maxWeights )
{
if( numItems <= 0 )
{
cout << "Invalid Items";
}
if ( maxWeights <= 0 )
{
cout << "Impossible";
}
while( maxWeights > 0 )
{
if(numItems%2==0) //for even
{
numItems = numItems / 2;
maxWeights--;
}
else
{
numItems = (numItems -1)/ 2; //for odd
maxWeights--;
}
}
if( numItems <= 1 )
{
cout << "Possible";
}
else
{
cout << "Impossible";
}
}
void main()
{
int numItems1,maxWeights1;
cout<<"enter numItems"<<endl;
cin>>numItems1;
cout<<"maxWeights"<<endl;
cin>>maxWeights1;
cout<<numItems1 "AND" maxWeights1<<endl;
cout<<CheckPossibilities(numItems1, maxWeights1);
}
Your mistakes were trying to cout multiple strings in one line without concatting them in some way, either seperate with a << or a +. You also cant cout a void function because it tries to output void, you just need to call it and let the function do the outputting. With errors fixed the main should be
int main()
{
int numItems1,maxWeights1;
cout<<"enter numItems"<<endl;
cin>>numItems1;
cout<<"maxWeights"<<endl;
cin>>maxWeights1;
cout<<numItems1+"AND"+maxWeights1<<endl;
CheckPossibilities(numItems1, maxWeights1);
return 0;
}
Next time look at what line the error is thrown on when compiling and search those specific error because these were really simple and specific syntax errors that could be found by a google search easily.
void main()
{
int numItems1, maxWeights1;
cout << "enter numItems" << endl;
cin >> numItems1;
cout << "maxWeights" << endl;
cin >> maxWeights1;
cout << numItems1 << "AND" << maxWeights1 << endl;
CheckPossibilities(numItems1, maxWeights1);
}
you can never do this: cout<<CheckPossibilities(numItems1, maxWeights1); cout take standard output stream, not functions. And also you forgot to put << in cout << numItems1 << "AND" << maxWeights1 << endl; in this form, your code build succesful.

Run Time Check Failure #2 S C++ [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I'm having trouble on a C++ program. It keeps giving me a Run Time Error # 2 - S. It builds fine though. It builds fine. Main is supposed to call two other functions, one which puts ints into an array, the other which reverses the numbers' positions on the arrays, essentially creating a 'reverse' array.
#include "stdafx.h"
#include <iostream>
#include <array>
using namespace std;
void storeArray(int[], int);
void flipArray(int[], int);
int main()
{
const int arraysize = 10;
int foo[arraysize]; // everyone seems to use foo so I thought I'd try it
storeArray(&foo[arraysize], arraysize);
cout << endl;
flipArray(&foo[arraysize], arraysize);
return 0;
}
void storeArray(int foo[], int arraysize)
{
int counter;
counter = 0;
while (counter < arraysize)
{
cout << "Enter number " << (counter+1) << " : ";
cin >> foo[counter]; counter++;
}
return;
}
void flipArray(int foo[], int arraysize)
{
int counter2;
int counter3;
counter2 = 0;
counter3 = 9;
for (counter2; counter2 <= 9; counter2++)
{
cout << "Value number " << (counter2 + 1) << " is " << foo[counter3] << endl;
counter3--;
}
cout << endl;
return;
}
Any help would be appreciated. Thanks!
storeArray(&foo[arraysize], arraysize);
This passes the address just past the end of foo
Instead use:
storeArray(foo, arraysize);
And:
void storeArray(int* foo, int arraysize)
Likewise for:
flipArray(&foo[arraysize], arraysize);
EDIT: just noticed you're #include <array> and using C-style arrays. Not the C++ STL arrays defined in #include <array>

What is wrong in this solution to a codejam quiz? [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 7 years ago.
Improve this question
Link of the question https://code.google.com/codejam/contest/4224486/dashboard
My solution:
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
int type_A(vector<int>vec){ // function for method 1 of computation;
int ret = 0;
for(auto i = 0;i<vec.size()-1;i++){
int a = vec[i],b=vec[i+1];
if(a>b)ret = ret+(a-b);
}
return ret;
} // end of 1st function
int type_B(vector<int>vec){ // function for method 2 of computation
int ret = 0;
for(auto i = 0;i<vec.size()-1;i++){
if(i==vec.size()-2){
if(vec[i]>vec[i+1])ret+=(vec[i]-vec[i+1]);
}else{
ret += vec[i];
}
}
return ret;
}
// end of function
int main()
{
ifstream input("input_file.in");
ofstream output("output_file.out");
int t;
input>>t;
for(auto i =1;i<=t;i++){
int n;
input>>n;
vector<int>vec(n);
for(auto j = 0;j<vec.size();j++){
int x;
input >>x;
vec[j] =x;
}
output << "Case #" << i << ": " << type_A(vec) << " " << type_B(vec) << endl;
}
}
When I run some examples given with the problems , I get the correct output but when I upload my output file to codejam it says that the answer is incorrect . Please help .
Your algorithm is wrong for type 2. It just happens that all of the examples happen to have the greatest difference items being between the second last item and the last item, which your code relies on (the test for i==vec.size()-2). This is obviously not the case for all of the full tests.
You will need to think of a different algorithm.