C Programming - BESCK104E/204E
Module 5: Complete Answers
Q1. Define a pointer and explain the declaration and initialization of a pointer variable with an
example.
A pointer is a variable that stores the address of another variable.
Declaration:
int *ptr;
Initialization:
int x = 10; ptr = &x;
Accessing value:
printf("%d", *ptr);
Q2. Define a structure and explain its declaration and initialization with an example.
A structure is a user-defined data type that groups variables of different types.
Declaration:
struct Student {
int id;
char name[20];
float marks;
};
Initialization:
struct Student s1 = {1, "John", 89.5};
Q3. Explain how members of a structure are accessed, initialized, and declared.
Declaration:
struct Student s1;
Initialization:
[Link] = 101;
strcpy([Link], "Ravi"); [Link] = 95.5;
Access:
printf("%d %s %.2f", [Link], [Link], [Link]);
Q4. Implement structures to read, write and compute average marks and the students scoring above
and below the average marks for a class of N students.
#include <stdio.h>
struct Student {
char name[20];
float marks;
};
int main() {
struct Student s[100];
int n; float sum = 0, avg;
printf("Enter number of students: ");
scanf("%d", &n);
for(int i=0; i<n; i++) {
printf("Enter name and marks: ");
scanf("%s %f", s[i].name, &s[i].marks);
sum += s[i].marks;
avg = sum / n;
printf("Above average:\n");
for(int i=0;i<n;i++) if(s[i].marks > avg) printf("%s %.2f\n", s[i].name, s[i].marks);
printf("Below average:\n");
for(int i=0;i<n;i++) if(s[i].marks < avg) printf("%s %.2f\n", s[i].name, s[i].marks);
return 0;
Q5. Develop a program using pointers to compute the sum, mean, and standard deviation of all
elements stored in an array of N real numbers.
#include <stdio.h>
#include <math.h>
int main() {
float arr[100], *p, sum = 0, mean, sd = 0;
int n, i;
printf("Enter number of elements: ");
scanf("%d", &n);
p = arr;
for(i=0; i<n; i++) {
scanf("%f", p+i);
sum += *(p+i);
mean = sum / n;
for(i=0; i<n; i++) {
sd += pow(*(p+i) - mean, 2);
sd = sqrt(sd / n);
printf("Sum = %.2f\nMean = %.2f\nSD = %.2f\n", sum, mean, sd);
return 0;