Computer Programming
Computer Programming
2 Marks:
1D Array: Think of this like a simple list or a single row of items. It's a linear structure where
you have elements in a single dimension. For example, [1, 2, 3, 4, 5].
2D Array: This is more like a grid or a table with rows and columns, essentially an array of arrays. It
has elements in two dimensions. For instance:
Copy
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
Auto Variables:
Definition: auto variables are the default type for local variables in C. They are automatically created
when a function is called and destroyed when the function exits.
Scope and Lifetime: Limited to the block in which they are defined. They are re-initialized each time
the block is entered.
Example:
Copy
void exampleFunction() {
Static Variables:
Definition: static variables within a function retain their value between function calls. If declared
outside a function, their scope is limited to the file.
Scope and Lifetime: The scope is limited to the function/block in which they are defined, but their
lifetime extends across the entire run of the program.
Example:
void exampleFunction() {
static int num = 10; // This retains its value between calls
num++;
printf("%d\n", num);
}x
When you call exampleFunction multiple times, a static variable will keep its value, while an auto
variable will be reinitialized each time.
1. #include <stdio.h>
Purpose: This is a preprocessor directive that includes the Standard Input Output library. It
allows you to use functions like printf() for printing output to the console.
Angle Brackets: The angle brackets indicate that the header file is a standard library file.
2. Function Definition
Copy code
Return Type: int specifies that this function will return an integer value.
Function Name: calculateSum is the name of the function, which describes what it does.
Parameters: The function takes two parameters, int a and int b, which are both integers.
Function Body:
o The line int result = a + b; computes the sum of a and b and stores it in the variable
result.
o The return result; statement sends the computed value back to the caller.
3. int main()
Copy code
int main() {
return 0;
Main Function: int main() is the starting point of any C program. The operating system calls
this function when the program runs.
Function Call: calculateSum(5, 10) calls the calculateSum function, passing 5 and 10 as
arguments. The returned value is stored in the variable sum.
Output: printf("Sum: %d\n", sum); prints the result to the console. %d is a format specifier
used to display integers.
int main() {
printf("%d", arr[0][2]);
int main() {
return 0;
}
int main() {
int x = 5, *p = &x;
printf("%d", *p);
int *p = arr;
printf("%d", *(p+1));
return 0;
15.recursion factorial
17.reverse an array
16MARKS
1D Array
Definition: A one-dimensional (1D) array is a linear array or a simple list of elements that are
accessed using a single index. It is like a single row of data.
Example:
Copy
Definition: A two-dimensional (2D) array is an array of arrays, meaning it consists of rows and
columns. It’s like a matrix where elements are accessed using two indices, one for the row an
d one for the column.
Example:
Copy
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
2. Write a C program that swaps the values of two variables using the pointers.
3. Input - 2 3
123
456
25
36
4. Differentiate between pointer to pointer and pointer to an array. Use detailed example with
program to analyze the usage scenarios for each, focusing on their implications in complex data
management
5. Analyze the impact of different parameter passing techniques in C on program behavior and
memory usage. Discuss how each technique affects function execution with detailed
6. the concept of function pointers in C. Discuss how function pointers can be used to implement
callback mechanisms and dynamic function calls in C programming.
Array2: [3, 4, 5, 6]
9. local and global variables in terms of scope, lifetime, and memory allocation.
10. Examine the use of recursion function in C programming, comparing it with iterative solutions in
terms of efficiency and memory usage. Provide detailed examples