Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 21

SVKM’s NMIMS University

Mukesh Patel School of Technology Management & Engineering


COURSE: Programming for Problem Solving

Experiment: 16
PART A

(PART A: TO BE REFFERED BY STUDENTS)

Aim: To study concept of recursion function, structures and union in C programming

Learning Outcomes: Learner would be able to

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.

The following is an example of a structure declaration:


struct automobile {
int year;
char model[8];
int engine_power;
float weight;
};

Defining Structure Variables


After declaring a structure, you can define the structure variables. For instance, the following
structure variables are defined with the structure data type of automobile from the previous
section:

struct automobile sedan;

Referencing Structure Members with the Dot Operator


Now, let’s see how to reference a structure member. Given the structure automobile and the
structure variable sedan, for instance, I can access its member, year, and assign an integer to
it in the following way:

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.

Example: Referencing the members of a structure

$include <stdio.h>

main(void)
{
struct computer {
float cost;
int year;
int cpu_speed;
char cpu_type[16];
} model;

printf(“The type of the CPU inside your computer?\n”);


gets(model.cpu_type);
printf(“The speed(MHz) of the CPU?\n”);
scanf(“%d”, &model.cpu_speed);
printf(“The year your computer was made?\n”);
scanf(“%d”, &model.year);
printf(“How much you paid for the computer?\n”);
scanf(“%f”, &model.cost);

printf(“Here are what you entered:\n”);


printf(“Year: %d\n”, model.year);
printf(“Cost: $%6.2f\n”, model.cost);
printf(“CPU type: %s\n”, model.cpu_type);
printf(“CPU speed: %d MHz\n”, model.cpu_speed);

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

(PART B: TO BE COMPLETED BY STUDENTS)

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

Roll No.: Name:


Prog/Yr/Sem: Batch:
Date of Experiment: Date of Submission:

Task 1:

#include <stdio.h>

typedef struct student

char name[20];

int roll_no;

int marks[3];

int grand;

}stu;

int main()

int n;

scanf("%d", &n);

stu database[n];

for(int i = 0; i < n; i++)

{
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving

stu student;

printf("\nEnter Name: ");

scanf("%s", student.name);

printf("Enter Roll Number: ");

scanf("%d", &student.roll_no);

printf("Enter the marks for the 3 tests: ");

scanf("%d %d %d", &student.marks[0], &student.marks[1], &student.marks[2]);

student.grand = student.marks[0] + student.marks[1] + student.marks[2];

database[i] = student;

for(int i = 0; i < n; i++)

for(int j = i+1; j < n; j++)

if(database[i].grand > database[j].grand)

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

for(int i = n-1; i >= 0; i--)

stu s = database[i];

printf("%s\t%d\t%d\n", s.name, s.roll_no, s.grand);

return 0;

Task 2:

#include <stdio.h>

typedef struct date

int day, month, year;

}D;

typedef struct employee

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

emp e[n]; emp w[n];

for(int i = 0; i < n; i++)

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("Date of joining: ");

scanf("%d %d %d", &e[i].d.day, &e[i].d.month, &e[i].d.year);

w[i].d.day = e[i].d.day; w[i].d.month = e[i].d.month; w[i].d.year = e[i].d.year;

printf("1 for Max Min Salary, 2 for Ascending Salary, 3 for Ascending Date, 4 for dept
wise:\n ");

int ch; scanf("%d", &ch);

for(int i = 0; i < n; i++)

for(int j = i+1; j < n; j++)

if(e[i].salary > e[j].salary)

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("Name: %s\n", e[n-1].name);

printf("Code: %d\n", e[n-1].code);

printf("Salary: %d\n", e[n-1].salary);

printf("Dept: %d\n", e[n-1].dept);

printf("Date: %d/%d/%d\n", e[n-1].d.day, e[n-1].d.month, e[n-1].d.year);

printf("Min: \n");

printf("Name: %s\n", e[0].name);

printf("Code: %d\n", e[0].code);

printf("Salary: %d\n", e[0].salary);

printf("Dept: %d\n", e[0].dept);

printf("Date: %d/%d/%d\n", e[0].d.day, e[0].d.month, e[0].d.year);

break;

case 2:

for(int i = 0; i < n; i++)

printf("%s\t%d\t%d\t%d\t%d/%d/%d\n", e[i].name, e[i].code, e[i].salary, e[i].dept,


e[i].d.day, e[i].d.month, e[i].d.year);

break;

case 3:
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving

for(int i = 0; i < n; i++)

for(int j = i+1; j < n; j++)

if(w[i].d.year > w[j].d.year)

emp a = w[i];

w[i] = w[j];

w[j] = a;

else if(w[i].d.year == w[j].d.year)

if(w[i].d.month > w[j].d.month)

emp a = w[i];

w[i] = w[j];

w[j] = a;

else if(w[i].d.month == w[j].d.month)

if(w[i].d.day > w[j].d.day)

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

for(int i = 0; i < n; i++)

printf("%s\t%d\t%d\t%d\t%d/%d/%d\n", w[i].name, w[i].code, w[i].salary,


w[i].dept, w[i].d.day, w[i].d.month, w[i].d.year);

break;

case 4:

for(int i = 0; i < n; i++)

if(e[i].dept == 1)

printf("Dept 1\n");

printf("Name: %s\t", e[i].name);


SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving

printf("Code: %d\t", e[i].code);

printf("Salary: %d\t", e[i].salary);

printf("Date: %d/%d/%d\n", e[i].d.day, e[i].d.month, e[i].d.year);

else if(e[i].dept == 2)

printf("Dept 2\n");

printf("Name: %s\t", e[i].name);

printf("Code: %d\t", e[i].code);

printf("Salary: %d\t", e[i].salary);

printf("Date: %d/%d/%d\n", e[i].d.day, e[i].d.month, e[i].d.year);

else if(e[i].dept == 3)

printf("Dept 3\n");

printf("Name: %s\t", e[i].name);

printf("Code: %d\t", e[i].code);

printf("Salary: %d\t", e[i].salary);

printf("Date: %d/%d/%d\n", e[i].d.day, e[i].d.month, e[i].d.year);

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

printf("Name: %s\t", e[i].name);

printf("Code: %d\t", e[i].code);

printf("Salary: %d\t", e[i].salary);

printf("Date: %d/%d/%d\n", e[i].d.day, e[i].d.month, e[i].d.year);

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;

int day, month, year;

char address[50];

};

int main()

union data D;

printf("\nEnter Age: ");

scanf("%d", &D.age);

printf("Enter Address: ");

gets(D.address);

gets(D.address);

printf("Enter DOB: ");

scanf("%d %d %d", &D.day, &D.month, &D.year);

return 0;

Task4:

#include <stdio.h>

int gcd(int n1, int n2);


SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving

int main()

int n1, n2;

printf("Enter numbers: ");

scanf("%d %d", &n1, &n2);

int g = gcd(n1, n2);

printf("G.C.D: %d", g);

return 0;

int gcd(int n1, int n2)

if (n2 != 0)

return gcd(n2, n1 % n2);

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

Home Work Questions:


1. Write a program to implement merger sort using recursion
#include <stdio.h>

// Merge two subarrays L and M into arr


void merge(int arr[], int p, int q, int r) {

// Create L ← A[p..q] and M ← A[q+1..r]


int n1 = q - p + 1;
int n2 = r - q;

int L[n1], M[n2];

for (int i = 0; i < n1; i++)


L[i] = arr[p + i];
for (int j = 0; j < n2; j++)
M[j] = arr[q + 1 + j];
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving

// Maintain current index of sub-arrays and main array


int i, j, k;
i = 0;
j = 0;
k = p;

// Until we reach either end of either L or M, pick larger among


// elements L and M and place them in the correct position at A[p..r]
while (i < n1 && j < n2) {
if (L[i] <= M[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = M[j];
j++;
}
k++;
}

// When we run out of elements in either L or M,


// pick up the remaining elements and put in A[p..r]
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}

while (j < n2) {


arr[k] = M[j];
j++;
k++;
}
}
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving

// Divide the array into two subarrays, sort them and merge them
void mergeSort(int arr[], int l, int r) {
if (l < r) {

// m is the point where the array is divided into two subarrays


int m = l + (r - l) / 2;

mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);

// Merge the sorted subarrays


merge(arr, l, m, r);
}
}

// Print the array


void printArray(int arr[], int size) {
for (int i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}

// Driver program
int main() {
int n;
scanf("%d", &n);
int arr[n];

for(int i = 0; i < n; i++)


scanf("%d", &arr[i]);
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving

mergeSort(arr, 0, n-1);

printf("Sorted array: \n");


printArray(arr, n);
}

2. Write a program to demonstrate how to pass array of structures to a function


#include <stdio.h>

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

display(a); //passing structure to the function

return 0;
}

void display(struct d a)
{
printf("Name: %s\t", a.name);
printf("Phone: %d", a.phone);

You might also like