Sie sind auf Seite 1von 6

Marked Exercise #1

C++ Programming (EE6411)

Submitted By
Mehul Chandani: 14114526

Date
14th Oct 2015

Page 1 of 6

Pseudo-code Algorithm:
Variable
up
low
s
result

Description
Upper Limit
Lower Limit
Number intervals
Final answer computing

Basic Formula:
a) Function integrate (low, up, s):
x = (up - low) / s
result = addition from 0 to s-1 of x*( f (low) + f (low + x) + f (low +2x)
+ f (low +(s-1)*x))
b) Function f (x):
ans = x*x + 2*x + 3

Implementing code:
a)Function Inte(low, upper, noi):
{
x = (up-low)/noi;
for (i=0; i<noi; i++)
{
result += x*compute(lower+(i*x));
}
return result;
}
b) Funcation compute(x):
{
return (x*x + 2.0*x+3.0);
}

Page 2 of 6

Code Explanation:
Line
17
9 - 10
13-14
17
19
20
21
22
23
26
28-29
30
32-33
35
37-38
40-44

45-49

50-53

54-55
56
58-59

60-63

Explanation
Program details
Standard declaration of library
Function declaration
initialization of main
Variable to store upper limit
Variable to store lower limit
Variable to store number of iterations
Variable to store answer
Input for upper limit
If condition to check the upper limit before taking
lower limit.
Taking input for lower limit
Nested if to check the lower limit with given limit and
with upper limit
Taking input for number of iterations
Nested if condition to check the value greater then 0
Computing the inputs by calling a Inte function for
integration & printing output of final result
Else block of if condition (line 35) to end the program
with a message Invalid input if user enters invalid
number of iterations(less than zero or negative
number)
Else block of if condition (line 30) to end the program
with a message invalid input if user enter invalid
lower limit.
Else block of if condition (line 26) to end the program
with a message invalid input if user enter invalid
upper limit.
Return to main block and end of main block.
Inte function to compute given formula. Taking 3
inputs from main function.
Variable s to store the output from the given
formula, variable result to store the final output after
computing.
For loop to compute the given data till number of
iteration selected by user.
Page 3 of 6

64

Return statement to main with final output.

67-70

Function compute is to calculate x*x + 2.0*x+3.0 and


return value to function Inte.

Page 4 of 6

Source code:
/************************************************************
* Marked Exercise 1
* Created on : 13/10/2015
* Student no: 14114526
* Name: Mehul Chandani
*
************************************************************/
#include <iostream>
using namespace std;
// Function Declarations
double Inte (double, double, int); // Function to integrate
double compute(double); // Calculates the value of the function x^2 + 2x +
3
// Main Function
int main()
{
double up = 0; // upper limit
double low = 0; // lower limit
int s = 0; // step size
double out = 0; // output variable
cout << "Enter the upper limit between -10.5 to 30.75" << endl;
cin >> up ;
if (up >= -10.75 && up <= 30.75)
{
cout << "Enter the lower limit between -10.5 to 30.75" << endl;
cin >> low;
if (low >= -10.75 && low <= 30.75 && low < up)
{
cout << "Enter number of iterations to solve(greater then 0)" <<
endl;
cin >> s;
if (s > 0)
{
out = Inte(low, up, s);
cout << "Integration value is : " << out << endl;
}
else
{
cout << "Invalid Input" << endl;
}
}
else
{
cout << "Invalid Input" << endl;
}
}
else
{
cout << "Invalid Input" << endl;
}
return 0;
}
double Inte(double lower, double upper, int noi)

Page 5 of 6

{
double s = (upper-lower)/noi;
double result = 0.0;
for (int i=0; i<noi; i++)
{
result += s*compute(lower+(i*s));
}
return result;
}
double compute(double x)
{
return (x*x + 2.0*x+3.0);
}

Page 6 of 6

Das könnte Ihnen auch gefallen