0% found this document useful (0 votes)
16 views11 pages

Computing Lab 3

The document contains a series of C++ exercises from Lab 3, covering various programming concepts such as angle quadrants, month and day mapping, grade conversion, marriage status identification, temperature conversion, solving quadratic equations, finding the maximum of three numbers, leap year determination, parity checking, positive or negative number identification, and triangle classification. Each exercise includes code snippets demonstrating the implementation of these concepts. The exercises are designed to enhance understanding of control structures, functions, and data types in C++.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views11 pages

Computing Lab 3

The document contains a series of C++ exercises from Lab 3, covering various programming concepts such as angle quadrants, month and day mapping, grade conversion, marriage status identification, temperature conversion, solving quadratic equations, finding the maximum of three numbers, leap year determination, parity checking, positive or negative number identification, and triangle classification. Each exercise includes code snippets demonstrating the implementation of these concepts. The exercises are designed to enhance understanding of control structures, functions, and data types in C++.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Lab 3

Exc 3:
//Exc 3: angle quandrant
#include <iostream>
#include <cmath>

using namespace std;

int main() {
float angle;
cout<< "Enter angle (in range [0;360)): ";
cin>>angle;

int quad = floor(angle/90);

switch (quad) {
case 0: {
cout<<"first quadrant";
break;
}

case 1: {
cout<<"second quadrant";
break;
}

case 2: {
cout<<"third quadrant";
break;
}

case 3: {

Lab 3 1
cout<<"forth quadrant";
break;
}

default: {
cout<<"invalid angle";
break;
}
}
}

Exc 4
//Exc 4: Months and days
#include <iostream>
#include <string>
#include <map>

using namespace std;

int main() {
string months[] = {"january", "february", "march", "april", "m
int days[] = {31,28,31,30,31,30,31,31,30,31,30,31};
map<string, int> mp;
for (int i =0; i<sizeof(months)/sizeof(months[0]);i++) {
mp[months[i]] = days[i];
}

string month;
cout<<"enter the month: ";
cin>>month;
cout<<month<<" has "<<mp[month]<<" days";
return 0;
}

Lab 3 2
Exc 5
//Exc 5: Grade
#include <iostream>

using namespace std;

int main() {
int grade;
char cgrade;
cout<<"Enter your grade: ";
cin>>grade;

while (grade <0 || grade >100) {


cout<<"Invalid grade try again: ";
cin>>grade;
}

if (grade < 60) {


cgrade = 'F';
} else if (grade <70) {
cgrade = 'D';
} else if (grade <80) {
cgrade = 'C';
} else if (grade <90) {
cgrade = 'B';
} else {
cgrade = 'A';
}

cout<< "Your grade is equivalent to "<<cgrade;


return 0;
}

Lab 3 3
Exc 6:
//Exc 6: Marriage
#include <iostream>

using namespace std;

int main() {
char marriage;
cout<<"Enter marriage status: ";
cin>>marriage;
marriage = tolower(marriage);

switch (marriage){
case 'm': {
cout<<"Individual is married";
break;
}
case 'd': {
cout<<"Individual is divorced";
break;
}
case 'w': {
cout<<"Individual is widowed";
break;
}
default: {
cout<<"An invalid code is entered";
break;
}
}
return 0;
}

Lab 3 4
Exc 7
//Exc 7: Converting Program
#include <iostream>

using namespace std;

float convertTemp(char metrics, float temp);

int main() {
char quit, metrics;
float temp;

cout<<"quit the converting program? (y/n): ";


cin>>quit;

if (quit == 'n'){
cout<<"convert from C or F? (c/f): ";
cin>>metrics;
metrics = toupper(metrics);
cout<<"enter the temperature you want to convert: ";
cin>>temp;

float converted = convertTemp(metrics, temp);


cout<<temp<<metrics<<" = "<<converted<<((metrics=='C')? 'F'
}
return 0;
}

float convertTemp(char metrics, float temp) {


if (metrics == 'C')
return (9/5)*temp +32;
if (metrics == 'F')
return (5/9)*(temp-32);

Lab 3 5
return -1; //Maybe we can do sth better here
}

Exc 8
//Exc 8: General quadratic equation
#include <iostream>
#include <array>
#include <cmath>

using namespace std;

array<float,3> solveQuad(float a, float b, float c); //1st argum

int main() {
float a,b,c;
cout<<"f(x)=ax^2+bx+c"<<endl;
cout<<"enter a,b,c: ";
cin>>a>>b>>c;
if (a!=0) {
array<float,3> sol = solveQuad(a, b, c);
if (sol[0]){
if (sol[1] == sol[2]) {
cout<<"f(x) has only solution "<<sol[1];
} else {
cout<<"f(x) has 2 distinct real roots: "
<<sol[1]<<" and "<<sol[2];
}
} else {
cout<<"There is no real solution";
}
} else {
if (b!=0) {
cout<<"f(x) has one real root: "<<-c/b;
}

Lab 3 6
else {
if (c!=0) {
cout<<"f(x) has no root";
} else {
cout<<"f(x) has infinitely many roots";
}
}
}
return 0;
}

array<float,3> solveQuad(float a, float b, float c) {


float root1, root2, del = b*b-4*a*c;
if (del <0) {
return {0,0,0};
} else {
if (del == 0) {
root1 = -b/(2*a); root2 = -b/(2*a);
} else {
root1 = (-b+sqrt(del))/(2*a);
root2 = (-b-sqrt(del))/(2*a);
}
return {1,root1, root2};
}

Exc 9
//Exc 9: Max of 3 numbers
#include <iostream>

using namespace std;

int main() {
int a,b,c;
cout<<"enter a,b,c: ";

Lab 3 7
cin>>a>>b>>c;
int max = a;
if (max < b) {
max = b;
}
if (max < c) {
max = c;
}
cout<<"max(a,b,c)= "<<max;
return 0;
}

Exc 10
//Exc 10: leap year
#include <iostream>
#include <cmath>

using namespace std;

int main() {
int year;
cout<<"enter year: ";
cin>>year;

if ((year%4 ==0 && year%100!=0) || year%400==0) {


cout<<"This is leap year";
} else {
cout<<"This is not leap year";
}
return 0;
}

Exc 11

Lab 3 8
//Exc 11: parity
#include <iostream>

using namespace std;

int main() {
int n;
cout<<"enter number: ";
cin>>n;

cout<<"This number is "<<((n%2)? "odd": "even");


return 0;
}

Exc 12
//Exc 12: pos or neg?
#include <iostream>

using namespace std;

int main() {
int n;
cout<<"enter number: ";
cin>>n;

if (n==0) {
cout<<"It is neither positive nor negative";
} else {
cout<<"It is "<<((n>0)? "positive": "negative");
}
return 0;
}

Lab 3 9
Exc 13
//Exc 13: triangle
#include <iostream>

using namespace std;

int max(int a, int b, int c);

int main() {
int a,b,c;
cout<<"enter 3 side length of the triangle: ";
cin>>a>>b>>c;

//Must check if it is a triangle first


if (a+b+c>2*max(a,b,c)) {
if (a==b) {
if (a==c) {
cout<<"This triangle is equilateral";
} else {
cout<<"This triangle is isosceles";
}
} else {
if (a==c||b==c) {
cout<<"This triangle is isosceles";
} else {
cout<<"This triangle is scalene";
}
}
} else {
cout<<"This is not a triangle";
}
return 0;
}

Lab 3 10
int max(int a, int b, int c) {
int max = a;
if (max < b) {
max = b;
}
if (max < c) {
max = c;
}
return max;
}

Lab 3 11

You might also like