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

Cs3251 UNIT IV QBANK

programming in C

Uploaded by

merrysummer
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)
7 views6 pages

Cs3251 UNIT IV QBANK

programming in C

Uploaded by

merrysummer
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/ 6

UNIT IV

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;
};

// Declare the union for student


union StudentUnion {
char name[50];
int age;
float gpa;
};
struct StudentStruct student1;
name: 50 bytes
age: 4 bytes (assuming an int is 4 bytes)
gpa: 4 bytes (assuming a float is 4 bytes)
Total: 58 bytes
union StudentUnion student2;
name: 50 bytes (largest member)
age: 4 bytes
gpa: 4 bytes
Total: 50 bytes (the size of the largest member)
Memory Allocation:
Structure: Allocates separate memory for each member.
Union: Allocates a single shared memory block, large enough for the largest member.
Accessing Members:
Structure: All members can be accessed and hold valid values independently.
Union: Only the most recently assigned member holds a valid value; previous values are
overwritten.

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);

double squareRoot = sqrt(number);


printf("Square root of %.2lf = %.2lf\n", number, squareRoot);
return 0;
}
4 Illustrate a code to access structure member variables through array of structure variables. AP
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);
}
5 Show the difference between array and a structure by declaring and defining a structure and AP
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.
6 With point(x,y),show copying and comparing operation on structure.
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");
}
7 If we have structure B nested inside structure A, when do we declare structure B? AP
Ans:
struct B {
int member1;
float member2;
};
// Declare structure A with structure B nested inside
struct A {
char name[50];
int id;
struct B nested;
};
struct A instanceA;
8 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.(8)
Ans:
#include <stdio.h>
#include <stdlib.h> // For dynamic memory allocation
// Define the Book structure
struct Book {
char title[100];
char author[100];
float price;
};
int main() {
// Declare a pointer to a Book structure
struct Book *ptrBook;
// Allocate memory for a Book structure
ptrBook = (struct Book *)malloc(sizeof(struct Book));
// Check if memory allocation is successful
if (ptrBook == NULL) {
printf("Memory allocation failed!\n");
return 1; // Exit program with error
}
// Assign values to the members using the pointer
printf("Enter title: ");
scanf("%s", ptrBook->title);
printf("Enter author: ");
scanf("%s", ptrBook->author);
printf("Enter price: ");
scanf("%f", &ptrBook->price);
// Print the details using the pointer
printf("\nBook details:\n");
printf("Title: %s\n", ptrBook->title);
printf("Author: %s\n", ptrBook->author);
printf("Price: %.2f\n", ptrBook->price);
// Free the allocated memory
free(ptrBook);
return 0;
}
9 Write a C program that defines a structure called Employee with members name, id, and salary. AP
Input details for two employees and display them.(7)
Ans:
#include <stdio.h>

// Define the Employee structure


struct Employee {
char name[100];
int id;
float salary;
};

int main() {
// Declare variables to store details of two employees
struct Employee emp1, emp2;

// Input details for the first employee


printf("Enter details for the first employee:\n");
printf("Name: ");
scanf("%s", emp1.name);
printf("ID: ");
scanf("%d", &emp1.id);
printf("Salary: ");
scanf("%f", &emp1.salary);

// Input details for the second employee


printf("\nEnter details for the second employee:\n");
printf("Name: ");
scanf("%s", emp2.name);
printf("ID: ");
scanf("%d", &emp2.id);
printf("Salary: ");
scanf("%f", &emp2.salary);

// Display details of both employees


printf("\nDetails of the first employee:\n");
printf("Name: %s\n", emp1.name);
printf("ID: %d\n", emp1.id);
printf("Salary: %.2f\n", emp1.salary);

printf("\nDetails of the second employee:\n");


printf("Name: %s\n", emp2.name);
printf("ID: %d\n", emp2.id);
printf("Salary: %.2f\n", emp2.salary);

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);

// Allocate memory for the employees


Employee* employees = (Employee*)malloc(numEmployees * sizeof(Employee));

// Input details for each employee


for (int i = 0; i < numEmployees; ++i) {
printf("Enter details for Employee %d:\n", i + 1);
printf("Name: ");
scanf("%s", employees[i].name);
printf("ID: ");
scanf("%d", &employees[i].id);
printf("Salary: ");
scanf("%lf", &employees[i].salary);
}

// Display employee information


printf("\nEmployee Details:\n");
for (int i = 0; i < numEmployees; ++i) {
printf("Employee %d:\n", i + 1);
printf("Name: %s\n", employees[i].name);
printf("ID: %d\n", employees[i].id);
printf("Salary: %.2lf\n", employees[i].salary);
}

// Free allocated memory


free(employees);
return 0;
}

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy