Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Assignment-6 (30/10/23)

Write your name, roll number and assignment number in the header of the
program file as comments. You may give your program file name as
<asgn><no_><RollNo>.c. Submit all the programs in the Moodle System 15
minutes before the end of the laboratory session. Provide also output files
containing results given the set of inputs given here. Please follow the
instruction for generating result files provided at the end.

1. Write a function computeStatistics(.) with appropriate parameter passing


and return values, which takes an array of real numbers, and return its mean,
and standard deviation, using pointer argument variables. Write a main
program, which reads two lists of real numbers, say list1 and list2, with the
numbers of elements N1 (to be read before reading list1) and N2 (to be read
before reading list2), respectively. The program computes their means and
standard deviations using the function computeStatistics(.). Let m1, and m2 be
means and, s1 and s2 be standard deviations of list1 and list2, respectively. The
program also computes the following statistics T using them:

T= (m1-m2)/ √ (s12/N1+s22/N2)

The program prints the following:

(i) Number of elements, mean and standard deviation of list1.

(ii) Number of elements, mean and standard deviation of list2.

(iii) The statistics T.

Show your outputs with the following input dataset:

(i) list1: N1: 5

46.5, 78.9, -3.5, 4.9, 12.3

list2: N2: 10
26.4, 88.1, -34.7, -40.2, 222.3, 56.4, 12.4, 43.2, -35.7, -88.8

(ii) list1: N1: 10

36.2, 68.5, 331.3, -44.4, -220.4, -66.2, -120.8, 430.3, -432.9, -45.7

list2: N2: 15

16.4, 18.1, -14.7, -43.2, 22.3, -156.2, 125.3, -143.2, -55.7, -78.8, 26.4, 18.2,
-4.5, 7.9, 27.3

2. Define a structure containing the following information.


Country: String of maximum 20 characters
Capital: String of maximum 20 characters
Area: Real number (in 1000 sq. km)
Number_of_rivers: integer
River: An array of names of rivers which can store maximum 5 rivers.

For the above, the program should ensure that Number_of_rivers for a record
should not exceed 5, and the variable is initialized to 0.
The name of a river is a variable length string and should be dynamically
allocated. (You may use a temporary array of fixed size to store the name which
is read.)
The memory allocated to a river name must be deleted when the river is
deleted.

For example: a record about India is shown below:


India
New Delhi
3287.263
2
”Ganga”
”Narmada”
Write a program which dynamically allocates an array of N (to be read) records,
and reads the data in such a way that the array stores the records in dictionary
order of the names of countries.

The program also implements the following function using appropriate list of
arguments:

AddRiverToCountry(.): It adds a river to the array of rivers of the corresponding


country by increasing the count of rivers. If the count exceeds 5, it does nothing
and returns -1. If the country is not found, it does not do any operation and
returns 0, else for a successful operation it returns 1.
DeleteRiverFromCountry(.): It deletes a river if it is in the array of rivers of a
country by decreasing the count of rivers. If the river is not in the list it returns
-1 and does nothing. If the country is not found, it does not do any operation
and returns 0, else for a successful operation it returns 1.

PrintRiversOfCountry(.): It prints the list of rivers separated by commas for a


country. For successful printing, it returns 1, else returns 0.

CountRiversOfCountry(.): It prints the number of rivers in a country. If the


country is in the array, it returns the number, else it returns -1.

ListRecordOfCountry(.): It prints the records of a country. If the country is in the


array it prints the record and returns 1, else returns 0 without doing any
printing.
After reading the list of records of various countries, the program performs any
of the following operations in a loop using the above functions. If the operation
is unsuccessful, the program also prints an appropriate error message.
(a) Add Country, River: Adds a River to a country.
(b) Delete Country, River: Deletes a river from a country.
(c) Print-rivers Country: Prints the number of cities presently in the list of a
country followed by the names of those cities.
(d) Quit: Quit from the loop.
At the end, the program prints the following
(i) The record of each country separately in a line:
Its name, Capital, Area and list of rivers.
(ii) The rivers shared by more than one country, Each river should be printed
separately in a line in the following format.
Name of the river, list of countries sharing it.

Run your program with the following test data (a file with the data of initial
records of students is also provided for your convenience):
Initial records:
N=10

India
New Delhi
3287.263

USA
Washington, DC
9161.966

Russia
Moscow
17075.2

Germany
Berlin
349.223

Israel
Tel Aviv
0.440

U.K.
London
242.495

Bangladesh
Dhaka
147.570

Pakistan
Islamabad
881.913

Srilanka
Colombo
65.610

Nepal
Kathmandu
147.181

In the loop perform the following operation:


Add India, Ganga
Add Pakistan, Sindhu
Add Srilanka, Mahaweli
Add India, Satudru
Add India, Godabari
Add India, Sindhu
Add USA, Missisipi
Add Russia, Volga
Add Srilanka, Gin
Add India, Kaveri
Delete Srilanka, Maduru
Delete India, Ganga
Add Bangaldesh, Padma
Add India, Mahananda
Add Bangladesh, Tista
Add USA, Hudson
Add Srilanka, Srilanka
Add Bangladesh, Icchamati
Add USA, Colorado
Add France, Sein
Add India, Ichhamati
Delete Germany, Rhein
Add Rhein, Germany
Add Bangladesh, Ganga
Delete USA, Hudson
Delete India, Tapti
Add USA, Missouri
Print-rivers India
Print-rivers Srilanka
Quit
For generation of output files

All the results for each assignment should be submitted together in a separate
file (named result.txt). Provide the result in a separate output file (named,
result_<assgn><no>.txt). Use standard output redirection feature to generate
the output file.
Hints. Suppose you would like to redirect your output to a file ‘result.txt’.
If you run the program with the following command
./a.out >result.txt
Output of your program (generated by printf(.) function) will be written
in file result.txt. You need to provide input from your input, by
remembering the sequence of inputs to be given.
If you execute the program multiple times, you may concatenate the
outputs in a single file by using the following redirection command:
./a.out >>result.txt
Input redirection (optional):
You may also store your input (the ordering as per requirement of
the program should be preserved) in an input file in.txt, and execute the
program as follows:

./a.out <in.txt >result.txt

You might also like