80
lab1/1.cpp
Normal file
80
lab1/1.cpp
Normal file
@@ -0,0 +1,80 @@
|
||||
#include <iostream>
|
||||
#include <string.h>
|
||||
|
||||
class Person {
|
||||
private:
|
||||
static const std::size_t MAX_NAME = 30;
|
||||
static const std::size_t MAX_ADDRESS = 30;
|
||||
char name[MAX_NAME];
|
||||
char address[MAX_ADDRESS];
|
||||
unsigned int age;
|
||||
float salary;
|
||||
|
||||
public:
|
||||
void getName();
|
||||
void getAddress();
|
||||
void getAge();
|
||||
void getSalary();
|
||||
void setName(const char[30], size_t);
|
||||
void setAddress(const char[100], size_t);
|
||||
void setAge(unsigned int);
|
||||
void setSalary(float);
|
||||
};
|
||||
|
||||
inline void Person::getName() {
|
||||
std::cout << name << std::endl;
|
||||
}
|
||||
|
||||
inline void Person::getAddress() {
|
||||
std::cout << address << std::endl;
|
||||
}
|
||||
|
||||
inline void Person::getAge() {
|
||||
std::cout << age << std::endl;
|
||||
}
|
||||
|
||||
inline void Person::getSalary() {
|
||||
std::cout << salary << std::endl;
|
||||
}
|
||||
|
||||
inline void Person::setName(const char name[], size_t size) {
|
||||
strncpy(this->name, name, (size <= MAX_NAME ? size : MAX_NAME - 1));
|
||||
|
||||
if (size > MAX_NAME)
|
||||
this->name[MAX_NAME - 1] = '\0';
|
||||
}
|
||||
|
||||
inline void Person::setAddress(const char address[], size_t size) {
|
||||
strncpy(this->address, address, (size <= MAX_ADDRESS ? size : MAX_ADDRESS - 1));
|
||||
|
||||
if (size > MAX_NAME)
|
||||
this->name[MAX_NAME - 1] = '\0';
|
||||
}
|
||||
|
||||
inline void Person::setAge(unsigned int age) {
|
||||
this->age = age;
|
||||
}
|
||||
|
||||
inline void Person::setSalary(float salary) {
|
||||
this->salary = salary;
|
||||
}
|
||||
|
||||
int main() {
|
||||
Person person;
|
||||
const char name[] = "Retard Singh";
|
||||
const char address[] = "420 RetardVille";
|
||||
unsigned int age = 44;
|
||||
float salary = 4.44;
|
||||
|
||||
person.setName(name, sizeof(name));
|
||||
person.setAddress(address, sizeof(address));
|
||||
person.setAge(age);
|
||||
person.setSalary(salary);
|
||||
|
||||
person.getName();
|
||||
person.getAddress();
|
||||
person.getAge();
|
||||
person.getSalary();
|
||||
|
||||
return 0;
|
||||
}
|
28
lab1/2.cpp
Normal file
28
lab1/2.cpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
|
||||
// Triangle
|
||||
double area(double a, double b, double c) {
|
||||
double s = (a + b + c) / 2;
|
||||
return std::sqrt(s * (s - a) * (s - b) * (s - c));
|
||||
}
|
||||
|
||||
// Rectangle
|
||||
double area(double a, double b) { return a * b; }
|
||||
|
||||
// Circle
|
||||
double area(double a) { return M_PI * a * a; }
|
||||
|
||||
int main() {
|
||||
|
||||
double a = 3, b = 4, c = 5;
|
||||
|
||||
std::cout << "Area of a triangle with sides " << a << ", " << b << " and "
|
||||
<< c << ": " << area(a, b, c) << std::endl
|
||||
<< "Area of rectangle with sides " << a << " and " << b << ": "
|
||||
<< area(a, b) << std::endl
|
||||
<< "Area of circle with radius " << a << ": " << area(a)
|
||||
<< std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
36
lab1/3.cpp
Normal file
36
lab1/3.cpp
Normal file
@@ -0,0 +1,36 @@
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
|
||||
double uexponent(double m, unsigned int n) {
|
||||
double ret;
|
||||
|
||||
if (!n)
|
||||
return 1;
|
||||
|
||||
if (n == 1)
|
||||
return m;
|
||||
|
||||
ret = uexponent(m, n / 2);
|
||||
ret *= ret;
|
||||
|
||||
if (n % 2)
|
||||
return ret * m;
|
||||
else
|
||||
return ret;
|
||||
}
|
||||
|
||||
double exponent(double m, int n = 2) {
|
||||
if (n >= 0)
|
||||
return uexponent(m, n);
|
||||
|
||||
return 1 / uexponent(m, std::abs(n));
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
double m = 4.1;
|
||||
int n = 4;
|
||||
|
||||
std::cout << m << " raised to " << n << ": " << exponent(m, n) << std::endl
|
||||
<< "When n is omitted: " << exponent(m) << std::endl;
|
||||
}
|
180
lab1/file.org
Normal file
180
lab1/file.org
Normal file
@@ -0,0 +1,180 @@
|
||||
* Write a program to take name, address as a character array, age as int , salary as float and contains inline functions to set the values and display it in class named person.
|
||||
|
||||
#+ATTR_LATEX: :options frame=single,breaklines=true
|
||||
#+begin_src cpp :tangle 1.cpp :results output :exports both :wrap src text
|
||||
#include <iostream>
|
||||
#include <string.h>
|
||||
|
||||
class Person {
|
||||
private:
|
||||
static const std::size_t MAX_NAME = 30;
|
||||
static const std::size_t MAX_ADDRESS = 30;
|
||||
char name[MAX_NAME];
|
||||
char address[MAX_ADDRESS];
|
||||
unsigned int age;
|
||||
float salary;
|
||||
|
||||
public:
|
||||
void getName();
|
||||
void getAddress();
|
||||
void getAge();
|
||||
void getSalary();
|
||||
void setName(const char[30], size_t);
|
||||
void setAddress(const char[100], size_t);
|
||||
void setAge(unsigned int);
|
||||
void setSalary(float);
|
||||
};
|
||||
|
||||
inline void Person::getName() {
|
||||
std::cout << name << std::endl;
|
||||
}
|
||||
|
||||
inline void Person::getAddress() {
|
||||
std::cout << address << std::endl;
|
||||
}
|
||||
|
||||
inline void Person::getAge() {
|
||||
std::cout << age << std::endl;
|
||||
}
|
||||
|
||||
inline void Person::getSalary() {
|
||||
std::cout << salary << std::endl;
|
||||
}
|
||||
|
||||
inline void Person::setName(const char name[], size_t size) {
|
||||
strncpy(this->name, name, (size <= MAX_NAME ? size : MAX_NAME - 1));
|
||||
|
||||
if (size > MAX_NAME)
|
||||
this->name[MAX_NAME - 1] = '\0';
|
||||
}
|
||||
|
||||
inline void Person::setAddress(const char address[], size_t size) {
|
||||
strncpy(this->address, address, (size <= MAX_ADDRESS ? size : MAX_ADDRESS - 1));
|
||||
|
||||
if (size > MAX_NAME)
|
||||
this->name[MAX_NAME - 1] = '\0';
|
||||
}
|
||||
|
||||
inline void Person::setAge(unsigned int age) {
|
||||
this->age = age;
|
||||
}
|
||||
|
||||
inline void Person::setSalary(float salary) {
|
||||
this->salary = salary;
|
||||
}
|
||||
|
||||
int main() {
|
||||
Person person;
|
||||
const char name[] = "Retard Singh";
|
||||
const char address[] = "420 RetardVille";
|
||||
unsigned int age = 44;
|
||||
float salary = 4.44;
|
||||
|
||||
person.setName(name, sizeof(name));
|
||||
person.setAddress(address, sizeof(address));
|
||||
person.setAge(age);
|
||||
person.setSalary(salary);
|
||||
|
||||
person.getName();
|
||||
person.getAddress();
|
||||
person.getAge();
|
||||
person.getSalary();
|
||||
|
||||
return 0;
|
||||
}
|
||||
#+end_src
|
||||
|
||||
#+RESULTS:
|
||||
#+begin_src text
|
||||
Retard Singh
|
||||
420 RetardVille
|
||||
44
|
||||
4.44
|
||||
#+end_src
|
||||
#+LATEX: \clearpage
|
||||
|
||||
* Using the concept of function overloading.Write function for calculating the area of triangle ,circle and rectangle.
|
||||
|
||||
#+ATTR_LATEX: :options frame=single,breaklines=true
|
||||
#+begin_src cpp :tangle 2.cpp :results output :exports both :wrap src text
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
|
||||
// Triangle
|
||||
double area(double a, double b, double c) {
|
||||
double s = (a + b + c) / 2;
|
||||
return std::sqrt(s * (s - a) * (s - b) * (s - c));
|
||||
}
|
||||
|
||||
// Rectangle
|
||||
double area(double a, double b) { return a * b; }
|
||||
|
||||
// Circle
|
||||
double area(double a) { return M_PI * a * a; }
|
||||
|
||||
int main() {
|
||||
|
||||
double a = 3, b = 4, c = 5;
|
||||
|
||||
std::cout << "Area of a triangle with sides " << a << ", " << b << " and "
|
||||
<< c << ": " << area(a, b, c) << std::endl
|
||||
<< "Area of rectangle with sides " << a << " and " << b << ": "
|
||||
<< area(a, b) << std::endl
|
||||
<< "Area of circle with radius " << a << ": " << area(a)
|
||||
<< std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
#+end_src
|
||||
|
||||
#+RESULTS:
|
||||
: Area of a triangle with sides 3, 4 and 5: 6
|
||||
: Area of rectangle with sides 3 and 4: 12
|
||||
: Area of circle with radius 3: 28.2743
|
||||
#+LATEX: \clearpage
|
||||
|
||||
* Write a program to find number m to power n. The function power takes a double value for m and int value for n. Use default value for n to make the function to calculate squares when this argument is omitted.
|
||||
|
||||
#+ATTR_LATEX: :options frame=single,breaklines=true
|
||||
#+begin_src cpp :tangle 3.cpp :results output :exports both :wrap src text
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
|
||||
double uexponent(double m, unsigned int n) {
|
||||
double ret;
|
||||
|
||||
if (!n)
|
||||
return 1;
|
||||
|
||||
if (n == 1)
|
||||
return m;
|
||||
|
||||
ret = uexponent(m, n / 2);
|
||||
ret *= ret;
|
||||
|
||||
if (n % 2)
|
||||
return ret * m;
|
||||
else
|
||||
return ret;
|
||||
}
|
||||
|
||||
double exponent(double m, int n = 2) {
|
||||
if (n >= 0)
|
||||
return uexponent(m, n);
|
||||
|
||||
return 1 / uexponent(m, std::abs(n));
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
double m = 4.1;
|
||||
int n = 4;
|
||||
|
||||
std::cout << m << " raised to " << n << ": " << exponent(m, n) << std::endl
|
||||
<< "When n is omitted: " << exponent(m) << std::endl;
|
||||
}
|
||||
#+end_src
|
||||
|
||||
#+RESULTS:
|
||||
: 4.1 raised to 4: 282.576
|
||||
: When n is omitted: 16.81
|
Reference in New Issue
Block a user