0% found this document useful (0 votes)
5 views6 pages

Assignment 2 Solved

The document is an assignment for the 2nd semester students of CSE/CST at St. Andrews Institute of Technology & Management, focusing on programming in C. It includes questions on call by reference, functions with arguments, factorial calculation, multiplication tables, pointers, file handling, and dynamic memory allocation, each accompanied by example code. The assignment aims to assess students' understanding of these fundamental programming concepts.

Uploaded by

inagar9990
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

Assignment 2 Solved

The document is an assignment for the 2nd semester students of CSE/CST at St. Andrews Institute of Technology & Management, focusing on programming in C. It includes questions on call by reference, functions with arguments, factorial calculation, multiplication tables, pointers, file handling, and dynamic memory allocation, each accompanied by example code. The assignment aims to assess students' understanding of these fundamental programming concepts.

Uploaded by

inagar9990
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

ST.

ANDREWS INSTITUTE OF TECHNOLOGY & MANAGEMENT, GURUGRAM

ASSIGNMENT NO:- 2

SEMESTER: 2nd

BRANCH: CSE/CST

SUBJECT NAME: PROGRAMMING IN PROBLEM SOLVING

SUBJECT CODE: ESC-CSE-101G

MAX MARKS: 20

Q1.

(A) Demonstrate the use of call by reference using an example.

In C, when we use call by reference, we pass the address of variables to a function. Changes made

inside the function reflect in the original variable.

Example:

#include <stdio.h>

void swap(int *a, int *b) {

int temp;

temp = *a;

*a = *b;

*b = temp;

int main() {

int x = 10, y = 20;

printf("Before swapping: x = %d, y = %d\n", x, y);

swap(&x, &y);

printf("After swapping: x = %d, y = %d\n", x, y);


return 0;

(B) Discuss functions with arguments in C.

In C, functions can take arguments which allow passing information to them when they are called.

Example:

#include <stdio.h>

void display(int age) {

printf("Age is: %d\n", age);

int main() {

display(25);

return 0;

Q2.

(A) Write a C program to find factorial of a given number using functions.

#include <stdio.h>

int factorial(int n) {

if (n == 0)

return 1;

else

return n * factorial(n - 1);

int main() {

int number;
printf("Enter a number: ");

scanf("%d", &number);

printf("Factorial of %d is %d\n", number, factorial(number));

return 0;

(B) Write a C program to write multiplication table of given input number.

#include <stdio.h>

void multiplicationTable(int num) {

for (int i = 1; i <= 10; i++) {

printf("%d x %d = %d\n", num, i, num * i);

int main() {

int number;

printf("Enter a number: ");

scanf("%d", &number);

multiplicationTable(number);

return 0;

Q3.

(A) Explain pointers with an example.

A pointer is a variable that stores the address of another variable.

Example:

#include <stdio.h>
int main() {

int x = 5;

int *p = &x;

printf("Value of x = %d\n", x);

printf("Address of x = %p\n", p);

printf("Value pointed to by p = %d\n", *p);

return 0;

(B) What is the difference between array of pointers and pointer to an array?

Array of Pointers vs Pointer to an Array:

- Array of pointers: array where each element is a pointer.

- Pointer to an array: pointer pointing to the whole array.

Examples:

Array of Pointers: char *arr[] = {"One", "Two", "Three"};

Pointer to an Array: int arr[5] = {1, 2, 3, 4, 5}; int (*ptr)[5] = &arr;

Q4.

(A) Explain file handling in C.

File handling in C allows us to store data permanently.

Steps:

1. Declare file pointer.

2. Open file using fopen().

3. Read/write using functions.

4. Close file using fclose().


Example:

#include <stdio.h>

int main() {

FILE *fp;

fp = fopen("example.txt", "w");

fprintf(fp, "Hello, file handling!");

fclose(fp);

return 0;

(B) Explain dynamic memory allocation in C.

Dynamic memory allocation allows memory to be allocated at runtime.

Functions used: malloc(), calloc(), realloc(), free()

Example:

#include <stdio.h>

#include <stdlib.h>

int main() {

int *ptr;

ptr = (int*) malloc(5 * sizeof(int));

if (ptr == NULL) {

printf("Memory not allocated.\n");

return 1;

for (int i = 0; i < 5; i++) {


ptr[i] = i + 1;

printf("%d ", ptr[i]);

free(ptr);

return 0;

You might also like