% Grade calculator, TAM 2020, Fall 2010
% Updated Dec 11, 2010
clc, clear

%Read in the raw grades 
TAM202_Final_GradesF10  % reads m file which in turn reads excel file  



%Mix prelims with redos
for i=1:9
  % Ave prelim and redo, if it helps
  prelimsredone(:,i) = (max((prelimsraw(:,2*i-1)'+prelimsraw(:,2*i)')/2, prelimsraw(:,2*i-1)') )';
end
prelims = prelimsredone(:,1:9); 
  

% Pick out best 10 of 11 Prelim and final exam scores
 exams= [prelims finalexam];
 examsmed = median(exams);
 examsmed([7 8 9]) = [24.5 19.25 10.25]; % calculate medians of makeup by hand
 for i=1:14
     if (examsmed(i)>21);  examsmed(i)=21; end
     if (examsmed(i)<12);  examsmed(i)=12; end
 end
 
 examsscaled=exams*0;
 grandmedian = 18; % Rescale all prelims to a grand median
 for i=1:nstud
     for j=1:14
         if exams(i,j) < examsmed(j)
             examsscaled(i,j) = exams(i,j)*grandmedian/examsmed(j);
         else
            examsscaled(i,j)= 25 - (25-grandmedian)* (25 - exams(i,j))/(25-examsmed(j));
         end
     end
 end

 
 sortedexams = flipud(sort(examsscaled'));   % sort from best to worst
 bestexams=sortedexams(1:10,:);       % best 10 prelim scores
 examtot = sum(bestexams)'/2.5;       % out of nominal score of 100
   examtotmed  =median(examtot);
   examtotmax  = max(examtot);

% Drop 2 lowest homeworks
 sortedhomeworks = flipud(sort(homeworks'));  % sort from best to worst
 besthomeworks=sortedhomeworks(1:12,:);       % best 7 prelim scores
 homeworktot = sum(besthomeworks)'/12;        % out of nominal score of 10
  homeworktotmed = median(homeworktot);
  homeworktotmax = max(homeworktot);

%LABS, 3 labs with up to 15 points each
  labtot = mean(labs')';
   labtotmed = median(labtot);
   labtotmax = max(labtot);
  
  
%Clicker grade
maxclick = max(clickerraw); % perfect attendance
clickerdrop = min(clickerraw,27);


clickerdrop= 5* clickerdrop/(maxclick-7) % Make the max = 5
    clickerdropmed = median(clickerdrop); 
    clickerdropmax = max(clickerdrop);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Calculate the big number

 total_grade =  examtot * 0.70 ...  % 70 percent of grade
              + homeworktot  ...    % 10 percent
			  + labtot ...          % 15 percent
              + bonus/3  ...        % top bonus is 25
              + clickerdrop ...     % max is 5
    		  + survey ;            % 1 percent
          
            total_grademed  = median(total_grade);
            total_grademax  = max(total_grade);
              
          
  %Rescale because total is more than 100
  % Need to pick a max
  %max_total_grade = 101;  
  %total_grade =100* total_grade/max_total_grade;
    
% DISTRIBUTION PLOT
% X axis is the total score
% Y axis is percent of students with that score or less
    x=sort(total_grade);
    plot(x,(1:nstud)*100/nstud);  

% LETTER GRADE CUTOFFS	
  cutoff=[   97  % A+
             92  % A
	  	     89  % A-
		         %
		     84  % B+
		     78  % B
		     75  % B-
		         %
		     72  % C+
		     68  % C
		     60  % C-
		         %
		     59  % D+
		     30  % D
		     30  % D-
		         %
	          0];% F

%ASSIGN LETTER GRADES	

% Make a  13 by 2 text array of the 13 possible letter grades		 				
  letter=reshape('A+A A-B+B B-C+C C-D+D D-F ',2, 13)';

% Initialize students letter_grade,   two spaces for each row
   letter_grade= setstr( 32* ones(nstud, 2));  

% Check to see what grade a student should get
   for j=1:nstud;    % once through for each student
     for i=13:-1:1;                %once through for each letter grade
       if ( total_grade(j)   >= cutoff(i) -.5  ); % a little generous to minimize whining
	        letter_grade(j,:) = letter(i,:)  ;
	   end;
     end;
   end;
		 
% Print to screen  total_grade number for each student, in spreadsheet order		 
   iorder=1:nstud;
   [total_grade(iorder,:) ]

space(1:nstud,1)=' '; format bank;  % Set up formatting for grade 

total_grade  = .01*round(total_grade*100);  % rounding of total_grade
examtot    = .01*round(examtot*100);  % rounding of total_grade
labtot         = .01*round(labtot*100);  % rounding of total_grade
homeworktot        = .01*round(homeworktot*100);  % rounding of total_grade

% Print to screen: names, total score and grades, from best to worst
   [y, iorder] = sort(total_grade);
   iorder=flipud(iorder);  % sort from best to worst
   for i = 1:nstud
%    disp([names(iorder(i),:)  letter_grade(iorder(i),:) ...
%          num2str(total_grade(iorder(i)))])
     
     name = names(i,:);
    disp(sprintf('%35s %4s %2.2f  ', name, letter_grade(iorder(i),:), ...
                  total_grade(iorder(i))))
   end
 
% Print to screen: names, total score and grades, alphbetical order
% Three printings, first is readable, 2nd two for pasting back into
% spreadsheet.
fprintf('Name                               Grade Tot exams  Lab   HW\n')
for i = 1:nstud
%     disp([names(i,:) letter_grade(i,:)  ...
%            num2str(total_grade(i)) ...
%            num2str(finalexamtot(i)) ...
%            num2str(prelimtot(i)) ...
%            num2str(labtot(i)) ...
%            num2str(hwtot(i))])
             name = names(i,:);
       disp(sprintf('%35s %3s %2.2f %2.2f %2.2f %2.2f ',name, letter_grade(i,:), ...
                  total_grade(i), examtot(i),  labtot(i), homeworktot(i)))
end% readable
  %letter_grade          % letters only, for pasting into spreadsheet
  % num2str(total_grade)  % numbers only, for pasting into spreadsheet

   
   % plot(total_grade(iorder), nstud:-1:1)
   % title('cumulative total grade');xlabel('score'); ylabel('rank');

   
