Explore BrainMass

Explore BrainMass

    Introduction to C - Conditionals and Basic Functions

    Not what you're looking for? Search our solutions OR ask your own Custom question.

    This content was COPIED from BrainMass.com - View the original, and get the already-completed solution here!

    1. Given the variables isFullTimeStudent and age , write an expression that evaluates to true if age is less than 19 or isFullTimeStudent is true.

    2.Write an expression that evaluates to true if and only if value of the integer variable isAMember is false.

    3. Write a conditional that assigns true ( 1 ) to the variable fever if the variable temperature is greater than 98.6 .

    4. Write the definition of a function printGrade , which has a char parameter and returns nothing. The function prints on a line by itself the message string Grade: followed by the char parameter (printed as a character) to standard output. Don't forget to put a new line character at the end of your line.

    Thus, if the value of the parameter is 'A', the function prints out Grade: A

    5. Write a statement that declares a prototype for a function add , which has two int parameters and returns an int .

    6. Write the definition of a function max that has three int parameters and returns the largest.

    © BrainMass Inc. brainmass.com September 27, 2022, 3:53 pm ad1c9bdddf
    https://brainmass.com/computer-science/c/introduction-conditionals-basic-functions-457272

    SOLUTION This solution is FREE courtesy of BrainMass!

    <Ol>
    <Li><B>Given the variables isFullTimeStudent and age , write an expression that evaluates to true if age is less than 19 or isFullTimeStudent is true.</B>

    Here, there are two conditions, and if either one is true, the expression should have value true. That is, the <I>||</I> (or) operator is to be used:

    <I>
    age < 19 || isFullTimeStudent == 1
    </I>

    <Li><B>Write an expression that evaluates to true if and only if value of the integer variable isAMember is false.</B>

    Here, if <I>isAMember</I> is false, the expression should be true, and if <I>isAMember</I> is false, the expression should be true. In other words, the expression's value should be the complement/negation of the truth value of <I>isAMember</I>, which means the <I>!</I> (not) operator should be used:

    <I>
    !isAMember
    </I>

    Another way of doing the same is:
    <I>
    isAMember == 0
    </I>
    (this directly checks whether isAMember is false)

    <Li><B>Write a conditional that assigns true ( 1 ) to the variable fever if the variable temperature is greater than 98.6 .</B>

    This can be done with an if-statement, as the value of <I>fever</I> needs to be changed if the condition is true, but otherwise nothing needs to be done:

    <I>
    if(temperature > 98.6)
    fever = 1;
    </I>

    <Li><B>Write the definition of a function printGrade , which has a char parameter and returns nothing. The function prints on a line by itself the message string Grade: followed by the char parameter (printed as a character) to standard output. Don't forget to put a new line character at the end of your line.

    Thus, if the value of the parameter is 'A', the function prints out Grade: A</B>

    The first statement gives all the required information about the start of the definition - it takes one parameter of type <I>char</I>, and returns nothing, that is, has return type <I>void</I>. To print the message string, use the <I>printf()</I> function. Since a <I>char</I> parameter is also to be printed, use the <I>%c</I> format specifier in <I>printf()</I>. Also, newline characters should be put at the start and end of the string, since the message should be in a line by itself:

    <I>
    void printGrade(char grade)
    {
    printf("nGrade: %cn", grade);
    }
    </I>

    <Li><B>Write a statement that declares a prototype for a function add , which has two int parameters and returns an int .</B>

    Again, the statement gives the information required for the declaration - the return type, as well as the types of the two arguments, is <I>int</I>:

    <I>
    int add(int, int);
    </I>
    Argument names need not be given in the function declaration (although you <I>can</I> give names if you wish).

    <Li><B>Write the definition of a function max that has three int parameters and returns the largest.</B>

    The function takes three <I>int</I> parameters, so the argument types are given. But the return type is not given. However, the function is supposed to return one of the three numbers (in fact, the largest of them), and since this will be an <I>int</I> in any case, the return type of the function must also be <I>int</I>. To find the largest, we use the following logic. The first number is (tentatively) assumed to be the largest, and its value is stored in a variable, say <I>large</I>. This value is then compared with the second number, and if it is larger than the second, nothing is changed, but if the second number is larger, the value of <I>large</I> is taken to be the value of the second number. Similarly, <I>large</I> is finally compared to the third number and the same thing is done. Then, <I>large</I> is returned:

    <I>
    int max(int a, int b, int c)
    {
    int large;

    large = a;
    if(b > large)
    large = b;
    if(c > large)
    large = c;

    return large;
    }
    </I>
    </Ol>

    I have answered with as much explanation as I felt you might need. If you would like more explanation on any point, though, please let me know and I will respond as soon as I can.

    This content was COPIED from BrainMass.com - View the original, and get the already-completed solution here!

    © BrainMass Inc. brainmass.com September 27, 2022, 3:53 pm ad1c9bdddf>
    https://brainmass.com/computer-science/c/introduction-conditionals-basic-functions-457272

    ADVERTISEMENT