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

Computer Programming

Uploaded by

praneshsekar07
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Computer Programming

Uploaded by

praneshsekar07
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Computer Programming

2 Marks:

1.Diff betweeen 1d array and 2d array

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]

2. auto and static variables in a function.

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() {

auto int num = 10; // auto keyword is optional here


printf("%d\n", num);

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.

3.structure of a basic function definition `

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

int calculateSum(int a, int b) {

int result = a + b; // Function body

return result; // Return statement

 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() {

int sum = calculateSum(5, 10); // Function call

printf("Sum: %d\n", sum);

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.

 Return Statement: return 0; indicates that the program finished successfully.

4.Predict the output of the program.

int main() {

int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};

printf("%d", arr[0][2]);

5. Predict the output of the program.

int main() {

int a[3] = {1, 2, 3};

printf("%d", *(a + 2));

return 0;
}

6. role of the main function and its return type

7. use of NULL pointers

8. advantages and disadvantages of using dynamic memory allocation with pointers.

9. syntax for 2D array

10.local and global variable declaration

11.how recursion affect memory usage in c

12. Predict the output of the program.

int main() {

int x = 5, *p = &x;

printf("%d", *p);

13. int main() {

int arr[] = {10, 20, 30};

int *p = arr;

printf("%d", *(p+1));

return 0;

14.call by value call by reference

15.recursion factorial

16.how to declare and initialize pointer arrays

17.reverse an array

16MARKS

1.1d and 2d array example program (declaration,initialization syntax)

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

int array_1d[5] = {1, 2, 3, 4, 5}; // Declares and initializes a 1D array


2D Array

 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

int array_2d[3][3] = { // Declares and initializes a 2D array

{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

Output - Transposed Matrix: 1 4

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.

7. Input – Array1: [1, 2, 3, 4]

Array2: [3, 4, 5, 6]

Output - Merged Array without duplicates: [1, 2, 3, 4, 5, 6]

8. use of multi-dimensional arrays and pointer arithmetic to perform matrix operations

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

You might also like