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
Related
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 1 year ago.
Improve this question
#include <iostream>
#include <cmath>
//This program will calculate the area of a circle and the volume of the sphere using a given
radius value//
using namespace std;
void multivalues(int & , int & , int &, int&) ;
int main()
{
int value, squared, cubed, fourth;
value = 2;
multivalues(value,squared,cubed,fourth);
cout << "The value of the squared is " << squared << endl;
cout << "The value of the cubed is " << cubed << endl;
cout << "The value of the fourth is " << fourth << endl;
return 0;
}
void multivalues(int &val, int &sq, int &cb, int &fth);
{
sq = pow(val, 2);
cb = pow(val, 3);
fth = pow(val, 4);
}
This is for an assignment leading to understanding passing by reference. This program should calculate the area of a circle and the volume of the sphere using a given value. It doesn't compile however.
You have a ; where you don't want one:
void multivalues(int &val, int &sq, int &cb, int &fth);
{
sq = pow(val, 2);
cb = pow(val, 3);
fth = pow(val, 4);
}
Look at the end of the first line I included.
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().
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?
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.
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 8 years ago.
Improve this question
I try to remove elements from a vector and it works fine with the erase() methode, but after removing the element the size of the vector still the same.
std::vector<int> myvector;
myvector.push_back (1);
myvector.push_back (2);
myvector.push_back (3);//here the size is 3
myvector.erase(myvector.begin()+1);//I think normally the size should be 2 after removing the element
is there a function that can do that or should I do it manually, I'm new to c++ I checked the documentation and I didn't found a solution for this.
The size is changed then an element of a vector is removed with using member function erase. If you mean capacity then it will not be changed.
Here is a demonstrative program
#include <iostream>
#include <vector>
int main()
{
std::vector<int> v { 1, 2, 3 };
std::cout << "v.size() = " << v.size() << std::endl;
for ( int x : v ) std::cout << x << ' ';
std::cout << std::endl;
v.erase( v.begin() + 1 );
std::cout << "v.size() = " << v.size() << std::endl;
for ( int x : v ) std::cout << x << ' ';
std::cout << std::endl;
return 0;
}
The output is
v.size() = 3
1 2 3
v.size() = 2
1 3