# Tutor profile: Ramesan K.

## Questions

### Subject: Python Programming

Read a file with data about earthquakes over past 100 years and display the mean, avg, mode and other statistical parameters.

import re #regular expression operation import statistics #statistics function #The following function is to read the data from text file to a dictionary. You may use any other method to do the same.This ia more generalised way. def str2dict(filename="earthquakes.txt"): results = {} with open(filename, "r") as cache: # read file into a list of lines lines = cache.readlines() # loop through lines for line in lines: # skip lines starting with "--". if not line.startswith("--"): # replace random amount of spaces (\s) with tab (\t), # strip the trailing return (\n), split into list using # "\t" as the split pattern line = re.sub("\s\s+", "\t", line).strip().split("\t") # use first item in list for the key, join remaining list items # with ", " for the value. results[line[0]] = ", ".join(line[1:]) return results #he dictionary is returned newdict={}; newdict=str2dict("earthquakes.txt") #the data in file is stored in dictionary as trings print(newdict) #Note that the values of our interest richter scale values are the keys ric=[] #define new list to store the keys i.e the values of our interest for key in newdict: #accessing the keys and converting them to float and storing in list 'ric' ric.append(float(key)) print(ric)#Note that the values are now float #The followiing set of code uses the functions in statistics library. #Note that the mode function is commented because it shows an error since there is no unique value for mode in the given set of vaues in the text file you have given mean=statistics.mean(ric) median=statistics.median(ric) #mode=statistics.mode(ric) You may uncomment this for any other file. range=max(ric)-min(ric) #finding the range #printing the final values. print("Mean="+str(mean)) print("Median="+str(median)) #print("Mode="+str(mode)) print("Range="+str(range))

### Subject: MATLAB

In this exercise, we learn how to generate exponential and pulse sequences in MAT- LAB and to convolve and plot these sequences. To synthesize the exponential impulse response h[n] = (0.9)n for index values n = 0, 1, · · · , 50, we first need to define the index sequence (vector) n by typing n = [0:50] (“Return” or “Enter” is assumed after every command.) Note that the entire 51- point sequence is listed on the screen. To suppress this listing, we will usually add a semicolon (;) at the end of such commands, i.e., n = [0:50]; The vector form of the impulse response h[n] is then generated via the command h = (0.9).^n; (The parenthesis are not actually required here, but are necessary in other cases to distinguish the period in the operator (.^) from a decimal point.) To plot this signal versus its index n, we use plot(n,h,‘o’) After the plot appears, one returns the screen to text mode by hitting any key. To find out about additional features or returns commands, type help plot A sequence of length 51 with 30 ones can be generated in MATLAB by typing plong = [ones(1,30) zeros(1,21)]; Similarity, a sequence of length 51 with 20 ones can be generated in MATLAB by typing pshort = [ones(1,20) zeros(1,31)]; These two sequences can be convolved by typing y1 = conv(plong,pshort); Exercise 1. Part A. 1 Point: Plot the sequence plong. You may have to adjust the scales of the two axies of your plots so all of the plot can be seen. To adjust your axies, you may wish to used the MatLab command “axis.” You can find out the syntax of this command and examples of its use by Googling the phrase: MatLab command axis. Part B. 1 Point: Plot the sequence pshort. Part C. 4 Points: Plot the sequence y1. (Note that you will need a new index vector n1 of the correct length to produce this plot. Use the whos command to help.) Part D. 4 Points: Look at the plots that you generated. Do the plots have the expected shapes and amplitudes? Is the duration of the output sequence correct? Explain. You can measure peak amplitudes using, e.g., peak1 = max(y1) By typing peakloc = find(y1==peak1) you can find the location (index) of the peak.) Exercise 2. The sequences pshort and h can be convolved by typing the command y2 = conv(h,pshort); Part A. 1 Point: Plot the sequence h. Part B. 4 Points: Plot the sequence y2. Part C. 5 Points: Do the results have the expected shapes and amplitudes? Is the du- ration of the output sequence correct? Explain.

display('Exercise 1') n = [0:50]; plong = [ones(1,30) zeros(1,21)]; figure plot(n,plong,'o') axis([0,55,0,2]) xlabel('n') ylabel('f(n)') title('plong') pshort = [ones(1,20) zeros(1,31)]; figure plot(n,pshort,'x') axis([0,55,0,2]) xlabel('n') ylabel('f(n)') title('pshort') y1 = conv(plong,pshort); n1 = [0:100]; figure plot(n1,y1,'*') axis([0,110,0,25]) xlabel('n') ylabel('f(n)') title('y1 ( Convolution of plong and pshort)') display('The plots have expected shape and amplitudes. The duration of the output sequesnce is also as expected.') peak1 = max(y1) peakloc = find(y1==peak1) ---------------------------------------------------------------------------------------------------------- display('Exercise 2') n = [0:50]; pshort = [ones(1,20) zeros(1,31)]; h = (0.9).^n; figure plot(n,h,'x') axis([0,55,0,2]) xlabel('n') ylabel('f(n)') title('h') y2 = conv(h,pshort); n2 = [0:100]; figure plot(n2,y2,'*') axis([0,110,0,10]) xlabel('n') ylabel('f(n)') title('y2 (Convolution of h and pshort)') display('The plots have expected shape and amplitudes. The duration of the output sequesnce is also as expected.')

### Subject: C++ Programming

Create a program to enter the details and sales of 3 salesmen for each quarter. Write function to display the total sales

#include<iostream>//Standard i/o header file #include<conio.h>//For getch() using namespace std; void add(string salesmen[3],double sales[3][4])//Function to store the values { for(int i=0;i<3;i++)//iterating through salesmen { //Obtaining the details of ith salesmen cout<<"\n Enter in the name for salesman "<<i+1<<":"; cin>>salesmen[i]; cout<<"\n Now enter in the sales for each quarter for "<<salesmen[i]<<"\n"; //Obtaining the sales details of ith salesman for(int j=0;j<4;j++)//iterating through each quarter { cout<<"\n Enter in data for quarter "<<j+1<<": "; cin>>sales[i][j]; } } } void display(string salesmen[3],double sales[3][4])//Function to display the details { double sum;//For the temporary storage of total ales of each salesman for(int i=0;i<3;i++)//iterating through each salesman { sum=0; for(int j=0;j<4;j++)//sales for each salesman { sum=sum+sales[i][j]; } cout<<"\n Total sales for "<<salesmen[i]<<" is $"<<sum;//displaying the total sum } } int main() { string salesmen[3];//for storing salesmen names double sales[3][4];//for storing sales of each quarter add(salesmen,sales);//calling the first function display(salesmen,sales);//calling the second function getch(); return 0; }

## Contact tutor

needs and Ramesan will reply soon.