#include <iostream> using namespace std; // Student ID: 20511146 // // Looping_Functions.cpp // // Created by Anand Venkataraman on 8/9/19. // Copyright © 2019 Anand Venkataraman. All rights reserved. // #include <iostream> #include <sstream> using namespace std; // Give the user 6 chances to guess the secret number n (0-10). If they get it, // say so and return true. Else say so and return false. bool play_game(int n){ printf("Welcome to my number guessing game\n\n"); int x; for (int i = 1; i <= 6; i++){ printf("Enter your guess: "); string s;getline(cin, s); istringstream iss(s); iss >> x;printf("\n"); printf("You entered: %d\n", x); if (x==n){ printf("You found it in %d guess(es).\n", i); return true; } } printf("\nI'm sorry. You didn't find my number.\n"); printf("It was %d\n\n", n); return false; } double etox(double x, size_t n){ double ans = 0; double X = 1.0; double fac = 1.0; for (size_t i = 0; i < n; i++){ if (i!=0) fac *= i;ans += X/fac;X *= x; } return ans; } // Return the number of occurrences of char c in string s size_t count_chars(string s, char c) { size_t occur = 0; for(size_t i = 0;i<s.length();i++){ if(s[i] == c){ occur += 1; } } return occur; } // Use Euclid's algorithm to calculate the GCD of the given numbers size_t gcd(size_t a, size_t b) { if (a == 0) return b; if (b == 0) return a; if (a == b) return a; if (a > b) return gcd(a-b, b); return gcd(a, b-a); } // Return a string of the form n1,n2,n3,... for the given AP. string get_ap_terms(int a, int d, size_t n) { string ap = ""; int currentTerm; for (size_t i=0; i<n; i++) { currentTerm = a+(i)*d; ap = ap + to_string(currentTerm) + ","; } ap = ap.substr(0,ap.size()-1); return ap; } // Return a string of the form n1,n2,n3,... for the given GP. string get_gp_terms(double a, double r, size_t n) { string gp=""; ostringstream ss2; for(size_t i=1;i<=n-1;i++){ ostringstream ss1; ss1<<a; gp.append(ss1.str()); gp.append(","); ss1.clear(); a=a*r; } ss2<<a; gp.append(ss2.str()) ; return gp; } double get_nth_fibonacci_number(size_t n) { size_t first=0; size_t second=1; size_t count = 0; if(n==0)return first; else if(n==1) return second; while(count<n){ int t = second; second = second+first; first=t; count++; } return second; } int main() { cout << "Hello, World!"; // Declarations of the functions in looping_functions.cpp cout<< "game: "<< play_game(3)<< "\n"; cout << "etox: " << etox( 2, 4)<< "\n"; cout << "size: "<< count_chars("stars",'s')<< "\n"; cout << "GCD..."<< gcd(16 ,2)<< "\n"; cout << "ap_terms: "<< get_ap_terms(1, 3,6)<< "\n"; cout<<"ap_terms: "<< get_gp_terms(4 , 0.5, 6)<< "\n"; cout<< "fib.."<< get_nth_fibonacci_number(6); return 0; }
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}