10 Marks

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Unit 1

What is an algorithm? What are its characteristics? Give an example.

An algorithm is a step-by-step procedure or a set of rules to solve a specific problem or perform


a task. It provides a clear and finite sequence of operations to achieve the desired outcome.

Characteristics of an Algorithm:

1. Finiteness: It must terminate after a finite number of steps.

2. Definiteness: Each step must be precisely defined.

3. Input: It takes zero or more inputs.

4. Output: It produces at least one output.

5. Effectiveness: The steps must be basic enough to be carried out, in principle, by a human or
machine.

Algorithm: Find the Largest of Two Numbers

1. Start
2. Input the first number a
3. Input the second number b
4. If a > b then
Output a (a is the largest)
5. Else
Output b (b is the largest)
6. End

Unit 2

Explain the structure of C program using example Describe the steps used in execution
of C program

Structure of a C Program:
1. Preprocessor Directives: Instructions like #include to include header files.

2. Global Declarations: Global variables or function prototypes.

3. Main Function: The starting point of the program.

4. Function Definitions: Functions that define specific tasks.

Example:

#include <stdio.h> // Preprocessor directive

int main() {
printf("Hello, World!\n"); // Output statement
return 0;
}

Steps in Execution of C Program:

1. Preprocessing: Handles directives like #include.

2. Compilation: Converts C code into assembly language.

3. Assembly: Converts assembly into machine code.

4. Linking: Combines object files and libraries into an executable.

5. Execution: The program runs, starting from the main() function.

MST 2
Unit 3

Differentiate between function declaration, function definition and function call

1. Function Declaration:

A function declaration informs the compiler about the function's name, return type, and
parameters, but it does not provide the actual implementation.

It is also called a function prototype.

Example:

int add(int, int); // Function declaration (prototype)

2. Function Definition:

A function definition provides the actual body of the function, including the logic that will be
executed when the function is called.

It contains the function's return type, name, parameters, and the code that defines what the
function does.

Example:

int add(int a, int b) { // Function definition


return a + b;
}

3. Function Call:

A function call is used to invoke a function and pass the required arguments to it. It tells the
program to execute the function's code.

Example:

int result = add(5, 3); // Function call


Unit 4

What do you mean by pointers? Write a program to print the addressPointers in C/C++:

A pointer is a variable that stores the memory address of another variable. Instead of holding a
data value directly, it holds the address of a variable, allowing you to indirectly manipulate the
data stored at that address.

Pointers are especially useful for dynamic memory management, passing large structures or
arrays to functions, and more efficient code execution.

Syntax:

* (dereference operator) is used to access the value stored at the address the pointer is pointing
to.

& (address-of operator) is used to get the address of a variable.

Example Program:

The following program demonstrates how to use a pointer to print the address of a variable
along with its value:

#include <stdio.h>

int main() {
int num = 10; // Declare an integer variable
int *ptr; // Declare a pointer to integer

ptr = &num; // Store the address of 'num' in 'ptr'

// Print the value and address of 'num' using the pointer


printf("Value of num: %d\n", *ptr); // Dereference pointer to get value
printf("Address of num: %p\n", ptr); // Print the address stored in pointer

return 0;
}

Output:

Value of num: 10
Address of num: 0x7ffeedc1a6ec (Address may vary)

You might also like