Task 1
Introduction to for loop
%Use of for loop
close all clear clc n=input('Number of inputs= '); if (n>0) for i=1:n r=input('radius= '); 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 %Task for students %Change the code such that if the radius is negative, it will show an error message and continue without incrementing the loop.
Task 2
Another spin on task 1
%Another spin on the previous example close all clear clc close all clear clc r=input('Radius (Give and array): '); 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 %Task for Students %Modify the code so that if the radius is negative the output will show the area an circumference for absolute value of that radius
Task 3
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
Task 4
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=[]; grade={}; 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]; grade=[grade 'A+'] elseif (mark>=75) gp=[gp 3.75]; grade=[grade 'A'] elseif (mark>=70) gp=[gp 3.50]; grade=[grade 'A-'] elseif (mark>=65) gp=[gp 3.25]; grade=[grade 'B+'] elseif (mark>=60) gp=[gp 3.00]; grade=[grade 'B'] elseif (mark>=55) gp=[gp 2.75]; grade=[grade 'B-'] elseif (mark>=50) gp=[gp 2.50]; grade=[grade 'C+'] elseif (mark>=45) gp=[gp 2.25]; grade=[grade 'C'] elseif (mark>=40) gp=[gp 2.00]; grade=[grade 'D'] else gp=[gp 0.00]; grade=[grade 'F'] 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
Based on Task 1
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.
Based on Task 2
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
Based on Task 4
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 Subject Credit Mark Grade Gradepoint Bangla 3 75 A 3.75 English 3 65 B+ 3.25 Maths 5 55 B- 2.75 CGPA is 3.15909