Pdf download focus on fundamentals with c






















All you need is some basic computer literacy and a desire to take up programming. The rest you will learn from the book. This book is indeed for beginners, but it teaches concepts and skills that even experienced professional programmers lack. Software companies are riddled with a shocking amount of self-taught amateurs who, despite having programmed on a salary for years, have no grasp of the fundamentals of programming and have no idea what a hash table is, how polymorphism works and how to work with bitwise operations.

Learn the basics of programming first and then the technologies. Otherwise, you risk having your programming skills crippled, more or less, for years, if not for life. If, on the other hand, you have programming experience, examine this book in details and see if you are familiar with all subjects we have covered, in order to decide whether it is for you or not. It is very likely that even if you have several years of experience, you might not be able to work well with data structures; you might not be able to evaluate the complexity of an algorithm; you might not have mastered in depth the concepts of object-oriented programming including UML and design patterns ; and you might not be acquainted with the best practices for writing high-quality programming code.

Save my name, email, and website in this browser for the next time I comment. Notify me of follow-up comments by email. Notify me of new posts by email. This site uses Akismet to reduce spam. Learn how your comment data is processed. Programmer Books. The loop continues as long as the expression is TRUE. You need to make sure that the expression will stop at some point otherwise it will become an infinite loop. The w hile loop is suitable in cases where the exact number of repetitions is not known in advance.

Hello World! In Program In such cases use of w hile loop is desirable than the for loop. In program The conditions inside the w hile loop check whether the counter is not equal to zero num!

Next we need to make sure that the program loops only 5 times. That is achieved by decrementing the counter n u m - - at each round. If the counter is not decremented the program will loop forever. Modify Program The only difference between the do- w hile loop and other loops is that in the do- w hile loop the condition comes after the statement s.

User can enter any number of prices and the program will add them up. It will terminate only if zero or any negative number is entered.

In order to calculate the total or terminate the program there should be at least one input from the keyboard. Therefore in this type of a program do- w hile loop is recommended than the w hile loop. Then based on the user selection it should display the correct message.

After displaying the correct message it should again display the menu. The program should exit when the user enter 0. In summary, the for loop is recommended for cases where the number of repetitions is known in advance.

The w hile loop is recommended for cases where the number of repetitions are unknown or unclear during the development process. The do- w hile loop is recommended for cases where the loop to be executed needs to run at least once regardless of the condition.

However, each type of loop can be interchanged with the other two types by including proper control mechanisms. You can nest loops of any kind inside another to any depth you want. However having large number of nesting will reduce the readability of your source code. However the pr int f function does not support displaying text on a row that you have already printed. Therefore first you need to fully complete the first row and then you should go to the next. Therefore the loop which handles printing of individual rows should be the outer loop and one which prints elements within a row columns should be the inner loop.

The control will be transferred to the first statement following the loop block. If you have nested loops, then the break statement inside one loop transfers the control to the immediate outer loop. The break statement can be used to terminate an infinite loop or to force a loop to end before its normal termination. Notice that in this example the for loop is written as a decrement rather than an increment.

At this point the loop will terminate because of the br e a k keyword. The program should allow users to terminate the program at any time by pressing any key before it displays all the messages. The k bhit function is defined in the header file con io. Consider the following example. However a number should not be divided by 0. It takes the following form: e x it int e x it - code The exit-code is used by the operating systems and may also be used by the calling program.

By convention, an exit-code of 0 indicates a normal exit where as any other value indicates an abnormal exit or an error. If a program is to be terminated before the r e t u r n 0 ; statement in within the m a in function following code can be used: exit 0 ; Exercise 5. The program should display all the sine values from 0 to degrees at 5 degrees increments and it should display only 20 rows at a time.

Using an array we can store five values of type int with a single identifier without having to declare five different variables with a different identifier. Arrays are useful when you store related data items, such as grades received by the students, sine values of a series of angles, etc. Like any other variable in C an array must be declared before it is used. The typical declaration of an array is: da t a - t ype a r r a y- na m e [ no- of- e le m e nt s] ; Notice that the array name must not be separated from the square brackets containing the index.

When declaring an array, the number of array elements should be constant. As arrays are blocks of static memory locations of a given size, the compiler must be able to determine exactly how much memory to allocate at compilation time.

The following array can hold marks for five subjects. Then in the second loop it computes total of all marks stored in the array and finally it computes the average. The array that was used in Program It can be extended to store marks of many students using a two- dimensional array. Such an array is declared in the following form: da t a - t ype a r r a y- na m e [ size - 1 ] [ size - 2 ] ; You can declare an array to hold marks of students with store marks of 5 subjects as in the following example: int students[][5]; The first index defines the number of students and the second index defines the number of subjects.

Initialising marks of the first student can be performed in the following manner. A two-dimensional array is initialised in the same way. The following statement declares and initialises a two-dimensional array of type int which holds the scores of three students in five different tests. Calculate the average of each students marks and the average of marks taken by all the students. Therefore functions can be classified as built-in and user defined. A modular program is usually made up of different functions, each one accomplishing a specific task such as calculating the square root or the factorial.

In general a modular program consists of the m a in function followed by set of user defined functions as given below: include …… de fine …..

It starts with the include directive, followed by the de fine directive if any then followed by the prototypes of functions. The prototype is a declaration of a function used in the program. Then comes the program building block which includes the m a in function and implementation of the user defined functions. Some functions may accept certain input parameters such as pr int f , some may return a value of a certain data type such as k bhit and some may accept as well as return a value sqr t.

Every C program has at least one function, the m a in. When a program is executed the m a in is called automatically. The m a in may call other functions and some of them might call other functions.

Each function has a unique name and when the name is encountered while in execution the control of the program is transferred to the statement s within the function. When the function is ended returns the control is resumed to the next statement following the function call. Well designed functions perform a specific and easily understood task. Complicated tasks should be broken down into multiple functions and then each can be called in the proper order.

Later you can implement the function. The prototype tells the compiler in advance about some characteristics name of the function, return type and parameters of a function. The function name is any legal identifier followed by the function parenthesis without any space in between.

If the function does not return a value, the data type is defined as void. The arguments or parameters come inside the parenthesis, preceded by their types and separated by the commas. If the function does not any parameters, it is either kept blank or the word void is used inside the parenthesises. Following are some examples for prototypes: void exit int x ; int kbhit ; double power double x, double y ; The first function returns no value and it takes an integer argument.

Second prototype returns an integer value and has no inputs. Third function returns a double value and takes two double values. It starts with function header, which is the same as the function prototype but does not end with a semicolon.

The function prototype and the definition must have exactly the same return type, name and parameter list. If they do not match the compiler will issue error messages during compilation.

Example 7. Implement calculation of circumference and areas as separate functions. As given below: float area int r ; float area float r ; Both these functions calculate the area of a circle given the radius. However the first function accepts an integer as the input while the second function accepts a floating point number.

Based on the type of the given input the program dynamically calls the correct function. The expression a r e a floa t r a dius ; converts the integer value to a floating point number this process is called casting before calling the function. Although you can have multiple functions with the same name but with different input parameters, you cannot have functions with the same name with a different output parameter.

It is also possible to have the same name for functions with different behaviour with different input parameters, however best practices suggest that no two functions should have the same name unless they perform identical tasks. In order to avoid errors when using functions, you have to have a clear understanding about the mechanism of passing variables from one function to another.

Variables declared within a block are scoped only to that block; they can be accessed only within that block and go out of existence when the execution of the block is completed. A variable declared inside a function is called a local variable. Scope of a local variable is limited to the function. Such variables are not seen by any other functions including the m a in function.

When you pass a variable to a function such as the variable r a dius in Program This means that the value of the passed variable cannot be changed by any other function. Even if you use another variable with the same name in the function, you still have two local variables isolated from each other.

On the other hand you can also define global variables which are accessible from any function within the same source file.

The global variable can be defined within the program but anywhere outside function block including the m a in function. You can modify its value in either place and read it from another place. A well written C source code should not have any global variables unless they are specifically required since an accidental change to a global variable may produce erroneous results.

The value passed must be of the declared type. If the function definition differs or if you pass a value of a wrong data type you will get a compilation error. Similarly a function with many input parameters can have a default value for each parameter.

Exercise 7. Although it may appear a little confusing for a novice programmer they are a powerful tool and handy to use once they are mastered. The power of C compared to most other languages lies with proper use of pointers. Computers use memory to store both instructions and values of variables of a program. Each of these memory cells has an address associated with it. Whenever a variable is declared the system allocates some memory to hold the value of the variable.

Such memory location can be accessed by providing the memory address. Suppose the address of that memory location is Then after executing above expression the memory address should hold Since memory addresses are simple numbers, they can also be assigned to some variables.

Such variables that hold memory addresses are called pointers. Therefore a pointer is nothing but a variable that contains an address which is a location of another variable in memory.

Therefore in computation even the address of the pointer can be used. The location of a variable in memory is system dependent and therefore the address of a variable is not known directly. The fourth pr int f function displays the address of the pointer. Example 8. Strings in C are handled differently than most other languages. A pointer is used to keep track of a text string stored in memory.

It will point to the first character of the string. By knowing the beginning address and the length of the string, the program can locate it. Pointers can be used to manipulate arrays rather than using an index. The name of an array points to the first element of the array. Using files you can save your output data permanently and retrieve them later. A file in general is a collection of related records, such as student information, marks obtained in an exam, employee salaries, etc.

A connection can be established using the fope n function. Table 9. If the file does not exist create a new one. If the file exists it will be overwritten. New data will be added to the end of the file. When you open a file it would be better to make sure that the 6 Structures are used to store records with different data types.

If the establishment of a connection is successful the function returns a pointer to the file. If an error is encountered while establishing a connection the functions returns N ULL. When all the file processing is over the connection should be closed.

Closing the connection is important as it writes any remaining data in the buffer to the output file. The function fclose is used to close the file. When a file is successfully closed the function fclose returns a ze r o and any other value indicates an error. Reading Characters from a File To read one character at a time you can use the ge t c function.

Example 9. This is my first file In Program Then the expression if fp! Then the program continues to read the rest of the characters in the file until it finds the EOF mark. Afterwards the connection to the file is closed using the fclose function. Reading a String from a File In real-life applications it is more useful to read one string at a time rather than one character.

With every read, the program has to check for the line feed LF character so it can find the end of each string. Also it must check for the EOF mark which comes at the end of the file.

The fge t s function can be used to read a string at a time. The function fge t s returns a ch a r pointer. One deficiency in fge t s is that it can only read to a fixed character buffer, therefore you need to know in advance the maximum number of characters in a string. Suppose the file does not have more than characters in a line. Writing Character to a File To write a character to a file the pu t c function can be used. It has the form: put c c, fp where c is the character while fp is the file pointer.

It returns an int value which indicates the success or the failure of the function. It returns the int value of the character if it is successful, if not it returns EOF mark. The function pu t c is used to write characters in the message to the file. A pointer is used to point to the string and the pointer is incremented by one memory location at a time.

However sometimes you may want to write one string at a time. Two functions, namely fpu t s and fpr in t f can be used for this purpose. The fpr in t f function is identical to the pr int f function only difference being that it writes to a file rather than to the screen. Exercise 9. The program should have a menu similar to the following: Menu 1.

Add new friend. Display contact info. The process of compiling and linking depends on the environment in which it is carried out, the particular compiler or the linker involved and tools used if any. Integrated Development Environments IDEs are software tools which provide facilities for; editing, compiling, linking and debugging software will cover in Lab5.

No other tools will be used. When you invoke gcc, it normally does pre-processing, compilation, assembly and linking. The overall options allow you to stop this process at an intermediate stage.

For example, the - c option says not to run the linker. Then the output consists of object files output by the assembler. Step 1: Type the C program given below using text editor such as Kw r it e and then save it as t e st 1.

Now you can enter any Linux shell command using the shell. Use the ls command to see whether a. Some of these errors result in source code that does not confirm to the syntax of the programming language and are caught by the compiler at compile time.

Some others however, surface only at run- time and cause programs to fail at performing their intended tasks. Such defects in programs are called bugs and the process of eliminating them is called debugging. The text below also shows what you may expect next! Notice that we are not trying to link the output of the compiler yet.

This is because of the errors in the source code. Typically, there will be two types of messages reported errors and warnings. Errors, are cases where the supplied source code does not confirm to the syntax of the language. Warnings on the other hand indicate bad coding practices on part of the programmer such as unused variables. The way errors are reported depends on the compiler and the IDE used.

Notice that for each syntax error, the compiler reports the file name and the line number where it found the error. Step 3: Lets, try to correct some of the errors reported above.

Notice that most of the error messages are concerned with line 8. Open the file t e st 2. You may also want to take in to consideration the following warning: test2. If you corrected the error on line 8 properly, you should now get set of messages similar to the following: test2. After correcting the error, go back to command mode and locate the cursor at the point of first error, i.

To correct it, modify the line 10 to be like as shown below. If you correctly followed all the above instructions, you should not get any more error messages.

To obtain an executable program, this file must be linked with any necessary libraries using an appropriate linker. Normally, any C program needs to be linked with the standard C library and this is usually done automatically without you having to specify it explicitly. However any other libraries must be specified to the linker if they are to be linked with your program.

To link the object file t e st 2. The cause of the problem is the sqr t function. Whenever, any mathematical functions i. Press En t e r key after typing the three values for the coefficients a , b and c.

Such sets of values used for testing a program are known as test cases. Step 6: When developing software, syntax errors are probably the easiest to detect and eliminate. Referring to the previous step, did the program work correctly? If you tried a test case where a is not equal to 1, you would notice that it fails.

Detecting bugs in programs is not an easy task. Now, go ahead and correct the expression on your own. Once you compile and link the program, try the test case s for which the program failed earlier and see whether it works correctly now. Step 8: Next, try the following test case against your program and see how it behaves when a is equal to zero. Looking at the source code, it is clearly evident that if the user enters a value of 0 for a, this leads to a division by zero on later in the program lines 17 and One possible solution is to check the value the user entered for coefficient a using an if statement.

Since we check before we divide, even if the user enters a value of zero, it does not lead to a division by zero. Are all languages compiler based like C?

Explain the difference between a compiler error and a warning? What is a bug with reference to software? Does program testing show the presence of bugs or their absence? Explain your answer. Given that you have the C source file test3. To compile compile only the file using gcc. What are the output files produced by this command?

To compile and link the file with the standard C library. What is the name of the executable produced? Modify the command in c to change the name of the executable file to test from its default name. To link the object file produced in a above, with the math library in addition to the standard C library. This book integrates the classic fields of mechanics - statics, dynamics, and strength of materials - using examples from biology and medicine.

The book is excellent for teaching either undergraduates in biomedical engineering programs or health care professionals studying biomechanics at the graduate level. Extensively revised from a successful th Foundations of Analytical Chemistry. This book offers a completely new approach to learning and teaching the fundamentals of analytical chemistry.

It summarizes basic concepts of the field on the basis of slides. Each of the nine chapters offers the following features: - Introduction: Summary. General scheme. Teaching objectives.



0コメント

  • 1000 / 1000