how do I program 2 things in 1 function file without loop

This function will display a blackjack hand to the command window. Its input argument, hand, will be a row vector containing the hand as described: A blackjack hand is a row vector containing numbers between 1 and 13 representing the cards. The correspondence between numbers and cards is1 = ace, 2 = two, ... 11 = jack, 12 = queen, 13 = king. It has no output argument. The contents of the display_hand()function will be as follows:
a.Define a row vector with characters representing the card values, like so:cards = [‘A’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’, ‘T’, ‘J’, ‘Q’, ‘K’]; The character corresponding to a card value is accessed by indexing. For example if the card is a ten, you can display the corresponding character using the function fprintf(‘%c’,cards(10))
b.The entire hand can be displayed with a single fprintf() call as follows:fprintf(‘%c ’,cards(hand))
c.Once the hand has been displayed, print a new line using fprintf(‘\n’)
How can I make all these 3 conditions in 1 function external file ?

1 comentario

You're given nearly every single character that needs to be typed in the function in the question, so it's really not clear what problem you're facing.
I suggest you make a start at writing the code and if you do face a problem then come back with what you have written and ask a specific question.

Iniciar sesión para comentar.

Respuestas (1)

You need to define a function in MATLAB with the name 'display_hand'. You can refer here. Your function takes an input a row vector(say v). You have 3 conditions to work on:- a. Defining a row vector ('cards') -
cards=[A, 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K];
b. Displaying the blackjack hand -
fprintf('%c',cards(v));
c. Just print a newline character.
I guess this problem is pretty simple and you should be able to do this easily.

3 comentarios

Stephen23
Stephen23 el 26 de Sept. de 2018
Editada: Stephen23 el 26 de Sept. de 2018
Because [] is a concatenation operator note that this
cards=[A, 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K];
is equivalent to this:
cards = 'A23456789TJQK';
except that it is more complex, and it uses characters that are not valid in MATLAB for defining strings or char arrays and so throws an error:
You seem to have copied the expression:-
cards=[A, 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K];
from some source into the MATLAB editor. Try deleting the single quotes manually and again insert single quotes. While you copied and pasted the expression, single quotes got copied in a format which is not supported in MATLAB. Just insert your own single quotes around each of the 13 characters from your keyboard and it will work fine!
Stephen23
Stephen23 el 28 de Sept. de 2018
Editada: Stephen23 el 28 de Sept. de 2018
"You seem to have copied the expression...from some source into the MATLAB editor."
Yes, I did indeed. And that "some" source was your answer:
"While you copied and pasted the expression, single quotes got copied in a format which is not supported in MATLAB"
Not at all: your answer does not use single quotes. I just checked the HTML using an online character code lookup tool, and these are the characters that you actually used in your answer:
  • LEFT SINGLE QUOTATION MARK U+2018
  • RIGHT SINGLE QUOTATION MARK U+2019
They certainly don't look like single quotes to me. Rather intriguingly you did use single quotes around the terms 'display_hand' and 'cards', all correctly encoded as:
  • APOSTROPHE U+0027

Iniciar sesión para comentar.

Categorías

Más información sobre Signal Integrity Kits for Industry Standards en Centro de ayuda y File Exchange.

Productos

Preguntada:

el 19 de Sept. de 2018

Editada:

el 28 de Sept. de 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by