All the Lab 1 exercises are C programming exercises; they serve both of the purposes given above.
The penalty for handing in an assignment after the Due Date but before the Late Due Date is 3 marks. In other words, X/Y becomes (X-3)/Y if the assignment is late. There will be no credit for assignments turned in after the Late Due Date; they will be returned unmarked.
Put the staple in the upper right corner of your assignment. (If you put it in the upper left corner, it is likely to go through important code on your printouts.)
Hand in your assignment in the box for your lab section in the west hallway on the 2nd floor of the ICT Building.
When the compiler finds a problem in your source code it generates one of two types of messages: errors and warnings. If the word warning appears in the message, it is, logically enough, a warning. Otherwise it is an error, even if the word error does not appear.
An error means that something in your code violates the rules of C, meaning that the compiler cannot translate the source code. In the case of an error it is obvious that your program must be fixed.
A warning, on the other hand, is generated when the compiler finds something that is legal in C but is bad style or has a meaning that is probably not what you intended. If there are warnings but no errors, the compiler will translate your source code.
The command gcc339 is a version of the gcc (GNU C compiler) command; gcc339 generates a lot more warning messages than gcc does without any warning options turned on. The command
gcc339 myprog.cis equivalent to
gcc -g -Wall -ansi -pedantic -Wstrict-prototypes \ -Wmissing-prototypes myprog.cObviously the former command is easier to type.
The gcc339 command was set up on the Electrical and Computer Engineering Linux systems to help ENCM 339 students deal with some of the mistakes and misunderstandings that beginning C programmers often make.
termrec is a utility which allows you to make a record of your program input and output.
Information on the use of termrec can be obtained in an old ENCM 339 course Web site. Click on the link to Recording a terminal session with termrec .
C variables declared outside of all function definitions in a C source file are external variables. Unlike local variables, external variables can be directly accessed from many different function definitions.
External variables are often called ``global variables''. I have chosen to use the term external variable to be compatible with the book The C Programing Language, by Kernighan and Ritchie.
There are two kinds of external variables: those declared to be static and those not declared to be static. External variables declared to be static can be directly accessed by all functions in the source file where the variable is declared. (This use of the keyword static is confusing, because it has nothing to do with static memory allocation.) External variables not declared to be static can be directly accessed by all functions in a program.
(If you are working on a program where an external variable is in fact accessed from more than one source file, you need to know the rules regarding the extern keyword. See a good C text for details, and read carefully.)
External variables, regardless of whether they are declared as static, are always statically allocated. So, according to rules you should have learned in ENCM 339, external variables are found in the static storage region, are allocated and initialized before main starts, and remain allocated as long as the program runs.
Unlike variables in activation records, external variables that don't have initializers are initialized to zero. For example, consider this variable declaration:
int foo[5];If foo were local to some function, you could not count on the elements of foo to have any particular values. But if foo were an external variable, its elements would all be initialized to zero.
Hand in your diagrams.
You can have a long career writing high-quality C and C++ code for a huge variety of applications and never once use a goto statement. Just about everything you can do with goto can be done more clearly some other way. Use of goto tends to make code unreasonably hard to understand and debug.
Nevertheless, it's useful to know what goto does, for the following reasons:
Consider the following simple program:
#include <stdio.h>
int main(void)
{
int i;
for (i = 0; i < 4; i++)
printf("i is %d. ", i);
printf("\n");
return 0;
}
Obviously, the output is
i is 0. i is 1. i is 2. i is 3.Here is an equivalent program written using goto statements:
#include <stdio.h>
int main(void)
{
int i;
i = 0;
loop_beginning:
if (!(i < 4)) goto past_loop_end;
printf("i is %d. ", i);
i++;
goto loop_beginning;
past_loop_end:
printf("\n");
return 0;
}
The output is exactly the same as that of the earlier program
with the for loop.
The identifiers loop_beginning and past_loop_end are examples of what are called labels. A label is used to name a paticular statement; a colon must appear between a label and the statement it names. A goto statement has the following syntax:
goto label;A goto statement causes the flow of statement execution to jump to whatever statement is named by the label. This should be reasonably clear from the example.
#include <stdio.h>
int main(void)
{
int outer, inner;
outer = 3;
outer_loop:
if (outer == 0) goto quit_outer_loop;
inner = 0;
inner_loop:
if (inner > outer) goto quit_inner_loop;
printf(" %d", 100 * outer + inner);
inner++;
goto inner_loop;
quit_inner_loop:
printf(" **\n");
outer--;
goto outer_loop;
quit_outer_loop:
printf("*****\n");
return 0;
}
Check your work by typing in the program, compiling it,
and running it.
(If it doesn't compile, goes into an infinite loop when it runs,
or crashes,
you have made a typing error.)
There is nothing to hand in for this exercise.
if (expression) goto label;
else while for switch && ||
Consider the following simple fragment of normal C:
if (x >= 0)
y = x;
else
y = -x;
The above is illegal in Goto-C for two reasons: the if
statement is not in an acceptable form, and
the banned keyword else is used.
One way to translate the fragment to Goto-C is as follows:
if (x < 0) goto else_code; y = x; goto end_if; else_code: y = -x; end_if:
There is nothing to hand in for this exercise.
Hand in a printouts of your completed program and a termrec file showing the program output.
if (condition) goto label;at the top of the loop to skip past the end of the loop when appropriate and you need a statement of the form
goto label;to jump backwards after the loop body has been executed. For example, this code:
while (i >= 0) {
printf("%d\n", i);
i--;
}
can be written as:
loop_top:
if (i < 0) goto past_end;
printf("%d\n", i);
i--;
goto loop_top;
past_end:
Remember that the condition to quit looping is the opposite
of the condition to continue looping.
There is nothing to hand in for this exercise.
if (y > 0 && x / y >= 10) {
foo(x);
bar(y);
}
can be coded as
if (y <= 0) goto end_if; if (x / y < 10) goto end_if; foo(x); bar(y); end_if:Expressions with || are a bit trickier.
There is nothing to hand in for this exercise.
Copy the directory /local/courses/encm369/lab1/exG
Read the file newton.c carefully.
Make an executable using the following command:
gcc339 newton.c -lm(The -lm option is needed to tell the linker to search the math library for machine code for sin and cos. That's a lower-case L, not a number 1.)
Clarification added Tues Jan 20 about 5:20pm: The original newton.c file contained a section of commmented-out code, which was confusing. You were not supposed to translate that code to Goto-C. I have edited the file to remove the confusing comment. The rest of the C code is totally unchanged.
Run the executable four times, using the following text as input:
Test your modified program with the same input given above for the original program. Make sure it always generates exactly the same output as the original program.
Make a termrec recording of your program runs with the four specified inputs, and hand that in a printout of that along with a printout of the final version of your source file.
How many lines in your + sign? 7 Here is your picture ... * * * ******* * * *
Here is another example dialogue: How many lines in your X? 5 Here is your picture ... * * ***** * *The number of lines must be odd; the minimum number of lines allowed is 3 and the maximum is 21. If the user enters an out-of-range integer or an even integer or input that can't be converted into an integer, then the program should print an error message and not try to draw a picture.
Hint: First write the program with regular C loops and if-statements. Make sure it works. Then make a copy of your program under a new name and start translating it to Goto-C.
Hand in printouts of your completed source file and a termrec file demonstrating several runs of your program.