translate the following "pseudocode" into a c program.
here's the code:
#include<stdio.h>
void main()
{
int num;
/* read an integer from the keyboard */
scanf("%d", &num);
/* if the integer is a multiple of 7 */
if(num % 7 == 0)
{
/* display "buzz" */
printf("buzz\n");
}
/* if the last digit of the integer is 7 */
if(num % 10 == 7)
{
/* display "bang" */
printf("bang\n");
}
/* display "goodbye" */
printf("goodbye\n");
}
the following program is supposed to calculate the area of a circle, but it has all kinds of errors. fix them.
#include stdio.h
#define PI = 3.1415
void main()
{
int radius;
double area;
scanf("%d", radius);
PI * square(radius);
printf("area is %d\n", area);
}
int square(int x)
{
return(x*2);
}
here's the fixed up version:
/* need <> in #include */
#include<stdio.h>
/* no '=' needed for #define */
#define PI 3.1415
/* need a prototype for square() */
int square(int x);
void main()
{
int radius;
double area;
/* need an & in scanf */
scanf("%d", &radius);
/* need to assign PI * square(radius) to area */
area = PI * square(radius);
/* need to use %lf instead of %d */
printf("area is %lf\n", area);
}
int square(int x)
{
/* x*2 does not square x, but x*x does */
return(x*x);
}
trace the following program.
bonus question: this program actually computes something meaningful. what is it doing?
#include<stdio.h>
void main()
{
int f=4;
int i;
int b=0;
int o=1;
int n;
for(i=2; i < f; i++)
{
n=o+b;
b=o;
o=n;
}
printf("%d\n", n);
}
line 1 is int f=4;
line 2 is int i;
line 3 is int b=0;
line 4 is int o=1;
line 5 is int n;
line 6 is for(i=2; i < f; i++)
line 7 is n=o+b;
line 8 is b=o;
line 9 is o=n;
line 10 is printf("%d\n", n);
here's the trace:
line # | f | i | b | o | n
-------+-----+-----+-----+-----+-----
1 | 4 | ? | ? | ? | ?
2 | 4 | ? | ? | ? | ?
3 | 4 | ? | 0 | ? | ?
4 | 4 | ? | 0 | 1 | ?
5 | 4 | ? | 0 | 1 | ?
6 | 4 | 2 | 0 | 1 | ?
7 | 4 | 2 | 0 | 1 | 1
8 | 4 | 2 | 1 | 1 | 1
9 | 4 | 2 | 1 | 1 | 1
6 | 4 | 3 | 1 | 1 | 1
7 | 4 | 3 | 1 | 1 | 2
8 | 4 | 3 | 1 | 1 | 2
9 | 4 | 3 | 1 | 2 | 2
6 | 4 | 4 | 1 | 2 | 2
10 | output "2"
this program computes the "f"th fibonacci number. n keeps track of the current number in the sequence, o keeps track of the last number in the sequence, and b keeps track of the second to last number in the sequence.
what will the following program output?
hint: look at the inner for loop first, and figure out
what it does [how many times does it loop? what does it do each
time?]. then look at the outer for loop, and you should
be able to figure out what the program does.
#include<stdio.h>
void main()
{
int i;
int j;
for(i=1; i < 16; i=i+i)
{
for(j=0; j < i; j++)
{
printf("hi");
}
printf("\n");
}
}
the key to figuring this out, as the hint suggests, is to figure out what the inner for loop does first. the inner for loop says:
for(j=0; j < i; j++)
{
printf("hi");
}
from reading this, we know that this loop will print hi
i times
[for(j=0; j < i; j++) is a very
common pattern for for loops... it should be familar to
you]. so, if i is 5, this will print hi 5
times. if i is 10, then the loop will print
hi 10 times.
after the inner for loop, we have printf("\n");. this
just prints a newline.
okay, now we know what all the stuff inside the outer for loop
does. for some value of i, it will print hi
i times, then print a newline.
now let's figure out how many times the outer for loop runs, and what
the values of i will be each time. the loop says
for(i=1; i < 16; i=i+i).
we know that the first value of i will be 1, and we know
that we get the next value of i by running the assignment
statement i=i+i, and we know that the loop stops when
i is 16 or more. with this knowledge, we can build a
table of the values of i:
values of i
-----------
1
2
4
8
16
so the loop runs 4 times, with i values 1,2,4,8. when
i hits 16, the loop stops.
putting everything together, we know that the stuff inside the outer
loop prints hi i times, then prints a newline, and we
know that the outer loop repeats 4 times, with i values
1,2,4,8. so, the output will look like:
hi hihi hihihihi hihihihihihihihi
what will the following program output?
#include<stdio.h>
int f(int x);
int g(int y);
void main()
{
int n=3;
printf("main %d\n", f(g(n)));
}
int f(int x)
{
printf("f %d\n", x);
return(x+1);
}
int g(int y)
{
printf("g %d\n", y);
return(y*2);
}
we know that the value of n is 3, because of the line
that says int n=3;. so, all we need to do is figure out
the value of f(g(3)).
this is what makes this question tricky. in c, f(g(3))
works just like it does in math: to figure out what its value is, you
need to do work from inside to outside. so, the first thing we need to
do is figure out the value of g(3).
when we run g(3), it does a printf, which outputs "g
3", then it returns 3*2, which is 6.
we now know that g(3) worked out to 6. so, to figure out
what f(g(3)) is, we can just figure out what
f(6) is.
when we run f(6), it does a printf, which outputs "f
6", then it returns 6+1, which is 7.
we now know that f(g(3)) worked out to 7 [and it did a
couple printfs along the way]. we did all the function
calls we need to, so the printf in main
outputs "main 7".
to summarize, the output is:
g 3 f 6 main 7
what will the following program output?
#include<stdio.h>
void main()
{
int x;
int y;
int z;
int* p1;
int* p2;
x=2;
p1 = &y;
*p1 = 3;
p2 = p1;
*p2 = 4;
z = 5;
p2 = &z;
*p2 = *p1+x;
printf("%d %d %d %d %d\n", x, y, z, *p1, *p2);
}
to explain this problem, i will show you another way of thinking about pointers.
when you declare a variable, you can think of it as a box with a
name and a type. so, when we say int x; this creates a
box named x that holds integers. when we say int* p1;,
this creates a box named p1 that holds a pointer to an integer.
so, after the computer processes the first five lines of the program, we can draw a picture like this:
we have boxes for x, y, and z, and these boxes hold integers. we don't know what the values of these integers are yet. the boxes for p1 and p2 hold pointers to integers, and we represent pointers as arrows. we don't know what p1 and p2 are pointing to yet, so the arrows are pointing off to nowhere for now.
okay, let's see how the picture changes as the computer runs our
program. when the computer processes the command x=2,
the picture now looks like this:
pretty straightforward, i hope. since we said x=2, we
get a 2 in the box named x. next, the computer does
p1 = &y. the &y means "get the address of
y", and the assignment statement puts the address of y into p1. so,
this command makes p1 point to y. so the picture looks like this:
note that the arrow points to the box, not to the value in the
box. don't forget that a pointer points to a variable, not a
value. next, the computer does *p1 = 3. the
*p1 says to follow the arrow in p1, and the assignment
statement says to put a 3 in there. so, following the arrow in p1, we
see that the arrow points to the box for y, so we put a 3 in the box
for y. the picture looks like this:
next, we have to do p2 = p1. this means that p2 must
point to the same thing that p1 points to. so, the picture looks like
this:
next, we have *p2 = 4. this means we follow the arrow in
p2, and put a 4 in whatever the arrow points to.
don't forget that a variable can only have one value at a time. this means that the 4 replaces the 3 that was in the y box before.
next, we have z = 5. this is pretty easy:
next, there's p2 = &z. this makes p2 point at the z box:
after that, we have *p2 = *p1+x. to figure out what this
does, we need to first figure out what *p1+x
is. *p1 says to follow the arrow in p1, and grab what's
in the box. x says to grab what's in the x box. so, we
grab what's in the box pointed to by p1 [which is the number 4], and
we grab what's in the x box [which is the number 2], and we add them,
and we get 6. the assignment statement says that we need to put this 6
into the box pointed to by p2 [that's what *p2
means]. so:
finally, we have a printf statement that prints out the values of x, y, z, *p1, and *p2. the values of x, y, and z are 2, 4, and 6 respectively, and we can determine the values of *p1 and *p2 by following the arrows, so *p1 and *p2 are 4 and 6 respectively. to summarize, the output is:
2 4 6 4 6
what will the following program output?
#include<stdio.h>
void increment(int x, int* y);
void main()
{
int x=1;
int y=2;
int* p=&y;
increment(x, p);
printf("%d %d %d\n", x, y, *p);
}
void increment(int x, int* y)
{
x++;
(*y)++;
printf("%d %d\n", x, *y);
}
i'll explain this one by drawing pictures as well. remember all that stuff about local scope... each function has its own set of variables, and they can't directly access variables in other functions.
after we process the first three lines of main, the picture looks like this:
we draw a box around the whole thing and label it "main" to indicate that we can only refer to these variables from within main.
after we set things up, main calls increment(x,p). this
means we need to run the increment function, and pass it values x, and
p. remember that c passes everything by value, which means that the
things that are passed get copied into new variables.
in the declaration of increment, we indicate that it takes two
arguments, an integer which will be called x, and a pointer to an
integer, which will be called y. when we call
increment(x,p), we are saying that the actual values for
the arguments are x and p. so, increment's x has the same value as
main's x, and increment's y has the same value as main's p. so, the
picture looks like this:
next, we need to run the code in the body of increment. first, we do
x++, which increases the value in x by 1.
since we are running code inside increment, we changed increment's x,
not main's x! next, we need to do (*y)++. this
says to follow the arrow in the y box, and increase the value in the
target box by 1. so:
next, we do a printf that displays the values of x and *y, which are 2 and 3, respectively. again, we are running code inside of the increment function, so we use increment's variables, not main's.
now there is no more code to run in increment, so we return to main, and we display the values of x, y, and *p. looking at our picture, we see that the values are 1, 3, and 3, respectively.
so, to summarize, the output is:
2 3 1 3 3
the factors of a number are all the numbers that evenly divide it. for example, the factors of 6 are 1, 2, 3, and 6, because 6 can be evenly divided by all those numbers.
write a program that reads a number from the user, and prints out all
the factors of that number. so, if i input 24, the
computer should display 1 2 3 4 6 8 12 24.
the best way to tackle non-trivial programming problems is to first think about how you would tell a child how to solve the problem. i would say something like this:
suppose the number you want to factor is called "x". take all the integers between 1 and x, and print out all the numbers that evenly divide x.
this is a good start. let's try to think a little more in terms of what we want the computer to do:
hopefully at this point it's pretty clear how to turn this into a c program:
#include <stdio.h>
void main()
{
int x;
int i;
scanf("%d", &x);
for(i=1; i <= x; i++)
{
if(x%i == 0)
printf("%d ", i);
}
printf("\n");
}
hopefully from this example you see that it's pretty easy to figure out how to solve the problem in english, then convert the english solution to c. i strongly suggest you approach programming problems this way.