LDP Assignment
LDP Assignment
LDP Assignment
1. 169 1 160 0 153 0 128 153 0 129 153 130 153 0 131 200 208 241 96
Assembly language
• It uses short descriptive words (MNEMONIC) to represent each of the machine
language instructions.
• It requires a translator knows as assembler to convert assembly language into
machine language so that it can be understood by the computer.
Q3. Explain Basic of block diagram and functions of various components of computer.
Ans)
A computer can process data, pictures, sound and graphics. They can solve
highly complicated problems quickly and accurately. A computer as shown in Fig.
performs basically five major computer operations or functions irrespective of
their size and make. These are
1. Input: This is the process of entering data and programs in to the computer
system. You should know that computer is an electronic machine like any other
machine which takes as inputs raw data and performs some processing giving
out processed data.
4. Output: This is the process of producing results from the data for getting useful
information. Similarly the output produced by the computer after processing must
also be kept somewhere inside the computer before being given to you in human
readable form.
5. Control: The manner how instructions are executed and the above operations
are performed. Controlling of all operations like input, processing and output are
step by step processing of all operations inside the computer.
Q4. Explain Flowcharts and Algorithms.
Ans)
Flowcharts
Algorithms
2. Interpreter:
- An interpreter, on the other hand, translates the source code into machine code and
executes it line by line.
- It doesn't produce a standalone executable; instead, it interprets and executes the code
directly.
- Errors are reported as soon as they are encountered, allowing for easier debugging during
runtime.
3. Assembler:
- Assemblers are used for low-level programming languages like assembly language, which
is a human-readable representation of machine code.
- An assembler translates assembly language code into machine code or object code.
- Unlike compilers and interpreters, assemblers deal with a one-to-one correspondence
between assembly language instructions and machine code instructions.
UNIT- 2: Fundamentals and Control Structure in C
Example:-
1. A constant is a name given to the variable whose values can’t be altered or changed.
2. A constant is very similar to variables in the C programming language, but it can hold
only a single variable during the execution of a program.
3. It means that once we assign value to the constant, then we can’t change it
throughout the execution of a program- it stays fixed.
Example of constant-
#include<stdio.h>
void main(){
int q = 9 ;
const int a = 10 ;
q = 15 ;
a = 100 ; // creates an error
printf(“q = %d\na = %d”, q, a ) ;
}
Variables
1. Variables are containers for storing data values, like numbers and characters.
2. In C, there are different types of variables (defined with different keywords), for example:
3. int - stores integers (whole numbers), without decimals, such as 123 or -123
4. float - stores floating point numbers, with decimals, such as 19.99 or -19.99
5. char - stores single characters, such as 'a' or 'B'.
6. Char values are surrounded by single quotes
Example-
#include <stdio.h>
int main() {
// Create variables
int myInt = 15;
// Integer (whole number)
printf("%d\n", myInt);
printf("%f\n", myFlo);
printf("%c\n", myChar);
return 0;
Output-
15
5.990000
D
Q3. Explain operators.
Ans) An operator is a symbol that tells the compiler to perform specific mathematical or logical
functions.
C language is rich in built-in operators and provides the following types of operators −
Continue statement
• The continue statement in C programming works somewhat like the break statement.
Instead of forcing termination, it forces the next iteration of the loop to take place, skipping
any code in between.
• For the for loop, continue statement causes the conditional test and increment portions
of the loop to execute. For the while and do...while loops, continue statement causes the
program control to pass to the conditional tests.
Go-to statement
• Go-to is an virtual loop
• The go-to statement allows us to transfer control of the program to the specified label.
• Go-to is keyword
Q5. Explain Nesting of control structures.
Ans)
In C, nesting of control structures refers to the practice of placing one control
structure (like a loop or conditional statement) inside another.
This creates a hierarchical or layered structure for controlling the flow of the
program.
For example:
#include <stdio.h>
int main() {
int i, j;
for (i = 1; i <= 5; i++) {
// Outer loop
for (j = 1; j <= i; j++) {
// Inner loop
printf("* ");
}
printf("\n");
}
return 0;
}
In general terms, a data type is an attribute that tells a computer how to interpret the value. Types
Of Data Types In C- There are majorly two main categories of Data Type in C:
1. Primitive/Primary Data Type- The C programming language has five primitive or primary data
types.
2. Derived Data Types- Derived data types are primary data types that are grouped together. You can
group many elements of similar data types. These data types are defined by the user.
Variables in C are named storage locations whose values can change during program
execution, declared with a specific data type (e.g., `int x;`).
Q8. What is type conversion? Explain it.
Ans)
Type conversion in C are of 2 types :-
1) Implicit Type Conversion- Implicit type casting means conversion of data types without losing its
original meaning.
Example code:-
#include <stdio.h>
main()
{
int number = 1;
char character = 'J'; /*ASCII value is 74 */
int sum;
sum = number + character;
printf("Value of sum : %d\n", sum ); }
2) Explicit Type Conversion- Some scenarios of program in which we may have to force type
conversion Explicitly that is called Explicit type Conversion Suppose we have a variable div that
stores the division of two operands which are declared as an int data type.
Example code-
#include<stdio.h>
int main()
{
float a = 1.2; //int b = a;
int b = (int)a + 1;
printf("Value of a is %f\n", a);
printf("Value of b is %d\n",b);
return 0; }
Output-
Value of a is 1.200000
Value of b is 2
2. Comments in C:
- Comments in C are non-executable portions of code used for documentation. They
provide explanations or notes within the source code to improve readability. Comments can
be single-line (//) or multi-line (/* */).
3. Header Files in C:
- Header files in C contain declarations for functions, macros, data types, and other
constructs. They are included at the beginning of a C program using `#include` preprocessor
directive to provide information about the external features used in the program.
While Loop
• while is an entry controlled loop
• Statements inside the body of while are repeatedly executed till the condition is true
• while is keyword
do while Loop
• do while is an exit controlled loop.
• Statements inside the body of do while are repeatedly executed till the condition is true.
• Do and while are keywords.
for Loop
• for is an entry controlled loop
• Statements inside the body of for are repeatedly executed till the condition is true
• for is keyword
Syntax
Syntax
UNIT- 3: Pointers and Structures
Example in c:-
int arr[5]; // Declaration of an integer array with 5 elements
Example in c:-
int matrix[3][4]; // Declaration of a 3x4 integer matrix
Example in c:-
int numbers[5]; // Declaration of an integer array with 5 elements
Here, `numbers` is an array that can store 5 integers. The elements can be accessed using
indices, such as `numbers[0]`, `numbers[1]`, etc.
// Initialization
numbers[0] = 1;
numbers[1] = 2;
// ...
Q5. Explain the difference between built-in functions and user-defined functions. Provide
examples of each.
Ans:
- Built-in Functions: These are functions that are provided by the programming language or
libraries. For example, in C, `printf()` and `scanf()` are built-in functions.
Example in c:-
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
- User-defined Functions: These are functions created by the programmer. They provide a
way to modularize code and reuse it. Example:
Example in c:-
#include <stdio.h>
// User-defined function
int add(int a, int b) {
return a + b;
}
int main() {
int result = add(3, 4);
printf("Sum: %d\n", result);
return 0;
}
Q6. Describe the process of defining and declaring a function in programming. Provide an
example of a user-defined function with parameters and a return value.
Ans:
- Defining and Declaring a Function:
// Declaration
int add(int a, int b);
// Definition
int add(int a, int b) {
return a + b;
}
// Function declaration
int add(int a, int b);
int main() {
int result = add(3, 4);
printf("Sum: %d\n", result);
return 0;
}
// Function definition
int add(int a, int b) {
return a + b;
}
Ans:
- Function prototypes declare the function's name, return type, and parameter types without
providing the actual implementation. They serve as a forward declaration to inform the
compiler about the existence of a function before it's used in the code. This allows the
compiler to perform type checking and catch errors early.
Example in c:-
// Function prototype
int add(int a, int b);
int main() {
int result = add(3, 4);
// ...
return 0;
}
// Function definition
int add(int a, int b) {
return a + b;
}
Q8. Explain the concept of recursion in functions. How does a recursive function call itself?
Provide an example of a recursive function and explain its functionality.
Ans:
- Recursion in Functions:
Recursion is a programming concept where a function calls itself directly or indirectly to
solve a problem. In a recursive function, there should be a base case to stop the recursion and
prevent an infinite loop.
- Example in c:-
#include <stdio.h>
int main() {
int result = factorial(5);
printf("Factorial: %d\n", result);
return 0;
}
Example in c:-
// Function declaration with parameters
int add(int a, int b);
// Function definition
int add(int a, int b) {
return a + b;
}
Example in c:-
void modifyValue(int x) {
x = 10;
}
int main() {
int num = 5;
modifyValue(num);
// 'num' remains 5
return 0;
}
Example in c:-
void modifyReference(int *x) {
*x = 10;
}
int main() {
int num = 5;
modifyReference(&num);
// 'num' is now 10
return 0;
}
Example in c:-
#include <stdio.h>
#define PI 3.1415
int main() {
printf("Value of PI: %f\n", PI);
return 0;
}
- Recursion and Macros: Recursion is a programming concept where a function calls itself.
Macros in C are defined using `#define` and can be recursive.
Example in c:-
#include <stdio.h>
#define FACTORIAL(n) (n == 0 ? 1 : n * FACTORIAL(n - 1))
int main() {
printf("Factorial of 5: %d\n", FACTORIAL(5));
return 0;
}
int main() {
int result = ackermann(2, 3);
printf("Ackermann(2, 3): %d\n", result);
return 0;
}
UNIT- 4: Pointers and Structures
Q1. Declare a pointer variable and initialize it to store the address of an integer variable.
Ans)
#include <stdio.h>
int main() {
int num = 42;
int *ptr = #
return 0;
}
Q2. Create a pointer to a pointer (double pointer) and assign it the address of the above
pointer variable.
Ans)
#include <stdio.h>
int main() {
int num = 42;
int *ptr = #
int **doublePtr = &ptr;
return 0;
}
int main() {
int arr[5] = {1, 2, 3, 4, 5};
// Pointer to an array
int (*ptrToArray)[5] = &arr;
return 0;
}
Q4. Write a function that takes an integer array as a parameter, calculates the sum of its
elements, and returns a pointer to the result.
Ans)
#include <stdio.h>
#include <stdlib.h>
return result;
}
int main() {
int numbers[] = {1, 2, 3, 4, 5};
int *result = sumArray(numbers, 5);
return 0;
}
struct Point {
int x;
int y;
};
int main() {
struct Point p1;
p1.x = 10;
p1.y = 20;
return 0;
}
Q6. Explain structure as functions and structures as pointers.
Ans)
#include <stdio.h>
struct Point {
int x;
int y;
};
int main() {
struct Point p1 = {10, 20};
// Structure as a pointer
struct Point *ptr = &p1;
return 0;
}