The output is:
2 6 24
Here is the reason why.
i starts out with 1, and n with 2. We enter the loop, check whether i is less than 10, which it is, then multiply i(1) by n(2), which results in i becoming 2. We add 1 to n(2), resulting in n becoming 3. We now write i(2).
We go back to the test, which succeeds, and multiply i(2) by n(3), which results in i becoming 6. We add 1 to n(3), resulting in n becoming 4. We now write i(6).
We go back to the test, which succeeds, and multiply i(6) by n(4), which results in i becoming 24. We add 1 to n(4), resulting in n becoming 5. We now write i(24).
We go back to the test, which fails, terminating the loop.
The output is:
27 9 3 10 6 24
Here is the reason why.
The a in main starts out with 10, and b in mainwith 6. We then call weird passing it 10 and 6.
Within weird, its b is set to 10 and its c is set to 6. We then divide its c by 2, resulting in c being 3. We subtract 1 from b, resulting in it being 9. And we multiply b(9) and c(3) together and store the result (27) in a. We then write this a, b, and c. We return a - c, which is 24, and is stored in the c in the main program. We then write the main's a, b, and c.
The changes are:
The changes are:
One solution for draw_triangle is:
int draw_triangle(int max_size)
{
void draw_line(int n);
int i;
int sum;
sum = 0;
for (i = max_size; i >= 1; i = i - 1)
{
draw_line(i);
sum = sum + i;
}
return sum;
}
void draw_line(int n)
{
int i;
for (i = 1; i <= n; i = i + 1)
printf("$");
printf("\n");
}
One solution for this program (that takes into account that there might be no input at all) is:
main()
{
int value;
int largest;
n = scanf("%i", &largest);
if (n == 1) /* got at least one value */
{
while (scanf("%i", &value) == 1)
{
if (value > largest)
largest = value;
}
printf("The largest input value is %i\n", largest);
}
return 0;
}
[EE150 Home Page | EE150 Exam Information Page | Top Of Page]