Cs3251 UNIT IV QBANK
Cs3251 UNIT IV QBANK
PART A
1 Create a structure ‘Book’ and declare members for the structure with structure variable. AP
Ans:
struct Book {
char title[100];
char author[50];
int pages;
float price;
};
struct Book myBook;
2 Declare a structure and a union for ‘student’ and show their difference in memory allocation AP
and accessing members.
struct StudentStruct {
char name[50];
int age;
float gpa;
};
3 Write a program that utilizes the sqrt function from the <math.h> library to calculate the AP
square root of a user-inputted number.
Ans:
#include <stdio.h>
#include <math.h>
int main() {
double number;
printf("Enter a number: ");
scanf("%lf", &number);
int main() {
// Declare variables to store details of two employees
struct Employee emp1, emp2;
return 0;
}
1 Illustrate a code to access structure member variables through array of structure variables. AP
0 Ans:
struct Student {
char name[50];
int age;
float gpa;
}students[3];
for(int i = 0; i < 3; i++) {
printf("Student %d Details:\n", i + 1);
printf("Name: %s\n", students[i].name);
printf("Age: %d\n", students[i].age);
printf("GPA: %.2f\n\n", students[i].gpa);
}
1 Show the difference between array and a structure by declaring and defining a structure and AP
1 array variable.
Ans:
Array Declaration and Definition:
An array scores of integers with 3 elements is declared and initialized with values {85, 90, 78}.
The elements of the array are accessed using indices (scores[0], scores[1], scores[2]).
Structure Declaration and Definition:
A structure Student is defined with three members: name (a character array), age (an integer),
and gpa (a float).
A structure variable student1 of type Student is declared.
The members of student1 are initialized using strcpy for the name and direct assignment for
age and gpa.
Accessing Array Elements:
The array elements are accessed and printed using a loop, demonstrating that all elements are
of the same type and are accessed by their indices.
Accessing Structure Members:
The structure members are accessed and printed using the dot operator (.), demonstrating that
a structure can hold different types of data grouped together
1 With point(x,y),demonstrate copying and comparing operation on structure. AP
2 Ans:
struct Point {
int x;
int y;
};
struct Point point1 = {10, 20};
struct Point point2;
point2=point1;
if (p1.x == p2.x && p1.y == p2.y) {
printf("The points are equal.\n");
} else {
printf("The points are not equal.\n");
}
1 Define a structure ‘Book’ and write a C code to access the structure members to perform AP
3 operations in main function.
Ans:
struct Book {
char title[50];
char author[50];
float price;
int publicationYear;
};
struct Book myBook;
For accessing member variables with structure variable we use dot(.) operator.
strcpy(myBook.title, "The Great Gatsby");
strcpy(myBook.author, "F. Scott Fitzgerald");
myBook.price = 10.99;
myBook.publicationYear = 1925;
1 Use ‘typedef’ to define a new type ‘Person’ for a structure containing ‘name’ and ‘age’. Write a AP
4 code to initialize and print.
Ans:
typedef struct {
char name[50];
int age;
} Person;
Main(){
Person person1;
strcpy(person1.name, "John Doe");
person1.age = 30;
printf("Person Details:\n");
printf("Name: %s\n", person1.name);
printf("Age: %d\n", person1.age);
}
1 Define a self-referential structure Node for a singly linked list and write a code to create and link AP
5 two nodes.
Ans:
struct Node {
int data;
struct Node* next;
};
struct Node* firstNode = (struct Node*)malloc(sizeof(struct Node));
firstNode->data = 10;
firstNode->next = NULL;
secondNode->data = 20;
secondNode->next = NULL;
// Link the first node to the second node
firstNode->next = secondNode;
1 Write a program to dynamically allocate memory for a structure Employee, initialize its AP
6 members, and print the details.
Ans:
typedef struct {
char name[50];
int id;
double salary;
} Employee;
int main() {
int numEmployees;
printf("Enter the number of employees: ");
scanf("%d", &numEmployees);
PART B
1 Write a C program that defines a structure called Employee with members name, id, and AP
salary. Input details for two employees and display them.
2 Write a nested structure where Employee contains an Address structure with members street, AP
city, and zip. Write a program to input and print an employee's details.
3 Define a union within a structure to store different types of books in a library using C program AP
and to access the member variables and print it.
4 Demonstrate the use of different storage classes and visibility within a C program to store AP
variables and perform operations.
5 Define a structure for employee records and use pointers to access and modify the members AP
of the structures. Write a code to print the same.
6 Define and implement functions to add, delete, and display student records using a singly AP
linked list using C program.
7 Create an array to store multiple students and perform operations on this array. AP
8 Define a typedef for a structure representing a television (make, model, year). Write a AP
program to create an array of television using this typedef. Implement functions to:
i)Add a new television to the array
ii)Display all televisions
iii)Find the newest television in the array
Use pointers to pass the array to these functions.
9 Create an array of Person structures with members name and age. Write a program to print the AP
details of all persons in the array.
10 Create a structure ‘Book’ with members title, author, and price. Use a pointer to a Book AP
structure to assign values and print the details.
11 Write a C program to demonstrate pass by value and reference parameter passing. Define a AP
function increment. Input an integer in main, call increment function, and print the incremented
value.
12 Define a typedef for a structure representing a vehicle (make, model, year). Write a program to AP
create an array of vehicles using this typedef. Implement functions to:
i)Add a new vehicle to the array
ii)Display all vehicles
iii)Find the newest vehicle in the array
Use pointers to pass the array to these functions.