[go: up one dir, main page]

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

SPL Assignment 8

The document outlines a laboratory assignment for the Structured Programming Language course at United International University, detailing various experiments involving the use of structures in C programming. It includes code examples for creating and manipulating data types such as 'College', 'Course', and 'Employee', along with explanations of their functionality. The document also contains practice problems and examples demonstrating the application of structures in C.

Uploaded by

abujubair0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views12 pages

SPL Assignment 8

The document outlines a laboratory assignment for the Structured Programming Language course at United International University, detailing various experiments involving the use of structures in C programming. It includes code examples for creating and manipulating data types such as 'College', 'Course', and 'Employee', along with explanations of their functionality. The document also contains practice problems and examples demonstrating the application of structures in C.

Uploaded by

abujubair0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

United International University

Department of EEE

Course Code: EEE 2402 Trimester: FALL

Course Title: Structured Programming Language Laboratory

Section: A Group No: 02

Faculty Name: Md. Nure Alam Dipu

Experiment No: 08

Experiment Name: Structure in C

Submitter Name: Abu Jubair Submitter ID: 0212320028

Group Members:

Name: ID:

Junayet Antor 0212320018

Nafisul Islam Shifa 0212230061

Abu Jubair 0212320028

Date of Experiment: 5 January, 2025 Date of Submission: 12 January, 2025


Lab Assignment
Question :1(i)
Explanation: Firstly, we create a datatype “College” which contains characters and integers of
required fields. ‘myCollege’ is the declaration of the datatype we made earlier and we can use
this to enter the strings of the fields. We use ‘snprintf()’ to specify the maximum number of
characters to write so that we can prevent buffer overflow.

‘myCollege.collegeCode’ contains a certain number which limits the string to 3 characters plus
the null terminator. Same also goes for ‘myCollege.coolegeName’. After that, we declare the
strings of the fields which apparently shows in the output by using ‘printf()’. As we declare
characters in collegeCode and collegeName, we use %s while printing and use %d for the
integers. Finally, we return at 0 indicating the successful execution.

Code:

#include <stdio.h>

struct College

char collegeCode[5];

char collegeName[100];

int yearOfEstablishment;

int numberOfCourses;

};

int main()

struct College myCollege;

snprintf(myCollege.collegeCode, 4, "GSC");

snprintf(myCollege.collegeName, 100, "Example College");


myCollege.yearOfEstablishment = 2000;

myCollege.numberOfCourses = 10;

printf("College Code: %s\n", myCollege.collegeCode);

printf("College Name: %s\n", myCollege.collegeName);

printf("Year of Establishment: %d\n", myCollege.yearOfEstablishment);

printf("Number of Courses: %d\n", myCollege.numberOfCourses);

return 0;}

Result:

Lab Assignment
Question :1(ii)
Explanation: Firstly, we create a datatype “College” which contains characters and integers of
required fields. ‘myCollege’ is the declaration of the datatype we made earlier and we can use
this to enter the strings of the fields. We use ‘snprintf()’ to specify the maximum number of
characters to write so that we can prevent buffer overflow.

‘myCollege.collegeCode’ contains a certain number which limits the string to 3 characters plus
the null terminator. Same also goes for ‘myCollege.coolegeName’. After that, we declare the
strings of the fields which apparently shows in the output by using ‘printf()’. As we declare
characters in collegeCode and collegeName, we use %s while printing and use %d for the
integers. Finally, we return at 0 indicating the successful execution.

Code:

#include <stdio.h>

#define max_courses 50

#define max_name_length 100

struct Course

char name[max_name_length];

int duration;

int numberOfStudents;

};

int main()

struct Course courses[max_courses];

struct Course exampleCourse = {"Introduction to Programming", 12, 35};

courses[0] = exampleCourse;

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

printf("Duration: %d weeks\n", courses[0].duration);

printf("Number of Students: %d\n", courses[0].numberOfStudents);

return 0;}

Result:
Lab Assignment
Question :2
Explanation: Using ‘struct’, we create a user-defined datatype which contains several fields
like emp_id, name, designation and salary. We use integer for emp_id and character data type
others. ‘double’ datatype has used to represent floating-point numbers with double precision
and provides a larger range of values than what the ‘float’ data type. It is suitable for scientific
and engineering calculations that require high precision.

In ‘int main()’, we let users to choose the number of employees will be contained in the
database by using ‘num_employees’. After implying the datatype ‘Employee’, we use a ‘for’
loop which helps users to provide the information until the loop ends. As i=0 in the loop, we
have to increase it by 1 while showing employee number because Employee 0 is practically
incorrect. We use [^\n] in the ‘scanf()’ during adding Name to Address. The ^ symbol within
square brackets [] is used to create a negated character class, meaning it will match any
character that is not listed. In this case, ^\n means any character except the newline character.

After adding the information by users, we use another for loop to generate the list give all of
the information. As users have given the information of 3 employees, i=0,1 and 2 for the ‘for’
loop. We use %d for integers, %s for strings and %.2lf for the floating numbers with 2 numbers
after fraction.

Code:

#include <stdio.h>

struct Employee

{
int emp_id;

char name[20];

char designation[20];

char address[50];

double salary;

};

int main()

int num_employees,i;

printf("Enter the number of employees: ");

scanf("%d",&num_employees);

struct Employee employees[num_employees];

for (i=0;i<num_employees;i++)

printf("Enter details for employee %d:\n",i+1);

printf("Employee ID: ");

scanf("%d",&employees[i].emp_id);

printf("Name: ");

scanf(" %[^\n]",employees[i].name);

printf("Designation: ");

scanf(" %[^\n]",employees[i].designation);

printf("Address: ");

scanf(" %[^\n]",employees[i].address);

printf("Salary: ");
scanf("%lf",&employees[i].salary);

printf("\n");

printf("Employee Records:\n");

for (i=0;i<num_employees;i++)

printf("Employee %d:\n",i+1);

printf("Employee ID: %d\n",employees[i].emp_id);

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

printf("Designation: %s\n",employees[i].designation);

printf("Address: %s\n",employees[i].address);

printf("Salary: %.2lf\n",employees[i].salary);

printf("\n");

return 0;}

Result:
Practice Problems
Question :1
Explanation: In this code we can understand the use of structure in C.

After the header file we have used “struct” to declare a new datatype called “A”, and this
datatype will contain an integer type and a character datatype namely, “marks” and “grade”
respectively. Then we have declared a variable of A datatype called “A1”, which is global
variable.

In the main function we have declared another variable of A datatype called B1. Then in the
next line we have assigned the value of A1. So, the integer part will be 80 and the character
part will be A. Only if that was possible. -_- Then we simply just printed the value of A1. But to
get the access of the variable we need to use A1.marks for the integer value and A1.grade for
the character element.

Then, we have assigned the value of B1 same as A1. So now when we print the value of B1 we
will get the same result.

Result:
Practice Problems
Question :2
Explanation: In this code we first have declared a new type of datatype called “tag” by the
help of struct. This datatype can store an integer (i) and a character (c) type data. Then we have
also declared a void type function called “func” which takes variable of tag datatype.

In the main function, we have declared a variable of “tag” datatype called “var” where the
values are assigned “2” for integer and “s” for character. Then we have called func function
where the input is var.

What is happening in “func” function: The func function just prints the value of the variable,
number and then character respectively.

Result:
Example
Question :1
Explanation:
Here 'Distance' is our own build data type with an integer data type declared by 'int feet' and a
float data type declared by 'float inch' in it. 'dist1', 'dist2', 'sum' are those variables whose data
type is Distance. Now we will take user input to assign values to these variables. The values can
be either integer or float data types. To store these values, we use 'variable.feet' and
'variable.inch'.

For instance, if the user inputs 7.9 inches, we will store the value as '&dist1.inch'. This means
the value will be saved as a float data type since we declared 'inch' as a float type variable in
our custom 'Distance' data type.

After collecting the user input, we will calculate the values. 'sum.feet' indicates that the value
of 'sum' will be stored as an integer data type. This way, we get the feet and inch values
separately. However, if 'sum.inch' exceeds 12, it will be converted to 1 foot.

To address this issue, we use a conditional operation: when 'sum.inch > 12', 'sum.feet' will
increase by 1 and 'sum.inch = sum.inch - 12'.

Result:
Example
Question :2
Explanation: Here 'student' is our own build data type which has integer, character, and float
type data in it declared by 'int id', 'char name[20]', 'float percentage'. 'record' is the variable
whose data type is student, and it is a one-dimensional array.

Then initialize the integer id number by 'record.id', initialize the character type values by
'record.name', and a float type value by 'record.percentage'. For character type, we use the
'strcpy' function to copy the string into name. For showing the result, we print the information
by printf command.

Result:
Example
Question :3
Explanation: 'entry' is our own build data type that has an array of character data type
declared by 'char fname[20]', 'char phone[10]', and 'char lname[20]' in it. 'list[4]' is the variable
of our structured data type, and the size of the list is 4. Then we use a loop where we initialize
the loop variable 'i=0', and the loop will run till 'i<4' and increase every time by 1.

Now we take input from the user under the loop. The user will give the first name, and we will
save the data in 'list[i].fname'. Similarly, the last name and phone number will be taken from
the user by following this step.

At last, for printing the output, we again use a loop where we initialize 'i=0', and the loop will
run till 'i<4'. Each time, the value will increase by 1, and that will show us the result that has
been given.

Result:

You might also like