0% found this document useful (0 votes)
8 views7 pages

Pop Module 3 Answers

The document provides an overview of programming concepts in C, including definitions and examples of functions, parameter passing techniques, array declarations and initializations, and implementations of binary search and sorting algorithms. It explains the differences between function prototypes, definitions, and calls, as well as the characteristics of arrays. Additionally, it includes sample C programs for various tasks such as finding the transpose of a matrix and sorting an array.

Uploaded by

sachinsbs1913
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)
8 views7 pages

Pop Module 3 Answers

The document provides an overview of programming concepts in C, including definitions and examples of functions, parameter passing techniques, array declarations and initializations, and implementations of binary search and sorting algorithms. It explains the differences between function prototypes, definitions, and calls, as well as the characteristics of arrays. Additionally, it includes sample C programs for various tasks such as finding the transpose of a matrix and sorting an array.

Uploaded by

sachinsbs1913
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

PRINCIPLE OF PROGRAMMING USING C

BPOPS103
MODULE 3
QUESTION AND ANSWER’S

1. Define Function. Write the syntax of a function. Explain the


categories of function with examples.
Ans :

A function is a block of code that performs a specific task and can


be called multiple times within a program.
Syntax:
return_type function_name(parameters) {
// Function body
return value; // Optional
}
Categories of Functions:
1)Function without parameters and without return
value
void greet() {
printf("Hello, World!\n");
}
2)Function with parameters and without return value
void display(int num) {
printf("Number: %d\n", num);
}
3)Function without parameters and with return value
int getValue() {
return 10;
}
4)Function with parameters and with return value
int sum(int a, int b) {
return a + b;
}
2. Differentiate pass by value and pass by address parameter
passing techniques.
Ans :

In C, there are two ways to pass arguments to a function:


Aspect Pass by Value Pass by Address
Modificatio No effect on original Modifies original
n value value
Memory Uses more memory Uses less memory
Data Transfers the
Copies the value
Transfer memory address
Risky as changes
Safer as original data
Safety reflect in original
remains unchanged
data
Example of Pass by Value:

#include <stdio.h>
void modifyValue(int x) {
x = 10;
}
int main() {
int a = 5;
modifyValue(a);
printf("Value of a: %d", a); // Output: 5
return 0;
}

Example of Pass by Address:

#include <stdio.h>
void modifyValue(int *x) {
*x = 10;
}
int main() {
int a = 5;
modifyValue(&a);
printf("Value of a: %d", a); // Output: 10
return 0;
}
3. How 2 – dimensional arrays are declared and initialized?
Write a ‘C’ program to find the transpose of a matrix.
Ans :

Declaration & Initialization:


int matrix[3][3] = {{1,2,3}, {4,5,6}, {7,8,9}};

Transpose Program:

#include <stdio.h>
void main() {
int a[3][3], i, j;
printf("Enter elements: ");
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
scanf("%d", &a[i][j]);
printf("Transpose:\n");
for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++)
printf("%d ", a[j][i]);
printf("\n");
}
}

4. Write a C program to implement Binary Search for integers.


Ans :

#include <stdio.h>
int binarySearch(int arr[], int low, int high, int key) {
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == key) return mid;
else if (arr[mid] < key) low = mid + 1;
else high = mid - 1;
}
return -1;
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int key = 3;
int index = binarySearch(arr, 0, 4, key);
if (index != -1)
printf("Element found at index %d", index);
else
printf("Element not found");
return 0;
}

5. Explain function call, function definition and function


prototype with syntax and example for each.
Ans :

Function Prototype
o Declares a function before its definition.
o Helps the compiler recognize the function.
Syntax:
return_type function_name(data_type, data_type);
Example:
int sum(int, int);

Function Definition
Contains the actual implementation of the function.

Syntax:
return_type function_name(data_type param1, data_type param2)
{
// Function body
return value;
}
Example:

int sum(int a, int b) {


return a + b;
}
Function Call
Executes the function.

Syntax:

variable = function_name(arguments);

Example:

int result = sum(5, 10);

Complete Example:

#include <stdio.h>

// Function Prototype
int sum(int, int);

int main() {
int result = sum(5, 10); // Function Call
printf("Sum: %d", result);
return 0;
}

// Function Definition
int sum(int a, int b) {
return a + b;
}
Output:
Sum: 15

6. What is an array? Explain the declaration and initialization


of 1-D arrays.
Ans :
An array is a collection of elements of the same data type stored
in contiguous memory locations. It allows efficient access and
manipulation of data using an index.

Declaration of 1-D Array


Syntax:
data_type array_name[size];

Example:

int numbers[5];

Initialization of 1-D Array

Partial Initialization:

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

Complete Example

#include <stdio.h>

int main() {
int numbers[5] = {10, 20, 30, 40, 50}; // Declaration and
Initialization

printf("Array elements: ");


for (int i = 0; i < 5; i++) {
printf("%d ", numbers[i]); // Accessing array elements
}

return 0;
}

7. Write a C program to sort the array elements in ascending


order.
Ans :
#include <stdio.h>

void sort(int arr[], int n) {


for (int i = 0; i < n - 1; i++)
for (int j = 0; j < n - i - 1; j++)
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}

int main() {
int n, i, arr[100];

printf("Enter number of elements: ");


scanf("%d", &n);

printf("Enter elements: ");


for (i = 0; i < n; i++) scanf("%d", &arr[i]);

printf("Original array: ");


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

sort(arr, n);

printf("\nSorted array: ");


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

return 0;
}

You might also like