Skip to main content

Posts

Showing posts from April, 2013

Basic Computer Architecture

Q.1. Explain the different types of Memory Modules. Ans.: There are two types of memory modules : (i) SIMM : Single Inline Memory Modules (ii) DIMM : Double Inline Memory Modules These are small printed circuit cards (PCC) on which several DRAMS memory chips are placed. Such cards are plugged into the system board of the computer. The SIMM Circuit cards contain several memory chips with contacts placed on only one edge of this PCC whereas in DIMM, it‟s on both sides of the PCC. Q.2. Explain about the System Clock. Ans.: Every computer has got a system clock. It‟s located in the microprocessor. The clock is design by a piece of quartz crystal. The system clock keeps the computer system coordinated. It‟s an electronic system which keeps oscillating at specified times intervals, between 0 & 1. The speed at which this oscillation takes place is called the cycle of the clock. The time taken to reach from „0‟ to „1‟ and back is called clock cycle the speed of the system cl

Anatomy of a Computer

Q.1. What is CISC Microprocessor? Ans.: CISC stands for complex instruction set computer. It was developed by Intel. CISC is a type of design for the computers. CISC based computer will have shorter programs which are made up of symbolic machine language. The number of instructions on a CISC processor is more. Q.2. What is RISC Microprocessor? Ans.: RISC stands for reduced instruction set computer architecture. The properties of this design are : (i) A large number of general purpose registers and use of computers to optimize register usage. (ii) A limited & simple instruction set. (iii) An emphasis on optimizing the instruction pyre line . Q.3. What are the different types of Memory? Ans.: The memory in a computer is made up of semi-conductions. Semi-conduction memories are of two types : (1) RAM : Random Access Memory (2) ROM : Read Only Memory (1) RAM : The Read and write (R/W) memory of a computer is called RAM. The User can write information to it and read

Need of an Accountant

Accountant Indian Institute of Management Kozhikode Address:  Indian Institute of Management Kozhikode IIMK Campus P. O. Postal Code:  673570 City  Kozhikode State  Kerala Pay Scale:  Rs. 9300-34800 + Grade Pay: Rs. 4200/- Educational Requirements:  B.Com/BBA/Inter CA / ICWA with 55% marks and Six (06) years relevant post qualification experience in PB-1, 5200-20200; GP 2800 or equivalent service and pay Qualifications:  B.Com/BBA/Inter CA / ICWA with 55% marks and Ten (10) years relevant post qualification experience in PB-1, 5200-20200; GP 2400 or equivalent service and pay. Date Posted:  04/11/2013 Details will be available at: http://www.iimk.ac.in/newwebsite/announcements/careers/2013/webadvt11042013.php No of Post:  02 How To Apply:  Printed copy of the application containing self-attested copies of following should be sent by Speed Post/ Registered Post/ Courier, clearly writing on the top Left Corner of the Envelope “Application for the post

Lab Assistant Needed

Lab Assistant University of Hyderabad Address:  School of Life Sciences, University of Hyderabad, Gachibowli Postal Code:  500046 City  Hyderabad State  Andhra Pradesh Pay Scale:  Rs. 8,000/- pm Educational Requirements:  Graduation (BA/BSc/BCom/BTech) with preferably Science background at high school level with minimum three years’ experience of working in a lab Qualifications: Experience Requirements:  The application without experience certificates would be rejected. Desired Skills:  Knowledge of computer based software operation such as Excel and Word for accounting and familiarity with purchase procedure is desirable. Details will be available at:  http://www.uohyd.ac.in/images/recruitment/jrf_la_260413.pdf How To Apply:  Candidates interested in these positions should send their full bio-data along with academic record, a statement clearly explaining how their skills are relevant to the position and the name/contact information for three refer

Agricultural Technology Management Agency

Agricultural Technology Management Agency Address:  Agricultural Technology Management Agency, Bridge House, Birhata Postal Code:  713101 City  Burdwan State  West Bengal Pay Scale:  Rs. 12,000/- per month Educational Requirements:  B. Tech/MCA Date Posted:  04/09/2013 Experience Requirements:  One year experience. Details will be available at:  http://bardhaman.nic.in/noticeboard/recru_atma_100413.pdf No of Post:  01 How To Apply:  Candidates should apply in the enclosed prescribed application form along with self attested copies of certificates addressed to the Project Director, ATMA, Burdwan, C/o. office of the Deputy Director of Agriculture (Administration), Burdwan, Bridge House, Birhata, Burdwan, Pin-713101 by ordinary post within 10th May, 2013. Original documents and proof of age & Residence & voter ID card to be produced at the date of interview. Last Date:  10th May, 2013 Age Limit:  45 Years More info at :- Job Search

Other Preprocessor commands

There are a handful more preprocessor commands which can largely be  ignored by the beginner. They are commonly used in "include" files to make  sure that things are not defined twice. NOTE : ‘true’ has any non zero value in C. ‘false’ is zero.  #undef         This undefines a macro, leaving the name free. #if                 This is followed by some expression on the same line. It allows                       conditional compilation. It is an advanced feature which can be                       used to say: only compile the code between ‘#if’ and ‘#endif’                        if the value following ‘#if’ is true, else leave out that code altogether. This is      D ifferent from not executing code—the code will  not even be compiled. #ifdef     This is followed by a macro name. If that macro is defined then                 this is true.

Understanding Macro Functions

A more advanced use of macros is also permitted by the preprocessor. This involves macros which accept parameters and hand back values. This works by defining a macro with some dummy parameter, say x. For example: a macro which is usually defined in one of the standard libraries is abs() which means the absolute or unsigned value of a number. It is defined below: #define ABS(x) ((x) < 0) ? -(x) : (x) The result of this is to give the positive (or unsigned) part of any number or variable. This would be no problem for a function which could accept parameters, and it is, in fact, no problem for macros. Macros can also be made to take parameters. Consider the ABS() example. If a programmer were to write ABS(4) then the preprocessor would substitute 4 for x. If a program read ABS(i) then the preprocessor would substitute i for x and so on. (There is no reason why macros can’t take more than one parameter too. The programmer just includes two dummy parameters with differe

Conversion characters

The conversion characters for scanf are not identical to those for printf and it is much more important to be precise and totally correct with these than it is with printf. d                        denary integer (int or long int) ld                       long decimal integer x                        hexadecimal integer o                       octal integer h                       short integer f                        float type lf                       long float or double e                       float type le                      double c                      single character s                     character string The difference between short integer and long integer can make or break a program. If it is found that a program’s input seems to be behaving strangely, check these carefully. (See the section on Errors and Debugging for more about this.)

Preprocessor Commands

C is unusual in that it has a pre-processor. This comes from its Unix  origins. As its name might suggest, the preprocessor is a phase which occurs  prior to compilation of a program. The preprocessor has two main uses: it  allows external files, such as header files, to be included and it allows macros  to be defined. This useful feature traditionally allowed constant values to be  defined in Kernighan and Ritchie C, which had no constants in the language. Pre-processor commands are distinguished by the hash (number) symbol  ‘#’. One example of this has already been encountered for the standard  header file ‘stdio.h’. #include <stdio.h>  is a command which tells the preprocessor to treat the file ‘stdio.h’ as if it were the actually part of the program text, in other words to include it as  part of the program to be compiled. Macros are words which can be defined to stand in place of something  complicated: they are a way of reducing the amount of typing in a program  an

Variables, Types and Declarations in C

A variable is a seqeuence of program code with a name (also called its  identifier ). A name or identifier in C can be anything from a single letter to  a word. The name of a variable must begin with an alphabetic letter or the  underscore ‘_’ character but the other characters in the name can be chosen  from the following groups: a .. z (any letter from a to z) A .. Z (any letter from A to Z) 0 .. 9 (any digit from 0 to 9) _ (the underscore character) Some examples of valid variable names are: a total Out_of_Memory VAR integer etc...  In C variables do not only have names: they also have types. The type  of a variable conveys to the the compiler what sort of data will be stored in it. In BASIC and in some older, largely obsolete languages, like PL/1, a  special naming convention is used to determine the sort of data which can  be held in particular variables. e.g. the dollar symbol ‘$’ is commonly used  in BASIC to mean that a variable is a string and the percentage ‘%’ sy

Style Note in C

Some programmers complain about the use of global variables in a program.  One complaint is that it is difficult to see what information is being passed  to a function unless all that information is passed as parameters. Sometimes  global variables are very useful however, and this problem need not be crip pling. A way to make this clear is to write global variables in capital letters  only, while writing the rest of the variables in mainly small letters.. int GLOBALINTEGER; .... { int local integer; } This allows global variables to be spotted easily. Another reason for restrict ing the use of global variables is that it is easier to debug a program if only  local variables are used. The reason is that once a function capsule is tested  and sealed it can be guaranteed to work in all cases, provided it is not af fected by any other functions from outside. Global variables punch holes in the sealed function capsules because they allow bugs from other functions  to creep into t

Assigning variables to one another

Variables can be assigned to numbers: var = 10; and assigned to each other: var1 = var2; In either case the objects on either side of the = symbol must be of the same type. It is possible (though not usually sensible) to assign a floating point number to a character for instance. So i nt a, b = 1; a = b; is a valid statement, and: float x = 1.4; char ch; ch = x; is a valid statement, since the truncated value 1 can be assigned to ch. This is a questionable practice though. It is unclear why anyone would choose to do this. Numerical values and characters will interconvert because characters are stored by their ASCII codes (which are integers!) Thus the following will work: int i; char ch = ’A’; i = ch; printf ("The ASCII code of %c is %d",ch,i); The result of this would be: The ASCII code of A is 65

Breaking out early

Suppose that a program is in the middle of some awkward process in a function which is not main(), perhaps two or three loops working together, for example, and suddenly the function finds its answer. This is where the beauty of the return statement becomes clear. The program can simply call return(value) anywhere in the function and control will jump out of any number of loops or whatever and pass the value back to the calling statement without having to finish the function up to the closing brace }. myfunction (a,b) /* breaking out of functions early */ int a,b; { while (a < b) { if (a > b) { return (b); } a = a + 1; } }

What is Programming style

C is actually a free format language. This means that there are no rules about how it must be typed, when to start new lines, where to place brackets or whatever. This has both advantages and dangers. The advantage is that the user is free to choose a style which best suits him or her and there is freedom in the way in which a program can be structured. The disadvantage is that, unless a strict style is adopted, very sloppy programs can be the result. The reasons for choosing a well structured style are that: • Long programs are manageable only if programs are properly organized. • Programs are only understandable if care is taken in choosing the names of variables and functions. • It is much easier to find parts of a program if a strict ordering convention is maintained. Such a scheme becomes increasingly difficult to achieve with the size and complexity of the problem.

Understanding Filename

The compiler uses a special convention for the file names, so that we do  not confuse their contents. The name of a source program (the code  which you write) is ‘filename.c’. The compiler generates a file of ob ject code from this called ‘filename.o’, as yet unlinked. The final pro gram, when linked to libraries is called ‘filename’ on Unix-like operating  systems, and ‘filename.EXE’ on Windows derived systems. The libraries  themselves are also files of object code, typically called ‘liblibraryname.a’  or ‘liblibraryname.so’. Header files are always called ‘libname.h’.  The endings ‘dot something’ (called file extensions) identify the contents  of files for the compiler. The dotted endings mean that the compiler can  generate an executable file with the same name as the original source – just  a different ending. The quad file and the object file are only working files  and should be deleted by the compiler at the end of compilation. The ‘.c’ suffix is to tell the compiler that th

File handling functions

fopen() FILE *fopen(const char *path, const char *mode); The fopen() function is used to open a file and associates an I/O stream with it. This function takes two arguments. The first argument is a pointer to a string containing name of the file to be opened while the second argument is the mode in which the file is to be opened. The mode can be : ‘r’    :  Open text file for reading. The stream is positioned at the beginning of the file. ‘r+’ :  Open for reading and writing. The stream is positioned at the beginning of the file. ‘w’   :  Truncate file to zero length or create text file for writing. The stream is positioned at the beginning of the file. ‘w+’ : Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is positioned at the beginning of the file. ‘a’    : Open for appending (writing at end of file). The file is created if it does not exist. The stream is positioned at the end of the file. ‘a+’ : Open for rea

File Input/output in C

Input and output to and from files is identical to that at the command line, except the fprintf and fscanf functions are used and they require another argument. This additional argument is called a file pointer. In order to write two floating point numbers to a file, you first need to declar the file pointer with the FILE type, and you need to open it, as in float x=1, y=2; FILE *file; file = fopen(‘‘file.txt’’,’’w’’); fprintf(file,’’%f %f\n’’,x,y); fclose(file); The function fprintf is identical to the printf function, except now we see it has another argument file, which is a pointer to the file. Before you use the file variable, you need to open the file with file = fopen(‘‘file.txt’’,’’w’’); This opens up the file ‘‘file.txt’’ and the ‘‘w’’ which is the mode and indicates how the file will be used. The following three modes are allowed: Mode String Open for reading “r” Open for writing “w” Open and append “a” When you are done with the file, you close it

Defining your own types

C allows you to define your own types, and this can make your codes much more legible. For example, you can define a type called price that you can use to define variables that you use to represent the price of certain objects. To do so, you would use typedef float price; int main(void) { price x, y; } The typedef is used in the following way: typedef existing_type new_type; The price type example is identical to using float x, y; but it is useful to use typedef when you would like the ability to change the types of certain variables throughout your codes by only changing one line. That is, if you wanted to change all of your prices to type double, then you would only need to change the typedef line to typedef double price;

Basic structure of a C program

As we saw in the hello.c example in the last lecture, Every C program must contain a main function , since the main function is the first function called when you run your program at the command line. In its simplest form, a C program is given by int main(void) { printf(‘‘Hello world!\n’’); } The int stands for the “return type”, which says that the main function returns an integer if you tell it to do so. We will get into more detail with functions and return types, but you can return a number to the command line with the return function in C, as in int main(void) { printf(‘‘Helloworld!\n’’); return 2; }

Understanding Tree structures

Tree Structures Introduction In many situation, an efficient way to represent data structures is to use trees. A tree can be defined recursively as an object containing some data and references to a certain number of subtrees. This definition leads to a hierarchical structure, in which trees with no sub-trees are called leaves. The other ones are called internal nodes. More mathematically, a tree is a graph with no cycles. Those data structures are very useful to store and organize informations associ- ated to comparable values. Here we give an example of an associative memory int -> string.

Overloading the >> operator

The left operand of >> will always be an istream and the right operand will be whatever we want : #include <iostream> class Crazy { public: double a, b; }; istream & operator >> (istream &i, Crazy &c) { return i >> (c.a) >> (c.b); } int main(int argc, char **argv) { Crazy x; cin >> x; } The ostream can not be copied, and will always exist as a lvalue (by definition printing modifies its state), so you have to alway pass it by reference and return a reference : #include <iostream> void dumb1(ostream &s) {} void dumb2(ostream s) {} int main(int argc, char **argv) { dumb1(cout); dumb2(cout); } /usr/lib/gcc-lib/i586-pc-linux-gnu/2.95.1/../../../../include/g++-3/streambuf.h:12 ‘ios::ios(const ios &)’ is private Here the line 8 tries to pass the stream by value, thus to call a copy constructor, which is private.

Fusion sort

The usual dumb algorithms for sorting things require a number of operations proportional to the square of the number of elements to sort (O(n2)). In practice, the used algorithms require a number of operations proportional to n×log n. The first one is the fusion sort. The main point is that given two sorted list of numbers, generating the sorted merged list needs a number of operations proportional to the size of this result list. Two index indicate the next elements to take from each list, and one indicates where to store the smallest of the two (see figure 7.3). This process can be iterated, starting with packets of size 1 (which are already sorted ...) and merging them each time two by two (see figure 7.4). After k iterations of that procedure, the packets are of size 2k, so the number of iterations for this process is log2 n where n is the total number of objects to sort. Each step of this main process cost the sum of the sizes of the resulting packets, which is n. Finally t

Big-O Notation

Big-O Notation Why ? How ? To estimate the efficiency of an algorithm, the programmer has to be able to  estimate the number of operations if requires to be executed.  Usually the number of operations is estimated as a function of a parameter (like  the number of data to work on, or the expected precision of a computation, etc.) For example : for(i = 0; i < n; i++) { ... } has a cost proportional to n. for(i = 1; i < n; i = i*2) { ... } has a cost proportional to log2 n for(i = 0; i < n; i++) for(j = 0; j<n*n; j++) { ... } has a cost proportional to n3.

Anti-bug tools

GDB The most standard debugging tool on UNIX is the GNU Debugger gdb. Its main functionnality is to display the piece of code which procuced a crash. To do it, compile your code with the -g option, so that debugging information will be added to the executable. This information is mainly a correspondance between the machine langage instructions and locations in the source. Then, execute the program from gdb. For instance int main(int argc, char **argv) { int size = 100; int a[size]; for(int i = 0; i < 100 * size; i++) a[i] = i; } > g++ -o bang -g bang.cc > gdb ./bang

How to find bugs

Print information during execution The best way to find errors is to print a lot of information about the internal state of the program. For instance, if a program remains frozen, the first thing to do is to print something when a few checkpoints are met cout << "Checkpoint #1\n"; for(int i = 1; i < 1000; i++) cout << "i = " << i << "\n"; cout << "Checkpoint #2\n"; for(int j = 0; j < 100; j = j*2) cout << "j = " << j << "\n"; cout << "Checkpoint #3\n"; Also, printing values supposed to vary or to remain constant is a good way to spot errors. Write the same routine twice Usually, any routine can be written in a short, dirty, computationally expensive and maybe even numerically approximative way. This is a good technique to check that the fancy and correct version does what it is supposed to do. For instance, computation of a deri

How to avoid bugs

Write in parts Finding one bug in the 20 lines you typed for the last fifteen minutes is easier than finding fifty bugs in the two thousands lines you have typed for one month. Good identifiers Use long identifiers such as sum_of_the_weights instead of short ones. Use longer identifiers for variables and functions used in many parts of the program. If a variable is used only in a 5 line loop, it can be called s with no risk. If you are really lazy, at least use acronyms (ws for weight sum for instance). Also, reuse the same names and parameter order everywhere. Avoid at all cost this sort of mess int rectangle_surface(int xmin, int ymin, int xmax, int ymax) { return (xmax - xmin) * (ymax - ymin); } int rectangle_diagonal_length(int xmin, int xmax, int ymin, int ymax) { return sqrt(double( (xmax - xmin) * (xmax - xmin) + (ymax - ymin) * (ymax - ymin) ) ); }

The Bug Zoo

The program crashes: Segmentation fault This family of problem is extremely large and contains two main sort of errors: access to non-authorized part of the memory and system calls with incorrect parameter values. Unauthorized memory access It when you try to read or write to a memory address 1. totally meaningless (for instance a non-initialized pointer) 2. out of bounds 3. not allocated anymore Note that a memory access with an unitialized pointer may corrupt the memory without actually crashing the program. For instance #include <iostream> int main(int argc, char **argv) { int b; int a[10]; b = 4; for(int i = 0; i < 100; i++) { a[i] = 12; cout << b << " "; cout.flush(); } }

Declaration Vs definition

Declaration Vs definition The declaration of a function specifies the return and parameter types. The definition specifies the part of the program associated to the function, i.e. the statement between { }. Up to now, we have always done the declaration and the definition simultaneously, but declaration can be done before the definition. This is useful because a function can be called at a given location in the program only if the declaration we done before. #include <iostream> int function1(int x); int function2(int x) { if(x == 0) return 0; else return function1(x-1); } int function1(int x) { if(x == 0) return 1; else return function2(x/2); } int main(int argc, char **argv)  {cout << function1(15) << ’\n’; }

Testing Null Pointer

Pointer arthmatic  A pointer can be implicitly converted into a bool. All non-null pointers are  equivalent to true and the null one is false. The convention is that the null  pointer correspond to a non-existing object. Static pointers are not initialized to 0 when the program starts, so  you have to be very careful when using this convention  For example, if we want to write a function that count the number of characters  into a character strings, and returns 0 if the parameter pointer is null : #include <iostream> int length(char *s) { if(s) { char *t = s; while(*t != ’\0’) t++; // The difference of two pointers is an integer return t-s;

Dynamic allocation

Dynamic allocation Why And How  In many situations the programmer does not know when he writes a program what objects he will need. It can be that he does not know if he will need a given object, or or that he does not know the size of a required array. The new operator allows to create an object at run-time. This operator takes as an operand a type, which may be followed either by a number of elements between [ ] or by an initial value between ( ) : char *c = new char; int *n = new int(123); double *x = new double[250]; The area of memory allocated by new is still used out of the pointer’s scope!

What are Built-in arrays

What are Built-in arrays What are Built-in arrays The "" operator defines arrays of char. Similarly, we can define an array of any type with the [ ] operator : int n[4] = { 1, 2, 3, 4 }; The compiler is able to determine by itself the size of an array, so you do not have to specify it : int poweroftwo[] = { 1, 2, 4, 8, 16, 32, 64, 128 }; As we said, the compiler keeps the information about the size of arrays (or strings), so the sizeof operator returns the size of the array as expected : #include <iostream> int main(int argc, char **argv) { int hello[] = { 1, 2, 3 }; char something[] = "abcdef"; cout << sizeof(hello) << ’ ’ << sizeof(something) << ’\n’; } The size of arrays is always known and has to be known at compilation time. Note : from that, you can compute the number of element of an array by dividing the sizeof the array by the sizeof the element type.

Understanding Functions in C++

Understanding Functions in C++ To re-use the same part of a program, we can define a function, which takes  parameters, and returns a result of various type. Typical definition contains theType of the value it returns, an identifier for its name, and the list of parameters  with their types. The evaluation of a function is done when the call operator  () is used. One argument (i.e. an expression) is provided to each parameter. An example makes things clearer. #include <iostream> // This function has one parameter called x double square(double x) { return x*x; } // This one has two parameters called x and y // It returns the largest k so that y^k <= x int maxexpon(double x, double y) { double z = 1; int result = 0; while(z <= x) { result++; z = z * y; } return result-1; } int main(int argc, char **argv) { double a, b; cin >> a >> b; // The argument is a for the first call and a+b for the second cout << square(a) + square(a + b) &l

The abort() function in C++

The abort() function is wich interrupt the execution of your program as if there was a serious error. Use it to handle non-expected behavior like out-of bounds exceptions : #include <iostream> int main(int argc, char **argv) { int x; cout << "Enter a non-null value : "; cin >> x; if(x == 0) { cerr << "Null value!\n"; abort(); } cout << 1/x << ’\n’; } The execution is the following : Enter a non-null value : 0 Null value! Aborted

The sizeof operator

We can know the memory usage of a given type using the sizeof operator. It takes either a variable name or a type name as parameter. #include <iostream> int main(int argc, char **argv) { int n; cout << sizeof(n) << ’ ’ << sizeof(double) << ’\n’; } The result is 4 8.

Streams, include files

Beside the built-in types, the C++ compiler is often accompanied with lot of files containing predefined types. The main one we will use in our example programs is the stream type. To use it, we need to indicate to the compiler to include in our source file another file called iostream (where this class type is defined). #include <iostream> int main(int argc, char **argv) { int k; k = 14; k = k + 4; k = k * 2; cout << k << ‘\n’; } The variable cout is defined in the included file and is of type ostream. The only thing we need to know for now is that we can display a variable of a built-in type by using the << operator. We can also read values with cin and the >> operator.  The following program gets two float values from the user and prints the ratio. #include <iostream> int main(int argc, char **argv) { double x, y; cin >> x >> y; cout << x / y << ’\n’; } Note that with recent versions of the GNU C+