I'm using std::vector for storing my values into MyValues from my classes A and B. Inserted elements/values depends on the type of class. If it's A, inserts {x, y}, if B, inserts {x, y, z}. And I loop it, so i can try to add multiple values of it, to just test it. But when I loop A.ases 5 times and then B.bses 7 times, looped MyValues has 31 single elements for some reason. Is possible to add elements and "preserve forms" (If it's A -> {x, y}, if B, -> {x, y, z}), so I can get same values, like the ones I inserted and work with it?
classes:
std::vector<int> MyValues;
class A {
public:
short int x, y;
A() {
this->x = this->y = 0;
}
};
class B : public A{
public:
short int z;
B() {
this->z = 0;
}
};
main:
int main() {
A ases[5];
B bses[7];
for (short int a = 0; a < 5; a++) {
ases[a].x = 5 + a;
ases[a].y = 4 + a;
MyValues.insert(MyValues.end(), { ases[a].x, ases[a].y });
}
for (short int b = 0; b < 7; b++) {
bses[b].x = 2 + b;
bses[b].y = 3 + b;
bses[b].z = 1 + b;
MyValues.insert(MyValues.end(), { bses[b].x, bses[b].y, bses[b].z });
}
for (int v : MyValues) std::cout << "x y z = " << v << std::endl;
return 0;
}
output:
x y z = 5
x y z = 4
x y z = 6
x y z = 5
x y z = 7
x y z = 6
x y z = 8
x y z = 7
x y z = 9
x y z = 8
x y z = 2
x y z = 3
x y z = 1
x y z = 3
x y z = 4
x y z = 2
x y z = 4
x y z = 5
x y z = 3
x y z = 5
x y z = 6
x y z = 4
x y z = 6
x y z = 7
x y z = 5
x y z = 7
x y z = 8
x y z = 6
x y z = 8
x y z = 9
x y z = 7
If you are using visual studio 2019 then you should be able to turn on C++17. This demo uses std::optional. Not sure how efficient it is, but it's convenient.
#include <iostream>
#include <vector>
#include <optional>
struct Base
{
int x, y;
std::optional<int> z;
Base(int _x, int _y) :
x(_x),
y(_y)
{}
Base(int _x, int _y, int _z) :
x(_x),
y(_y),
z(_z)
{}
};
int main()
{
Base b1 {1, 2, 3};
Base b2 {4, 5};
Base b3 {9, 9, 9};
std::vector V {b1, b2, b3};
for (auto& item : V)
{
std::cout << item.x << " " << item.y << " ";
if (item.z.has_value())
std::cout << item.z.value();
std::cout << std::endl;
}
}
And for demo: https://godbolt.org/z/nbedjb
You could make your MyValues a vector of vectors, like this:
std::vector<std::vector<int>> MyValues;
Then, without changing the definitions of your A and B classes, you can insert 2- or 3-element vectors, using much the same code as you already have in your main function. You would only need to change the way you display the data; I have shown one way, in the code below, but there are many others, depending on how 'pretty' you want your output to be.
int main()
{
A ases[5];
B bses[7];
for (short int a = 0; a < 5; a++) {
ases[a].x = 5 + a;
ases[a].y = 4 + a;
MyValues.insert(MyValues.end(), { ases[a].x, ases[a].y });
}
for (short int b = 0; b < 7; b++) {
bses[b].x = 2 + b;
bses[b].y = 3 + b;
bses[b].z = 1 + b;
MyValues.insert(MyValues.end(), { bses[b].x, bses[b].y, bses[b].z });
}
// No change required up to here; just change the code for output...
for (auto v : MyValues) {
std::cout << "{ ";
bool first = true;
for (auto i : v) {
if (!first) std::cout << ", ";
std::cout << i;
first = false;
}
std::cout << " }" << std::endl;
}
return 0;
}
Output from above:
{ 5, 4 }
{ 6, 5 }
{ 7, 6 }
{ 8, 7 }
{ 9, 8 }
{ 2, 3, 1 }
{ 3, 4, 2 }
{ 4, 5, 3 }
{ 5, 6, 4 }
{ 6, 7, 5 }
{ 7, 8, 6 }
{ 8, 9, 7 }
Related
Given two int I want to get all the common digits and print out them separated by spaces.
So for example, if int x=1234; int y=41567; then I want to print out: 1 4.
This is my code. It does not work properly. When I run it, it prints 0 1 2 3 4 5 then stops.
I don't want to use vector nor arrays.
void problema3() {
int x, y, kX=0, kY=0;
cout << "x="; cin >> x;
cout << "y="; cin >> y;
int cx = x;
int cy = y;
for (int i = 0; i < 10; i++) {
kX = 0;
kY = 0;
x = cx;
y = cx;
while (x != 0 || kX==0) {
if (x % 10 == i) kX=1;
x /= 10;
}
while (y != 0 || kY == 0) {
if (y % 10 == i) kY=1;
y /= 10;
}
if (kX == 1 && kY == 1) cout << i << ' ';
}
}
int main()
{
problema3();
return 0;
}
If you're allowed to use std::set then you can do what you want as follows:
#include <iostream>
#include <set>
void print(int x, int y)
{
int individual_number1 = 0, individual_number2 = 0;
std::set<int> myset;
int savey = y;//this will be used to reset y when the 2nd do while loop finishes
do
{
individual_number1 = x % 10;
do
{
individual_number2 = y % 10;
if(individual_number1 == individual_number2)
{
myset.insert(individual_number1);
break;
}
y = y / 10;
}while( y > 0);
y = savey;
x = x / 10;
} while (x > 0);
//print out the element of the set
for(int i: myset)
{
std::cout<<i<<" ";
}
}
int main()
{
int x = 1234, y = 41567;
print(x, y);
return 0;
}
The output of the above program is as follows:
1 4
which can be seen here.
Your main bug is when assigning copies of cy.
//...
for (int i = 0; i < 10; i++) {
//...
x = cx;
y = cx; // <-- BUG! should read y = cy;
But that's not the only bug in your program.
Your digit detection logic is wrong. In particular, zero is not handled correctly, and since you did not put that reusable code in a function, your program is way more complex than it needs.
Here's the corrected logic for digit detection.
// checks if base 10 representation of a positive integer contains a certain digit (0-9)
bool hasDigit(int x, int d)
{
do
{
if (x % 10 == d)
return true;
x /= 10;
} while (x != 0);
return false;
}
Your main loop then becomes:
// assuming int x, y as inputs.
// ...
for (int i = 0; i < 10; ++i)
{
if (hasDigit(x, i) && hasDigit(y, i))
std::cout << i << ' ';
}
Which leaves very little room for bugs.
You can play with the code here: https://godbolt.org/z/5c5brEcEq
I'm testing some programms for my lectures. I'm creating classes and use a paramterlist to initalize a field but the second variable doesn't change.
#include <iostream>
using namespace std;
class Punkt {
int x;
int y;
public:
Punkt(int a = 0, int b = 0)
{
x = a;
y = b;
}
void printXY()
{
cout << "x= " << x << " y= " << y << endl;
}
};
int main() {
Punkt pFeld[] = { (1, 1), (2, 2), (3, 3) };
for (int i = 0; i < 3; i++)
pFeld[i].printXY();
cin.get();
};
No error messages. Expected result was that x and y change, while actual result is that only x changes and y stays 0.
This
(1, 1)
is an expression with the comma operator.
In fact this initialization
Punkt pFeld[] = { (1, 1), (2, 2), (3, 3) };
is equivalent to
Punkt pFeld[] = { 1, 2, 3 };
So the constructor with the second default argument equal to 0 is called three times.
Use instead
{ 1, 1 }
Here is your updated code
#include <iostream>
using namespace std;
class Punkt {
int x;
int y;
public:
Punkt(int a = 0, int b = 0)
{
x = a;
y = b;
}
void printXY()
{
cout << "x= " << x << " y= " << y << endl;
}
};
int main() {
Punkt pFeld[] = { {1, 1}, {2, 2}, {3, 3} };
for (int i = 0; i < 3; i++)
pFeld[i].printXY();
cin.get();
}
Its output is
x= 1 y= 1
x= 2 y= 2
x= 3 y= 3
Pay attention to that the semicolon after the function main is redundant.
Passing (1, 1) to the constructor of Punkt, the comma operator will return the 2nd operand as the result (the 1st operand is discarded), so you're only passing one int with value 1 to the constructor. That's why y is always initialized as 0.
What you want should be
Punkt pFeld[] = { {1, 1}, {2, 2}, {3, 3} }; // list initialization since C++11
or
Punkt pFeld[] = { Punkt(1, 1), Punkt(2, 2), Punkt(3, 3) };
so, I got a code and I need to express my answers into 3 columns with x,y,a in program, by compiling I only get the calculation answers - 1 column with y value throughout the cycle [5] . How do I make it to a table with x, y, a columns and their values? Do I need another for cycle? Any suggestions are appreciated.
#include < iostream>
using namespace std;
float fy(float x, float a);
int main()
{
float a[5] = { -8, -6, -4, -2, 0 }
float y = 0;
int i = 0;
for (float x = -1; x <= 1; x += 0.5)
{
cout << fy(x, a[i]) << endl;
i++;
}
cin.get();
return 0;
}
float fy(float x, float a)
{
float y = 0;
if (sin(x)*a > 0)
y = sqrt(sin(x)*a);
else
cout << "no solution\n";
return y;
}
I guess what you want is something like this:
x y a
-1 2.59457 -8
-0.5 1.69604 -6
0 -nan -4
0.5 -nan -2
1 -nan 0
Right?
Following code should do the job:
#include <iostream>
#include <math.h>
using namespace std;
float fy(float x, float a);
int main() {
float a[5] = {-8, -6, -4, -2, 0};
float y = 0;
int i = 0;
cout << "x"
<< "\t"
<< "y"
<< "\t"
<< "a" << endl;
for (float x = -1; x <= 1; x += 0.5)
{
cout << x << "\t" << fy(x, a[i]) << "\t" << a[i] << endl;
i++;
}
cin.get();
return 0;
}
float fy(float x, float a) {
float y = 0;
if (sin(x) * a > 0)
y = sqrt(sin(x) * a);
else
y = sqrt(-1);
return y;
}
The problem in your code is that you only print the result of the function. So you neither print x nor you print the a[i] value. Check the replaced cout line and you'll see how to print the other values.
int main() {
int x = 1, y = 2, z = 3, w = 4;
#define formula x + y * z % w
x++;
do_something1(formula);
y++;
do_something2(formula);
z++;
do_something3(formula);
w++;
do_something4(formula);
#undef formula
return 0;
}
I'm currently using #define to prevent repeating long rvalue. Is there any better alternative way to do this?
Use lambda expression:
int main() {
int x = 1, y = 2, z = 3, w = 4;
auto formula = [&] { return x + y * z % w; };
x++;
do_something1(formula());
y++;
do_something2(formula());
z++;
do_something3(formula());
w++;
do_something4(formula());
return 0;
}
How can I swap the values of two variables ?
This works, but I'd would prefer a one liner :
int a = 10, b = 30;
a = a + b;
b = a - b;
a = a - b;
Perhaps that's cheating but there's simply :
std::swap(a, b);
std::tie(x,y) = std::make_pair(y,x);
But std::swap(x,y) is much more readable and probably more efficient.
int main()
{
int x = 10, y = 10;
y = (x + y)-y;
}
Solved a little bit but not completely but solved if you use this code
int swap(int *x, int y){
*x = (*x + *y) - *x;
return 0;
}
int main()
{
int x = 10;
int y = 5;
y = (x + y) - y + swap(&x, &y);
cout << x << endl << y << endl;
}