Input A, B, C x1 - B B - 4ac 2a

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 31

QUADRATIC EQUATION: ax2 + bx + c = 0 Input a, b, c

Y
_____________

a equ 0 ? disc = b2 - 4ac


Not a Quadratic Equ bx + c = 0 Y Two real factors of x

x1= -b b2 - 4ac 2a

disc > 0 ?
Two complex factors of x
______

disc < 0 ?
______________

x = -c b Print x

x1= -b + disc 2a
______

x1= -b + abs (disc) i 2a 2a


_____________

One real factors of x

x1= -b - disc 2a Print x1, x2

x = -b 2a Print x

x1= -b - abs (disc) i 2a 2a Print x1, x2

QUADRATIC EQUATION: ax2 + bx + c = 0 #include<iostream.h> #include<math.h> void main() { cout << "SOLUTION OF QUADRATIC EQUATION" << endl; // How much float a,b,c ; variables are to be declared and what will be the type // write prompt for a"; andcin input cout <<instructions Enter the for Value of \ a \ = >> a a; // write prompt for b"; andcin input cout <<instructions Enter the for Value of \ b \ = >> b b; // write instructions for prompt for c and input c cout << Enter the Value of \ c \ = "; cin >> c; cout << endl << RESULT: ; // write instruction to declare and double disc = ( (b*b) - (4*a*c) ) ; calculate disc = b2 - 4ac if ( a != 0 ) // equation is quadratic equation of the form ax2 + bx + c = 0
{ if ( disc > 0 ) // write cout instruction to calculate and output the first real factor of x // write cout instruction to calculate and output the second real factor of x else if ( disc < 0 ) // write cout instruction to calculate and output the first complex factor of x // write cout instruction to calculate and output the second complex factor of x else } // disc == 0 // write cout instruction to calculate the only value of x

else }

// if a = 0, equation is a linear equation of the form bx + c = 0 cout << endl << x = " << ( (-1)*c) / b ;

QUADRATIC EQUATION: ax2 + bx + c = 0 #include<iostream.h> #include<math.h> void main() { cout << "SOLUTION OF QUADRATIC EQUATION" << endl; float a,b,c; cout << Enter the Value of \ a \ = "; cin >> a; cout << Enter the Value of \ b \ = "; cin >> b; cout << Enter the Value of \ c \ = "; cin >> c; cout << endl << RESULT: ; double disc = ( (b*b) - (4*a*c) ) ; if ( a != 0 ) {{ if ((disc disc if >> 00 ) ) cout << endl = x1 = "( << ( (-1*b) ) +(disc) ( (sqrt {{cout << endl <<<< x1 " << (-1*b) / (2*a)/)(2*a) + ( (sqrt ) / (disc) (2*a) ) ) ; / (2*a) ) ; cout << endl << x2 = " << ( (-1*b) / (2*a) ) - ( (sqrt (disc) ) / (2*a) ) ; } cout << endl << x2 = " << ( (-1*b) / (2*a) ) - ( (sqrt (disc) ) / (2*a) ) ; } else if ( disc < 0 ) else if ( disc < 0 ) { disc = fabs ( disc ) ; { disc = fabs ( disc ) ; cout << endl << x1 = " << ( (-1*b) / (2*a) ) << + " << ( (sqrt (disc) ) / (2 * a ) ) << i "; cout << endl << x1 = " << ( (-1*b) / (2*a) ) << + " << ( (sqrt (disc) ) / (2 * a ) ) << i "; cout << endl << x2 = " << ( (-1*b) / (2*a) ) << - " << ( (sqrt (disc) ) / (2 *a ) ) << i ; } cout << endl << x2 = " << ( (-1*b) / (2*a) ) << - " << ( (sqrt (disc) ) / (2 *a ) ) << i ; } else if ( disc == 0 ) else cout << endl << x = " << ( (-1*b) / (2*a) ) ; } cout << endl << x = " << ( (-1*b) / (2*a) ) ; else } cout << endl << x = " << ( (-1)*c) / b ; }

Write a C++ program that computes and outputs the volume of a cone, the user will input the diameter of its base and its height.

volume of cone = x Radius2 x Height


where = 3.1415927
What is the minimum numbers of variables to be declared ? What should be their Data Type ? Is there any constant declaration and initialization ? How can You use the value of ?
Use cout to prompt for input & cin for input the values of the variables Use cout for proper labeling the output & printing its value VOLUME OF CONE Enter the height of Cone = 10.75 Enter the Radius of its base = 3. 45

Height

Radius

The Volume of the Cone = 401. 9726764513

Write a c++ program that computes the mean and standard deviation of a set of five integer values The mean is the sum of the five values divided by five

Formula for the Standard Deviation is

Where n = 5, x i refers to each of five values, and is the mean. Note that although the individual values are integers, the results are the floating point values
There are five steps in the program

1. 2. 2. 3. 4.

Declare input, output variables and variables which store the intermediate results Input Five integer numbers Calculate sum, divide it by five to get the Mean Calculate Standard Deviation Output the Mean and Standard Deviation

1.

Decide what should be the output variables, input variables and variables used to store results of intermediate steps There are two output variables 1. is the mean, let we identify it by mean, as it will be a fractional number, so its type will be of type float. 2. s is the standard deviation, let we identify it by sd, its type will also be float so our declaration will be as float mean, sd ; There are five input integer variables, It is better to save them in an integer array of size 5 instead of declaring five individual integer variables const int size = 5; int x [ size ]; Note that the subscript of array is started from 0, so the formulae can be rewritten as

When calculating the mean, we should have an integer variable which will hold the sum of all input numbers,

let us call it sum and initialize it to 0, we will add to it each array element x [ i ] one by one, when all five array elements have been added, we will get the sum so in the declaration int sum = 0 ;

When calculating the Standard Deviation, we should have an float variable which will hold the sum of all

let us call it sumofsqdiff and initialize it to 0, we will add each one by one, when all squares of five differences have been added, we can calculate the standard deviation

Now we can write the complete declaration part as float sd, mean, sumofsqdiff = 0 ; const int size = 5 ; int x [ size ] = {0} ; int sum = 0; 2. Now we have to input five integer numbers into x array, we can use for loop for this purpose First we should prompt the user what is supposed to input and how Suppose we input the five integer value on one line, each separated by space So we should prompt cout << endl << "Enter the Five Integer Numbers separated by spaces"<< endl; endl at the start will assure that the prompt will be started on new line, and at the end it assure that the user will input from the next line to it For inputting five integers into x array for ( int i = 0 ; i < size ; i++ ) cin >> x [ i ] ; Recall we can write it as for ( int i = 0 ; i < size ; cin >> x [ i++ ] ) ;

3. In the third step first we have to sum the five array elements, and store it in integer variable sum. we have already initialize sum to 0 for ( i = 0 ; i < size ; i++ ) ; sum = sum + x [ i ] ; Can we optimize it as ? for ( i = 0 ; i < size ; sum += x [ i++ ] ) ; // i has been already declared in // previous for loop, so dont re-declare it

Now we can easily calculate mean as mean = sum / size ; but sum and size are both integer variables and expression sum / size evaluates to integer (fractional part of the result will be truncated), so use explicit type casting i.e., mean = ( float (sum)) / ( float (size)) ;

4. In the fourth step, first we will calculate the sum of all sumofsqdiff

and store it in variable

we have already initialize the sumofsqdiff to 0 again we will use for loop, see the progress steps for ( i = 0 ; i < size ; i ++ ) sumofsqdiff = sumofsqdiff + ( x [ i ] mean ) 2 ; but x [ i ] is integer and mean is float, so for ( i = 0 ; i < size ; i ++ ) sumofsqdiff = sumofsqdiff + (float ( x [ i ] ) mean ) 2 ; or for ( i = 0 ; i < size ; i ++ ) sumofsqdiff = sumofsqdiff + pow( (float ( x [ i ] ) mean ), 2 ) ; or for ( i = 0 ; i < size ; sumofsqdiff += pow ( ( float ( x [ i++ ] ) mean ) , 2 ) ) ; Now we can calculate the Standard Deviation as
____________________________________

sd = sumofsqdiff / ( size-1) or sd = sqrt ( sumofsqdiff / ( size-1) )

5. In the fifth step we will simply output the mean and standard deviation with proper labeling

cout << endl << "mean=" << mean; cout << endl << "Standard Deviation=" << sd;
Now all the five steps to construct the program has been completed,

Have you complete the program ? try to compile it and run it,

#include<iostream.h> #include<math.h> void main() { float sd, mean, sumofsqdiff = 0 ; const int size = 5 ; int x [ size ] = {0} ; int sum = 0; cout << endl << "Enter the Five Integer Numbers separated by spaces"<< endl; for ( int i = 0 ; i < size ; cin >> x [ i++ ] ) ;

for ( i = 0 ; i < size ; sum += x [ i++ ] ) ; mean = ( float ( sum ) ) / ( float ( size ) ) ;
for ( i = 0 ; i < size ; sumofsqdiff += pow ( ( float ( x [ i++ ] ) mean ) , 2 ) ) ; sd = sqrt ( sumofsqdiff / ( size-1) ) ; cout << endl << "mean=" << mean; cout << endl << "Standard Deviation=" << sd; }

Stirling Formula
__________________

n! = e -n nn 2 n
Is used to calculate the Factorial of large numbers without using any loop or recursive function. Here = PI e = base of natural Logarithm Write a program which prompts the user to enter a digit, then calculate the Factorial using stirling formula, and output the result with proper labeling.

Selection Sort: A selection sort searches an array looking for the smallest element in the array , then the smallest element is swapped with the first element of the array. The process is repeated for the subarray beginning with the second element of the array. Each pass of the array results in one element being placed in its proper location. When the subarray being processed contains one element, the array is sorted. Write a function selectionsort to perform this algorithm.

let us review some basic operations let a is an array of size 5 a [0] a [1] a [2] a [3] a [4] i.e., 16 10 6 4 3 const int arraySize = 5 ; a [ arraySize ] = { 16, 10, 6, 4, 3 } ; Swapping two elements of array means interchanging their locations To swap a [ 2 ] with a [ 4 ], we need an intermediate memory location ( a variable ), other wise contents will be overwritten, lets declare a variable temp. int temp = a [ 2 ] ; // save the value of a [ 2 ] in temp, a[2]=a[4]; // copy the value of a [ 4 ] in a [ 2 ] a [ 4 ] = temp ; // copy the saved value of a [ 2 ] in a [ 4 ]

Selection Sort: A selection sort searches an array looking for the smallest element in the array , then the smallest element is swapped with the first element of the array. The process is repeated for the subarray beginning with the second element of the array. Each pass of the array results in one element being placed in its proper location. When the subarray being processed contains one element, the array is sorted. Write a function selectionsort to perform this algorithm. let us review some basic operations let a is an array of size 5 i.e., const int arraySize = 5 ; a [ arraySize ] = { 16, 10, 6, 4, 3 } ; Swapping two elements of array means interchanging their locations

a [0] a [1] a [2] a [3] a [4] 16 10 6 4 3

FOR LOOP - REVISION


for ( init-expressionopt ; cond-expressionopt ; loop-expressionopt ) statement ;
Init-expression Init-expression Init-expression

False
cond-expression True statement Loop expression Body True Statements break statements Cond-expression

False
True

Cond-expression

Body

Statements continue statements

Body

Loop expression

Loop expression

LINEAR ARRAYS
let
const int arraySize = 5; int s[arraySize]; Initialization of every element to zero: s[arraySize]={0}; for (int i=0; i<arraySize; i++) s [ i ] = 0; for (int i=0; i<arraySize; s[i++]=0); Initialize every element w.r.t. its position for (int i=0; i<arraySize; i++) s [ i ] = i+1; for (int i=0; i<arraySize; s[i]=1+(i++)); Initialize every element w.r.t. its index for (int i=0; i<arraySize; i++) s [ i ] = i; for (int i=0; i<arraySize; s[i]=i++);
s 0 0 0 0 0 s

LINEAR ARRAYS
let
const int arraySize = 5; int i, s[arraySize]; Sum up all the elements int sum = 0; for (i=0; i<arraySize; i++) sum += s [ i ]; Product of all the elements int product = 1; for (i=0; i<arraySize; i++) product *= s [ i ]; Input array cout << \n\nEnter Array of << size << elements; for (i=0; i<size ; i++) cin >> s[i]; for (i=0,cout<< "\n\nEnter Array of "<< size << " elements= "; i<size ; cin >> s[i++]); Printing array cout << You have entered Array = ; for (i=0; i<size; i++); cout << s[j] << ; for (i=0,cout<< "You Have entered Array= "; i<size; cout << a[i++]<<" ");

Write a Program which inputs a linear array of size 5, Print that array, print the sum of all elements and print the product of all elements. The output should look like this #include<iostream.h> void main() { const int size = 5 ; int a [ size ] = { 0 } ; int i = 0, sum = 0, prod = 1; for ( i = 0, cout << "\n\nEnter Array of "<< size << " elements = "; i<size ; cin >> a [ i++ ] ) ; for ( i =0, cout << "You Have entered Array = "; i<size; cout << a [ i++ ] << " ) ;

for ( i=0; i<size ; i++ ) sum+= a [ i ] ;


for ( i=0; i<size ; i++ ) prod *= a [ i ] ; cout << "\n Array Element Sum = "<< sum << " and Product = "<< prod ; } Enter array of 5 elements = 1 2 3 4 5 You have entered Array = 1 2 3 4 5 Array Element Sum = 15 and Product = 120

Dual Scripted Arrays


let
const int rows = 3; const int cols = 4; int a[rows][cols], r, c; Initialization of every element to zero: a[rows][cols]={0}; for (r=0; r<rows; r++) for (c=0;c<cols; a[r][c++]=0); Inputting values in 2D-Matrix or Dual Scripted Array char *inmsg=Enter input matrix of order ; cout << inmsg << rows << X << cols << \n; for (r=0; r<rows; r++) for (c=0; c<cols; c++) cin >> a[r][c]; for (r=0,cout << inmsg << " << rows << "X << cols <<" \n"; r<rows; r++) for (c=0; c<cols; cin >> a[r][c++]);
rows 0 0 1 2 cols 1 2 3

Dual Scripted Arrays


let
const int rows = 3; const int cols = 4; int a[rows][cols], r, c;
rows 0 0 1 2 cols 1 2 3

Printing 2D-Matrix or Dual Scripted Array char *outmsg="\nEntered Matrix is \n"; cout << outmsg << rows << "X << cols; for (r=0; r<rows; r++) { cout << endl; for (c=0; c<cols; c++); cout << setw(3) << a[r][c] << ; }

for (r=0, cout<<outmsg<<" "<<rows<<"X"<<cols; r<rows; r++) for (c=0, cout<<endl; c<cols; cout<< setw(3) <<a[r][c++]<<" ");

Write a program which will input a 3X2 matix, and then print it, the input/output should be as given #include<iostream.h> #include<iomanip.h> void main() { const int rows=3; const int cols=2; int r, c, a[rows][cols], aT[cols][rows]; char *inmsg="Enter Input Marix of Order"; char *outmsg="\nEntered Matrix is"; cout << endl << endl; for (r=0, cout<<inmsg<<" "<<rows<<"X"<<cols<<"\n"; r<rows; r++) for (c=0; c<cols; cin>>a[r][c++]);

for (r=0, cout<<outmsg<<" "<<rows<<"X"<<cols; r<rows; r++) for (c=0, cout<<endl; c<cols; cout<< setw(3) <<a[r][c++]<<" "); } Enter Input Matrix of Order 3X2 5 6 2 3 1 5 Entered Matrix is 5 6 2 3 1 5

ADDITION OF MATRICES
Both Matrices should be of same order
a00 a10 a20 a01 a11 a21 b00 b10 b20 b01 b11 b21 a00 + b00 a10 + b10 a20 + b20 a01 + b01 a11 + b11 a21 + b21

sum00 sum10 sum20

sum01 sum11 sum21

We require a third matrix sum (of the same order as that of a or b ) to store the result of addition Obviously we can show the above notations as
a[0][0] a[1][0] a[0][1] a[1][1] b[0][0] b[0][1] b[1][1] a[0][0] + b[0][0] a[0][1] + b[0][1] a[1][1] + b[1][1]

b[1][0]

a[1][0] + b[1][0]

a[2][0]

a[2][1]

b[2][0]

b[2][1]

a[2][0] + b[2][0]

a[2][1] + b[2][1]

sum[0][0] sum[1][0] sum[2][0]

sum[0][1] sum[1][1] sum[2][1]

Write a program which will input two 3X2 matricesx, add them and then print out the summing matrix, the input/output should be as given #include<iostream.h> #include<iomanip.h> void main() { const int rows=3; const int cols=2; int r, c, a[rows][cols], b[rows][cols], sum[rows][cols]; char *inmsg="Enter Marix of Order"; char *outmsg="\nSum of matrices is"; cout << endl << endl; for (r=0, cout<<inmsg<<" "<<rows<<"X"<<cols<<"\n"; r<rows; r++) for (c=0; c<cols; cin>>a[r][c++]); for (r=0, cout<<inmsg<<" "<<rows<<"X"<<cols<<"\n"; r<rows; r++) for (c=0; c<cols; cin>>b[r][c++]); for (r=0; r<rows; r++) for (c=0; c<cols; c++) sum[r][c]=a[r][c]+b[r][c]; for (r=0, cout<<outmsg<<" "<<cols<<"X"<<rows; r<rows; r++) for (c=0, cout<<endl; c<cols; cout<<setw(4)<<sum[r][c++]<<" "); }
Enter Input Matrix of Order 3X2 5 6 2 3 1 5 Enter Input Matrix of Order 3X2 1 2 2 3 3 4 Sum of matrices is 6 8 4 6 4 9

TRANSPOSE OF A MATRIX
If a is a Matrix of Order mxn then an nxm matrix at obtained by inter-changing the rows and columns of a is called the transpose of a

a =

a00 a01 a10 a11 a20 a21

at

b00 b01 b10 b11

b02 b12

Write a program which will input two 3X2 matricesx, add them and then print out the summing matrix, the input/output should be as given #include<iostream.h> #include<iomanip.h> void main() { const int rows = 3 ; const int cols = 2 ; int r, c, a[rows][cols ], aT[cols][rows ] ; char *inmsg = "Enter Input Marix of Order ; char *outmsg = "\nTranspose matrix is "; cout << endl << endl;

for (r=0, cout << inmsg <<" << rows << "X <<cols<<"\n"; r<rows; r++) for (c=0; c<cols; cin >> a[r][c++]);
for (r=0; r<rows; r++) for (c=0; c<cols; c++) aT[c][r] = a[r][c];

for (r=0, cout << outmsg << " << cols << "X << rows; r<cols; r++) for (c=0, cout << endl; c<rows; cout << aT[r][c++] << " ");
}
Enter Input Matrix of Order 3X2 5 6 2 3 1 5 Transpose Matrix is 2X3 5 2 1 6 3 5

IDENTITY MATRIX
A Square Matrix A = [ a i j ] is said to be an Identity or Unit Matrix, if
aij=0 aij=1 for all for all ij i=j

e.g.,

1 0 0 0 0

0 1 0 0 0

0 0 1 0 0

0 0 0 1 0

0 0 0 0 1

is an identity matrix of Order 5X5

Write a Program which inputs a square matrix, and figure out whether it is an identity matrix or not, Properly Labels the input and output.

MULTIPLICATION OF TWO MATRICES


Two Matrices A and B can be multiplied for the product AB, if number of Columns of A = number of Rows of B

a00 a01 a02 a10 a11 a12


Rows1=2 Cols1 =3

b00 b01 b10 b11

+ =

b20 b21
Rows2=3 Cols2 =2

a10b00 +a11b10 +a12b20 a10b01 +a11b11 +a12b21


Rows1=2 Cols2 =2

We can rewrite individual elements of product Matrix

prod00= a00b00 +a01b10 +a02b20 prod10= a10b00 +a11b10 +a12b20


Can You Trace some patterns in the subscripts

prod01= a00b01 +a01b11 +a02b21 prod11= a10b01 +a11b11 +a12b21 prod01= a00b01 +a01b11 +a02b21 prod11= a10b01 +a11b11 +a12b21

prod00= a00b00 +a01b10 +a02b20 prod10= a10b00 +a11b10 +a12b20

MULTIPLICATION OF TWO MATRICES


prod00= a00b00 +a01b10 +a02b20
prod10= a10b00 +a11b10 +a12b20
In the product Matrix Rows = Rows1 Cols = Cols2 So to calculate each element, we will have to use two nested For loops for (r=0; r<rows1; r++) for (c=0; c<cols2; c++) { \\ some thing to be done here } Also note that there are 3 products in each element, which equals the Number of Cols in First Matrix (= Number of Rows in Second Matrix), So we extend our code for (r=0; r<rows1; r++) for (c=0; c<cols2; c++) for (int i=0;i<cols1; i++) { \\ some thing to be done here }

prod01= a00b01 +a01b11 +a02b21


prod11= a10b01 +a11b11 +a12b21

MULTIPLICATION OF TWO MATRICES


prod00= a00b00 +a01b10 +a02b20
prod10= a10b00 +a11b10 +a12b20

prod01= a00b01 +a01b11 +a02b21


prod11= a10b01 +a11b11 +a12b21

for (r=0; r<rows1; r++) for (c=0; c<cols2; c++) for (int i=0;i<cols1; i++) { prod [ r ] [ c ] += a [ ] [ ] * b [ ] [ ]; } Also note that some elements remains constant for (r=0; r<rows1; r++) for (c=0; c<cols2; c++) for (int i=0;i<cols1; i++) prod [ r ] [ c ] += a [ r ] [ ] * b [ ] [ c ]; Also note that elements which doesnt remains constant for (r=0; r<rows1; r++) for (c=0; c<cols2; c++) for (int i=0;i<cols1; i++) prod [ r ] [ c ] += a [ r ] [ i ] * b [ i ] [ c ]; Complete the program to input Two matrices, multiply them and store the value in third Prod Matrix

#include<iostream.h> #include<iomanip.h> void main() { const int rows1=3; const int rows2=cols1;

const int cols1=3; const int cols2=3;

int r, c, a[ rows1][ cols1], b[ rows2 ][ cols2 ], prod[ rows1][ cols2 ] = {0}; char *inmsg = "Enter Marix of Order"; char *outmsg = "\nProduct of matrices is"; cout << endl << endl; for ( r=0, cout << inmsg << " << rows1 << "X << cols1 << "\n ; r<rows1; r++ ) for ( c=0 ; c<cols1; cin >> a[ r ][ c++ ] ); for ( r=0, cout << inmsg << " << rows2 << "X << cols2 << "\n ; r<rows2 ; r++ ) for (c=0; c<cols2; cin>>b[r][c++]); for ( r=0 ; r<rows1; r++ ) for ( c=0 ; c<cols2 ; c++ ) for ( int i=0 ; i<cols1 ; i++ ) prod [ r ][ c ] += a [ r ][ i ]*b [ i ][ c ]; for ( r=0, cout << outmsg << " << rows1 << "X << cols2 ; r<rows1; r++ ) for ( c=0, cout << endl ; c<cols2 ; cout << setw(4) << prod[ r ][ c++ ] << " ); }

You might also like