Computer Programming – Useful Resources ”; Previous Next The following resources contain additional information on Computer Programming. Please use them to get more in-depth knowledge on this topic. Computer Networks Online Course 107 Lectures 8 hours Tutorialspoint More Detail Computer Fundamentals Online Training 47 Lectures 2.5 hours Tutorialspoint More Detail Computer Network Basics 20 Lectures 2 hours TELCOMA Global More Detail Print Page Previous Next Advertisements ”;
Category: Computer Programming
Computer Programming – Functions ”; Previous Next A function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing. You have already seen various functions like printf() and main(). These are called built-in functions provided by the language itself, but we can write our own functions as well and this tutorial will teach you how to write and use those functions in C programming language. Good thing about functions is that they are famous with several names. Different programming languages name them differently, for example, functions, methods, sub-routines, procedures, etc. If you come across any such terminology, then just imagine about the same concept, which we are going to discuss in this tutorial. Let”s start with a program where we will define two arrays of numbers and then from each array, we will find the biggest number. Given below are the steps to find out the maximum number from a given set of numbers − 1. Get a list of numbers L1, L2, L3….LN 2. Assume L1 is the largest, Set max = L1 3. Take next number Li from the list and do the following 4. If max is less than Li 5. Set max = Li 6. If Li is last number from the list then 7. Print value stored in max and come out 8. Else prepeat same process starting from step 3 Let”s translate the above program in C programming language − Live Demo #include <stdio.h> int main() { int set1[5] = {10, 20, 30, 40, 50}; int set2[5] = {101, 201, 301, 401, 501}; int i, max; /* Process first set of numbers available in set1[] */ max = set1[0]; i = 1; while( i < 5 ) { if( max < set1[i] ) { max = set1[i]; } i = i + 1; } printf(“Max in first set = %dn”, max ); /* Now process second set of numbers available in set2[] */ max = set2[0]; i = 1; while( i < 5 ) { if( max < set2[i] ) { max = set2[i]; } i = i + 1; } printf(“Max in second set = %dn”, max ); } When the above code is compiled and executed, it produces the following result − Max in first set = 50 Max in second set = 501 If you are clear about the above example, then it will become easy to understand why we need a function. In the above example, there are only two sets of numbers, set1 and set2, but consider a situation where we have 10 or more similar sets of numbers to find out the maximum numbers from each set. In such a situation, we will have to repeat, processing 10 or more times and ultimately, the program will become too large with repeated code. To handle such situation, we write our functions where we try to keep the source code which will be used again and again in our programming. Now, let”s see how to define a function in C programming language and then in the subsequent sections, we will explain how to use them. Defining a Function The general form of a function definition in C programming language is as follows − return_type function_name( parameter list ) { body of the function return [expression]; } A function definition in C programming consists of a function header and a function body. Here are all the parts of a function − Return Type − A function may return a value. The return_type is the data type of the value the function returns. Some functions perform the desired operations without returning a value. In this case, the return_type is the keyword void. Function Name − This is the actual name of the function. The function name and the parameter list together constitute the function signature. Parameter List − A parameter is like a placeholder. When a function is invoked, you pass a value as a parameter. This value is referred to as the actual parameter or argument. The parameter list refers to the type, order, and number of the parameters of a function. Parameters are optional; that is, a function may contain no parameters. Function Body − The function body contains a collection of statements that defines what the function does. Calling a Function While creating a C function, you give a definition of what the function has to do. To use a function, you will have to call that function to perform a defined task. Now, let”s write the above example with the help of a function − Live Demo #include <stdio.h> int getMax( int set[] ) { int i, max; max = set[0]; i = 1; while( i < 5 ) { if( max < set[i] ) { max = set[i]; } i = i + 1; } return max; } main() { int set1[5] = {10, 20, 30, 40, 50}; int set2[5] = {101, 201, 301, 401, 501}; int max; /* Process first set of numbers available in set1[] */ max = getMax(set1); printf(“Max in first set = %dn”, max ); /* Now process second set of numbers available in set2[] */ max = getMax(set2); printf(“Max in second set = %dn”, max ); } When the above code is compiled and executed, it produces the following result − Max in first set = 50 Max in second set = 501 Functions in Java If you are clear about functions in C programming, then it is easy to understand them in Java as well. Java programming names them as methods, but the rest of the concepts remain more or less same. Following is the equivalent program written in Java. You can try to execute it to see the output − Live Demo public class DemoJava { public static void main(String []args) { int[] set1 = {10, 20, 30, 40, 50}; int[] set2 = {101, 201, 301,
Programming – Quick Guide
Computer Programming – Quick Guide ”; Previous Next Computer Programming – Overview Introduction to Computer Program Before getting into computer programming, let us first understand computer programs and what they do. A computer program is a sequence of instructions written using a Computer Programming Language to perform a specified task by the computer. The two important terms that we have used in the above definition are − Sequence of instructions Computer Programming Language To understand these terms, consider a situation when someone asks you about how to go to a nearby KFC. What exactly do you do to tell him the way to go to KFC? You will use Human Language to tell the way to go to KFC, something as follows − First go straight, after half kilometer, take left from the red light and then drive around one kilometer and you will find KFC at the right. Here, you have used English Language to give several steps to be taken to reach KFC. If they are followed in the following sequence, then you will reach KFC − 1. Go straight 2. Drive half kilometer 3. Take left 4. Drive around one kilometer 5. Search for KFC at your right side Now, try to map the situation with a computer program. The above sequence of instructions is actually a Human Program written in English Language, which instructs on how to reach KFC from a given starting point. This same sequence could have been given in Spanish, Hindi, Arabic, or any other human language, provided the person seeking direction knows any of these languages. Now, let”s go back and try to understand a computer program, which is a sequence of instructions written in a Computer Language to perform a specified task by the computer. Following is a simple program written in Python programming Language − print “Hello, World!” The above computer program instructs the computer to print “Hello, World!” on the computer screen. A computer program is also called a computer software, which can range from two lines to millions of lines of instructions. Computer program instructions are also called program source code and computer programming is also called program coding. A computer without a computer program is just a dump box; it is programs that make computers active. As we have developed so many languages to communicate among ourselves, computer scientists have developed several computer-programming languages to provide instructions to the computer (i.e., to write computer programs). We will see several computer programming languages in the subsequent chapters. Introduction to Computer Programming If you understood what a computer program is, then we will say: the act of writing computer programs is called computer programming. As we mentioned earlier, there are hundreds of programming languages, which can be used to write computer programs and following are a few of them − Java C C++ Python PHP Perl Ruby Uses of Computer Programs Today computer programs are being used in almost every field, household, agriculture, medical, entertainment, defense, communication, etc. Listed below are a few applications of computer programs − MS Word, MS Excel, Adobe Photoshop, Internet Explorer, Chrome, etc., are examples of computer programs. Computer programs are being used to develop graphics and special effects in movie making. Computer programs are being used to perform Ultrasounds, X-Rays, and other medical examinations. Computer programs are being used in our mobile phones for SMS, Chat, and voice communication. Computer Programmer Someone who can write computer programs or in other words, someone who can do computer programming is called a Computer Programmer. Based on computer programming language expertise, we can name a computer programmers as follows − C Programmer C++ Programmer Java Programmer Python Programmer PHP Programmer Perl Programmer Ruby Programmer Algorithm From programming point of view, an algorithm is a step-by-step procedure to resolve any problem. An algorithm is an effective method expressed as a finite set of well-defined instructions. Thus, a computer programmer lists down all the steps required to resolve a problem before writing the actual code. Following is a simple example of an algorithm to find out the largest number from a given list of numbers − 1. Get a list of numbers L1, L2, L3….LN 2. Assume L1 is the largest, Largest = L1 3. Take next number Li from the list and do the following 4. If Largest is less than Li 5. Largest = Li 6. If Li is last number from the list then 7. Print value stored in Largest and come out 8. Else repeat same process starting from step 3 The above algorithm has been written in a crude way to help beginners understand the concept. You will come across more standardized ways of writing computer algorithms as you move on to advanced levels of computer programming. Computer Programming – Basics We assume you are well aware of English Language, which is a well-known Human Interface Language. English has a predefined grammar, which needs to be followed to write English statements in a correct way. Likewise, most of the Human Interface Languages (Hindi, English, Spanish, French, etc.) are made of several elements like verbs, nouns, adjectives, adverbs, propositions, and conjunctions, etc. Similar to Human Interface Languages, Computer Programming Languages are also made of several elements. We will take you through the basics of those elements and make you comfortable to use them in various programming languages. These basic elements include − Programming Environment Basic Syntax Data Types Variables Keywords Basic Operators Decision Making Loops Numbers Characters Arrays Strings Functions File I/O We will explain all these elements in subsequent chapters with examples using different programming languages. First, we will try to understand the meaning of all these terms in general and then, we will see how these terms can be used in different programming languages. This tutorial has been designed to give you an idea about the following most popular programming languages − C Programming Java Programming Python Programming A major part of the tutorial has been explained by
Computer Programming – Overview ”; Previous Next Introduction to Computer Program Before getting into computer programming, let us first understand computer programs and what they do. A computer program is a sequence of instructions written using a Computer Programming Language to perform a specified task by the computer. The two important terms that we have used in the above definition are − Sequence of instructions Computer Programming Language To understand these terms, consider a situation when someone asks you about how to go to a nearby KFC. What exactly do you do to tell him the way to go to KFC? You will use Human Language to tell the way to go to KFC, something as follows − First go straight, after half kilometer, take left from the red light and then drive around one kilometer and you will find KFC at the right. Here, you have used English Language to give several steps to be taken to reach KFC. If they are followed in the following sequence, then you will reach KFC − 1. Go straight 2. Drive half kilometer 3. Take left 4. Drive around one kilometer 5. Search for KFC at your right side Now, try to map the situation with a computer program. The above sequence of instructions is actually a Human Program written in English Language, which instructs on how to reach KFC from a given starting point. This same sequence could have been given in Spanish, Hindi, Arabic, or any other human language, provided the person seeking direction knows any of these languages. Now, let”s go back and try to understand a computer program, which is a sequence of instructions written in a Computer Language to perform a specified task by the computer. Following is a simple program written in Python programming Language − print “Hello, World!” The above computer program instructs the computer to print “Hello, World!” on the computer screen. A computer program is also called a computer software, which can range from two lines to millions of lines of instructions. Computer program instructions are also called program source code and computer programming is also called program coding. A computer without a computer program is just a dump box; it is programs that make computers active. As we have developed so many languages to communicate among ourselves, computer scientists have developed several computer-programming languages to provide instructions to the computer (i.e., to write computer programs). We will see several computer programming languages in the subsequent chapters. Introduction to Computer Programming If you understood what a computer program is, then we will say: the act of writing computer programs is called computer programming. As we mentioned earlier, there are hundreds of programming languages, which can be used to write computer programs and following are a few of them − Java C C++ Python PHP Perl Ruby Uses of Computer Programs Today computer programs are being used in almost every field, household, agriculture, medical, entertainment, defense, communication, etc. Listed below are a few applications of computer programs − MS Word, MS Excel, Adobe Photoshop, Internet Explorer, Chrome, etc., are examples of computer programs. Computer programs are being used to develop graphics and special effects in movie making. Computer programs are being used to perform Ultrasounds, X-Rays, and other medical examinations. Computer programs are being used in our mobile phones for SMS, Chat, and voice communication. Computer Programmer Someone who can write computer programs or in other words, someone who can do computer programming is called a Computer Programmer. Based on computer programming language expertise, we can name a computer programmers as follows − C Programmer C++ Programmer Java Programmer Python Programmer PHP Programmer Perl Programmer Ruby Programmer Algorithm From programming point of view, an algorithm is a step-by-step procedure to resolve any problem. An algorithm is an effective method expressed as a finite set of well-defined instructions. Thus, a computer programmer lists down all the steps required to resolve a problem before writing the actual code. Following is a simple example of an algorithm to find out the largest number from a given list of numbers − 1. Get a list of numbers L1, L2, L3….LN 2. Assume L1 is the largest, Largest = L1 3. Take next number Li from the list and do the following 4. If Largest is less than Li 5. Largest = Li 6. If Li is last number from the list then 7. Print value stored in Largest and come out 8. Else repeat same process starting from step 3 The above algorithm has been written in a crude way to help beginners understand the concept. You will come across more standardized ways of writing computer algorithms as you move on to advanced levels of computer programming. Print Page Previous Next Advertisements ”;
Tk – Special Variables
Tk – Special Variables ”; Previous Next In Tk, we classify some of the variables as special variables and they have a predefined usage/functionality. The list of special variables is listed below. Sr.No. Special Variable & Description 1 tk_library Used for setting the location of standard Tk libraries. 2 tk_patchLevel Refers to the current patch level of the Tk interpreter. 3 tk_strictMotif When non-zero, Tk tries to adhere to Motif look-and-feel as closely as possible. 4 tk_version Displays the Tk version. The above special variables have their special meanings for the Tk interpreter. Examples for using Tk special variables Lets see the examples for special variables. TK VERSION #!/usr/bin/wish puts $tk_version When you run the program, you will get a similar output as shown below. 8.5 TK LIBRARY PATH #!/usr/bin/wish puts $tk_library When you run the program, you will get a similar output as shown below. /Library/Frameworks/Tk.framework/Versions/8.6/Resources/Scripts TK PATCH LEVEL #!/usr/bin/wish puts $tk_patchLevel When you run the program, you will get a similar output as shown below. 8.6.1 TK STRICTMOTIF #!/usr/bin/wish puts $tk_strictMotif When you run the program, you will get a similar output as shown below. 0 Print Page Previous Next Advertisements ”;
WebAssembly – Modules
WebAssembly – Modules ”; Previous Next We have seen how to get a .wasm file from c /c++ code. In this chapter, we will convert the wasm into a WebAssembly module and execute the same in the browser. Let us use the C++ Factorial code as shown below − int fact(int n) { if ((n==0)||(n==1)) return 1; else return n*fact(n-1); } Open Wasm Explorer which is available at https://mbebenita.github.io/WasmExplorer/ as shown below − The first column has the C++ factorial function, the 2nd column has the WebAssembly text format and the last column has x86 Assembly code. The WebAssembly Text format − (module (table 0 anyfunc) (memory $0 1) (export “memory” (memory $0)) (export “_Z4facti” (func $_Z4facti)) (func $_Z4facti (; 0 😉 (param $0 i32) (result i32) (local $1 i32) (set_local $1 (i32.const 1) ) (block $label$0 (br_if $label$0 (i32.eq (i32.or (get_local $0) (i32.const 1) ) (i32.const 1) ) ) (set_local $1 (i32.const 1) ) (loop $label$1 (set_local $1 (i32.mul (get_local $0) (get_local $1) ) ) (br_if $label$1 (i32.ne (i32.or (tee_local $0 (i32.add (get_local $0) (i32.const -1) ) ) (i32.const 1) ) (i32.const 1) ) ) ) ) (get_local $1) ) ) The C++ function fact has been exported as “_Z4facti” in WebAssembly Text format. Click on the download button to download the wasm code and save the file as factorial.wasm. Now to convert the .wasm code to the module we have to do the following − Step 1 Convert the .wasm into arraybuffer by using ArrayBuffer. The ArrayBuffer object will return you a fixed-length binary data buffer. Step 2 The bytes from ArrayBuffer have to be compiled into a module by using WebAssembly.compile(buffer) function. The WebAssembly.compile() function compiles and returns a WebAssembly.Module from the bytes given. Here, is the Javascript code that is discussed in Step 1 and 2. <script type=”text/javascript”> let factorial; fetch(“factorial.wasm”) .then(bytes => bytes.arrayBuffer()) .then(mod => WebAssembly.compile(mod)) .then(module => {return new WebAssembly.Instance(module) }) .then(instance => { factorial = instance.exports._Z4facti; console.log(”Test the output in Brower Console by using factorial(n)”); }); </script> Code Explanation Javascript browser API fetch is used to get the contents of factorial.wasm. The content is converted to bytes using arrayBuffer(). The module is created from bytes by calling WebAssembly.compile(mod). The instance of a module is created using new WebAssembly.Instance(module) The factorial function export _Z4facti is assigned to variable factorial by using WebAssembly.Module.exports(). Example Here, is the module.html along with the javascript code − module.html <!doctype html> <html> <head> <meta charset=”utf-8″> <title>WebAssembly Module</title> </head> <body> <script> let factorial; fetch(“factorial.wasm”) .then(bytes => bytes.arrayBuffer()) .then(mod => WebAssembly.compile(mod)) .then(module => {return new WebAssembly.Instance(module) }) .then(instance => { factorial = instance.exports._Z4facti; console.log(”Test the output in Browser Console by using factorial(n)”); }); </script> </body> </html> Output Execute module.html in the browser to see the output − Print Page Previous Next Advertisements ”;
WebAssembly – WASM
WebAssembly – WASM ”; Previous Next WebAssembly is also called wasm, which is an improvement to Javascript. It is designed to run inside browsers just like javascript and also with nodejs. You happen to get wasm output, when any high level language like C, C++, Rust is compiled. Consider the following C program − int factorial(int n) { if (n == 0) return 1; else return n * factorial(n-1); } Make use of WasmExplorer, which is available at https://mbebenita.github.io/WasmExplorer/ to get the compiled code as shown below − The WebAssembly text format for factorial program is as stated below − (module (table 0 anyfunc) (memory $0 1) (export “memory” (memory $0)) (export “factorial” (func $factorial)) (func $factorial (; 0 😉 (param $0 i32) (result i32) (local $1 i32) (local $2 i32) (block $label$0 (br_if $label$0 (i32.eqz (get_local $0) ) ) (set_local $2 (i32.const 1) ) (loop $label$1 (set_local $2 (i32.mul (get_local $0) (get_local $2) ) ) (set_local $0 (tee_local $1 (i32.add (get_local $0) (i32.const -1) ) ) ) (br_if $label$1 (get_local $1) ) ) (return (get_local $2) ) ) (i32.const 1) ) ) Using the Wat2Wasm tool, you can view the WASM code, just like how it is mentioned below − Developers are not supposed to write code in wasm or learn to code in it, as it is mostly generated when you compile high level languages. Stack Machine Model In WASM, all the instructions are pushed on to the stack. The arguments are popped and the result is pushed back to the stack. Consider the following WebAssembly Text format that adds 2 numbers − (module (func $add (param $a i32) (param $b i32) (result i32) get_local $a get_local $b i32.add ) (export “add” (func $add)) ) The name of the function is $add, it takes in 2 params $a and $b. The result is a type 32-bit integer. The local variables are accessed using get_local and the add operation is performed using i32.add. The stack representation to add 2 numbers while execution will be as follows − In step 1 − The execution of get_local $a instruction, the first parameters i.e., $a is pushed on the stack. In step 2 − During execution of get_local $b instruction, the second parameters i.e., $b is pushed on the stack. In step 3 − The execution of i32.add will pop the elements from the stack and will push the result back to the stack. The value that remains in the end inside the stack is the result of the function $add. Print Page Previous Next Advertisements ”;
WebAssembly – Installation
WebAssembly – Installation ”; Previous Next In this chapter, will learn how to install Emscripten SDK to compile C/C++. Emscripten is a Low level virtual machine (LLVM) that takes bytecode generated from C/C++ and compiles it into JavaScript that can easily execute inside the browser. To compile C/C++ to WebAssembly, we need to first install Emscripten sdk. Install Emscripten sdk The steps to install Emscripten sdk are as follows − Step 1 − Clone the emsdk repo : git clone https://github.com/emscripten-core/emsdk.git. E:wa>git clone https://github.com/emscripten-core/emsdk.git Cloning into ”emsdk”… remote: Enumerating objects: 14, done. remote: Counting objects: 100% (14/14), done. remote: Compressing objects: 100% (12/12), done. remote: Total 1823 (delta 4), reused 4 (delta 2), pack-reused 1809 receiving obje cts: 99% (1819/1823), 924.01 KiB | 257.00 KiB/s Receiving objects: 100% (1823/1823), 1.01 MiB | 257.00 KiB/s, done. Resolving deltas: 100% (1152/1152), done. Step 2 − Enter inside the directory emsdk. cd emsdk Step 3 − For windows: Execute following command. emsdk install latest For linux, this command will take some time to install the necessary tools like java, python etc. Follow the below mentioned code − ./emsdk install latest Step 4 − To activate latest SDK execute following command in your terminal. For windows, execute the following command − emsdk activate latest For linux, execute the below mentioned command − ./emsdk activate latest Step 5 − To activate PATH and other environment variables run following command in your terminal. For windows, execute the command − emsdk_env.bat For linux, execute the following command − source ./emsdk_env.sh We are done installing the emsdk and can now compile C or C++ code. The compiling of C/C++ will be done in the next chapters. To compile any C or C++ code following is the command − emcc source.c or source.cpp -s WASM=1 -o source.html The output will give you a source.html file, source.js and source.wasm files. The js will have the api that will fetch the source.wasm and you can see the output, when you hit source.html in the browser. To just get the wasm file you can use following command. This command will give you only source.wasm file. emcc source.c or source.cpp -s STANDALONE_WASM Print Page Previous Next Advertisements ”;
Tk – Layout Widgets
Tk – Layout Widgets ”; Previous Next Layout widgets are used to handle layouts for the Tk application. Frame widget is used group other widgets and place, pack, and grid are layout manager to give you total control over your adding to windows. The list of available layout widgets are as shown below − Sr.No. Widgets & Description 1 Frame Container widget to hold other widgets. 2 Place Widget to hold other widgets in specific place with coordinates of its origin and an exact size. 3 Pack Simple widget to organize widgets in blocks before placing them in the parent widget. 4 Grid Widget to nest widgets packing in different directions. A simple Tk example is shown below for layout widgets − #!/usr/bin/wish frame .myFrame1 -background red -relief ridge -borderwidth 8 -padx 10 -pady 10 -height 100 -width 100 frame .myFrame2 -background blue -relief ridge -borderwidth 8 -padx 10 -pady 10 -height 100 -width 50 pack .myFrame1 pack .myFrame2 When we run the above program, we will get the following output − Print Page Previous Next Advertisements ”;
Tk – Canvas Widgets
Tk – Canvas Widgets ”; Previous Next Canvas is used for providing drawing areas. The syntax for canvas widget is shown below − canvas canvasName options Options The options available for the canvas widget are listed below in the following table − Sr.No. Syntax & Description 1 -background color Used to set background color for widget. 2 -closeenough distance Sets the closeness of mouse cursor to a displayable item. The default is 1.0 pixel. This value may be a fraction and must be positive. 3 -scrollregion boundingBox The bounding box for the total area of this canvas. 4 -height number Used to set height for widget. 5 -width number Sets the width for widget. 6 -xscrollincrement size The amount to scroll horizontally when scrolling is requested. 7 -yscrollincrement size The amount to scroll vertically when scrolling is requested. A simple example for canvas widget is shown below − #!/usr/bin/wish canvas .myCanvas -background red -width 100 -height 100 pack .myCanvas When we run the above program, we will get the following output − Widgets for Drawing in Canvas The list of the available widgets for drawing in canvas is listed below − Sr.No. Widget & Description 1 Line Draws a line. 2 Arc Draws an arc. 3 Rectangle Draws a rectangle. 4 Oval Draws an oval. 5 Polygon Draws a polygon. 6 Text Draws a text. 7 Bitmap Draws a bitmap. 8 Image Draws an image. An example using different canvas widgets is shown below − #!/usr/bin/wish canvas .myCanvas -background red -width 200 -height 200 pack .myCanvas .myCanvas create arc 10 10 50 50 -fill yellow .myCanvas create line 10 30 50 50 100 10 -arrow both -fill yellow -smooth true -splinesteps 2 .myCanvas create oval 50 50 100 80 -fill yellow .myCanvas create polygon 50 150 100 80 120 120 100 190 -fill yellow -outline green .myCanvas create rectangle 150 150 170 170 -fill yellow .myCanvas create text 170 20 -fill yellow -text “Hello” -font {Helvetica -18 bold} .myCanvas create bitmap 180 50 -bitmap info When we run the above program, we will get the following output − Print Page Previous Next Advertisements ”;