+ All Categories
Home > Education > C++ TUTORIAL 4

C++ TUTORIAL 4

Date post: 21-Jun-2015
Category:
Upload: wmhan-farhan
View: 173 times
Download: 0 times
Share this document with a friend
Description:
Learn how function sub program can be used
Popular Tags:
19
NAME: WAN MOHAMAD FARHAN BIN AB RAHMAN SJEM2231 STRUCTURED PROGRAMMING TUTORIAL 4/ LAB 4 (27 th OCTOBER 2014) QUESTION 1 Write a function program to find the factorial of a given number (N!) //Using for loop: #include <iostream> using namespace std; // Function definition (header and body) int factorial(int N) { int a, factorial=1; for(a=1; a<=N; a++) { factorial = factorial*a; } return factorial; // This value is returned to caller } int main() { int N; cout << "Please enter a number = "; cin >> N; cout << "The factorial for " << N << " is " << factorial(N) << endl; return 0; } //Output for for loop: Please enter a number = 6 The factorial for 6 is 720
Transcript
Page 1: C++ TUTORIAL 4

NAME: WAN MOHAMAD FARHAN BIN AB RAHMAN

SJEM2231 STRUCTURED PROGRAMMING

TUTORIAL 4/ LAB 4 (27th OCTOBER 2014)

QUESTION 1

Write a function program to find the factorial of a given number (N!)

//Using for loop:

#include <iostream>

using namespace std;

// Function definition (header and body)

int factorial(int N)

{

int a, factorial=1;

for(a=1; a<=N; a++)

{

factorial = factorial*a;

}

return factorial; // This value is returned to caller

}

int main()

{

int N;

cout << "Please enter a number = ";

cin >> N;

cout << "The factorial for " << N << " is " << factorial(N) << endl;

return 0;

}

//Output for for loop:

Please enter a number = 6

The factorial for 6 is 720

Page 2: C++ TUTORIAL 4

//Using if else statement

#include <iostream>

using namespace std;

long factorial ( int a )

{

if (a>1)

return (a*factorial(a-1));

else

return 1 ;

}

int main ()

{

long number;

cout << "Please insert a number : ";

cin >> number;

cout << number <<"! =" << factorial(number)<<endl;

return 0;

}

//Output of if-else statement

Please insert a number : 8

8! =40320

Page 3: C++ TUTORIAL 4

// Using the first way of while loop:

#include <iostream>

using namespace std;

// Function definition (header and body)

int factorial(int N)

{

int factorial=1;

while ( N > 0)

/* while loop continues until test condition N>0 is true */

{

factorial= factorial*N;

--N;

}

return factorial; // This value is returned to caller

}

int main()

{

int N;

cout << "Please enter a number = ";

cin >> N;

cout << "The factorial for " << N << " is " << factorial(N) << endl;

return 0;

}

Page 4: C++ TUTORIAL 4

//Output of first way of using while loop:

Please enter a number = 8

The factorial for 8 is 40320

//Using the second way of while loop:

#include <iostream>

using namespace std;

// Function definition (header and body)

int factorial(int N)

{

int n=1,factorial=1;

while ( n <= N)

/* while loop continues until test condition N>0 is true */

{

factorial= factorial*n;

n++;

}

return factorial; // This value is returned to caller

}

int main()

{

int N;

cout << "Please enter a number = ";

cin >> N;

cout << "The factorial for " << N << " is " << factorial(N) << endl;

Page 5: C++ TUTORIAL 4

return 0;

}

//Output using second way of while loop

Please enter a number = 8

The factorial for 8 is 40320

QUESTION2

Using function program ODD() and EVEN(), find the sum of odd (1+3+ ... +N) and even

(2+4+6+…+N) numbers less than N

//Using for loop

#include <iostream>

using namespace std;

// Function definition (header and body)

int sum_even(int N)

{

int i, sum_even=0;

for(i=1; i<N; i++)

{

if(i%2==0)

sum_even=sum_even +i;

}

return sum_even;

}

int sum_odd(int N)

{

int i,sum_odd=0;

for(i=1; i<N; i++)

{

if(i%2!=0)

sum_odd = sum_odd +i;

}

Page 6: C++ TUTORIAL 4

return sum_odd;

}

int main()

{

int N;

cout<<"Please enter a number N: ";

cin>>N;

cout<<"\nThe summation of even number of N is: " <<sum_even(N) <<endl;

cout<<"The summation of odd number of N is: " <<sum_odd(N) <<endl;

return 0;

}

//Output by using for loop:

Please enter a number N: 10

The summation of even number of N is: 20

The summation of odd number of N is: 25

//Using while loop

#include <iostream>

using namespace std;

// Function definition (header and body)

int sum_even(int N)

{

int i=1, sum_even=0;

while (i< N)

Page 7: C++ TUTORIAL 4

{

if (i%2 ==0)

sum_even = sum_even + i;

i++;

}

return sum_even;

}

int sum_odd(int N)

{

int i=1,sum_odd=0;

while (i< N)

{

if (i%2 !=0)

sum_odd = sum_odd + i;

i++;

}

return sum_odd;

}

int main()

{

int N;

cout<<"Please enter a number N: ";

cin>>N;

cout<<"\nThe summation of even number of N is: " <<sum_even(N) <<endl;

cout<<"The summation of odd number of N is: " <<sum_odd(N) <<endl;

Page 8: C++ TUTORIAL 4

return 0;

}

//Output for while loop

Please enter a number N: 8

The summation of even number of N is: 12

The summation of odd number of N is: 16

//Using do while loop:

#include <iostream>

using namespace std;

/* Function definition (header and body) */

int sum_even(int N)

{

int i=1, sum_even=0;

do

{

if (i%2 ==0)

sum_even = sum_even + i;

i++;

} while (i< N);

return sum_even;

}

int sum_odd(int N)

{

int i=1,sum_odd=0;

do

Page 9: C++ TUTORIAL 4

{

if (i%2 !=0)

sum_odd = sum_odd + i;

i++;

} while (i< N);

return sum_odd;

}

int main()

{

int N;

cout<<"Please enter a number N: ";

cin>>N;

cout<<"\nThe summation of even number of N is: " <<sum_even(N) <<endl;

cout<<"The summation of odd number of N is: " <<sum_odd(N) <<endl;

return 0;

}

//Output for do while loop

Please enter a number N: 8

The summation of even number of N is: 12

The summation of odd number of N is: 16

Page 10: C++ TUTORIAL 4

QUESTION 3

Write a function program to solve the quadratic equations of the form ax2 + bx + c = 0, where a,

b, and c are given real number and x is the unknown.

#include <iostream>

#include <cmath>

using namespace std;

/**function prototype or declaration, when int main() is placed on the top**/

float discriminant(float a, float b, float c);

void TWOREAL(float a,float b,float c);

void ONEREAL(float a,float b,float c); //also called as two equal solution

void COMPLEX(float a,float b,float c);

int main()

{

float a, b, c;

cout << "This program is used to solve Quadratic Equation\n";

cout << "in the form of ax^2 + bx +c\n\n";

do

{

cout << "Enter value a:";

cin >> a;

}

while (a == 0); // this is a condition where value of a cannot equal to 0

cout << "Enter value b:";

cin >> b;

cout << "Enter value c:";

Page 11: C++ TUTORIAL 4

cin >> c;

if (discriminant(a,b,c) > 0)

TWOREAL(a,b,c);

else if (discriminant(a,b,c) == 0)

ONEREAL(a,b,c);

else

COMPLEX(a,b,c);

return 0;

}

float discriminant(float a, float b, float c)

{

float d = b*b-4*a*c;

return d;

}

void TWOREAL(float a,float b,float c)

{

float x1,x2;

x1 = (-b + sqrtf(discriminant(a,b,c)) )/(2*a);

x2 = (-b - sqrtf(discriminant(a,b,c)) )/(2*a);

cout << "\nThe DISCRIMINANT is " << discriminant(a,b,c) << endl;

cout << "It has TWO REAL solution which are " << x1 << " , " << x2 << endl;

}

Page 12: C++ TUTORIAL 4

void ONEREAL(float a,float b,float c) // or we can call it as two equal solution

{

float x1;

x1 = -b/(2*a);

cout << "\nThe DISCRIMINANT is " << discriminant(a,b,c) << endl;

cout << "It has ONE REAL solution which is " << x1 << endl;

}

void COMPLEX(float a,float b,float c)

{

float x1,x2;

x1 = -b/(2*a);

x2 = sqrtf(-discriminant(a,b,c))/(2*a);

cout << "\nThe DISCRIMINANT is " << discriminant(a,b,c) << endl;

cout << "It has TWO COMPLEX solutions which are ";

cout << x1 << "+" << x2 << "i , "<< x1 << -x2 << "i" << endl;

}

// Output for Question 3:

Output1 :

This program is used to solve Quadratic Equation

in the form of ax^2 + bx +c

Enter value a:1

Enter value b:6

Page 13: C++ TUTORIAL 4

Enter value c:8

The DISCRIMINANT is 4

It has TWO REAL solution which are -2 , -4

Output2 :

This program is used to solve Quadratic Equation

in the form of ax^2 + bx +c

Enter value a:2

Enter value b:3

Enter value c:4

The DISCRIMINANT is -23

It has TWO COMPLEX solutions which are -0.75+1.19896i , -0.75-1.19896i

Output 3:

This program is used to solve Quadratic Equation

in the form of ax^2 + bx +c

Enter value a:1

Enter value b:8

Enter value c:16

The DISCRIMINANT is 0

It has ONE REAL solution which is -4

QUESTION 4

Write a function program to find the mean (average) of N numbers.

/*The programming codes below are to calculate summation of(1+2+3…N)*/

//Using for loop:

#include <iostream>

using namespace std;

float average(int N)

Page 14: C++ TUTORIAL 4

{

int a,b,sum=0;

float result;

for ( a=1;a<=N;a++)

{

cout << “ Value “ << a << “ is: “ ;

cin >> b;

sum=sum+b;

result=float (sum)/N;

}

return (result);

}

int main ()

{

int N;

cout << "Please insert a value,N : ";

cin >> N;

cout << "The average of N values is " << average(N) << endl;

return 0;

}

//Output for for loop :

Please insert a value,N : 5

Value 1 is:10

Value 2 is:3

Value 3 is:7

Value 4 is:2

Value 5 is:114

The average of N values is 27.2

//Using while loop

#include <iostream>

using namespace std;

float average(int N)

Page 15: C++ TUTORIAL 4

{

int a=1,b, sum=0;

float result;

while(a<=N)

{

cout << "Value " << a << " is: " ;

cin >> b;

sum= sum + b ;

a++;

result=float (sum)/N;

}

return (result);

}

int main ()

{

int N;

cout << "Please insert a value N : ";

cin >> N;

cout << "The average of N values is " << average(N) << endl;

return 0;

}

//Output for while loop:

Please insert a value,N : 5

Value 1 is:10

Value 2 is:3

Value 3 is:7

Value 4 is:2

Value 5 is:114

The average of N values is 27.2

Page 16: C++ TUTORIAL 4

//Using do while loop

#include <iostream>

using namespace std;

float average(int N)

{

int a=1, b, sum=0;

float result;

do

{

cout << "Value " << a << " is: " ;

cin >> b;

sum= sum + b ;

a++;

result=float (sum)/N;

} while(a<=N) ;

return (result);

}

int main ()

{

int N;

cout << "Please insert a value N : ";

cin >> N;

cout << "The average of N values is " << average(N) << endl;

return 0;

}

Page 17: C++ TUTORIAL 4

//Output for do while loop

Please insert a value,N : 5

Value 1 is:10

Value 2 is:3

Value 3 is:7

Value 4 is:2

Value 5 is:114

The average of N values is 27.2

QUESTION 5

Using the function program, calculate the formula C(n,k )=

( )

#include <iostream>

using namespace std;

/*** function prototype or declaration***/

int fact(int n);

void COMB(int n,int k);

int main ()

{

int n,k;

cout << "This program is used to calculate C(n,k)." << endl;

do

{

cout << "Value of n should be larger than k, n>k" << endl;

cout << "Enter positive integer n:";

cin >> n;

cout << "Enter positive integer k:";

cin >> k;

}

Page 18: C++ TUTORIAL 4

while (n < 0 || k<0 || n<k);

COMB(n,k);

return 0;

}

int factorial(int n)

{

int fact = 1;

if (n == 0)

return 1;

else

{

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

fact = fact * i;

return fact;

}

}

void COMB(int n,int k)

{

int comb = factorial(n)/(factorial(k)*factorial(n-k));

cout << "C(" << n << "," << k << ")= " << comb << endl;

}

//Output for Question 5:

This program is used to calculate C(n,k).

Value of n should be larger than k, n>k

Enter positive integer n:5

Page 19: C++ TUTORIAL 4

Enter positive integer k:7

Value of n should be larger than k, n>k

Enter positive integer n:-4

Enter positive integer k:-7

Value of n should be larger than k, n>k

Enter positive integer n:3

Enter positive integer k:2

C(3,2)= 3


Recommended