Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

An if

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

1) An if-else ladder is a type of decision-making construct in computer programming that

allows you to make multiple conditional checks and execute different code blocks
based on the outcome of those checks. It's called a "ladder" because it resembles a
series of steps or branches that the program follows based on the conditions being
evaluated.
Start: The flowchart begins with a start point, usually depicted as an oval shape.

Decision: The first step in an if-else ladder is to make a decision based on a


condition. This condition can be a comparison between two values, a boolean
expression, or any other logical statement. The flowchart will have a diamond-
shaped decision symbol to represent this step.

If condition is true: If the condition in the if statement is true, the flowchart will
follow the arrow labeled "yes" to the next step, which represents the code block
that will be executed if the condition is true. This code block will be enclosed
within a rectangle shape in the flowchart.

Else-if condition is true: If the condition in the if statement is false, the flowchart
will follow the arrow labeled "no" to the next decision point. This is where the
else-if condition is evaluated. If this condition is true, the flowchart will follow the
arrow labeled "yes" to the corresponding code block that will be executed. This
code block will also be enclosed within a rectangle shape in the flowchart.

Repeat else-if condition evaluation: If the else-if condition is false, the flowchart
will follow the arrow labeled "no" to the next decision point. This process will
repeat for as many else-if conditions as needed.

Else: If none of the conditions in the if and else-if statements are true, the
flowchart will follow the arrow labeled "no" to the final step, which represents the
code block that will be executed in the else case. This code block will also be
enclosed within a rectangle shape in the flowchart.
End: The flowchart ends with an end point, usually depicted as an oval shape.

2) A recursive function is a type of computer programming function that calls itself as part
of its own execution. In other words, a recursive function is a function that solves a
problem by breaking it down into smaller, similar sub problems and solving them in a
repetitive manner until a base case is reached, at which point the function returns a
result. The results from each level of recursion are combined or processed to produce
the final result.

3) Programming, functions are needed for modularity, code reusability, abstraction,


organization, maintainability, and testing. Functions allow for encapsulating specific
functionality, promoting code reuse, hiding implementation details, organizing code,
improving maintainability, and facilitating testing.

4) struct Student {
int rollNo; // Roll number of the student
char name[100]; // Name of the student (assuming a maximum length of 100
characters)
};

5) A two-dimensional array in C is a data structure that represents a table or a matrix with


rows and columns. It is also known as a matrix or a 2D array.
Declaration:
data_type array_name[row_size][column_size];

Initialization:
// Example of manual initialization
int matrix[3][4]; // Declaration
matrix[0][0] = 1; // Initialization
matrix[0][1] = 2;
//

6)
 Call by Value: In call by value, the value of the argument is passed to the function.
This means that any changes made to the parameter within the function do not
affect the original argument outside the function. The function works with a local
copy of the argument.

 Call by Reference: In call by reference, a reference or memory address of the


argument is passed to the function. This means that any changes made to the
parameter within the function directly affect the original argument outside the
function. The function works with the original argument directly.

7)
An array of structures in programming refers to a collection of structures of the same data
type, stored in contiguous memory locations. Each structure in the array can have multiple
members or fields, which can be of different data types.
8) #include <stdio.h>

// Define a structure to represent student information


struct Student {
int rollNo;
char name[50];
int age;
};

int main() {
// Declare and initialize an array of structures
struct Student students[3] = {
{101, "Alice", 20},
{102, "Bob", 22},
{103, "Charlie", 21}
};

// Access and print values of the array of structures


printf("Roll No\tName\tAge\n");
for (int i = 0; i < 3; i++) {
printf("%d\t%s\t%d\n", students[i].rollNo, students[i].name, students[i].age);
}
9) continue statement: The continue statement is used to skip the remaining statements
within the current iteration of a loop and move on to the next iteration. It is often used
in loops, such as for and while, to bypass certain iterations based on a condition.

}
#include <stdio.h>

int main() {
// Print even numbers from 1 to 10 using a for loop with continue statement
for (int i = 1; i <= 10; i++) {
if (i % 2 == 1) {
continue; // Skip odd numbers
}
printf("%d\n", i); // Print even numbers
}

return 0;
}

break statement: The break statement is used to immediately terminate the execution of
a loop and exit the loop entirely. It is often used in loops, such as for and while, to
prematurely end the loop based on a certain condition.

#include <stdio.h>

int main() {
// Print numbers from 1 to 5 using a while loop with break statement
int i = 1;
while (i <= 5) {
if (i == 3) {
break; // Terminate the loop when i becomes 3
}
printf("%d\n", i);
i++;
}
return 0;
}

10) Pointer arithmetic is a feature in programming languages that allows you to perform
arithmetic operations on pointers, which are variables that store the memory addresses
of other variables. Here's a brief explanation of pointer arithmetic with an example in
C:

#include <stdio.h>

int main() {
int arr[5] = {10, 20, 30, 40, 50};
int *ptr = arr; // Pointer to the first element of the array

printf("Original Array: ");


for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]); // Print original array values
}
printf("\n");

printf("Pointer Arithmetic: ");


for (int i = 0; i < 5; i++) {
printf("%d ", *(ptr + i)); // Print values using pointer arithmetic
}
printf("\n");

return 0;
}

In this example, we have an integer array arr with 5 elements, and a pointer ptr that
points to the first element of the array. We use pointer arithmetic to access the elements
of the array by incrementing the pointer ptr in each iteration of the loop. The expression
*(ptr + i) dereferences the pointer ptr + i to access the value stored at the memory
location pointed to by the incremented pointer. The result is the same as accessing arr[i]
directly.

11) #include <stdio.h>


void selectionSort(int arr[], int n) {
int i, j, minIdx, temp;
for (i = 0; i < n - 1; i++) {
minIdx = i;
for (j = i + 1; j < n; j++) {
if (arr[j] < arr[minIdx]) {
// Update the index of the minimum element
minIdx = j;
}
}
// Swap the minimum element with the first unsorted element
temp = arr[i];
arr[i] = arr[minIdx];
arr[minIdx] = temp;
}
}

int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);

printf("Original Array: ");


for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}

// Call selectionSort function to sort the array


selectionSort(arr, n);

printf("\nSorted Array: ");


for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}

return 0;
}
12) #include <stdio.h>

int main() {
int n = 5; // number of rows in the pattern

printf("Pattern:\n");

// Loop for each row


for (int i = 1; i <= n; i++) {
// Loop for printing numbers from 1 to i
for (int j = 1; j <= i; j++) {
printf("%d", j);
}
printf("\n");
}

return 0;
}

13) while loop: It is an entry-controlled loop, where the condition is checked before
executing the loop body. If the condition is false initially, the loop will not be executed
at all.

do-while loop: It is an exit-controlled loop, where the loop body is executed at least
once, even if the condition is false, as the condition is checked after executing the loop
body.

14) A pointer in C is a variable that stores the memory address of another variable. It
allows direct manipulation of memory and is often used for dynamic memory
allocation, passing parameters to functions by reference, and for efficient memory
management.

15) A switch-case statement in C is a control flow statement that allows multiple


conditional branches based on the value of a single expression. It has the following
syntax:
switch (expression) {
case constant_1:
// code to be executed if expression == constant_1;
break;
case constant_2:
// code to be executed if expression == constant_2;
break;
// more cases can be added here
default:
// code to be executed if no case matches;
}

16) If statement: It is used for conditional execution of a block of code based on a given
condition.
For loop: It is used for repeating a block of code a fixed number of times.
While loop: It is used for repeating a block of code as long as a given condition is true.
Do-while loop: It is used for repeating a block of code at least once, and then as long as
a given condition is true.

17) A function declaration in C specifies the function's return type, name, and
parameter list
#include <stdio.h>

// Function declaration
int add(int num1, int num2);

int main() {
int a = 5, b = 7;
int sum = add(a, b); // Function call
printf("Sum: %d\n", sum);
return 0;
}

// Function definition
int add(int num1, int num2) {
return num1 + num2;
}

You might also like