Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help with a function. I\'m on the last one for the week and the answer is

ID: 3726789 • Letter: I

Question

I need help with a function. I'm on the last one for the week and the answer is eluding me and fellow students. I just need to know how to write the function. I've never dealth with structs inside structs before.

#include <iostream>

#include <cmath>

#include <string>

using namespace std;

struct Card {

char suit;

int rank;

};

struct Hand {

Card cards[5];

};

//Do not modify anything on or above the line below this

//YOUR_CODE_BELOW

void printHighCard(const Hand& h)

{

//IN HERE

}

//YOUR_CODE_ABOVE

//Do not modify anything on or below the line above this

int main()

{

Hand h1;

for(int c = 0; c < 5; c++) {

cin >> h1.cards[c].rank;

cin >> h1.cards[c].suit;

}

printHighCard(h1);

return 0;

}

Explanation / Answer

/*
struct elements can be accessed by using .(dot)

Struct Hand contains Struct Card type variable so you need to add 2 .(dot)'s to reach exact variable

like Hand.Card.suit or Hand.Card.suit

Since Hand is variable so in place of Hand you should you h.Card.suit.

so in your function you can access like
for(int c = 0; c < 5; c++) {
cout<< h.cards[c].rank<<" "<< h.cards[c].suit;

}
  
So now you can perform any operation
  
Since printHigh Card definition is not clear , So I assumed you want to print card with highest
rank and your below function is printing highest rank and it's suit.


To find highest rank I used compared each element rank and with previous stored rank in highestRank variable.

ex-
allranks=[1,2,3,4]

//Assign first rank to the maxrank variable

maxrank=allranks[0]=1

Now compare maxrank with every other value and keep updating
maxrank<allrank[1] => true so maxrank=allranks[1]=2

repeat it.


Sample Input:

1 C
3 B
2 D
5 C
4 B

Sample Output:

Card with highest rank along with it's suit
5 C


*/

//Your program start from below line

#include <iostream>

#include <cmath>

#include <string>

using namespace std;

struct Card {

char suit;

int rank;

};

struct Hand {

Card cards[5];

};

//Do not modify anything on or above the line below this

//YOUR_CODE_BELOW

void printHighCard(const Hand& h)

{

//IN HERE

//Below is the way to print all the hand cards , you can comment below 3 lines if not required
/*cout<<"Rank Suit";
for(int c = 0; c < 5; c++) {
cout<< h.cards[c].rank<<" "<< h.cards[c].suit;

}
*/
//Finding and displaying card with highest rank suit.

int highestRank=h.cards[0].rank;
char highestSuit=h.cards[0].suit;

//Comparing remaining 4 card's rand with above highestRank and found rand greater than it then we will update it.
for(int c=1;c<5;c++)
{
if(h.cards[c].rank>highestRank)
{
//updating rank
highestRank=h.cards[c].rank;

//updating suit
highestSuit=h.cards[c].suit;
}
}

//displaying
cout<<"Card with highest rank along with it's suit"<<endl;
cout<<highestRank<<" "<<highestSuit;

}

//YOUR_CODE_ABOVE

//Do not modify anything on or below the line above this

int main()

{

Hand h1;

for(int c = 0; c < 5; c++) {

cin >> h1.cards[c].rank;

cin >> h1.cards[c].suit;

}

printHighCard(h1);

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote