Thursday, October 3, 2019

Programming Languages: Types and Uses

Programming Languages: Types and Uses 1. Introduction C Programming is a computer language that is structured and disciplined approach to program design. Its a good systems programming language. This is faster than java, more predictable performance and it has low level operations and it has a lot scope for bugs. This C programming has no Boolean type . It is used by using int false is zero and true is non-zero A simple C program int main()Text surrounded by /* and */ is ignored by computer and it is used to describe the program #include Preprocessor directive tells the computer to load contents of a certain file allows standard input/output operations C++ programs contain one or more functions, exactly one of which must be main and Parenthesis is used to indicate a function . int means that main returns an integer value and braces indicate a block.The bodies of all functions must be contained in braces . printf( Welcome to C!n ); Instructs computer to perform an action specifically and prints string of characters within quotes.Entire line is called a statement. All statements must end with a semicolon.- means escape character. Indicates that printf should do something out of the ordinary.n is the newline character return 0; A way to exit a function. return 0, in this case, means that the program terminated normally . Right brace } Indicates end of main has been reached. Linker When a function is called, linker locates it in the library and Inserts it into object program. If function name misspelled, linker will spot error because it cannot find function in library. Calculator This is use very frequently in everyday life by almost everyone . In this calculator we can only perform operations like addition (+), subtraction (-), division (%), multiplication(*), only integer valued functions. We get accurate values too . 2. Scientific Calculator This calculator can perform all the operations that normal calculator can do and it can also perform the operations like Trigonometry: Sine, Cosine, Tangent, Inverse^-1 Angles: DEG, DMS, Time Memories: M, K1, K2 Programs: LRN, COMP, HLT, (x) 3. C-Programming 3.1 What is c- programming ? A vocabulary and set of grammatical rules for instructing a computer to perform specific tasks . The term programming language usually refers to high-level languages, such as BASIC, C, C++, COBOL, FORTRAN, Java, and Pascal. Each language has a unique set of keywords (words that it understands) and a special syntax for organizing program instructions. Regardless of what language you use, you eventually need to convert your program into machine language so that the computer can understand it. There are two ways to do this: compile the program , interpret the program . 3.2 Programming process 3.2.1 Analyze the Problem A programmer must know what information will go into the software, how it will process the information, and what will result. All software must work with three concepts to be successful: Input: Information that comes from an external source and enters the software an. Input can come from typing on a keyboard, from records in a database, or from clicking on image with the mouse Processing: Manages information according to a piece of softwares logic. Processing is what the software does to the input it receives. This can be anything from adding a few numbers together to mapping the earths climate. Output: The information software produces after it has processed input. Output can appear on a computer screen, in a printout, or in records in a database. 3.2.2 Develop an Algorithm Algorithms are the steps needed to solve a problem using pseudocode or flowcharts. After creating an algorithm, programmers will check it. A logic error is a mistake in the way an algorithm solves a problem. Programmers check their algorithms by inputting test data and checking the logic by hand or with a calculator. 3.2.3. Document the Program: pseudocode Pseudocode uses English statements to create an outline of the necessary steps for a piece of software to operate. Programmers call these steps an algorithm. An algorithm is a set of specific steps that solves a problem or carries out a task. While there is no set of rules for writing pseudocode, it usually follows rules such as: Using simple English, Putting one command on a line, Placing any important words in bold, Starting from the top and work toward the bottom, Separating processes with spaces to form modules. 3.2.4 Write Code for the Program Code is when a programmer translates an algorithm into a programming language. 3.2.5 Run the Program Programmers also use program flowcharts to plot the softwares algorithm. A program flowchart is a graphical depiction of the detailed steps that software will perform. Unlike pseudocode, which has less structure, in flowcharts programmers must use symbols. 3.2.6 Testing the programs Debugging the process of finding errors in software code. Bugs are a common name for software errors. When programmers debug code, they look for syntax, run-time, and logic errors. Syntax errors mistakes in a software codes grammar. If you are supposed to use a semi-colon (;) and you use a colon (:) instead, you have made a syntax error. Run-time errors mistakes that occur when a programmer runs the software code . Logic errors mistake made in the way an algorithm solves a problem. 4. Types of programming 4.1 Object-oriented programming Object-oriented programming (OOP) is any programming language that uses objects to code software. An object instance is an exact copy of an object in OOP. An event-driven language responds to actions users perform on the program. Its an event when you click on a button, use a pull-down menu, or scroll down a window. In an event-driven language, each event triggers the program to action. An OOP program models the world of active objects. An object may have its own memory, which may contain other objects. An object has a set of methods that can process messages of certain types. A method can change the objects state, send messages to other objects, and create new objects. An object belongs to a particular class, and the functionality of each object is determined by its class. A programmer creates an OOP application by defining classes. 4.2 The Main OOP Concepts Inheritance: a subclass extends a superclass; the objects of a subclass inherit features of the superclass and can redefine them or add new features. Event-driven programs: the program simulates asynchronous handling of events; methods are called automatically in response to events. OOP Benefits Facilitates team development and Easier to reuse software components and write reusable software. Easier GUI (Graphical User Interface) and multimedia programming . 4.3 Web-programming Hyper Text Markup Language (HTML) is the basic language for web programming. JavaScript a scripting language that allows you to add interactivity and other features to a Web page. Java Applets A small piece of software that enables applications to run on a Web page. Dynamic HTML combines cascading style sheets, JavaScript, etc., to bring high interactivity to Web sites. VBScript an interpreted scripting language based on Visual Basic. It is similar to JavaScript but only Microsofts Internet Explorer Web browser can use it. Software Development Tools Editor: programmer writes source code for te program. Compiler: translates the source into object code (instructions specific to a particular CPU). Linker: converts one or several object modules into an executable program. Debugger: stepping through the program in slow motion, helps find logical mistakes (bugs). 5. Array Group of consecutive memory locations and Same name and type To refer to an element, specifying Array name and Position number Format: arrayname[position number] First element at position 0 and n element array named c: c[0], c[1]c[n-1] Array elements are like normal variables c[0] = 3; printf( %d, c[0] ); Perform operations in subscript. If x = 3, c[5-2] == c[3] == c[x] When declaring arrays, specify: Name, Type of array, Number of elements arrayType arrayName[ numberOfElements ]; int c[ 10 ]; float myArray[ 3284 ]; Declaring multiple arrays of same type and Format similar to regular variables int b[ 100 ], x[ 27 ]; Examples Using Arrays Initializers int n[5] = {1, 2, 3, 4, 5 }; If not enough initializers r there, rightmost elements become 0 and If too many are there then its a syntax error. int n[5] = {0} All elements is 0 C arrays have no bounds checking and If size is omitted, initializers determine it int n[] = { 1, 2, 3, 4, 5 }; 5 initializers, therefore 5 this iselement array Character arrays String hello is really a static array of characters and Character arrays can be initialized using string literals char string1[] = first; null character terminates strings string1 actually has 6 elements char string1[] = { f, i, r, s, t, }; Access individual characters string1[ 3 ] is character s Array name is address of array, so not needed for scanf scanf( %s, string2 ) ; Reads characters until whitespace encountered and Can write beyond end of arrays. 5.1 Interpretation interpreter = program that executes program statements and generally one line or command at a time for a limited processing and its easy to debug, make changes, view intermediate results. 5.2 Compilation translates statements into machine language and does not execute, but creates executable program to perform optimization over multiple statements in order to changing requires recompilation and it can be harder to debug, since executed code may be different. 5.3 Compiling a C Program Preprocessor macro substitution and conditional compilation of a source-level transformations and output is still C. 5.4 Compiler generates object file from machine instructions. Source Code Analysis is a front end parses programs to identify its pieces variables, expressions, statements, functions, etc. depending on language (not on target machine). Code Generation is a back end generating machine code from analyzed source may optimize machine code to make it run more efficiently very dependent on target machine. Symbol Table map between symbolic names and items like assembler, but more kinds of information. 5.5 Linker combine object files (including libraries) into executable image. 5.6 Sorting Arrays Sorting data is Important computing application and eventually every organization must sort some data into Massive amounts of sorted information. Bubble sort (sinking sort) has Several passes through the array and Successive pairs of elements are compared in order to increasing order (or identical ), no change is there If decreasing order is done and if elements exchanged. 6. Algorithms There are many fundamental problems that arise in engineering and other areas of application. These include sorting data, searching for specific data values, numerical integration, finding roots of functions, solving ordinary differential equations and solving systems of linear equations and We will spend about four weeks studying important algorithms for these problems. Algorithms are the steps needed to solve a problem using pseudocode or flowcharts. After creating an algorithm and its programmers check its logic. A logic error is a mistake in the way an algorithm solves a problem. Programmers check their algorithms by inputting test data and checking the logic by hand or with a calculator. 7. IDE à ¢Ãƒ ¢Ã¢â‚¬Å¡Ã‚ ¬ Integrated Development Environment The integrated development environment Combines editor, compiler, linker, debugger, other tools and Has GUI (graphical user interface), Compiles + links + runs at the click of a button Helps put together a project with several modules (source files) . Compiler: checks syntax and generates machine-code instructions so that its not needed to run the executable program to run faster . Interpreter: checks syntax and executes appropriate instructions while interpreting the program statements and must remain installed while the program is interpreted so the interpreted program is slower. Platform independent . Load from the Internet faster than source code. Interpreter is faster and smaller than it would be for Java source. Source code is not revealed to end users. Interpreter performs additional security checks, screens out malicious code. 8. Preprocessor Directives #include Before compiling, copy contents of header file (stdio.h) into source code. Then Header files typically contain descriptions of functions and variables needed by the program. no restrictions it could be any C source code #define STOP 0 Before compiling, it replaces all instances of the string STOP with the string 0 Called a macro and its Used for values that wont change during execution, but might change if the program is reused. (Must recompile) . main Function Every C program must have a function called main(). This is the code that is executed when the program is running. The code for the function lives within brackets. main() { /* code goes here */ } Variable Declarations Variables are used as names for data items. And Each variable has a type, which tells the compiler how the data is to be interpreted (and how much space it needs, etc.). int counter; int startPoint; int is a predefined integer type in C. Input and Output Variety of I/O functions in C Standard Library. And must include to use them. printf(%dn, counter); String contains characters to print and formatting directions for variables. This call says to print the variable counter as a decimal integer, followed by a linefeed (n). scanf(%d, startPoint); String contains formatting directions for looking at input. This call says to read a decimal integer and assign it to the variable start Point . Can print arbitrary expressions, not just variables printf(%dn, startPoint counter); Print multiple expressions with a single statement printf(%d %dn, counter, startPoint counter); Different formatting options: %d decimal integer %x hexadecimal integer %c ASCII character %f floating-point number Examples of C-Programming *Program to implement an array #include #include #define MAX 3 void insert ( int *, int pos, int num ) ; void del ( int *, int pos ) ; void reverse ( int * ) ; void display ( int * ) ; void search ( int *, int num ) ; void main( ) { int arr[3] ; clrscr( ) ; insert ( arr, 1, 11 ) ; insert ( arr, 2, 12 ) ; insert ( arr, 3, 13 ) ; printf ( nElements of Array: ) ; display ( arr ) ; insert ( arr, 2, 222 ) ; insert ( arr, 3, 333 ) ; printf ( nnAfter insertion: ) ; getch( ) ; } { int i ; for ( i = MAX 1 ; i >= pos ; i ) arr[i] = arr[i 1] ; arr[i] = num ; } { int i ; for ( i = pos ; i arr[i 1] = arr[i] ; arr[i 1] = 0 ; } { int i ; for ( i = 0 ; i { int temp = arr[i] ; arr[i] = arr[MAX 1 i] ; arr[MAX 1 i] = temp ; } } { int i ; for ( i = 0 ; i { if ( arr[i] == num ) { printf ( nnThe element %d is present at %dth position., num, i + 1 ) ; return ; } } if ( i == MAX ) printf ( nnThe element %d is not present in the array., num ) ; } } *Program to allocate memory dynamically for strings, and store their addresses in array of pointers to strings #include #include #include #include void main( ) { char *name[5] ; char str[20] ; int i ; clrscr( ) ; for ( i = 0 ; i { printf ( Enter a String: ) ; gets ( str ) ; name[i] = ( char * ) malloc ( strlen ( str ) + 1 ) ; strcpy ( name[i], str ) ; } printf ( nThe strings are: ) ; for ( i = 0 ; i printf ( n%s, name[i] ) ; for ( i = 0 ; i free ( name[i] ) ; getch( ) ; } 9. Conclusion and future work Conclusion Finally the conclusion is that a small study on the C-programming and how it is used to built scientific calculator. Future work The future work is that to implement scientific calculator based on the c-programming .

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.