CS19001/CS19002 Programming and Data Structures Laboratory | Autumn/Spring semester |
Miscellaneous information for students
Here is a set of suggestions/instructions/comments that the students must read carefully. This page may grow later. Visit it again.Credit distribution
- 40% of the total credit for the course will come from the lab.
- The three lab tests contribute 75% to your lab performance -- the first lab test 20%, the second 25% and the third 30%. Lab instructors, on their individual decisions, may alter these distributions. For example, each lab test may be given a credit of 25%.
- The remaining 25% of your lab performance will come from weekly assignments. Each assignment will be evaluated in a total of 20.
- The total marks you get from the lab tests and your weekly performance will be multiplied by the fraction of lab sessions you attend. Failure to attend a session will be excused only when proper official documents explaining the cause of absence are furnished. Informal requests will not be entertained.
How to submit your assignments
There are three posibilities for receiving your submissions:
- On-line submission
- Print-outs (Details below)
- FTP to a server in the Lab
Details on these different modes will be added soon.
Taking print-outs
- Read the printing instructions carefully.
- Read the printing instructions carefully.
- Read the printing instructions carefully.
- Multiple print-outs will not be allowed for the same problem.
- In the output, report what you are asked to demonstrate and report only what you are asked to demonstrate. We are not interested in any additional information, however attractive that might appear to you.
- If you are provided with a set of test inputs, you must submit the sessions of your program on these inputs.
- Output without echoed input will invalidate the entire submission (thereby leading to zero credit), even when the program and the output are correct.
- No print-out request will be entertained after 12:15/4:15 pm.
- Be absolutely certain before firing the print-out. Check the printer file with care and only when you feel that the file is perfectly ready for getting printed, launch the print command.
- Read the printing instructions carefully.
- Read the printing instructions carefully.
- Read the printing instructions carefully.
Indentation
You should learn how to indent a C program. Properly indented programs are legible, promote easier debugging and provide protection against certain queer compilation errors.Indentation does not only mean that a few lines of your code leave extra blank spaces on their left. Certain conventions regarding these spaces are necessary for this indentation to be a proper indentation. This also includes proper placement of the curly braces ({ and }). A sequence of C statements enclosed within a matching pair of braces is called a block.
Fix an indentation amount for your codes. In industry, people usually use a tab as the indentetation amount. Since a tab (normally) means eight characters, programs with large levels of nesting of blocks may face difficulty with such a large indentation amount. You may instead use 2 or 3 spaces as the indentation amount.
- Indenting functions: Every function requires a block, namely the body of the function. In the first line write the return type (optional), the name and the list of parameters (within parentheses). In the second line write only the opening brace {. In the next line start the body of the function. Every line in the body will receive an indentation of the amount you decided earlier. After the body write the closing brace in a single line.
#include <stdio.h> #include <stdlib.h> #include <math.h> #include <time.h> #include "mydefs.h" float myRoutine ( int n , float f ) { int k; Leave the indentation amount in every line inside this block. ... ... ... return(3.1415926535); } mySecondRoutine ( ) { Declare variables Declare variables Declare variables Indented instruction Indented instruction ... Indented instruction } int main ( int argc , char *argv[] ) { Declare variables Declare variables Declare variables Declare variables Indented instruction Indented instruction ... Indented instruction }- Nested blocks: Conditional statements (if, else) and loops (for, while, do etc.) with multiple instructions require blocks. Here write the condition and the opening brace on the same line. Then give an additional indentation of the chosen amount for each line inside the block. Finally write the closing brace in a single line without the extra indentation inside the block. Thus every nesting of block incurs additional indentation inside the block. A one-line if (or for or while or ...) statement can be thought of as a block with invisible enclosing braces. This is demonstrated below.
#include <stdio.h> int fibonacci1 ( int n ) { if (n < 0) { printf("Error: Non-negative argument expected.\n"); return(-1); } if (n <= 1) return (n); else return (fibonacci1(n-1) + fibonacci1(n-2)); } int fibonacci2 ( int n ) { int this, prev, next, i; if (n < 0) { printf("Error: Non-negative argument expected.\n"); return(-1); } if (n == 0) return (0); prev = 0; this = 1; for (i=2; i<=n; i++) { next = this + prev; prev = this; this = next; } return (this); } int main () { int response, n, fibn; while (1) { printf("\n\n"); printf("Enter 1 for calling the recursive function,\n"); printf(" 2 for calling the iterative function,\n"); printf(" 0 to terminate.\n"); printf("Your choice : "); scanf("%d",&response); /* Echo response for printout */ if (response == 0) exit(0); if ( (response == 1) || (response == 2) ) { printf("n = "); scanf("%d",&n); /* Echo n for printout */ if (response == 1) { printf("Calling the recursive function...\n"); fibn = fibonacci1(n); } else { printf("Calling the iterative function...\n"); fibn = fibonacci2(n); } printf("Fib(n) = %d\n", fibn); } } }Miscellaneous topics
- Always #include <stdio.h> in any C program you write. Sometimes it matters for getting the correct result. Sometimes your program may even refuse to compile without this include directive.
- Use the math library, if and only if there is no other easy way out. For example, if you want to compute the cube of an integer, call the multiplication operator (*) twice. For higher integral powers use a for loop.
int x,y,i,n; x = 123; y = x * x * x; x = 2; n = 30; y = 1; for (i=1; i<=n; i++) y *= x;Use the function pow in the math library, when and only when one of the operands is not an integer, like pow(3.1416,2) or pow(2,3.1416) or pow(2.7183,3.1416). Calling pow to compute integral powers of integers may even lead to erroneous results.
- The C++ style one-line comment // works with the gnu C compiler (gcc) you are using, but may cease to work in other compilers. Try to avoid this style and use /* ... */ for both single-line and multi-line comments.
- For gcc int is same as long int. Here is a list of the applicable ranges for integer data types.
Data type Minimum value Maximum value
char -128 127 unsigned char 0 255 short -32768 32767 unsigned short 0 65535 int -2147483648 2147483647 unsigned int 0 4294967295 long -2147483648 2147483647 unsigned long 0 4294967295 long long -9223372036854775808 9223372036854775807 unsigned long long 0 18446744073709551615