0% found this document useful (0 votes)
68 views4 pages

Lab Sheet 9

1. The document describes structures in C programming, including how to define, declare, and access structure variables. It provides an example of a Person structure. 2. Practice exercises are provided to work with structures, including creating a Company structure, defining a complex number structure, and reading student records from a file into an array of structures. 3. Functions are described to populate the student records array, generate email addresses, store emails, print records, and calculate average CGPA. A second problem involves analyzing names from a sample population file to determine if shorter names are preferred.

Uploaded by

CHAHAT GULATI
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)
68 views4 pages

Lab Sheet 9

1. The document describes structures in C programming, including how to define, declare, and access structure variables. It provides an example of a Person structure. 2. Practice exercises are provided to work with structures, including creating a Company structure, defining a complex number structure, and reading student records from a file into an array of structures. 3. Functions are described to populate the student records array, generate email addresses, store emails, print records, and calculate average CGPA. A second problem involves analyzing names from a sample population file to determine if shorter names are preferred.

Uploaded by

CHAHAT GULATI
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/ 4

BIRLA INSTITUTE OF TECHNOLOGY & SCIENCE, PILANI (RAJ.

)
CS F111 Computer Programming
LABORATORY SESSION #9
(Structures, recursion)

Define Structures

Syntax of struct

struct structureName {
dataType member1;
dataType member2;
...
};

Example,

struct Person {
char name[50];
int citNo;
float salary;
};

Create struct Variables

struct Person {
// code
};
int main() {
struct Person person1, person2, p[20];
return 0;
}

Access Members of a Structure

person2.salary

Example,

#include <stdio.h>
#include <string.h>
// create struct with person1 variable
struct Person {
char name[50];
int citNo;
float salary;
} person1;

int main() {
// assign value to name of person1
strcpy(person1.name, "George Orwell");
// assign values to other person1 variables
person1.citNo = 1984;
person1. salary = 2500;
// print struct variables
printf("Name: %s\n", person1.name);
printf("Citizenship No.: %d\n", person1.citNo);
printf("Salary: %.2f", person1.salary);

return 0;
}

Practice Exercise

1 Write a program to create a structure named company which has name, address,
phone and noOfEmployee as member variables. Read name of company, its
address, phone and noOfEmployee. Finally display these members‟ value.

2 Define a structure “complex” (typedef) to read two complex numbers and perform
addition, subtraction of these two complex numbers and display the result.

3 The following details of students are made available in a file (as comma separated fields):
ID number, name, gender (M or F), age (a whole number),
residential status (H for hostel resident, D for day scholar), and
CGPA.
Two sample rows (i.e., two student records) of the data file are shown below:

2014A7PS0108P,Indra Gopinath,F,21,H,8.55

2017B1TS1055P,Mohammed Farhan,M,18,D,0.0

Write a well-commented C program, made modular using user-defined functions, to


accomplish the tasks listed below. You may use the file stud_records.c. The main()
function with function calls, function prototypes as well as definitions are all there. The
data file is available in stud_records.txt which you can use.
(a) Read each student record from the data file, and store in an array of structures,
each element of the array representing a record. Write a function with the following
prototype to do this task. void populateRecords(struct studsarr[], int
no_studs);
/* function to get data into the array */
(b) The complete definition of the function for generating the email address (for a given
student ID number) has been provided to you. void generateEmailAddress(char
idno[], char email[]);
/* function definition already given */
Read the function definition and learn how it works, and also how to write
meaningful comments for code. Also note the use of the function atoi() in the
function. You can look up the manual of this function to know more about it.
Now write the following function that calls generateEmailAddress() and then stores
the email address in the right field of each student record of the array:
void storeEmailAddresses(struct studsarr[], int no_studs);

(c) Print out details of all students by writing a function whose prototype is: void
printRecords(struct studs [], int);
Formatted output should appear as shown below (and in the next page):
2014A7PS0108P Indra Gopinath
Gender: F
Age: 21
Residence status: Hostel
CGPA: 8.55
Email: [email protected]
-------------------------------------------------
2017B1TS1055P Mohammed Farhan
Gender: M
Age: 18
Residence status: Day scholar
CGPA: Not available
Email: [email protected]
-------------------------------------------------
(d) Write a function to compute the average CGPA of all CGPAs that are available, and to
return that a value back to main():
float calculateAvgCG(STUD *, int);

4 "Do Indian parents prefer giving shorter names over longer ones to their children?" You
will write a well-commented and modular C program to help get a possible answer to
this question, using data from a given sample population. You will use the following
structure definition in your program:
typedef struct { char fname[70]; /* first name (a single word) */
int freq; /* how many people in the sample population have this
name */ int len; /* stores the length of the name */
} NAME;

Given a sample size of N individuals, and a positive integer threshold denoting a length,
your program should do these tasks:

(a) Obtain data for N individuals and store it appropriately using the function:
void getData(NAME arr[]);

(b) Calculate and print the percentage of people in the sample population whose
first names are shorter than or equal to the threshold length, using another
function:
float findPercent(NAME arr[], int threshold);

The file names.txt has sample data – each line (except the first one) containing a unique
first name and the frequency of its occurrence in the sample population. For example,
in the sample data shown below, there are six people with the name Abhinav, four with
Abhimanyu, two with Abhay and one each of Abhas, Abhay, Abhijeet and Abhijit:

ABHAS 1
ABHAY 2
ABHIJEET 1
ABHIJIT 1
ABHIMANYU 4
ABHINAV 6

The first line of the file contains two numbers – the number of unique names (stored one
per row) and the value for threshold.

You might also like