# CSE 432 Fall 2019 Lab 2

Introduction to for loop

%Use of for loop

close all
clear
clc

n=input('Number of inputs= ');
if (n>0)
for i=1:n
area=pi*r*r;
circ=2*pi*r;
fprintf('Radius= %g\tArea= %g\tCircumference= %g\n', r, area, circ)
end
else
fprintf('ERR:Number must be Positive\n');
end

%Change the code such that if the radius is negative, it will show an error message and continue without incrementing the loop.

%Another spin on the previous example
close all
clear
clc

close all
clear
clc

area=pi.*r.*r;
circ=2*pi.*r;

for i=1:length(r)
fprintf('Radius= %g\tArea= %g\tCircumference= %g\n', r(i), area(i), circ(i));
end
%Modify the code so that if the radius is negative the output will show the area an circumference for absolute value of that radius



String input output. Adding a variable in an array.

close all
clear
clc
%String input output

n=input('No of strings: ');
var={};
for i=1:n
s=input('Enter a string: ','s');
var=[var s];
end

for i=1:length(var)
fprintf('String %g %s\n', i, var{i});
end

Create a Matlab Script to find the cgpa of n subjects, where n will be taken as input. All the Name of subject, credits will be taken as input first and stored in an array. Afterward all the marks will be taken as input and the grade of each subject with final CGPA will be shown at last.

%CGPA calculator
close all
clear
clc

sub={};
cred=[];
marks=[];
gp=[];
cg=[];

n=input('Number of subjects= ');
for i=1:n
subject=input('name of subject: ', 's');
sub=[sub subject];
credit=input('Number of credits: ');
cred=[cred credit];
mark=input('mark: ');
marks=[marks mark];
end

for i=1:length(sub)
mark=marks(i);
if (mark>=80)
gp=[gp 4.00];
elseif (mark>=75)
gp=[gp 3.75];
elseif (mark>=70)
gp=[gp 3.50];
elseif (mark>=65)
gp=[gp 3.25];
elseif (mark>=60)
gp=[gp 3.00];
elseif (mark>=55)
gp=[gp 2.75];
elseif (mark>=50)
gp=[gp 2.50];
elseif (mark>=45)
gp=[gp 2.25];
elseif (mark>=40)
gp=[gp 2.00];
else
gp=[gp 0.00];
end
end

cg=cred.*gp;
cgpa=sum(cg)/sum(cred);
fprintf('\n\nTotal credit= %g CGPA= %g\n', sum(cred), cgpa );

fprintf('Sl Subject Credit Mark Grade GP');
for i=1:length(sub)
fprintf('\n%2g %7s %6g %4g %5s %g' , i, sub{i}, cred(i), marks(i), grade{i}, gp(i));
end

## Homework

Change the code in task 1 such that if the radius is negative, it will show an error message and continue without incrementing the loop.

Modify the code in task 2 so that if the radius is negative the output will show the area an circumference for absolute value of that radius

Create a Matlab Script to find CGPA of n students, where n will be taken as input. Then take the name of student and marks obtained in individual subjects.
Finally show the students result in a table and print the students CGPA.

Subjects and credits will be stored in an array. Subjects and credits will be the ones that you have in this semester.

Sample input
Number of students =1
Name of Studnet 1: XYZ
Marks in Bangla for XYZ: 75
Marks in English for XYZ: 65
Marks in Maths for XYZ: 55

Sample Output

Students name: XYZ
CGPA is 3.15909