6 Replies - 694 Views - Last Post: 05 December 2010 - 12:27 PM Rate Topic: -----

#1 sinister18  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 27-November 10

ternary operator in c

Posted 27 November 2010 - 06:13 PM

hello

I encouterd the follwoing code lines from a certain code:

printf("\nYour hat size is %c %c%c%c\n",
size[0][i], size[1][i], (size[1][i]==' ') ? ' ' : '/', size[2][i]);

why the author used 4 "%c" instead of 3?

if anyone is interested the full code is down here....


/* Program 5.6 Know your hat size - if you dare... */
#include <stdio.h>
#include <stdbool.h>
int main(void)
{
/* The size array stores hat sizes from 6 1/2 to 7 7/8 */
/* Each row defines one character of a size value so */
/* a size is selected by using the same index for each */
/* the three rows. e.g. Index 2 selects 6 3/4. */
char size[3][12] = { /* Hat sizes as characters */
{'6', '6', '6', '6', '7', '7', '7', '7', '7', '7', '7', '7'},
{'1', '5', '3', '7', ' ', '1', '1', '3', '1', '5', '3', '7'},
{'2', '8', '4', '8', ' ', '8', '4', '8', '2', '8', '4', '8'}
};
int headsize[12] = /* Values in 1/8 inches */
{164,166,169,172,175,178,181,184,188,191,194,197};
float cranium = 0.0; /* Head circumference in decimal inches */
int your_head = 0; /* Headsize in whole eighths */
int i = 0; /* Loop counter */
bool hat_found = false; /* Indicates when a hat is found to fit */
/* Get the circumference of the head */
printf("\nEnter the circumference of your head above your eyebrows "
"in inches as a decimal value: ");
scanf(" %f", &cranium);
/* Convert to whole eighths of an inch */
your_head = (int)(8.0*cranium);
/* Search for a hat size */
/* A fit is when your_head is greater that one headsize element */
/* and less than or equal to the next. The size the the second */
/* headsize value. */
for (i = 1 ; i < 12 ; i++)
/* Find head size in the headsize array */
if(your_head > headsize[i-1] && your_head <= headsize[i]) 
{
hat_found = true;
break;
}
if(your_head == headsize[0]) /* Check for min size fit */
{
i = 0;
hat_found = true;
}
if(hat_found)
printf("\nYour hat size is %c %c%c%c\n",
size[0][i], size[1][i], (size[1][i]==' ') ? ' ' : '/', size[2][i]);
/* If no hat was found, the head is too small, or too large */
else
{
if(your_head < headsize[0]) /* check for too small */
printf("\nYou are the proverbial pinhead. No hat for"
" you I'm afraid.\n");
else /* It must be too large */
printf("\nYou, in technical parlance, are a fathead."
" No hat for you, I'm afraid.\n");
}
return 0;
}


This post has been edited by macosxnerd101: 27 November 2010 - 06:20 PM
Reason for edit:: Please use code tags when posting code


Is This A Good Question/Topic? 0
  • +

Replies To: ternary operator in c

#2 Guest_c.user*


Reputation:

Re: ternary operator in c

Posted 27 November 2010 - 07:36 PM

printf(
    "\nYour hat size is %c %c%c%c\n",
    size[0][i],
    size[1][i],
    (size[1][i] == ' ' ? ' ' : '/'),
    size[2][i]
); 


This post has been edited by c.user: 27 November 2010 - 07:36 PM

Was This Post Helpful? 0

#3 sinister18  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 27-November 10

Re: ternary operator in c

Posted 05 December 2010 - 08:23 AM

View Postc.user, on 27 November 2010 - 06:36 PM, said:

printf(
    "\nYour hat size is %c %c%c%c\n",
    size[0][i],
    size[1][i],
    (size[1][i] == ' ' ? ' ' : '/'),
    size[2][i]
); 



please help me I am stuck with this...
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6035
  • View blog
  • Posts: 23,417
  • Joined: 23-August 08

Re: ternary operator in c

Posted 05 December 2010 - 08:25 AM

Stuck HOW?

This is what happens when all you're interested in doing is copying and pasting code :censored:
Was This Post Helpful? 0
  • +
  • -

#5 sinister18  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 27-November 10

Re: ternary operator in c

Posted 05 December 2010 - 08:33 AM

Dear jack, I learn it in order to know not to complete chore or something like that...
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6035
  • View blog
  • Posts: 23,417
  • Joined: 23-August 08

Re: ternary operator in c

Posted 05 December 2010 - 08:41 AM

Read up on the ternary operator

The logic is described by the comment in the code:
/* The size array stores hat sizes from 6 1/2 to 7 7/8 */
/* Each row defines one character of a size value so */
/* a size is selected by using the same index for each */
/* the three rows. e.g. Index 2 selects 6 3/4. */



If the character at the location size[i][1] is a ' ' then a ' ' will be printed, otherwise a / character will be printed.

Did you give any consideration to running the program and seeing what happened? I don't even know if the code is right.
Was This Post Helpful? 1
  • +
  • -

#7 sinister18  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 27-November 10

Re: ternary operator in c

Posted 05 December 2010 - 12:27 PM

thx jack...:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1