Lab Sheet 9
Lab Sheet 9
)
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;
};
struct Person {
// code
};
int main() {
struct Person person1, person2, p[20];
return 0;
}
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
(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.