Professional Documents
Culture Documents
J017 Prac15
J017 Prac15
Experiment: 16
PART A
1. Create and store values in user defined data types like structure and union
2. Demonstrate the use of recursion function, structures and union.
Task 1:
Create a structure ‘student’ which stores name, roll no, marks of 3 tests (each out of 100) and grand
total. Write a program to accept all the information for n records of the students, calculate their grand
total and arrange them in the descending order of their grand total.
Task 2:
Declare a structure to store the following information of an employee -
Employee Code
Employee Name
Salary
Department Name
Date of joining (it is a structure consisting of day, month and year)
Write a c program to store the data of n employees where n is given by the user. Include a
menu that will all user to select any of the following features:
1. Use a function to display the employee information getting the maximum and
minimum salary
2. Use a function to display the employee records in ascending order according to their
salary
3. Use a function to display the employee records in ascending order according to their
date of joining
4. Use a function to display that department wise employee records.
Task 3:
Write a program to implement a simple database program that will store a person’s details
such as age, date of birth and address using the concept of union
Task 4:
Write a program to find the greatest common divisor of two numbers using recursion
function.
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving
Theory:
In C, a structure collects different data items in such a way that they can be referenced as a
single unit.
There are several major differences between an array and a structure. Besides the fact that
data items in a structure can have different types, each data item has its own name instead of
a subscript value. In fact, data items in a structure are called members of the structure.
Declaring Structures
The general form to declare a structure is
struct struct_tag {
data_type1 variable1;
data_type2 variable2;
data_type3 variable3;
.
.
.
};
Here struct is the keyword used in C to start a structure declaration. struct_tag is the tag name
of the structure. variable1, variable2, and variable3 are the members of the structure. Their
data types are specified respectively by data_type1, data_type2, and data_type3. As you can
see, the declarations of the members have to be enclosed within the opening and closing
braces ({ and }) in the structure declaration, and a semicolon (;) has to be included at the end
of the declaration.
sedan.year = 1997;
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving
Arrays of Structures
In C, you can declare an array of structures by preceding the array name with the structure
name. For instance, given a structure with the tag name of x, the following statement:
struct x array_of_structure[8];
declares an array, called array_of_structure, of struct x. The array has eight elements, each
element being a single instance of struct x.
$include <stdio.h>
main(void)
{
struct computer {
float cost;
int year;
int cpu_speed;
char cpu_type[16];
} model;
return 0;
}
OUTPUT:
The type of the CPU inside your computer?
Pentium
The speed(MHz) of the CPU?
100
The year your computer was made?
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving
1996
How much you paid for the computer?
1234.56
Here are what you entered:
Year: 1996
Cost: $1234.56
CPU type: Pentium
CPU speed: 100 MHz
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving
PART B
Students must submit the soft copy as per following segments within two hours of
the practical. The soft copy must be uploaded on the portal at the end of the
practical. The filename should be PPS_batch_rollno_experimentno Example:
PPS_B2_B001_Exp1
Task 1:
#include <stdio.h>
char name[20];
int roll_no;
int marks[3];
int grand;
}stu;
int main()
int n;
scanf("%d", &n);
stu database[n];
{
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving
stu student;
scanf("%s", student.name);
scanf("%d", &student.roll_no);
database[i] = student;
stu a = database[i];
database[i] = database[j];
database[j] = a;
printf("Name\tRoll\tTotal\n");
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving
stu s = database[i];
return 0;
Task 2:
#include <stdio.h>
}D;
{
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving
char name[20];
int code;
int salary;
int dept;
D d;
}emp;
int main()
int n;
scanf("%d", &n);
printf("\nName: ");
scanf("%s", e[i].name);
strcpy(w[i].name, e[i].name);
printf("Code: ");
scanf("%d", &e[i].code);
w[i].code = e[i].code;
printf("Salary: ");
scanf("%d", &e[i].salary);
w[i].salary = e[i].salary;
printf("Dept: ");
scanf("%d", &e[i].dept);
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving
w[i].dept = e[i].dept;
printf("1 for Max Min Salary, 2 for Ascending Salary, 3 for Ascending Date, 4 for dept
wise:\n ");
emp a = e[i];
e[i] = e[j];
e[j] = a;
switch(ch)
{
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving
case 1:
printf("Max: \n");
printf("Min: \n");
break;
case 2:
break;
case 3:
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving
emp a = w[i];
w[i] = w[j];
w[j] = a;
emp a = w[i];
w[i] = w[j];
w[j] = a;
emp a = w[i];
w[i] = w[j];
w[j] = a;
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving
break;
case 4:
if(e[i].dept == 1)
printf("Dept 1\n");
else if(e[i].dept == 2)
printf("Dept 2\n");
else if(e[i].dept == 3)
printf("Dept 3\n");
else if(e[i].dept == 4)
printf("Dept 4\n");
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving
break;
return 0;
Task 3:
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving
#include <stdio.h>
union data
int age;
char address[50];
};
int main()
union data D;
scanf("%d", &D.age);
gets(D.address);
gets(D.address);
return 0;
Task4:
#include <stdio.h>
int main()
return 0;
if (n2 != 0)
else
return n1;
}
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving
Conclusion (Learning Outcomes): Reflect on the questions answered by you jot down your
learnings about the Topic: Pointers
// Divide the array into two subarrays, sort them and merge them
void mergeSort(int arr[], int l, int r) {
if (l < r) {
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
// Driver program
int main() {
int n;
scanf("%d", &n);
int arr[n];
mergeSort(arr, 0, n-1);
struct d //structure
{
char name[50];
int phone;
};
void display(struct d); //function with structure as a parameter
int main()
{
struct d a;//instance if the structure
printf("Enter name: ");
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving
gets(a.name);
printf("Enter phone: ");
scanf("%d", &a.phone);
return 0;
}
void display(struct d a)
{
printf("Name: %s\t", a.name);
printf("Phone: %d", a.phone);