0% found this document useful (0 votes)
28 views8 pages

Assignment 1

The document discusses solving problems related to rational numbers and finding the maximum subarray sum. It defines a Rational class to represent rational numbers and implements various arithmetic operations on rational numbers. It then discusses 4 algorithms to find the maximum subarray sum and compares their time complexities.
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)
28 views8 pages

Assignment 1

The document discusses solving problems related to rational numbers and finding the maximum subarray sum. It defines a Rational class to represent rational numbers and implements various arithmetic operations on rational numbers. It then discusses 4 algorithms to find the maximum subarray sum and compares their time complexities.
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
You are on page 1/ 8

ASSIGNMENT 1:

PROBLEM 1:

CODE:

#include<bits/stdc++.h>

using namespace std;

int gcd(int a, int b)

if (a == 0)

return b;

return gcd(b % a, a);

class Rational

{ private:

int numerator;

int denominator;

public:

Rational()

numerator=0;

denominator=0;

Rational(int n,int d)

numerator=n;

denominator=d;

Rational SetRational(int n,int d)

numerator=n;denominator=d;
return *this; // dereferencing the this pointer for pointing the current object

Rational add(Rational r1, Rational r2)

{ Rational r;

int x=(r1.denominator*r2.denominator)/gcd(r1.denominator,r2.denominator); // lcm

r.numerator= r1.numerator*(x/r1.denominator)+(r2.numerator*(x/r2.denominator));

r.denominator=x;

return r;

Rational simplify()

int common_divisor=gcd(numerator,denominator);

numerator/=common_divisor;

denominator/=common_divisor;

return *this;

Rational subtract(Rational r1,Rational r2)

Rational r;

int x=(r1.denominator*r2.denominator)/gcd(r1.denominator,r2.denominator); //lcm

r.numerator= r1.numerator*(x/r1.denominator)-(r2.numerator*(x/r2.denominator)); //
numerator

r.denominator=x;

return r;

Rational multiply(Rational r1,Rational r2)

Rational r;

r.numerator=r1.numerator*r2.numerator;

r.denominator=r1.denominator*r2.denominator;

return r;
}

Rational divide(Rational r1,Rational r2)

Rational r;

r.numerator=r1.numerator*r2.denominator;

r.denominator=r1.denominator*r2.numerator;

return r;

void display()

cout<<numerator<<"/"<<denominator<<endl;

};

int main()

Rational r1(288,14),r2(35,15);

cout<<"Rational 1 : ";

r1.simplify();

r1.display();

cout<<"Rational 2 : ";

r2.simplify();

r2.display();

Rational sum=r1.add(r1,r2);

cout<<"ADDITION: ";

sum.simplify();

sum.display();

Rational difference=r1.subtract(r1,r2);

cout<<"SUBTRACTION: ";

difference.simplify();

difference.display();
Rational multi=r1.multiply(r1,r2);

cout<<"MULTIPLICATION: ";

multi.simplify();

multi.display();

Rational div=r1.divide(r1,r2);

cout<<"DIVISION: ";

div.simplify();

div.display();

return 0;

OUTPUT:

PROBLEM 2:

CODE:

#include<bits/stdc++.h>

using namespace std;

int maxSubSum1(int arr[],int n)

int maxSum=0;

for(int i=0;i<n;i++)
{

for(int j=i;j<n;j++)

int thisSum=0;

for(int k=i;k<=j;k++)

thisSum+=arr[k];

if(thisSum>maxSum)

maxSum=thisSum;

return maxSum;

int maxSubSum2(int arr[],int n)

int maxSum=0;

for(int i=0;i<n;i++)

int thisSum=0;

for(int j=i;j<n;j++)

thisSum+=arr[j];

if(thisSum>maxSum)

maxSum=thisSum;

return maxSum;

int max3(int a,int b,int c)

{
return a>b?a>c?a:c:b>c?b:c;

int maxSubSum3(int arr[],int n)

int maxSum=0,thisSum=0;

for(int j=0;j<n;j++)

thisSum+=arr[j];

if(thisSum>maxSum)

maxSum=thisSum;

else if(thisSum<0)

thisSum=0;

return maxSum;

int maxSubSum4(int arr[],int left,int right)

if(left==right) // base case

if(arr[left]>0)

return arr[left];

else

return 0;

int center=(left+right)/2;

int maxLeftSum=maxSubSum4(arr,left,center);

int maxRightSum=maxSubSum4(arr,center+1,right);

int maxLeftBorderSum=0,leftBorderSum=0;

for(int i=center;i>=left;i--)

{
leftBorderSum+=arr[i];

if(leftBorderSum>maxLeftBorderSum)

maxLeftBorderSum=leftBorderSum;

int maxRightBorderSum=0,rightBorderSum=0;

for(int j=center+1;j<=right;j++)

rightBorderSum+=arr[j];

if(rightBorderSum>maxRightBorderSum)

maxRightBorderSum=rightBorderSum;

return max3(maxLeftSum,maxRightSum,maxLeftBorderSum+maxRightBorderSum);

int main()

int size;

cout<<"Enter the size of the array: ";

cin>>size;

int arr[size];

for(int i=0;i<size;i++)

{int r=rand()%1999-999;

arr[i]=r;}

clock_t starttime,endtime;

starttime=clock();

maxSubSum1(arr,size);

endtime=clock();

cout<<"The time required for algorithm 1: "<<double(endtime-


starttime)/double(CLOCKS_PER_SEC)<<"sec"<<endl;

cout<<"The result of the algorithm 1: "<<maxSubSum1(arr,size)<<endl;

starttime=clock();

maxSubSum2(arr,size);
endtime=clock();

cout<<"The time required for algorithm 2: "<<double(endtime-


starttime)/double(CLOCKS_PER_SEC)<<"sec"<<endl;

cout<<"The result of the algorithm 2: "<<maxSubSum2(arr,size)<<endl;

starttime=clock();

maxSubSum3(arr,size);

endtime=clock();

cout<<"The time required for algorithm 3: "<<double(double(endtime-


starttime)/double(CLOCKS_PER_SEC))<<"sec"<<endl;

cout<<"The result of the algorithm 3: "<<maxSubSum3(arr,size)<<endl;

starttime=clock();

maxSubSum4(arr,0,size-1);

endtime=clock();

cout<<"The time required for algorithm 4: "<<(double(endtime-


starttime)/double(CLOCKS_PER_SEC))<<"sec"<<endl;

cout<<"The result of the algorithm 4: "<<maxSubSum4(arr,0,size-1)<<endl;

OUTPUT:

You might also like