CS1101C Lab 2 (Odd Week)

CS1101C Lab 2 (Odd Week)

Light Refraction

The deadline for this lab question is Wednesday 28 September 2005, 23:59:59 hours.

The name of your C program file must be called refraction.c, files with any other name will not be marked.

The Task

As light passes through different substance/medium (e.g. air, water, glass etc), the path may deviate. This effect is known as light refraction and is shown in the diagram below.

As a means to differentiate different medium, refraction index can be calculated based on the incidence and refraction angle.

The formula is given as:

    Refraction Index =  sin(Incidence Angle) / sin(Refraction Angle)

where the incidence medium is assumed to be a vacuum.

In this lab, you need to write a program to help processing experiment data collected on this topic. A scientist friend of yours has performed the experiment on a number of substances. For each substance, a number of data set is collected. You are to calculate the refraction index for each data set, and also the average refraction index for each substance. At the end of your program, you should report the substance with the maximum refraction index.

The experiment data are collected in a file called "refraction1.txt". The file is organized in the following fashion:

·       The first line gives the total number of substances tested.

·       Then the data for each substance follows, with the following format:

o      The first line gives the total number of experiments performed.

o      For each experiments, the incidence angle and refraction angle (in degrees) are listed on a single line separated by a space.

 

Sample Run

The following shows a sample run, where $ indicates the Unix prompt. You are reminded to follow the sample outcome exactly, else marks will be deducted. User input is shown in bold font. The data file given is an example. We will test your program with other data files.

 

The refraction1.txt data file contains the following lines:

3               //3 substances
5               //1st substance with 5 experiments
50 50               //experiment 1 of substance 1
40 41               //experiment 2 of substance 1
30 30               //....
10 9                //....
60 60               //experiment 5 (last) of substance 13
2         //2nd substance with 2 experiments    
35 26       //experiment 1 of substance 2
63 42       //experiment 2 (last) of substance 2
3            //3rd (last) substance with 3 experiments
20 8        //experiment 1 of substance 3
50 18       //…
71 23       //experiment 3 (last) of substance 3

Note that the comments are included for explanation purpose only, they are not in the actual data file.

 
$ gcc -Wall –lm refraction.c -o refraction
$ ./refraction
 
Substance No.1
        Data No.1 Angles (in:50,out:50) RIndex = 1.00
        Data No.2 Angles (in:40,out:41) RIndex = 0.98
        Data No.3 Angles (in:30,out:30) RIndex = 1.00
        Data No.4 Angles (in:10,out:9) RIndex = 1.11
        Data No.5 Angles (in:60,out:60) RIndex = 1.00
        Average RIndex = 1.02
 
Substance No.2
        Data No.1 Angles (in:35,out:26) RIndex = 1.31
        Data No.2 Angles (in:63,out:42) RIndex = 1.33
        Average RIndex = 1.32
 
Substance No.3
        Data No.1 Angles (in:20,out:8) RIndex = 2.46
        Data No.2 Angles (in:50,out:18) RIndex = 2.48
        Data No.3 Angles (in:71,out:23) RIndex = 2.42
        Average RIndex = 2.45
 
Substance No.3 has the maximum refraction index of 2.45
 
 

This document, index.html, has been accessed 17 times since 25-Jun-24 11:57:13 +08. This is the 1st time it has been accessed today.

A total of 12 different hosts have accessed this document in the last 445 days; your host, 216.73.216.39, has accessed it 1 times.

If you're interested, complete statistics for this document are also available, including breakdowns by top-level domain, host name, and date.