hello, i am jeremy.
the framework for all programs that will be written in this class is:
#include <stdio.h>
/* constants are defined here */
/* nice big comment describing what your program does goes here */
void main()
{
/* your code goes here */
}
if you're curious, stdio is short for standard input and output.
a constant is something with an unchanging value [for the lifetime of the program, at least], for example, pi. to define pi as a constant in our program, we say:
#define PI 3.1415
we put this right after the #include line. note that
there is no semicolon at the end of this line! after we define PI, we
can use it in our main(), like this:
circumference = 2 * PI * radius;
an integer is a whole number (including negative numbers), such as 1,
500, 0, or -216. integer variables are declared with the
int keyword, like this:
int diameter; int num_students = 150; int two = 2;
note that we can specify an initial value for any variable we
declare. if we don't specify an initial value, the computer will pick
a random initial value for us. so the value of
num_students is 150, but the value of
diameter might be 2, 65535, -7, or any other integer value.
in standard c, you have to declare all your variables at the top of
your main() before you can do anything else. i don't
think this rule is enforced by visual c++, though.
to print out the value of an integer variable, we use printf with the
%d format specifier. for example:
printf("the diameter is %d\n", diameter);
the \n in the printf format string generates a
newline. it has the same effect as pressing the enter key in a word
processor [or pushing the carriage return [the big lever] on a
typewriter]: it moves the cursor to the next line, and all the way to
the leftmost column. you can use \n anywhere you like in
the format string, for example:
printf("apple\n\nbananna\ncherry\n");
will produce:
apple bananna cherry
a real number is a number with a decimal, such as 2.5, -6.43, or
5.0. in c, a number must have a decimal point to be considered a real
number. so, 5 is considered an integer, and 5.0 is considered a real
number. real number variables are declared with the
double keyword, like this:
double area; double funds = 25.63; double two = 2.0;
again, since we don't specify an initial value for area,
the computer will pick one for us.
to print out the value of a double, we use printf with the
%lf format specifier [yes i am sure it's
%lf, check the announcements on the class webpage]. for
example:
printf("the area is %lf\n", area);
characters are any symbol that can be typed with the keyboard [and
some others, too], such as 'Q', 'f', '!', or '%'. character variables
are declared with the char keyword, like this:
char letter; char symbol = '*'; char two = '2';
again, letter could have any value, since we don't
initialize it. note that characters are always in single quotes.
to print out the value of a character, we use printf with the
%c format specifier. for example:
printf("the symbol is %c\n", symbol);
most of the arithmetic operators in c are fairly straightforward: + is
used for addition, - is used for subtraction [- can also be used to
negate things, for example -x], * is used for
multiplication, and / is used for division.
an operator that you may not be familiar with is the modulus operator,
which is a fancy name for remainder. recall that 11 divided by 4 is 2,
with a remainder of 3. so, in c, 11 / 4 calculates the
whole part [the fancy word for this is 'quotient'], which is
2, and 11 % 4 calculates the remainder,
which is 3.
in c, the result of any arithmetic operation can be a rational number
only when one of the operands is a rational number. this means that
11 / 4 is 2, and 11.0 / 4 is
2.75 [remember that a number must have a decimal point to
be considered a real number. 11 is an integer, 11.0 is real].
in c, operators follow the usual math precedence rules: stuff in parentheses gets evaluated before anything else, then multiplications and divisions happen, then additions and subtractions.
from the programmer's point of view, computers are tremendously powerful, yet extremely dumb machines. as a programmer, if you want the computer to do something, you have to give the computer a very detailed list of things that need to be done [that's what a program is, after all]. so, programming is all about taking a large and complicated task, and breaking it down into small steps that the computer can understand. this is what makes programming hard, and [for some people] fun.
when you're up against a difficult programming problem, try to break the problem up into smaller sub-problems, then solve the sub-problems one at a time. it's difficult to show examples of this now, but i'll demonstrate this technique more as the quarter progresses.
another very important bit of programming advice is to always test your program incrementally. this means that you should write a little piece of your program, then test just that little part, to make sure it works the way you think it should. why? because when problems appear, it's a lot easier to find the cause of the problem in a little piece of your program it is to find the cause of the problem in your whole program. again, it's difficult to show examples of this now, as our programs aren't that big yet, but i'll try to demonstrate this more also.
also, a c program must be compiled before it can be run. this also means that if you change your c program, you need to re-compile it before running it again.
note: some of these exercies are significantly harder than the exercises in the book. it's okay if you can't figure out the difficult ones, as long as you understand the answers. of course, if you ever have questions about anything, the ta's and tutors are available to help you.
write a program that computes the area of the circle with radius 5. PI should be a constant, and the radius should be an integer. the output should be a real number.
write a program that computes a player's rating. rating is determined by points, wins, and losses. the formula for rating is: [points*10+(wins-losses)*100]/[wins+100]. rating, points, wins, and losses are all integers.
what will happen when i run the following program?
#include <stdio.h>
void main()
{
char right = ')';
char semi = ';';
char letter;
int lucky_number = 24;
double balance = 25.43;
letter = 'b';
balance = balance + balance;
lucky_number = lucky_number + 1 / 2;
printf("hello\n\nyour %calance is %lf%c your lucky number is %d. ",
letter, balance + 1 / 2, semi, lucky_number);
printf("%c%c\n", semi, right);
}
this one is tricky. write a program that displays the last digit of a positive integer. so, if my positive integer is 987654, the program will output 4. hint: modulus will be helpful...