Class Activities for CS1445 - Spring 2012


Wed Feb 12
Lab (6)

Mon Feb 10
talke about loops and if statements some more. Wrote a program for the Monte Carlo computation of PI

% MonteCarlo PI computation
nOfPoints = input('Enter number of points: ');
circleR = input('Enter the radius: ');

countInside = 0;
for i = 1: nOfPoints
x = -circleR + rand()*(2*circleR);
y = -circleR + rand()*(2*circleR);
r = sqrt(x^2 + y^2);
if r < circleR
countInside = countInside + 1;
end
end

ratio = countInside/nOfPoints;
areaOfCircle = ratio*(2*circleR)^2;
myPI = areaOfCircle/circleR^2


Fri Feb 17
Completed the Quadratic Equation Program with simple if ... elseif ... end statements. No composite conditions.

We started improving the Quadratic program by combining some of the conditions to make composite conditions.

Wed Feb 15
Lab (5)

Mon Feb 13
Discuss if end, if elseif end...

We started designing a program for quadratic equation.

Fri Feb 10
We completed the Volume Calculator program that consisted of a main program,
and 3 functions.
We used if ... else if ... else statement.


% The Main program for computing volume for various shapes
%
choice = input('Enter 1-Sphere, 2-Cylindre,3-Cube, other exit ');
if choice == 1
radius = input('Enter the radius of the shpere: ');
volume = sphereVolume(radius);
fprintf('The volume of sphere with radius %d cm is %f cm^3 \n', radius, volu
elseif choice == 2
radius = input('Enter the radius of the cylindre: ');
height = input('Enter the height of the cylindre: ');
volume = cylindreVolume(radius);
fprintf('The volume of cylindre with radius & height % cm is %d cm is %f cm^
elseif choice == 3
length = input('Enter the length of the cube: ');
volume = cubeVolume(length);
fprintf('The voloume of cube with length % cm is %f cm^3 \n', length,volume)
else
fprint('You havent made a valid choice \n');
end
%==================== Main ends here ================


function volume = cubeVolume(length)
% This Function computes the volume of a sphere
% Input: radius in cm
% Output: volumne in cm^3
clc
volume = length ^ 3;

%==================== Sphere ends here ================
function volume = cubeVolume(length)
% This Function computes the volume of a sphere
% Input: radius in cm
% Output: volumne in cm^3
clc
volume = length ^ 3;
%==================== Cube ends here ================
function volume = cylindreVolume(radius, height)
% This Function computes the volume of a sphere
% Input: radius in cm
% Output: volumne in cm^3
clc
volume = height*pi()*radius ^ 2;
%==================== Cylindre ends here ================

Wed Feb 8-Lab 4
Using fprintf and input
if statment, if ... else statement, if .. else if .. else.

Mon Feb 6
We wrote a program that asked the user to enter the radius of a sphere in cm and computed the volume of that sphere in cm^3.
% This program computes the volume of a sphere
% Input: radius in cm
% Output: volumne in cm^3
clc
radiums = input('Enter an integer for radious in cm: ');
volume = (3.0/4)*pi()*radius ^ 3;

fprintf('Valume of sphere with radius %d (cm) is %f (cm^3) \n ', radius, volume);


Then we learned to write a function that instead of asking for the radius would take the radius as its parameetr.
function volume = sphereVolume(radius)
% This Function computes the volume of a sphere
% Input: radius in cm
% Output: volumne in cm^3
clc
volume = (3.0/4)*pi()*radius ^ 3;

fprintf('Valume of sphere with radius %d (cm) is %f (cm^3) \n ', radius, vol


We need to save the as sphereVolume.m and then at the comman prompt can run it
sphereVolume(2)
which takes 2 as the radius and returns the volume.

Fri Jan 27-Lecture
Started Introduction (1) - Pre-defined functions, variables, different types of variables,
Arrays, index and content in an array
Microlab (1)
Microlab (2)

Wed Jan 25-Lab
Lab (2)

Mon Jan 23-Lecture
Finishing Notes (1)
What is a variable? variable types. Mathematical operations

Fri Jan 20-Lecture
Introduction, Syllabus, Computer Usage Policy, discussion about the lab
Textbook discussions
Problem Solving Strategies, Microlab 1

Wed Jan 18-Lab
Lab (1)