Share
Explore BrainMass

Intro to C Programming

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

2.Write a conditional that assigns 10,000 to the variable bonus if the value of the variable goodsSold is greater than 500,000 .

3 Write an if/else statement that compares the double variable pH with 7.0 and makes the following assignments to the int variables neutral , base , and acid :
0, 0, 1 if pH is less than 7
0, 1, 0 if pH is greater than 7
1, 0, 0 if pH is equal to 7.

4Write an if/else statement that adds 1 to the variable minors if the variable age is less than 18 , adds 1 to the variable adults if age is 18 through 64 , and adds 1 to the variable seniors if age is 65 or older.

5. Given the integer variables x and y , write a fragment of code that assigns the larger of x and y to another integer variable max .

Assume that grade is a variable whose value is a letter grade-- any one of the following letters: 'A', 'B', 'C', 'D', 'E', 'F', 'W', 'I'. Assume further that there are the following int variables, declared and already initialized: acount, bcount, ccount, dcount, ecount, fcount, wcount, icount.

Write a switch statement that increments the appropriate variable (acount, bcount, ccount, etc.) depending on the value of grade. So if grade is 'A' then acount is incremented; if grade is'B' then bcount is incremented, and so on.

6. Write a switch statement that tests the value of the char variable response and performs the following actions:
if response is y , the message Your request is being processed is printed
if response is n , the message Thank you anyway for your consideration is printed
if response is h , the message Sorry, no help is currently available is printed
for any other value of response , the message Invalid entry; please try again is printed

7. Write an if/else statement that compares the value of the variables soldYesterday and soldToday , and based upon that comparison, assigns salesTrend the value -1 or 1 .

-1 represents the case where soldYesterday is greater than soldToday ; 1 represents the case where soldYesterday is not greater than soldToday .

Solution Preview

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

The value of <I>fever</I> is to be changed (to 1) if <I>temperature</I> is greater than 98.6, but otherwise, there should be no change. So a simple if statement can be used:

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

<Li><B>Write a conditional that assigns 10,000 to the variable bonus if the value of the variable goodsSold is greater than 500,000 .</B>

This is the previous problem itself, with the names changed. Replace true with 10,000, <I>fever</I> with <I>bonus</I>, <I>temperature</I> with <I>goodsSold</I>, and 98.6 with 500,000, and you get the answer:

<I>
if(goodsSold > 500000)
bonus = 10000;
</I>

(Of course, you cannot use commas in the numbers).

<Li><B>Write an if/else statement that compares the double variable pH with 7.0 and makes the following assignments to the int variables neutral , base , and acid :
0, 0, 1 if pH is less than 7
0, 1, 0 if pH is greater than 7
1, 0, 0 if pH is equal to 7.</B>

Here, we need the so called if-else ladder, since there are three cases. The code is very similar to the structure of the problem statement:

<I>
if(pH < 7.0)
{
neutral = 0;
base = 0;
acid = 1;
}
else if(pH > 7.0)
{
neutral = 0;
base = 1;
acid = 0;
}
else
{
neutral = 1;
base = 0;
acid = 0;
}
</I>

Since there are three statements to be executed in each case, we need to enclose these in curly brackets { } (the brackets are optional only ...

Solution Summary

The solution discusses the intro to c-programming.

$2.19