//============================================================================ // Name : Looping.cpp // Author : Chandan R. Rupakheti // Version : 1.0 // Copyright : You are free to copy, modify and redistribute this program without attribution. // Description : Examples of Counter Controlled and Event Controlled Loops. // Code snippets are mostly borrowed from the EE261 textbook with slight modifications. //============================================================================ #include #include #include using namespace std; void findFactorial() { cout << "============================================================================" << endl; cout << "Factorial Computation" << endl; cout << "============================================================================" << endl; // Read a number int n; cout << "Input a number: "; cin >> n; int factorial = 1; int counter = 1; while (counter <= n){ factorial *= counter; counter++; } cout << "Factorial: " << factorial << endl; } void countSpace() { cout << "============================================================================" << endl; cout << "Space counter in a line" << endl; cout << "============================================================================" << endl; cout << "Please enter a line: "; int spaces = 0; char data; cin.get(data); while(data != '\n'){ if(data == ' ') ++spaces; cin.get(data); } cout << "Number of spaces in your input: " << spaces << endl; } void guessSquareRoot() { cout << "============================================================================" << endl; cout << "Square Root Guesser" << endl; cout << "============================================================================" << endl; cout << "Please enter a number: "; float square; cin >> square; float guess = square / 4.0; bool goodEnough = false; while (goodEnough){ guess = (square/guess + guess)/2.0; cout << guess << endl; goodEnough = fabs(square-guess*guess) < 0.001; } cout << "My square root guess is: " << guess; } void sumFiveOddNums() { cout << "============================================================================" << endl; cout << "Sum of odd numbers computation" << endl; cout << "============================================================================" << endl; int sum = 0; int number = 0; int count =0; bool lessThanFive = true; while (lessThanFive){ // Read the number cout << "Enter a number: "; cin >> number; // Check if the number is odd if (number % 2 == 1){ count++; sum = sum + number; lessThanFive = count < 5; } else { cout << "Discarding " << number << "! It is a even number!" << endl; } } cout << "Sum: " << sum << endl; } // We extended the digrams frequency analyzer problem to trigrams in class void trackingPreviousValue() { cout << "============================================================================" << endl; cout << "Trigrams frequency analyzer." << endl; cout << "============================================================================" << endl; ifstream inFile; inFile.open("data.txt"); // Check if inFile is in error state or not if(!inFile) { cout << "ERROR: Unable to open file!" << endl; return; } // Digram characters to be checked char firstChar; char secondChar; char thirdChar; cout << "Please enter an english trigram: "; cin.get(firstChar); cin.get(secondChar); cin.get(thirdChar); char prevChar; char currChar; char nextChar; inFile.get(prevChar); inFile.get(currChar); inFile.get(nextChar); int count = 0; while (inFile){ if(prevChar == firstChar && currChar == secondChar && nextChar == thirdChar) count++; prevChar = currChar; currChar = nextChar; inFile.get(nextChar); } cout << "Frequency of trigram [" << firstChar << secondChar << thirdChar << "] is: " << count; } void nestedLoops() { cout << "============================================================================" << endl; cout << "Nested Loop Example" << endl; cout << "============================================================================" << endl; cout << "Enter number of stars: "; int starCount; cin >> starCount; cout << "Press 'Q' to end program!" << endl; while (cin){ cout << endl; int loopCount = 1; while (loopCount <= starCount) { cout << '*'; a = loopCount++; cout << loopCount; } cout << endl << endl; cout << "Press 'Q' to end program!" << endl; cout << "Enter number of stars: "; cin >> starCount; } cout << "Goodbye!" << endl; } int main() { findFactorial(); // countSpace(); // guessSquareRoot(); // sumFiveOddNums(); // trackingPreviousValue(); // Try checking freq(the) > freq(ing) > freq(and) // nestedLoops(); return 0; }