What is wrong in this solution to a codejam quiz? [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
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.

Related

Apply function that uses matrix elements to update attribute [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 3 days ago.
Improve this question
I have an Eigen::MatrixXd and I would like to take all the elements in the matrix to update an attribute // within the class. However when I run the following I get errors.
#include <cmath>
#include <iostream>
#include <Eigen/Core>
double x = 0;
void Increment(double x, double increment)
{
value = value + x + increment;
}
int main()
{
Eigen::MatrixXd m(2, 2);
m << 1, 1, 1, 1;
std::cout << m << std::endl << "becomes: ";
std::cout << std::endl << m.unaryExpr(&Exp(1)) << std::endl;
std::cout << value << std::endl << " is the new value";
}
Expecting to get a value of "8" for last cout statement

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().

What exactly does this compiler error want me to perform? [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 3 years ago.
Improve this question
Thank you in advance for reading ! So this is the code :
#include<iostream>
#include<vector>
std::vector<int> MonkeyCount(int n);
int main() {
MonkeyCount(4);
return 0;
}
std::vector<int> MonkeyCount(int n) {
std::vector<int> MonkeyCountV;
for (unsigned int i = 1; i <= n; i++) {
MonkeyCountV.push_back(i);
}
for (unsigned int i = 0; i <= MonkeyCountV.size(); i++) {
std::cout << MonkeyCount.at(i) << " ";
}
return MonkeyCountV;
}
and the error is on line 23 : error C2227: left of '->at' must point to class/struct/union/generic type
Now i red something about this, but i use this from an example i found on the internet on how to print a vector, and in that exaple, in works. The exaple is this :
#include <iostream>
#include <vector>
void print(std::vector<int> const& input);
int main()
{
std::vector<int> input = { 1, 2, 3, 4, 5 };
print(input);
return 0;
}
void print(std::vector<int> const& input)
{
for (unsigned int i = 0; i < input.size(); i++) {
std::cout << input.at(i) << ' ';
}
}
std::cout << MonkeyCount.at(i) << " ";
Should be:
std::cout << MonkeyCountV.at(i) << " ";
The way you have it is trying to do ".at(i)" on the function itself.
The name of your vector is MonkeyCountV but in the cout statement you are using MonkeyCount which is actually the name of your function. Make sure you have your variables name typed correctly.

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>

trying to solve project euler number 14 [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
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?