 Welcome to this session where we will look at the use of arrays for solving computational problems. We are seen in the last session that C plus plus provides array data structure. Arrays can be declared to have a type and a size. Remember that all elements of the array have the same type and they are all accessed by using index expression and an index value must be between 0 and size minus 1. We also remember that iterative constructs can be easily used to examine and process all elements of an array in whatever desired order. For example, if I wish to scan all elements of the array one after another, a for loop is ideal to do that. Here is an example for i equal to 0 i less than n i plus plus. Then I can write a code to process array elements. Observe the starting value and end condition. I want to process the array elements from 0 to n minus 1. So, I should start with i equal to 0 and I should check the final condition to be i less than n. Having seen this, we shall now proceed to look at the use of arrays in solving computational problems. In this session, we specifically look at a simple problem of finding average and standard deviation of some data. In this case, the data is marks scored in a quiz by students in a class. Here is the definition of the problem. n students of a class have taken a quiz. Their marks have to be processed to calculate the average and the standard deviation. We assume that at most 600 students are there in the class. Why do we need this number? We need this to define the size of the array, so that the array can hold marks for all students. We proceed with the analysis of this problem, which tells us that we need to calculate the average. We already know how to calculate the average. So, we will use the same algorithm. That is, we start with the declaration of an array of the type integer. We read and store n marks in that array and we find the sum of all n marks. That is, marks of all n students divide the sum by n to get the average. Observe the question mark here, type integer. Should that be integer? Should that be float? Well, here the assumption is that the marks scored by the students are of the type integer, but this has a bearing as we shall see soon when we build the program further. Next we examine standard deviation. All of you would have studied this term in your school mathematics, but here is a quick recap. This is a statistical term and it describes how data values are spread around the average value. A large standard deviation indicates that data values are far away from the average, many of them, whereas a smaller value indicates that most data values are close to the average. Standard deviation has several practical utilities, for example, in quality control or to understand the spread of data, etcetera. We look at the definition of the standard deviation. So, if mu is the average of n marks, the standard deviation sigma is given by this formula. Basically, you find out the difference of each mark from the average and square that difference. Sum it over for all the elements divided by n and you get the square difference of which you take a square root. The square root represents the square root of the quantity in parenthesis. Now, C plus plus provides a library with many mathematical functions and we have a function to calculate the square root. Please note that you will have to include in your program a header file, just like we say hash include i o stream, we will have to say hash include c math. With this, now we proceed to design the program. We use the familiar algorithm to calculate average. So, as we saw earlier, read and store n marks in an array for marks, iteratively find the sum and divide it by n to get the average, which is mu. Next, we set up another iteration over i to scan all array elements again. This time, we calculate the difference diff between marks and mu and the square of diff is added to the running sum. We divide the sum by n and calculate square root to get sigma. Here is a program. We declare the array in mark 600, the variable i and n. We declare sum diff mu and sigma as flow. Why do we do that? Please note that we have assumed that marks are integer. So, obviously the sum should also be integer. However, we notice that mu and sigma are likely to be fractional numbers and if we find out the average mu by simply dividing sum by n and if sum is integer, we will get into a problem. We will lose out the fractional value and therefore, we define sum as flow. Next, we read n elements of the marks array. So, we read the value of n. Notice once again, the for iteration control i equal to 0 while as the n. This iteration will read all the n marks. We now calculate mu. So, we start with sum equal to 0. Again, iterate over all the elements of the marks array. Keep adding every element to sum and finally, when we finish the loop, we divide sum by n to get the mu. This is what I meant earlier when I said if sum and n are both integers, I will lose the fractional path. Since we have declared sum to be a floating point variable, n will be converted. The value of n will be converted to a floating point value and I will get the correct average mu. Next, I calculate the standard deviation sigma. I again start with sum equal to 0 because that is the starting point. I want to accumulate sum of the square differences. I set up the iteration again exactly the same way, but this time I first calculate the difference of i th element of marks with the average and calculate the square of that difference which I add to the running sum. Having calculated this sum, I now find out sigma by saying square root of sum upon n. Remember, square root is a function. You already are familiar with how functions are called. The difference is we are not writing this function here, but we depend upon the library called CMath to provide us with this function. Finally, we produce the output. Of course, you can see that we are producing an output with appropriate strings explaining what we are saying. So, statistics of quiz one marks for the class, number of students is n, average marks is mu and standard deviation of marks is sigma. This completes the program. From now onwards, we shall be looking at complete programs which solve practical problems. Since we are going to write programs for such purposes, it is important that we remember a few practical aspects of program. For example, when we run this program, a user might commit mistake while giving data. What if a user has 1000 students? The user gives the value of n as 1000. However, we must convey to the users that the limit is 600 marks. So, at least our program should validate the input. If the value of n is out of bound, we must give an error message and exit the program. How do we exit the program? We normally say return 0. This type will say return with some value other than 0, so that the operating system is conveyed the fact that program did not work properly. So, this is the code for validating input. After reading n, if n is less than 1 or if n is greater than 600, I output a string invalid value of n, print the value of n and return minus 1. If I include this in my program at the beginning, the program will run properly even for invalid value of n. So, we recall, we state that for solving any problem, it is useful to write a small design document outlining the algorithm, where we should indicate the variables and arrays to be used. Then, while writing the program, it is preferable to write program segments in logical order. It is preferable to write explanatory commands and it is preferable to use simple and easy to understand constructs. With these tapes, we should be able to write good programs which not only work correctly, but are very readable, understandable and therefore, modifiable. In summary, we have learnt how to use arrays to solve simple computational problems. In the next session, we will consider another problem using arrays. Thank you.