I am having troubles by accessing 2 of my arrays at the same time with my friend function.
Here is my code (the important part):
int main(){
// Array with fracture, example 3/4
cFracture cFArr[8] = {cFracture(3,4), cFracture(24,6)};
// One fracture have to be added to other
// I am missing something here ->
add(cFArr[0]);
return 0;
}
I need to understand, how to parse 2 Arrays at the same time.
Here is my add function, here is nothing in, i need to understand how to get 2 arrays at the same time...
int add(cFracture add_f){
cout << "Result after adding fracture 1 to fracture 2: " << add_f.a << "/" << add_f.b << endl;
return 0;
}
And here is my class:
cFracture{
int a;
int b;
public:
cFracture(int a_in = 0, int b_in = 0){
a = a_in;
b = b_in;
}
friend int add(cFracture add_f);
};
How should I go on? How to get 2 arrays at the same time, so i can add one to the other? It have to be done with friend function.
Just add another argument to the function.
int add(cFracture add_f1, cFracture add_f2){
cout << "Result after adding fracture 1 to fracture 2: " << add_f1.a + add_f2.a << "/" << add_f1.b + add_f2.b << endl;
return 0;
}
and change the friend declaration to match the new function.
cFracture{
int a;
int b;
public:
cFracture(int a_in = 0, int b_in = 0){
a = a_in;
b = b_in;
}
friend int add(cFracture, cFracture);
};
Then you can do:
add(cFarr[0], cfArr[1]);
in the main() function.
Related
A program is divided into N functions.
Like the following code snippets: after calling each function, I wanna show the progress count/N
how to count N at compile time ?
#include <iostream>
using namespace std;
double progress()
{
int const total = 4; // how to get 4?
static int counter = 0;
return static_cast<double>(counter++) / static_cast<double>(total);
}
int main()
{
cout << progress() << endl; // 0.25
cout << progress() << endl; // 0.5
cout << progress() << endl; // 0.75
cout << progress() << endl; // 1.0
return 0;
}
I tried constexpr function, but cannot increment a variable.
Imagine the following code:
int main() {
cout << "N = ";
int N;
cin >> N;
for (int i = 0; i < N; ++i) cout << 'progress()' << endl;
}
There is absolutely no way, the compiler can know how many times the function will be executed. So you need to determine the number using the logic of your data.
If you want to know how many times you call progress without loops, recursions, conditions etc., the only way I can think of is using external tool on the source file. E.g.
cat source.cpp | grep -o progress() | wc -l
Just remember to subtract 1 from the result, which accounts for the function definition.
You can't do it, but you could fake it by making the printing happen after N (function call count) is known.
static struct counter_impl {
int n = 0;
constexpr void operator ()() noexcept { ++n; }
friend std::ostream& operator<<(std::ostream& os, counter_impl const& self) {
os << std::fixed;
std::generate_n(std::ostream_iterator<double>(os, "\n"), self.n,
[i = 1, N = static_cast<double>(self.n)] () mutable { return i++ / N; });
return os << std::defaultfloat;
}
} counter;
int main() {
counter();
std::cout << counter; // 1.00
}
Live example on compiler explorer
Unfortunately, you cannot.
This is not something that can be determined at compile time.
See https://en.wikipedia.org/wiki/Halting_problem
Can someone explain why this code gives the output 10? When I try to analyse it my logic gives as result 11.
#include <iostream>
using namespace std;
class A {
public:
A() { a.a = a.b = 1; }
struct { int a,b; } a;
int b(void);
};
int A::b(void) {
int x=a.a;
a.a=a.b;
a.b=x;
return x;
};
int main(void) {
A a;
a.a.a = 0;
a.b();
cout << a.b() << a.a.b << endl;
return 0;
}
In the cout line, a.b() could either be called before or after a.a.b is evaluated. Beginners sometimes assume left-to-right evaluation in this sort of code but actually that is not a rule of C++.
Those two different possibilities would explain your 10 and 11. To avoid ambiguity you could write:
cout << a.b();
cout << a.a.b << endl;
(assuming that order was your intent).
Note: C++17 may change this and define left-right evaluation order for this code.
Other than using a debugger, you can also use cout statements to help keep track of when things are called.
To kind of help myself out tracing your program I fixed a bit of the indentation and added comments as to when things are happening:
#include <iostream>
using namespace std;
class A {
public:
A() {
a.a = a.b = 1;
}
struct {
int a,b;
} a;
int b(void);
};
int A::b(void) {
cout << "Within A::b()" << endl;
// swap a.a, a.b
int x=a.a;
a.a=a.b;
a.b=x;
cout << "a.a.a = " << a.a << " a.a.b: " << a.b << endl;
return x;
};
int main(void) {
// sets a.a.a = 1, a.a.b = 1
A a;
// sets a.a.a = 0, a.a.b = 1
a.a.a = 0;
// a.a.a = 1, a.a.b = 0
a.b();
// check output here
cout << a.b() << a.a.b << endl;
return 0;
}
The above program results with the following output on http://cpp.sh/ :
Within A::b()
a.a.a = 1 a.a.b: 0
Within A::b()
a.a.a = 0 a.a.b: 1
10
In all, it depends on whether or not a.b() or a.a.b is resolved first when you call cout. In this case, operator precedence is undefined due to how cout works. This stackoverflow post has some good info on this.
I am making a Poker game in C++, and I am just trying to get started.
I need the ability to compare "Hands", to see which one is greater, equal, or lesser.
So, I have a Hand class now, and I made two other sub-classes that are called Straight and ThreeOfKind (I will add the rest later.
The Hand class has a method called compareTo(Hand* otherHand), it then checks the hand ranking to see which one is better. Also, with the Straights and Three of a Kinds, you can compare them together when they are of the same rank. Like Straights with Straights and Three of a Kinds with Three of a Kinds.
I wrote some initial code today, and my problem is, when I try to call "Hand's" compareTo(Hand* otherHand) method and pass in a Hand, Straight, or Three of a Kind, the compiler complains as it is trying to force me to use the Straight's compareTo(Straight* otherStraight) method. So, I should have overloading, but it's not working.
So in the Straight class after inheritance is done, we should have these two methods:
int Hand::compareTo(Hand* otherHand);
int Straight::compareTo(Straight* otherStraight);
// But, if you do this, it works:
Straight myStraight1 = new Straight(7);
Straight myStraight2 = new Straight(5);
myStraight1.compareTo(myStraight2);
// This is valid...
// If you do this, then the compiler complains!
Straight myStraight3 = new Straight(10);
ThreeOfKind myTrips4 = new ThreeOfKind(3);
myStraight3.compareTo(myTrips4);
// This above line complains that you cannot convert a ThreeOfKind to a Straight
// Even though I am trying to use Hand's compareTo(Hand* otherHand) method and
// cast a Three of a Kind to a Hand object,
// it fails with the overloading!
Here is all the source code...
//////////////////////////
// C++ main header file //
//////////////////////////
#pragma once
class Hand {
private:
int ranking;
public:
Hand(int aRanking);
Hand();
int getRanking();
int compareTo(Hand* otherHand);
};
class Straight : public Hand {
private:
int highCard;
public:
Straight(int aHighCard);
Straight();
int getHighCard();
int compareTo(Straight* otherStraight);
};
class ThreeOfKind : public Hand {
private:
int tripsValue;
public:
ThreeOfKind(int aTripsValue);
ThreeOfKind();
int getTripsValue();
int compareTo(ThreeOfKind* otherThreeOfKind);
};
///////////////////////////
// C++ main .cpp file... //
///////////////////////////
#include <iostream>
#include "PokerTest1.h"
using namespace std;
Hand::Hand(int aRanking) {
this->ranking = aRanking;
}
Hand::Hand() {
this->ranking = 0;
}
int Hand::getRanking() {
return this->ranking;
}
int Hand::compareTo(Hand* otherHand) {
cout << "COMPARING HANDS..." << endl;
if (this->getRanking() < otherHand->getRanking()) {
cout << "HANDS RETURNING -1..." << endl;
return -1;
}
else if (this->getRanking() > otherHand->getRanking()) {
cout << "HANDS RETURNING 1..." << endl;
return 1;
}
cout << "HAND RANKINGS ARE EQUAL..." << endl;
if (this->getRanking() == 4 && otherHand->getRanking() == 4) {
cout << "HANDS ARE BOTH STRAIGHTS..." << endl;
Straight* myStraight1 = (Straight*)this;
Straight* myStraight2 = (Straight*)otherHand;
cout << "COMPARING BOTH STRAIGHTS..." << endl;
return myStraight1->compareTo(myStraight2);
}
else if (this->getRanking() == 3 && otherHand->getRanking() == 3) {
cout << "HANDS ARE BOTH THREE OF A KINDS..." << endl;
ThreeOfKind* myTrips1 = (ThreeOfKind*)this;
ThreeOfKind* myTrips2 = (ThreeOfKind*)otherHand;
cout << "COMPARING BOTH TRIPS..." << endl;
return myTrips1->compareTo(myTrips2);
}
return 0;
}
Straight::Straight(int aHighCard) : Hand(4) {
this->highCard = aHighCard;
}
Straight::Straight() : Hand(4) {
this->highCard = 0;
}
int Straight::getHighCard() {
return this->highCard;
}
int Straight::compareTo(Straight* otherStraight) {
cout << "INSIDE STRAIGHT COMPARE TO..." << endl;
if (this->highCard < otherStraight->highCard) {
cout << "STRAIGHT COMPARE RETURNING -1..." << endl;
return -1;
}
else if (this->highCard > otherStraight->highCard) {
cout << "STRAIGHT COMPARE RETURNING 1..." << endl;
return 1;
}
cout << "STRAIGHT COMPARE RETURNING 0..." << endl;
return 0;
}
ThreeOfKind::ThreeOfKind(int aTripsValue) : Hand(3) {
this->tripsValue = aTripsValue;
}
ThreeOfKind::ThreeOfKind() : Hand(3) {
this->tripsValue = 0;
}
int ThreeOfKind::getTripsValue() {
return this->tripsValue;
}
int ThreeOfKind::compareTo(ThreeOfKind* otherThreeOfKind) {
cout << "INSIDE STRAIGHT COMPARE TO..." << endl;
if (this->tripsValue < otherThreeOfKind->tripsValue) {
cout << "TRIPS COMPARE RETURNING -1..." << endl;
return -1;
}
else if (this->tripsValue > otherThreeOfKind->tripsValue) {
cout << "TRIPS COMPARE RETURNING 1..." << endl;
return 1;
}
cout << "TRIPS COMPARE RETURNIN 0..." << endl;
return 0;
}
int main()
{
// Test the classes...
// with Straight compared to a Three of a Kind.
// Should try to invoke Hand::compareTo(Hand* otherHand) { ... };
// But, instead, it try to invoke Straight::compareTo(Straight* otherStraight) { ... };
// If you put both these methods in the Straight class (rather than using inheritence, it works)
// If you delete Straight::compareTo(Straight* otherStraight) { ... }, the line below compiles
// It is just strange why it won't compile...
Straight* myStraightA = new Straight(9); // Straight of 5, 6, 7, 8, 9
ThreeOfKind* myTripsB = new ThreeOfKind(2); // Three of a Kind of 2, 2, 2
cout << "Compare results..." << endl;
cout << myStraightA->compareTo(myTripsB) << endl; // Compiler error...
return 0;
}
Also, here is a list of the hand rankings:
0 → high card
1 → pair
2 → two pair
3 → three of a kind
4 → straight
5 → flush
6 → full house
7 → quads
8 → straight flush
9 → royal flush
Basically I have a field in the Hand class that stores these rankings as integers. Just so you know.
Lastly, this is the compiler error message:
error C2664: 'int Straight::compareTo(Straight )': cannot convert argument 1 from 'ThreeOfKind' to 'Straight*'
You are trying to overload across classes.
The compiler looks for a compareTo method, finds it in Straight, and doesn't look at Hand. If you add an appropriate using statement, you can tell it to look at Hand's compareTo as well to accomplish your overloading.
class Straight : public Hand {
private:
int highCard;
public:
using Hand::compareTo; // <<< HERE
Straight(int aHighCard);
Straight();
int getHighCard();
int compareTo(Straight* otherStraight);
};
Instead of doing this, I'd recommend you use getRanking() for comparison between hands of different hand types, and define getTieBreaker() overridden by subclasses to handle cases of the same type of hand.
class Hand {
public:
int getRanking();
// virtual causes subclass' version to be called if done from reference or pointer.
virtual int getTieBreaker();
};
This simplifies how Hand compares:
int Hand::compareTo(Hand* otherHand) {
if (this->getRanking() < otherHand->getRanking()) {
return -1;
}
else if (this->getRanking() > otherHand->getRanking()) {
return 1;
}
if (this->getTieBreaker() < otherHand->getTieBreaker()) {
return -1;
}
else if (this->getTieBreaker() > otherHand->getTieBreaker()) {
return 1;
}
return 0;
}
And lets you define it in Straight:
class Straight : public Hand {
//...
public:
int getHighCard();
int getTieBreaker();
};
int Straight::getTieBreaker() {
return this->highCard;
}
I've encountered a problem while trying to create a code which converts decimal numbers to binary, using functions. At first I created the code using only main function and it worked fine, but decided to modify it to use function. I believe code is written right, however when I try to cout my answer I get a big number like 115120160758866453687091316369641637416.
This is the code
#include <iostream>
#include <math.h>
using namespace std;
int* unsigned_dec(int dec_M) { //function for converting absolute part of numbers
int bin[8] = { 0,0,0,0,0,0,0,0 };
int ind = 7;
int arr_ind = 0;
for (int base = (int)abs(dec_M); base > 0; base = base / 2) {
if (base % 2 == 0) {
bin[arr_ind] = 0;
ind--;
}
else {
bin[arr_ind] = 1;
ind--;
}
arr_ind++;
}
return bin;
}
int main() {// main function
int dec_N;
cin >> dec_N;
int* bin_main = unsigned_dec(dec_N); //we are not sure if we are assigning the returned value of function to array in correct
for (int i = 0; i <= 7; i++) {
cout << bin_main[i];
}
cout << endl;
return 0;
}
then I tried to change the cout code to
cout << bin_main[0] << bin_main[1] << bin_main[2] << bin_main[3] << bin_main[4] << bin_main[5] << bin_main[6] << bin_main[7] << endl;
And this worked fine.
Then I wrote the same 2nd variant of cout in other way
cout << bin_main[0];
cout << bin_main[1];
cout << bin_main[2];
cout << bin_main[3];
cout << bin_main[4];
cout << bin_main[5];
cout << bin_main[6];
cout << bin_main[7];
cout << endl;
and my code started to cout the same strange number. I think that all 3 ways of couts are almost the same (especially 2 and 3), but don't understand what makes it not to work.
int bin[8] = { 0,0,0,0,0,0,0,0 };
is allocated on stack. You should either allocate bin on heap
auto bin = std::unique_ptr<int, std::default_deleter<int[]>>(new int[8]);
or even better, use std::vector
you are returning pointer to local array of intbin[] in unsigned_dec. This array on stack of function unsigned_dec will get invalidated once another function from main gets called i.e cout operator .
As others have already mentioned: A function should never return a pointer to a local variable. Local variable is not valid when the function returns.
A better way is to use a vector and just make the function return the vector.
Something like:
#include <iostream>
#include <math.h>
using namespace std;
//function for converting absolute part of numbers
vector<int> unsigned_dec(int dec_M) {
vector<int> bin; // Create a vector
bin.resize(8, 0); // Fill it with 8 elements initialized to zero
int arr_ind = 0;
// Note: added check for arr_ind being valid
for (int base = (int)abs(dec_M); base > 0 && arr_ind < 8; base = base / 2) {
if (base % 2 == 0) {
bin[arr_ind] = 0;
}
else {
bin[arr_ind] = 1;
}
arr_ind++;
}
return bin; // Return the vector
}
int main() {
int dec_N;
cin >> dec_N;
vector<int> bin_main = unsigned_dec(dec_N);
for (int i = 0; i < bin_main.size(); i++) {
cout << bin_main[i];
}
cout << endl;
return 0;
}
Below is a output question.I am not able to understand why its answer is 30.
#include<iostream>
using namespace std; //namespace std is being used
int &fun()
{
static int x = 10; //x is static
return x;
}
int main()
{
fun() = 30;
cout << fun(); //fun() called
return 0;
}
OUTPUT:30
Can anybody tell why output is coming to be 30 and also can explain the role of static keyword
In computer programming, a static variable is a variable that has been allocated statically—whose lifetime or "extent" extends across the entire run of the program
void foo()
{
int a = 10;
static int b = 10;
a++;
b++;
std::cout << "a : " << a << " , b : " << b << std::endl;
}
A reference variable is an alias, that is, another name for an already existing variable. Once a reference is initialized with a variable, either the variable name or the reference name may be used to refer to the variable.
int a = 4;
int b = a;
int &c = a;
c++;
std::cout << "b = " << b << std::endl; //4
std::cout << "a = " << a << std::endl; //5
std::cout << "c = " << c << std::endl; //5
/* Becaues c is a refence to a, it means that
a and c are just different names to the same memory location
so updating either one updates the actual value in memory
*/
a++;
std::cout << "c = " << c << std::endl; //6
std::cout << "a = " << a << std::endl; //6
//consider the function below:
int &bar()
{
static int a = 5;
std::cout << "a is " << a << std::endl;
return a;
}
Testing the two:
int main()
{
for (int i = 0; i < 3; i++)
foo();
//for every call of foo():
//memory allocation for a is created and deleted when a goes out of scope
//memoery allocation for b extends through out the life of the program
//bar() returns a reference to "a" i.e
int reference_to_a = bar(); //prints 5
reference_to_a = 30;
bar(); //prints 30
bar() = 50; //prints 30 and later assigns 50 to the reference returned.
bar(); //prints 50
}
static make the variable persist across function calls.
which means static int x = 10; will be executed once when func is called for the first time.
int static_test()
{
static int x = 10;
x++;
return x;
}
static_test(); // first call will return 11
static_test(); // second call will return 12 because the value of x ( was saved and was then incremented)
static_test(); // third call will return 13
Now, you need to understand what reference are. To understand what reference are you need to understand pointers. I am guessing you will easily find some website explaining those two.
case 1:
#include<iostream>
using namespace std; //namespace std is being used
int &fun()
{
int x = 10; //x is static
return x;
}
int main()
{
fun() = 30;
cout << fun(); //fun() called
return 0;
}
Here, in the call fun(), we are declaring a local variable int x, which goes out of scope once it returns from fun().
so, in the line cout << fun() a new variable is declared and address of the new variable is returned.
case 2:
static int x = 10;
here, since variable 'x' is static, it can be initialized only once. i.e., the first time x is initialized to 5 and then over written to 30.
now when you are making the function call subsequent times, static int x = 5 is ignored. Hence, it returns the value 30