@ Monday 17th December 2007, 10:35 pm - Roger Gajraj
Hey guys, I promised to post some
information relating to areas I feel you should know for you SEMESTER
exams. A flu is trying to overcome me right now so I
will not elaborate too much nor do fancy formatting.
Please study more in depth and do not
just read this over.
1.
A ‘Procedure’
|
|
A set of instructions which describe the steps to be followed in order to carry out an activity. | ||
|
|
This can be for any activity:
|
||
|
|
If it is written in a computer language it is called a program. |
Describing Procedures Precisely
|
|
Flowcharts
|
||
|
|
Pseudocode
|
Top Down Programming with Stepwise Refinement
|
|
Designing a computer program by
specifying as little of the details of how the job will be done at the early
stages, but gradually defining tasks in more detail at later stages.
|
||||||||||
|
|
Top-Down Design is used in all areas of design! |
2.
http://www-ee.eng.hawaii.edu/~tep/EE150/book/chap14/section2.1.2.html
http://www.technoplaza.net/programming/lesson6.php
3.
Research and understand what really happens when a variable is created, and depending on WHERE in code it is created...find out how long it stays in memory.
4.
Understand and know how:
pass-by-reference....e.g "void myFunction( int *ptrToNum );"
..and then .. called like: "myFunction( &someVariable );
pass-by-value............e.g "void myFunction( int number );"
..and then .. called like: "myFunction( someVariable );
5.
Understand WHY files are used. Remember persistence of data?
6.
FINALLY: Try to review your last test!
GOOD LUCK TO ALL!
@ Monday 22nd October 2007, 10:35 pm - Roger Gajraj
Sorry for not
posting anything sooner. Meanwhile, here is a link to some online demo VIDEO
tutorials I found on C.
http://ww.vtc.com/cd/C-Programming-tutorials.htm
_______________________________________________________________________________________________________
@ Wednesday 10th October 2007, 2:01 am - Roger Gajraj
understanding the for loop!
int i;
for( i = 0; i < 5; i++ )
// means
initialize i to zero, check if i, at this time, is less than zero
{
// and if so THEN do code(...) in braces. Lastly, increment i by one(1) keep
repeating
/*...body of code*/
// whole process until i is not less than 5. Initialisation of i=0 is only done
at first loop.
}
// Therefore, the body of code runs when i=0, i=1, i=2, i=3 and i=4, which is
a total of five times!
N.B. You need to figure out how to change the numbers in the for construct to loop as many times as YOU want to
So, if you wanted the run the body of code 23 times, you needed to say: for( i=0; i<23; i++).
How many times do you think this loop will run for? : for( i=1; i<5; i++)
_______________________________________________________________________________________________________
@ Wednesday 10th October 2007, 1:48 am - Roger Gajraj
CONTROL CONSTRUCTS -
loops
loops are used to rerun some part of code.
C contains a number of looping constructs, such as the "while" loop:
/* while.c */
#include <stdio.h>
void main()
{
int test = 10;
while( test > 0 )
{
printf( "test = %d\n", test );
test = test - 2;
}
}
If "test" starts with an initial value less than or equal to 0 the "while" loop will not execute even once. There is a variant, "do", that will always execute at least once:
/* do.c */
#include <stdio.h>
void main()
{
int test = 10;
do
{
printf( "test = %d\n", test );
test = test - 2;
}
while( test > 0 );
}
The most common looping construct is the "for" loop, which creates a loop much like the "while" loop but in a more compact form:
/* for.c */
#include <stdio.h>
void main()
{
int test;
for( test = 10; test > 0; test = test - 2 )
{
printf( "test = %d\n", test );
}
}
Notice that with all these loops, the initial loop statement does not end with a ";". If a ";" was placed at the end of the "for" statement above, the "for" statement would execute to completion, but not run any of the statements in the body of the loop.
The "for" loop has the syntax:
for( <initialization>; <operating test>; <modifying expression> )
So:
for( i=0; i<5; i++)
// means
initialize i to zero, check if i, at this time, is less than zero
{
// and if so THEN do code(...) in braces. Lastly, increment i by one(1) keep
repeating
/*...body of code*/ // whole process until
i is not less than 5. i=0 is only one at first loop.
}
// Therefore, the body of code runs when i=0, i=1, i=2, i=3 and i=4, which is a
total of five times
All the elements in parentheses are optional. A "for" loop could be run indefinitely with:
for( ; ; )
{
...
}
-- although using an indefinite "while" is cleaner:
while( 1 )
{
...
}
_______________________________________________________________________________________________________
@ Wednesday 10th October 2007, 1:23 am - Roger Gajraj
CONTROL CONSTRUCTS - decision making
C also contains decision-making statements, such as "if":
/* if.c */
#include <stdio.h>
#define MISSILE 1
void fire( int weapon )
void main()
{
fire( MISSILE );
}
void fire( int weapon ) //function to fire. Takes in one integer value
{
if( weapon == MISSILE )
{
printf( "Fired missile!\n" );
}
if( weapon != MISSILE )
{
printf( "Unknown weapon!\n");
}
}
This example can be more easily implemented using an "else" clause:
/* ifelse.c */
void fire( int weapon )
{
if( weapon == MISSILE )
{
printf( "Fired missile!\n" );
}
else
{
printf( "Unknown weapon!\n");
}
}
Since there is only one statement in each clause the curly brackets aren't really necessary. This example would work just as well:
void fire( int weapon )
{
if( weapon == MISSILE )
printf( "Fired missile!\n" );
else
printf( "Unknown weapon!\n" );
}
However, the brackets make the structure more obvious and prevent errors if you add statements to the conditional clauses. The compiler doesn't care one way or another, it generates the same code.
There is no "elseif" keyword, but "if" statements can be nested by the following code.
NOTE TO STUDENTS: (not "elseif") but "else if" is valid because 'if' is nested in 'else' since if comes directly after. Notice that "else if" was used in class.
/* nestif.c */
#include <stdio.h>
#define MISSILE 1
#define LASER 2
void fire( int weapon )
void main()
{
fire( LASER );
}
void fire( int weapon )
{
if( weapon == MISSILE )
{
printf( "Fired missile!\n" );
}
else
{
if( weapon == LASER )
{
printf( "Fired laser!\n" );
}
else
{
printf( "Unknown weapon!\n");
}
}
}
This is somewhat clumsy. The "switch" statement does a cleaner job:
/* switch.c */
void fire( int weapon )
{
switch( weapon )
{
case MISSILE:
printf( "Fired missile!\n" );
break;
case LASER:
printf( "Fired laser!\n" );
break;
default:
printf( "Unknown weapon!\n");
break;
}
}
The "switch" statement tests the value of a single variable; unlike the "if" statement, it can't test multiple variables. The optional "default" clause is used to handle conditions not covered by the other cases.
Each clause ends in a "break", which causes execution to break out of the "switch". Leaving out a "break" can be another subtle error in a C program, since if it isn't there, execution flows right through to the next clause. However, this can be used to advantage. Suppose in our example the routine can also be asked to fire a ROCKET, which is the same as a MISSILE:
void fire( int weapon )
{
switch( weapon )
{
case ROCKET:
case MISSILE:
printf( "Fired missile!\n" );
break;
case LASER:
printf( "Fired laser!\n" );
break;
default:
printf( "Unknown weapon!\n");
break;
}
}
_______________________________________________________________________________________________________
@ Wednesday 10th October 2007, 12:58 am - Roger Gajraj
This is to supplement the notes you have on how code is compiled into an executable:
C compilation is a multi-pass process. To create a program, a C compiler system performs the following steps:
|
|
It runs the source file text through a "C
preprocessor". All this does is perform various text manipulations on the
source file, such as "macro expansion", "constant expansion", "file
inclusion", and "conditional compilation", which are also explained later.
The output of the preprocessor is a second-level source file for actual
compilation. You can think of the C preprocessor as a sort of specialized
automatic "text editor".
|
|
|
Next, it runs the second-level source file
through the compiler proper, which actually converts the source code
statements into their binary equivalents. That is, it creates an "object
file" from the source file.
|
|
|
The object file still cannot be executed,
however. If it uses C library functions, such as "printf()" in the example
above, the binary code for the library functions has to be merged, or
"linked", with the program object file. Furthermore, some addressing
information needs to be linked to the object file so it can actually be
loaded and run on the target system.
These linking tasks are performed by a "linker", which takes one or more object files and links them to binary library files to create an "executable" file that can actually be run. |
_______________________________________________________________________________________________________
@ Tuesday 9th October 2007, 10:24 pm - Roger Gajraj
I found this slideshow online. If you are interested you can take a look.
_______________________________________________________________________________________________________
@ Monday 8th October 2007, 2:00 pm - Roger Gajraj
The following is working code for the tax program:
#include <stdio.h>
// includes
#include <stdlib.h>
float tax(int gsal);
// function prototype
int main(int argc,
char *argv[]) // main
function
{
float taxes;
// container to hold tax
int sal;
// container to hold salary
printf("enter your gross Salary:"); //
ask user to enter salary
scanf("%d", &sal); // user input is
copied to variable "sal"
taxes = tax(sal); // the "tax"
function is called to compute and return float value by passing "sal" to it (sal
= user input).
// So return value is copied to "taxes".
printf("your gross salary is %d \n", sal);
// print salary
printf("your total taxes is %f\n", taxes);
// print tax
printf("Net salary is %f\n", (float)sal-taxes);
// print net salary
system("PAUSE"); // pauses screen
return 0; // exit main function and
return 0
}
float tax(int gsal) // function to compute tax
where it takes in an "int" value
{
float taxR = 0.0;
// container to hold tax value
if (gsal > 25000 && gsal <= 30000) //
if salary is more than 25000 yet less than or equal to 30000 then do next line
only
taxR = 0.20 * gsal - 25000;
// tax is calculated and put in "taxR"
else if (gsal>30000) // if previous if
statement was false, then check if salary is more than 30000. If so, then do
next line only
taxR = (0.20 * 5000) + (gsal - 30000)
* 0.33; // tax is calculated and put in "taxR"
return taxR; // returns "taxR" ( actual
tax calculated) to WHERE THE FUNCTION WAS CALLED!
}
/////////////////////////////////////////////////////////////////////////////////END OF CODE/////////////////////////////////////////////////////////////////////////////////////
NOTES:
Please email any questions to r_gajraj@hotmail.com.
This is the first post I am making and I will make more posts tonight. Please check back then.
In the meanwhile, http://www.howstuffworks.com/c.htm has a good tutorial on C
HINTS:
Please try to revise how the "compiler" works with all it's steps. (first set of notes)
_______________________________________________________________________________________________________