The output is:
c=4, s=6
Here is the reason why.
The for loop differs from most we have seen in that the test involves a different variable (s) than the one we are initializing and incrementing (c).
The table below shows what happens each iteration of the loop (after doing the c++).
s c
Before loop: 0 1
After 1st time: 1 2
After 2nd time: 3 3
After 3rd time: 6 4
The output is:
? * ** *** ?
Here's the reason why.
The loop runs i from 0 to 4, stopping when i is 5. When i is 0, we are going to the default case of the switch, writing a ?. When it is 1, 2 or 3, we do the appropriate case, writing out that many stars. When it is 4, we hit the default case again, writing the ?.
The output is:
0.00 3
Here is the reason why.
The expression x/y is 10/20, where 10 and 20 are integers, so it results in a 0, which is converted to a double and assigned to d.
The expression d/y is 7.5/2, which will use real division and give us 3.25. However, this 3.25 is assigned to an integer x, which causes the .25 to be truncated, so x is assigned a 3.
The output is:
4 17 9 13 17
Here is the reason why.
Before tweak is called the entire array t contains 17's. Inside tweak, changing s is equivalent to changing t, so we are modifying t[1] and t[2] to 9 and 13. But when we change t in tweak, that's changing the local variable and not the s that is passed to it.
The changes are (any 4 out of these 6):
The changes are:
The changes are (any 4 out of these 5):
The key here is to write a loop that runs through the table and for each element, divides the world into cases: that of a positive table element and that of a negative table element. For a positive element, you need to write a set of dots, the !, the number of +'s in the table element's value, and then dots to the end. This winds up being a set of calls to writeN. The negative case is similar.
void displayTable(int a[], int n, int dots, char pos, char neg)
{
void writeN(int c, int n);
int i;
for (i = 0; i < n; i++)
{
if (a[i] > 0)
{
writeN('.', dots);
writeN('!', 1);
writeN('+', a[i]);
writeN('.', dots - a[i]);
}
else
{
writeN('.', dots + a[i]);
writeN('-', -a[i]);
writeN('!', 1);
writeN('.', dots);
}
putchar('\n');
}
}
The key to this solution is to start with a loop that reads each character in the input, one character at a time. You then can construct a very simple state machine where you are either in a quote or not in a quote and reading a quote causes a transition between the states.
#include <stdio.h>
#define INQUOTE 0
#define NOT_INQUOTE 1
main()
{
int c;
int state = NOT_INQUOTE;
while ((c = getchar()) != EOF)
if (state == NOT_INQUOTE)
{
if (c == '"')
state = INQUOTE;
}
else /* INQUOTE */
{
if (c != '"')
putchar(c);
else
{
putchar('\n');
state = NOT_INQUOTE;
}
}
return 0;
}
If you don't like state machines, you can instead check each character
you read to see if it's a quote and then have a loop that keeps reading
and printing characters until it hits another quote.
#includemain() { int c; while ((c = getchar()) != EOF) { if (c == '"') { while ((c = getchar()) != '"' && c != EOF) putchar(c); putchar('\n'); } } return 0; }
[EE150 Home Page | E150 Exam Information Page | Top Of Page]