0% found this document useful (0 votes)
12 views

PIC QB

The document provides a comprehensive overview of algorithms, including their definition and factors affecting their quality such as time complexity, space complexity, and correctness. It also defines key programming concepts like assembler, compiler, interpreter, and linker, along with examples. Additionally, it covers C programming topics such as else-if ladder, arrays, formatted input/output statements, and a simple arithmetic program.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

PIC QB

The document provides a comprehensive overview of algorithms, including their definition and factors affecting their quality such as time complexity, space complexity, and correctness. It also defines key programming concepts like assembler, compiler, interpreter, and linker, along with examples. Additionally, it covers C programming topics such as else-if ladder, arrays, formatted input/output statements, and a simple arithmetic program.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

a) What is an Algorithm?

Explain Factors Affecting the Quality of


an Algorithm.
Definition of Algorithm:

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


consists of well-defined instructions that take input, process it, and produce an output.

Factors Affecting the Quality of an Algorithm:

1. Time Complexity – The execution time of an algorithm should be


minimal.
2. Space Complexity – The algorithm should use the least memory
possible.
3. Correctness – It should provide the correct output for all possible
inputs.
4. Simplicity – It should be easy to understand, implement, and
maintain.
5. Scalability – The algorithm should work efficiently even for large
inputs.
6. Generality – It should be applicable to a wide range of problems.
7. Efficiency – The algorithm should use minimal resources like CPU and
memory.

b) Definitions:
1) Assembler:

An assembler is a program that converts assembly language code into machine code (binary
instructions that a computer can understand).
Example: Assembly code (MOV A, B) is converted to binary (10110000).

2) Compiler:

A compiler translates the entire high-level language program (like C, C++) into machine code at
once before execution.
Example: gcc is a compiler for C language.

3) Interpreter:

An interpreter translates high-level language code line-by-line and executes it immediately.


Example: Python and JavaScript use interpreters.
4) Linker:

A linker combines multiple object files into a single executable file by resolving function and
variable references.
Example: It links main.o and math.o to create a.out.

c) Else-if Ladder in C

The else-if ladder is used when multiple conditions need to be checked sequentially. If one
condition is true, the corresponding block executes, and the rest are skipped.

Syntax:
if (condition1) {
// Code block 1
}
else if (condition2) {
// Code block 2
}
else if (condition3) {
// Code block 3
}
else {
// Default block (if none of the above conditions are true)
}
Example:
#include <stdio.h>

int main() {
int marks;
printf("Enter your marks: ");
scanf("%d", &marks);

if (marks >= 75) {


printf("Distinction\n");
}
else if (marks >= 60) {
printf("First Class\n");
}
else if (marks >= 50) {
printf("Second Class\n");
}
else if (marks >= 40) {
printf("Pass\n");
}
else {
printf("Fail\n");
}

return 0;
}

d) Array Definition and Characteristics (As per MSBTE in C)


Definition:

An array is a collection of elements of the same data type stored in contiguous memory
locations. It allows multiple values to be stored under a single variable name and accessed using
an index.

Characteristics of an Array:

1. Homogeneous Elements – All elements in an array must be of the


same data type.
2. Contiguous Memory Allocation – Elements are stored in adjacent
memory locations.
3. Fixed Size – The size of an array is defined at the time of declaration
and cannot be changed dynamically.
4. Index-Based Access – Each element is accessed using an index
(starting from 0).
5. Efficient Access – Direct access to any element using its index
provides fast retrieval.
6. Sequential Storage – Elements are stored sequentially in memory,
improving performance.

Example:
#include <stdio.h>

int main() {
int numbers[5] = {10, 20, 30, 40, 50}; // Declaring and initializing an
array

printf("First element: %d\n", numbers[0]); // Accessing elements using


index
printf("Second element: %d\n", numbers[1]);

return 0;
}
e) Formatted Input and Output Statements in C
Formatted Input (scanf)

 scanf is used to take input from the user with format specifiers to
specify the data type.
 Format specifiers:
o %d for integers
o %f for floating-point numbers
o %c for characters
o %s for strings

Formatted Output (printf)

 printf is used to display output with specific formatting options like


width, precision, and alignment.

Example:
#include <stdio.h>

int main() {
int age;
float salary;
char name[20];

// Formatted Input
printf("Enter your name: ");
scanf("%s", name);

printf("Enter your age: ");


scanf("%d", &age);

printf("Enter your salary: ");


scanf("%f", &salary);

// Formatted Output
printf("\nEmployee Details:\n");
printf("Name: %s\n", name);
printf("Age: %d years\n", age);
printf("Salary: %.2f INR\n", salary); // Displays salary with 2 decimal
places

return 0;
}
f) Declaration and Initialization of an Array in C
Declaration of an Array

An array in C is declared using:

data_type array_name[size];

Example:

int numbers[5]; // Declares an integer array of size 5


Initialization of an Array

Arrays can be initialized in different ways:

1. At Declaration:

int numbers[5] = {10, 20, 30, 40, 50};

2. Partial Initialization (Remaining values default to 0):

int numbers[5] = {10, 20}; // Remaining elements are 0

3. Without Specifying Size:

int numbers[] = {10, 20, 30}; // Size is automatically set to 3

4. Using Loops:

#include <stdio.h>

int main() {
int numbers[5]; // Declare an array of size 5

// Input values using a loop


printf("Enter 5 numbers:\n");
for (int i = 0; i < 5; i++) {
scanf("%d", &numbers[i]);
}

// Display array values


printf("Array elements are: ");
for (int i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}

return 0;
}
g) Program:
#include <stdio.h>

int main() {
int num1, num2, choice;
float result;

// Input two numbers


printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);

// Menu for operations


printf("\nSelect an operation:\n");
printf("1. Addition (+)\n");
printf("2. Subtraction (-)\n");
printf("3. Multiplication (*)\n");
printf("4. Division (/)\n");
printf("Enter your choice (1-4): ");
scanf("%d", &choice);

// Switch case for arithmetic operations


switch (choice) {
case 1:
result = num1 + num2;
printf("Result: %d + %d = %.2f\n", num1, num2, result);
break;
case 2:
result = num1 - num2;
printf("Result: %d - %d = %.2f\n", num1, num2, result);
break;
case 3:
result = num1 * num2;
printf("Result: %d * %d = %.2f\n", num1, num2, result);
break;
case 4:
if (num2 != 0) {
result = (float)num1 / num2; // Type casting for accurate
division
printf("Result: %d / %d = %.2f\n", num1, num2, result);
} else {
printf("Error: Division by zero is not allowed.\n");
}
break;
default:
printf("Invalid choice! Please enter a number between 1 and 4.\
n");
}

return 0;
}

You might also like