Docsity

Applications with Repetition Statements in C++, Study Guides, Projects, Research of C programming

Toggle sidebar
bg1
EE-163(C&P) lesson_06 EED FR, IA
Page 1 of 13
Applications with Repetition Statements
This lesson is aimed to introduce the ideas of algorithm implementation. Algorithm implementation is an
important job of engineer. We have learnt sufficient basic tools to implement programming solution for
some real world problems. Some tools will be taught afterwards and they will further strengthen your
ability to implement algorithms.
Algorithm
According to the author of our reference book [Dietel & Dietel], algorithm is defined as
ā€œAny solvable computing problem can be solved by executing of a series of actions in a specific order. A
procedure for solving a problem in terms of
1. The actions to execute
2. The order in which the actions execute
is called an algorithm.ā€
Objective of this lesson is to implement some simple algorithms using repetition statements.
les_06_code_01.cpp
Fibonacci Sequence with for loop
1. #include<iostream>
2. using namespace std;
3. int main (void)
4. {
5. // variable definition
6. int counter, n_terms;
7. cout<<"How many terms to generate : ";
8. cin>>n_terms;
9. // initializing newterm and prevterm to 1
10. //sum cantains the next number in the sequence
11. int newterm=1,prevterm=1,sum;
12. cout<<"\t\t Fibonacci Series"<<endl;
13. // display the first term
14. cout<<prevterm<<" ";
15. // for rest of the terms
16. for(counter=1;counter<n_terms;counter++)
17. {
18. if(counter%10==0)cout<<endl;
19. cout<<newterm<<" ";
20. sum=prevterm+newterm;
21. prevterm=newterm;
22. newterm=sum;
23. }
24. return 0;
25. }
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Applications with Repetition Statements in C++ and more Study Guides, Projects, Research C programming in PDF only on Docsity!

Applications with Repetition Statements

This lesson is aimed to introduce the ideas of algorithm implementation. Algorithm implementation is an important job of engineer. We have learnt sufficient basic tools to implement programming solution for some real world problems. Some tools will be taught afterwards and they will further strengthen your ability to implement algorithms.

Algorithm

According to the author of our reference book [Dietel & Dietel], algorithm is defined as ā€œ Any solvable computing problem can be solved by executing of a series of actions in a specific order. A procedure for solving a problem in terms of

_1. The actions to execute

  1. The order in which the actions execute is called an algorithm.ā€_ Objective of this lesson is to implement some simple algorithms using repetition statements. les_06_code_01.cpp Fibonacci Sequence with for loop
  2. #include
  3. using namespace std;
  4. int main (void)
  5. {
  6. // variable definition
  7. int counter, n_terms;
  8. cout<<"How many terms to generate : ";
  9. cin>>n_terms;
  10. // initializing newterm and prevterm to 1
  11. //sum cantains the next number in the sequence
  12. int newterm=1,prevterm=1,sum;
  13. cout<<"\t\t Fibonacci Series"<<endl;
  14. // display the first term
  15. cout<<prevterm<<" ";
  16. // for rest of the terms
  17. for(counter=1;counter<n_terms;counter++)
  18. {
  19. if(counter%10==0)cout<<endl;
  20. cout<<newterm<<" ";
  21. sum=prevterm+newterm;
  22. prevterm=newterm;
  23. newterm=sum;
  24. }
  25. return 0;
  26. }

Sample Output les_06_code_02.cpp Generating Fibonacci Series and computing Golden ratio by dividing successive terms

  1. #include
  2. using namespace std;
  3. int main (void)
  4. {
  5. // variable definition
  6. int counter, loopruns;
  7. // initializing newterm and prevterm to 1
  8. //sum cantains the next number in the sequence
  9. int newterm=1,prevterm=1,sum;
  10. cout<<"\t\t Fibonacci Series with Golden Ratio";
  11. // Input
  12. cout<<"\nHow many Fibonacci numbers do you want to see:";
  13. cin>>loopruns;
  14. // display the first term
  15. cout<<"\n The fibonacci numbers you ordered are:\nnumber:"<<prevterm;
  16. // for rest of the terms
  17. for(counter=1;counter<loopruns;counter++)
  18. {
  19. cout<<"\nnumber: "<<newterm;
  20. cout<<",Ratio of "<<newterm<<"/"<<prevterm<<"="<<((float)newterm/(float)prevterm);
  21. sum=prevterm+newterm;
  22. prevterm=newterm;
  23. newterm=sum;
  24. }
  25. return 0;
  26. }
  1. cin>>num;
  2. running_sum=running_sum+num;
  3. }
  4. // displaying why the program has stopped
  5. cout<<"\nSorry. Your running sum exceeded 1000.";
  6. return 0;
  7. } Sample Output les_06_code_04.cpp Digit counting (UIY Program)
  8. #include
  9. using namespace std;
  10. int main(void)
  11. {
  12. // variable definition
  13. int num,num_alias,digitcnt=0;
  14. // Taking input
  1. cout<<"\nEnter an integer to find "<<
  2. "the number of digits in it:";
  3. cin>>num;
  4. num_alias=num; // saving num so that it can be used at the end
  5. // while loop to calculate the running sum of input
  6. while(num_alias>0) // loop terminates when num_alias becomes zero
  7. {
  8. num_alias=num_alias/10;
  9. digitcnt=digitcnt+1;
  10. }
  11. // displaying result
  12. cout<<"\nNo of digits in "<<num<<
  13. " = "<<digitcnt;
  14. return 0;
  15. }
  16. // NOTE: The program is not valid for - ve numbers but can be made to work if
  17. // we make num_alias +ve, if it is - ve. les_06_code_05.cpp Character(s), word(s) and sentence(s) calculation
  18. #include
  19. #include<conio.h>
  20. using namespace std;
  21. int main(void)
  22. {
  23. // variable definition
  24. // to count characters, words and sentences
  25. int ch_cnt=0,word_cnt=0,sentence_cnt=0;
  26. // Taking one character at a time
  27. // as input in chinput
  28. char chinput;
  29. cout<<"\nEnter any text,"<<
  30. " press ESC to stop:\n";
  31. chinput=getche(); // taking input in
  32. //loop control variable
  33. //(chinput) before
  34. // entering the loop
  35. while(chinput!=27)// loop terminates when
  36. { // chinput is ASCII 27,
  37. // which is for ESC key
  38. ch_cnt=ch_cnt+1;

les_06_code_06.cpp Finding GCD , the Naive method

  1. #include
  2. using namespace std;
  3. int main()
  4. {
  5. int first_number;
  6. cout<<"Enter First Number : ";cin>>first_number;
  7. int second_number;
  8. cout<<"Enter Second Number: ";cin>>second_number;
  9. int gcd;
  10. for(int i=1;i<=first_number&&i<=second_number;i++)
  11. {
  12. if(first_number%i==0 && second_number%i == 0 )
  13. {
  14. gcd=i;
  15. }
  16. }
  17. cout<<"Greatest Common Divison (GCD) : "<<gcd<<endl;
  18. return 0;
  19. } Sample Output les_06_code_07.cpp Euclid Subtraction GCD Algorithm
  20. #include
  21. using namespace std;
  22. int main()
  23. {
  24. int num1, num2;
  25. cout << "Enter num1 : ";
  26. cin >> num1;
  1. cout << "Enter num2 : ";
  2. cin >> num2;
  3. while (num1 != num2 )
  4. {
  5. if (num1 > num2)
  6. {
  7. num1 = num1 - num2;
  8. }
  9. else
  10. {
  11. num2 = num2 - num1;
  12. }
  13. }
  14. cout << "GCD is " << num1;
  15. return 0;
  16. } Sample Output les_06_code_08.cpp Faster Euclid GCD
  17. #include
  18. using namespace std;
  19. int main()
  20. {
  21. int a, b;
  22. cin >> a >> b;
  23. while (b != 0)
  24. {
  25. int r = a%b;
  26. a = b;
  27. b = r;
  28. }
  29. cout <<"GCD : "<< a << endl;
  30. return 0;
  31. }
  1. int main (void)
  2. {
  3. int num,divisor,remainder;
  4. cout<<"Enter a number:";
  5. cin>>num;
  6. // Some conditions to increase program speed
  7. if((num>2) && (num%2==0))
  8. {
  9. cout<<"\n The no. is composite";
  10. return 0;
  11. }
  12. if((num>3)&&(num%3==0))
  13. {
  14. cout<<"\n The no. is composite";
  15. return 0;
  16. }
  17. // some defensive conditions
  18. if(num==2)
  19. {
  20. cout<<"\nThe number is prime";
  21. return 0;
  22. }
  23. //int count = 0;
  24. // divisor is required to increment only till
  25. //sqrt(num) and not num, prove it yourself.
  26. for(divisor=2;divisor<=(int)sqrt((double)num);divisor++)
  27. {
  28. remainder=num%divisor;
  29. if(remainder==0)
  30. {
  31. cout<<"\n Composite";
  32. break;
  33. }
  34. }
  35. if(remainder!=0)
  36. {
  37. cout<<"\n Prime";
  38. }
  39. return 0;
  40. }

les_06_code_12.cpp Square Root using Newton Raphson Method

  1. #include
  2. #include<conio2.h>
  3. #include
  4. using namespace std;
  5. int main(void)
  6. {
  7. float N,low,high,root;
  8. cout<<"Enter a +ve number to calculate its square root:";
  9. cin>>N;
  10. if(N<0)
  11. {
  12. cout<<"\n Sorry, wrong input, Re-run the program";
  13. }
  14. else
  15. {
  16. root=N/2.0;
  17. while(fabs(((root*root)-N))>0.0001)
  18. {
  19. root=root-((rootroot-N)/(2.0root));
  20. }
  21. }
  22. cout<<"\n square root of "<<N<<" = "<<root;
  23. return 0;
  24. } les_06_code_12.cpp Square Root using Bisection Method
  25. #include
  26. #include<conio2.h>
  27. #include
  28. using namespace std;
  29. int main(void)
  30. {
  31. float N,low,high,root;
  32. cout<<"Enter a +ve number to calculate its square root:";
  33. cin>>N;
  34. if(N<0)
  35. {
  36. cout<<"\nSorry, wrong input, Re-run the program";
  37. }
  38. else
  39. {
  40. low=0.0;
  41. if(N>1.0)// if number is greater than 1 (eg. 1.7)
  1. sum=sum*delta_x;
  2. cout<<"integral= "<<sum;
  3. return 0;
  4. }