Programming in C (PIC) - Module Bank (M2-T1)
Complete Solutions (All 10 Questions)
SUBJECT PIC
ASSIGNMENT NO 01
SUBMITTED BY Munyaradzi T Musemburi
REGISTER NUMBER 251FA06033
SECTION 52
SEMESTER 01
Q1(a) What is an array in C, and how do you declare it? Design a
function to read and display an array of N elements.
Answer
An array is a collection of elements of the same data type stored in contiguous memory locations.
Declaration: datatype arrayName[size];. Functions can take arrays as parameters to read and display
elements.
Flow Diagram (Concept Flow)
Start -> Read N -> Declare A[N] -> Input A[i] -> Print A[i] -> Stop
Algorithm
1. Read N
2. Declare integer array A[N]
3. Read N values into A
4. Display all values
C Program / Code
#include <stdio.h>
void readArray(int a[], int n){
for(int i=0;i<n;i++) scanf("%d",&a[i]);
}
void displayArray(int a[], int n){
for(int i=0;i<n;i++) printf("%d ",a[i]);
printf("\n");
}
int main(){
int n;
scanf("%d",&n);
int a[n];
readArray(a,n);
displayArray(a,n);
return 0;
}
Q2(a) Define two dimensional array with syntax and write a program
that accepts a 2D array (matrix).
Answer
A two-dimensional array is an array of arrays used to represent tabular data (rows and columns). Syntax:
datatype arrayName[rows][cols];.
Flow Diagram (Concept Flow)
Start -> Read r,c -> Read matrix A[r][c] -> Display matrix -> Stop
Algorithm
1. Read rows r and cols c
2. Declare matrix A[r][c]
3. Read elements row-wise
4. Print matrix
C Program / Code
#include <stdio.h>
int main(){
int r,c;
scanf("%d%d",&r,&c);
int a[r][c];
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
scanf("%d",&a[i][j]);
for(int i=0;i<r;i++){
for(int j=0;j<c;j++)
printf("%d ",a[i][j]);
printf("\n");
}
return 0;
}
Q3(a) Importance of Dynamic Memory Allocation. Explain malloc,
calloc, realloc and free.
Answer
Dynamic memory allocation allows memory to be allocated during runtime. It helps when the required size
is not known in advance. malloc() allocates a block of memory (uninitialized). calloc() allocates and
initializes to zero. realloc() changes the size of allocated memory. free() releases allocated memory back
to the system.
Flow Diagram (Concept Flow)
Start -> malloc/calloc -> Use memory -> realloc (optional) -> free -> Stop
Q4(a) Define a string and explain difference between %c, %s and
functions like strlen().
Answer
A string in C is a character array terminated by a null character '\0'. %c reads/prints a single character, %s
reads/prints a string. strlen() returns the length of a string excluding '\0'.
Flow Diagram (Concept Flow)
Start -> Input string -> Process using library functions -> Output -> Stop
Q5(a) List String Library Functions with syntax and explanation.
Answer
Common string library functions from <string.h>: strlen(str) length, strcpy(dest,src) copy, strcat(dest,src)
concatenate, strcmp(s1,s2) compare, strchr(str,ch) find character, strstr(str,sub) find substring.
Q6 Librarian stores book information using Structures. Discuss
advantages of structures.
Answer
Structures are user-defined data types that group different data types under one name. They are useful for
representing records like books (title, author, price, id). Advantages: better organization, easy data
handling, passing complete record to functions, improves readability.
Flow Diagram (Concept Flow)
Start -> Define struct -> Create variables/array of struct -> Read fields -> Display -> Stop
Q7(a) Array of structures. Program to store and display student/book
data.
Answer
An array of structures stores multiple records of the same structure type. It is used for storing multiple
entities like students/books.
Flow Diagram (Concept Flow)
Start -> Define struct -> Read N -> Loop read records -> Loop display records -> Stop
C Program / Code
#include <stdio.h>
struct Student{
int id;
char name[50];
float marks;
};
int main(){
int n;
scanf("%d",&n);
struct Student s[n];
for(int i=0;i<n;i++){
scanf("%d %s %f",&s[i].id,s[i].name,&s[i].marks);
}
for(int i=0;i<n;i++){
printf("%d %s %.2f\n",s[i].id,s[i].name,s[i].marks);
}
return 0;
}
Q8(a) Use of typedef keyword in C. When needed? Output of code
(concept).
Answer
typedef creates an alias (new name) for an existing data type. It improves readability and simplifies
complex declarations. Commonly used with structures and pointers, e.g., typedef struct Student
Student;.
Flow Diagram (Concept Flow)
Start -> Define typedef -> Use new type name -> Compile/Run -> Output
Q9(a) Declare array of pointers in C with example.
Answer
An array of pointers stores multiple pointer variables. Example: int *p[5]; is an array of 5 pointers to int.
Flow Diagram (Concept Flow)
Start -> Declare variables -> Store addresses in pointer array -> Access values -> Stop
C Program / Code
#include <stdio.h>
int main(){
int a=10,b=20,c=30;
int *p[3];
p[0]=&a; p[1]=&b; p[2]=&c;
for(int i=0;i<3;i++){
printf("%d ", *p[i]);
}
return 0;
}
Q10(a) Differences between array of integers and array of strings.
Answer
An integer array stores numeric values (int). A string array is typically a character array (char[]) ending with
'\0'. Memory: int array uses sizeof(int)*n bytes; string uses sizeof(char)*(len+1). Input/Output: integers use
%d; strings use %s.
Flow Diagram (Concept Flow)
Start -> Choose data type -> Declare array -> Input -> Process -> Output -> Stop