Evolution of Programming Languages - Prof. Jaiswal, Study Guides, Projects, Research of Computer Science

This document provides a comprehensive overview of the evolution of programming languages, tracing their development from the early days of computing. It covers the progression from machine language to assembly language, procedural languages, scripting languages, and functional languages. The document also discusses the different types of errors in programming, the importance of control statements, and the advantages of using functions. It explores the differences between library functions and user-defined functions, and includes examples of C programs demonstrating the use of recursive functions and string comparison. Additionally, the document addresses the concepts of overflow and underflow errors in the context of arrays, as well as the advantages of using structures over arrays. Finally, the document provides an overview of FORTRAN, a popular programming language, and includes an example program that calculates the sum of two numbers.

Typology: Study Guides, Projects, Research

2021/2022

Uploaded on 07/29/2024

shishir-basnet
shishir-basnet 🇳🇵

1 document

1 / 19

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
2073 Shrawan
Subject: Computer Programming (CT401)
1. a) What do you mean by Programming Language? Explain about the evolution of
programming languages.
A programming language is a formal language used to write computer
programs. It provides a set of rules and syntax that allows programmers to
communicate instructions to a computer system. These instructions are then
executed by the computer to perform specific tasks or solve problems. The
evolution of programming languages can be traced back to the early days of
computing. Here is a brief overview of the major stages in the evolution of
programming languages:
i. Machine Language: In the early days of computers, programmers had to
write instructions directly in machine language, which is a low-level
language consisting of binary code (0s and 1s) that the computer can
understand. This was a tedious and error-prone process.
ii. Assembly Language: To simplify programming, assembly languages
were developed. Assembly languages use mnemonic codes to represent
machine instructions, making it easier for programmers to write code.
However, programs written in assembly language are still specific to a
particular computer architecture.
iii. High-Level Languages: High-level languages were developed to make
programming more accessible and portable across different computer
systems. These languages are closer to human language and provide a
higher level of abstraction. Examples of early high-level languages
include Fortran, COBOL, and ALGOL.
iv. Procedural Languages: Procedural languages introduced the concept of
procedures or subroutines, which allowed programmers to break down
complex programs into smaller, reusable modules. Examples of
procedural languages include C and Pascal.
v. Object-Oriented Languages: Object-oriented programming (OOP)
languages introduced the concept of objects, which encapsulate data and
behavior. OOP promotes modular and reusable code, making it easier to
manage and maintain large software systems. Examples of object-
oriented languages include C++, Java, and Python.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13

Partial preview of the text

Download Evolution of Programming Languages - Prof. Jaiswal and more Study Guides, Projects, Research Computer Science in PDF only on Docsity!

2073 Shrawan Subject: Computer Programming (CT401)

  1. a) What do you mean by Programming Language? Explain about the evolution of programming languages.  A programming language is a formal language used to write computer programs. It provides a set of rules and syntax that allows programmers to communicate instructions to a computer system. These instructions are then executed by the computer to perform specific tasks or solve problems. The evolution of programming languages can be traced back to the early days of computing. Here is a brief overview of the major stages in the evolution of programming languages: i. Machine Language: In the early days of computers, programmers had to write instructions directly in machine language, which is a low-level language consisting of binary code (0s and 1s) that the computer can understand. This was a tedious and error-prone process. ii. Assembly Language: To simplify programming, assembly languages were developed. Assembly languages use mnemonic codes to represent machine instructions, making it easier for programmers to write code. However, programs written in assembly language are still specific to a particular computer architecture. iii. High-Level Languages: High-level languages were developed to make programming more accessible and portable across different computer systems. These languages are closer to human language and provide a higher level of abstraction. Examples of early high-level languages include Fortran, COBOL, and ALGOL. iv. Procedural Languages: Procedural languages introduced the concept of procedures or subroutines, which allowed programmers to break down complex programs into smaller, reusable modules. Examples of procedural languages include C and Pascal. v. Object-Oriented Languages: Object-oriented programming (OOP) languages introduced the concept of objects, which encapsulate data and behavior. OOP promotes modular and reusable code, making it easier to manage and maintain large software systems. Examples of object- oriented languages include C++, Java, and Python.

vi. Scripting Languages: Scripting languages, such as Perl, Ruby, and JavaScript, were designed to automate tasks and provide rapid prototyping capabilities. They are often used for web development, system administration, and other scripting tasks. vii. Functional Languages: Functional programming languages, such as Lisp, Haskell, and Erlang, focus on the evaluation of mathematical functions and immutable data. They emphasize declarative programming and offer powerful tools for handling concurrency and parallelism. viii. Domain-Specific Languages (DSLs): DSLs are designed for specific domains or industries. They provide specialized syntax and features tailored to solve problems in a particular field. Examples include SQL for database queries and MATLAB for numerical computing. ix. Concurrent and Parallel Languages: With the rise of multi-core processors and distributed systems, languages like Go and Rust have emerged to provide better support for concurrent and parallel programming. These languages offer built-in mechanisms for handling concurrency and managing shared resources. The evolution of programming languages continues to this day, with new languages being developed to address emerging needs and challenges in software development. Each new language aims to improve productivity, readability, performance, and maintainability, catering to different programming paradigms and application domains. b) What are the advantages of flowchart design? Write down the guidelines to be followed to draw a flowchart.  Advantages of Flowchart: a. Flowcharts are a better way of communicating the logic of the system. b. Flowcharts act as a guide for blueprint during program designed. c. Flowcharts help in debugging process. d. With the help of flowcharts programs can be easily analyzed. e. It provides better documentation. f. Flowcharts serve as a good proper documentation. g. Easy to trace errors in the software. h. Easy to understand. i. The flowchart can be reused for inconvenience in the future. j. It helps to provide correct logic.

The final step is loading. The operating system loads the executable file into memory and prepares it for execution. The loaded program is allocated memory space, and the necessary resources are allocated, such as stack and heap memory. The program's entry point is identified, and the execution begins from there. Here's a simplified example to illustrate the process: Consider a C program consisting of two source files: main.c and functions.c. main.c contains the main function, and functions.c contains some additional functions used by main.c. Compilation: main.c is compiled into main.o (object file). functions.c is compiled into functions.o (object file). Linking: The linker combines main.o and functions.o along with any necessary libraries to create an executable file, such as program.exe. Loading: The operating system loads program.exe into memory. The program's entry point, the main function, is identified. The execution of the program begins from the main function. During the compilation, linking, and loading process, the semicolon in C is used to separate statements and indicate the end of each statement. It helps the compiler and linker identify the boundaries of individual statements and ensure proper parsing and linking of the code. b) Discuss the types of errors in programming. How do you debug a C-program?  There are several types of errors that can occur in programming. These errors can be categorized into three main types: syntax errors, runtime errors, and logical errors. Syntax Errors: Syntax errors occur when the code violates the rules of the programming language. These errors are detected by the compiler during the compilation process. Common syntax errors include missing semicolons, mismatched parentheses, misspelled keywords, and incorrect variable declarations. Syntax errors prevent the code from being compiled and need to be fixed before the program can run. Runtime Errors: Runtime errors occur during the execution of a program. They are not detected by the compiler but are encountered when the program is running. Runtime errors can be caused by various factors, such as dividing by zero, accessing an

invalid memory location, or trying to perform an operation on incompatible data types. When a runtime error occurs, the program may crash or produce unexpected results. Debugging tools and techniques are used to identify and fix runtime errors. Logical Errors: Logical errors occur when the program runs without any syntax or runtime errors, but it does not produce the expected output or behaves incorrectly. These errors are caused by flaws in the program's logic or algorithm. Logical errors can be challenging to identify and fix because they require careful analysis of the code and understanding of the problem domain. Techniques such as code review, stepping through the code, and using debugging tools can help in finding and resolving logical errors. To debug a C program, you can follow these steps: Reproduce the Error: Identify the specific scenario or input that triggers the error. Reproducing the error consistently will help in understanding and debugging the problem. Use Debugging Tools: Debugging tools, such as debuggers, are integrated development environment (IDE) features that allow you to step through the code, set breakpoints, inspect variables, and track the program's execution. Use these tools to analyze the program's behavior and identify the source of the error. Print Debugging: Insert print statements in strategic locations within the code to output the values of variables, function calls, and other relevant information. This can help in understanding the flow of the program and identifying the point where the error occurs. Divide and Conquer: If the program is large or complex, try to isolate the error by narrowing down the scope of the code. Temporarily remove or comment out sections of the code to identify the specific part causing the error. Analyze Error Messages: Pay attention to any error messages or warnings generated by the compiler or runtime environment. These messages often provide valuable information about the nature and location of the error. Review Code and Logic: Carefully review the code and logic to identify any potential mistakes or logical errors. Analyze the algorithm and data structures used in the program to ensure they are correct and appropriate for the problem being solved.

Logical NOT (!): Inverts the Boolean value of the operand. Logical operators are often used in conditional statements to combine multiple conditions or create complex conditions. By using relational and logical operators, programmers can make decisions, control program flow, and create complex conditions based on comparisons and logical operations. b) Write a C program to display following pattern without using formatted input/output statements. Programming rogrammin ogrammi gramm ram a  #include <stdio.h> void displayPattern(char* str, int length) { int i, j; for (i = 0; i < length; i++) { for (j = 0; j < length - i; j++) { printf("%c", str[j]); } printf("\n"); } } int main() { char str[] = "Programming"; int length = sizeof(str) - 1; // excluding the null terminator displayPattern(str, length); return 0; }

  1. a) Why do we need control statements? Compare switch and if-else-if ladder with example.

 Control statements are essential in programming as they allow us to control the flow of execution based on certain conditions or criteria. They enable us to make decisions, perform repetitive tasks, and handle different scenarios within a program. Now, let's compare the switch statement and the if-else-if ladder in terms of their usage and functionality: if-else-if Ladder: The if-else-if ladder is a series of if-else statements that are chained together to handle multiple conditions. It allows us to check multiple conditions sequentially and execute different blocks of code based on the outcome of each condition. Here's an example: int num = 5; if (num == 1) { printf("Number is 1"); } else if (num == 2) { printf("Number is 2"); } else if (num == 3) { printf("Number is 3"); } else { printf("Number is not 1, 2, or 3"); } In this example, the if-else-if ladder checks the value of the variable num and executes the corresponding block of code based on the condition. If none of the conditions match, the final else block is executed. switch Statement: The switch statement is used to select one of many code blocks to be executed based on the value of a variable or an expression. It provides a more concise way to handle multiple conditions compared to the if- else-if ladder. Here's an example: int num = 2; switch (num) { case 1: printf("Number is 1"); break; case 2: printf("Number is 2"); break; case 3: printf("Number is 3"); break; default: printf("Number is not 1, 2, or 3");

return 0; }

  1. a) What are the advantages of using functions? Differentiate between Library functions and User-defined functions with suitable example.  Functions are an essential part of programming as they provide several advantages, including code reusability, modularity, and abstraction. Here are the advantages of using functions: Code Reusability: Functions allow you to write a block of code once and reuse it multiple times throughout your program. This saves time and effort, as you don't need to rewrite the same code again and again. Modularity: Functions promote modularity by breaking down a complex program into smaller, manageable parts. Each function can focus on a specific task or functionality, making the code easier to understand, test, and maintain. Abstraction: Functions provide a level of abstraction by hiding the implementation details of a particular functionality. This allows you to use functions without knowing how they are implemented, making the code more readable and reducing complexity. Code Organization: Functions help in organizing code by grouping related operations together. This improves code readability and makes it easier to navigate and understand the program's structure. Now, let's differentiate between library functions and user-defined functions: Library Functions: Library functions are pre-defined functions provided by libraries or frameworks that come with the programming language. These functions are already implemented and can be directly used in your program without having to write their code. Library functions provide a wide range of functionalities, such as mathematical operations, string manipulation, file handling, and more. They are typically included by including the appropriate header files. Here's an example using the printf function from the standard library: #include <stdio.h> int main() { printf("Hello, World!\n"); return 0;

In this example, printf is a library function that is used to print the string "Hello, World!" to the console. It is part of the standard library and does not require any additional code to be written. User-Defined Functions: User-defined functions are functions that you define in your program to perform specific tasks or operations. These functions are created by the programmer to meet the specific requirements of the program. User-defined functions can be written in any programming language and are typically defined outside the main function. Here's an example of a user-defined function in C: #include <stdio.h> void greet() { printf("Hello, User!\n"); } int main() { greet(); return 0; } In this example, greet is a user-defined function that prints the string "Hello, User!" to the console. It is defined before the main function and can be called from within the main function or any other part of the program. User-defined functions provide flexibility and allow you to create custom functionalities tailored to your program's needs. They enhance code reusability, modularity, and abstraction, making the code more organized and maintainable. b) Write a program to check whether a given number is Armstrong number or not using recursive function.  #include <stdio.h> #include <math.h> int isArmstrong(int num, int originalNum, int sum); int main() { int num; printf("Enter a number: ");

if (comparisonResult == 0) { printf("The strings are equal.\n"); } else if (comparisonResult < 0) { printf("The first string is lexicographically smaller than the second string.\n"); } else { printf("The first string is lexicographically larger than the second string.\n"); } return 0; } b) What are overflow and underflow errors in context of array? Write a program to add corresponding elements of two arrays. The results should form a new array.  Overflow and underflow errors in the context of arrays occur when the program tries to access elements outside the valid range of the array. Overflow Error: An overflow error occurs when the program tries to access an element beyond the upper bound of the array. This can happen if the program tries to access an index that is greater than or equal to the size of the array. It can lead to unexpected behavior, memory corruption, or program crashes. Underflow Error: An underflow error occurs when the program tries to access an element beyond the lower bound of the array. This can happen if the program tries to access a negative index or an index that is smaller than the lower bound of the array. Underflow errors can also lead to unexpected behavior, memory corruption, or program crashes. Here's an example program that adds corresponding elements of two arrays and stores the results in a new array: #include <stdio.h> void addArrays(int arr1[], int arr2[], int size, int result[]) { for (int i = 0; i < size; i++) { result[i] = arr1[i] + arr2[i]; } }

int main() { int arr1[] = {1, 2, 3, 4, 5}; int arr2[] = {6, 7, 8, 9, 10}; int size = sizeof(arr1) / sizeof(arr1[0]); int result[size]; addArrays(arr1, arr2, size, result); printf("Resultant array: "); for (int i = 0; i < size; i++) { printf("%d ", result[i]); } printf("\n"); return 0; } In this program, the addArrays function takes two arrays (arr1 and arr2), their size (size), and an empty array (result) as parameters. It iterates over the arrays using a for loop and adds the corresponding elements of arr1 and arr2, storing the results in the result array. Finally, the main function calls addArrays and prints the elements of the result array. Note: It is important to ensure that the size of the result array is large enough to accommodate the added elements. Otherwise, it can lead to overflow errors.

  1. a) Why should we prefer structure over array? Explain nested structure with example.  Structures and arrays serve different purposes and have different use cases. Here are some reasons why you might prefer structures over arrays: Grouping Related Data: Structures allow you to group related data of different types into a single entity. This can improve code organization and make it easier to understand and maintain. Arrays, on the other hand, are used to store a collection of elements of the same type. Flexibility in Data Representation: Structures provide flexibility in representing complex data structures. You can define a structure with multiple members of different types, allowing you to create custom data structures that suit your specific needs. Arrays, on the other hand, have a fixed size and can only store elements of the same type.

In this example, we have defined two structures: Date and Employee. The Date structure represents a date with day, month, and year. The Employee structure represents an employee with an ID, name, and birthdate (which is of type Date). The Employee structure is nested within the Date structure, creating a nested structure. This allows us to represent the birthdate as a member of the Employee structure. In the main function, we declare an emp variable of type Employee. We then assign values to its members using the dot operator (.) to access the nested structure's members. Finally, we print the employee's ID, name, and birthdate. Nested structures provide a way to represent hierarchical or composite data structures, where one structure is a member of another structure. This can be useful when dealing with complex data relationships and improves code organization and readability. b) Write a program to read name and roll number of 48 students from user and store them in file. If the file already contains data, your program should add new data at the end of the file.  #include <stdio.h> struct Student { char name[50]; int rollNumber; }; int main() { FILE *file = fopen("students.txt", "a"); // Open the file in append mode if (file == NULL) { printf("Error opening the file.\n"); return 1; } struct Student student; for (int i = 0; i < 48; i++) { printf("Enter the name of student %d: ", i + 1); scanf("%s", student.name); printf("Enter the roll number of student %d: ", i + 1); scanf("%d", &student.rollNumber);

fprintf(file, "%s %d\n", student.name, student.rollNumber); } fclose(file); // Close the file printf("Data has been stored in the file.\n"); return 0; }

  1. a) What is the structure of FORTRAN program?  A FORTRAN (Formula Translation) program typically follows a specific structure that consists of various sections and statements. Here is a general structure of a FORTRAN program: Program Statement: The program statement is used to define the name of the program. It is typically the first line of the program and starts with the keyword "PROGRAM" followed by the program name. For example: PROGRAM myProgram Declaration Section: The declaration section is used to declare variables, arrays, and other program entities. It is placed after the program statement and before the executable statements. Variable declarations start with the keyword "REAL", "INTEGER", "LOGICAL", or "CHARACTER" followed by the variable names. For example: REAL :: x, y, z Executable Statements: The executable statements section contains the main logic of the program. It consists of various statements that perform calculations, control program flow, and interact with the user. Executable statements can include arithmetic operations, assignment statements, control statements (IF, DO, etc.), input/output statements, and function/subroutine calls. End Statement: The end statement marks the end of the program. It is typically the last line of the program and starts with the keyword "END" followed by the program name. For example: END PROGRAM myProgram

END DO

WRITE(* ,* ) "The largest number is:", largestNumber END PROGRAM largestNumberProgram Group: Dharma raj Thapa (puL079BEI017) Samay BhaTTaraI (puL079BEI033) ShIShIr BaSnET (puL079BEI038) SujaL KoIraLa (puL079BEI043)