#include<iostream> int main() { std::cout << "sin(1) = " << std::sin(1.0) << std::endl; std::cout << "cos(1) = " << std::cos(1.0) << std::endl; std::cout << "tan(1) = " << std::tan(1.0) << std::endl; std::cout << "asin(1) = " << std::asin(1.0) << std::endl; std::cout << "acos(1) = " << std::acos(1.0) << std::endl; std::cout << "atan(1) = " << std::atan(1.0) << std::endl; } Output: sin(1) = 0.841471 cos(1) = 0.540302 tan(1) = 1.55741 asin(1) = 1.5708 acos(1) = 0 atan(1) = 0.785398
#include<iostream> #include<string> int main() { std::string input, invert; bool ok = false; while (!ok) { std::cout << "Enter a 5-digit binary number:"; std::getline (std::cin, input); if (input.size()==5) { invert = input; ok = true; for (auto c = invert.begin(); c!=invert.end(); ++c) { switch (*c) { case ('1'): *c = '0'; break; case ('0'): *c = '1'; break; default: ok = false; } } } if (!ok) { std::cout << "Bad input\n"; invert.clear(); input.clear(); } } std::cout << "Input:\t" << input << std::endl; std::cout << "Invert:\t" << invert << std::endl; }
#include<iostream> #include<vector> #include<list> #include<map> #include<time.h> int main() { srand ((unsigned)time(nullptr)); // Create a list of 100 values in the range 0 to 9. std::cout << "Number list:\t"; std::list<size_t> numbers; for (size_t i=0; i<100; ++i) { numbers.push_back (rand()%10); std::cout << numbers.back() << ' '; } std::cout << '\n' << std::endl; // Determine position(s) of each number. std::map<size_t, std::vector<size_t>> map; size_t pos = 0; for (auto it=numbers.begin(); it!=numbers.end(); ++it) map[*it].push_back(pos++); // Print number, frequency and position(s). for (auto it=map.begin(); it!=map.end(); ++it) { std::cout << "Number:\t\t" << (*it).first << std::endl; std::cout << "Frequency:\t" << (*it).second.size() << std::endl; std::cout << "Postions:\t"; const std::vector<size_t>& positions = (*it).second; for (auto it=positions.begin(); it!=positions.end(); ++it) std::cout << (*it) << ' '; std::cout << '\n' << std::endl; } }
#include<iostream> #include<iomanip> #include<vector> #include<algorithm> #include<random> #include<time.h> void initialise (std::vector<unsigned>& data) { // Pseudo-random number generator (range: 1 to 99). std::default_random_engine generator; generator.seed ((unsigned) time (NULL)); std::uniform_int_distribution<unsigned> distribution (1, 99); data.clear(); unsigned max_elements(50); while (max_elements--) data.push_back (distribution (generator)); } int linear_search(std::vector<unsigned>& data, unsigned value, unsigned& comparisons) { int index(-1); for( comparisons=0; comparisons<data.size() && index<0; ++comparisons) if (data[comparisons] -1) { std::cout << "not found. "; } else { std::cout << "found at index " << binary_index << ". "; } std::cout << "Comparisons: " << binary_comparisons << std::endl; } }
#include#includeunsigned input_num(std::string prompt){unsigned num = 0;while (1){std::cout
The Number - 2011 STD Test 1-9 was released on: USA: 24 June 2012
#include<iostream> int main() { std::cout << "sin(1) = " << std::sin(1.0) << std::endl; std::cout << "cos(1) = " << std::cos(1.0) << std::endl; std::cout << "tan(1) = " << std::tan(1.0) << std::endl; std::cout << "asin(1) = " << std::asin(1.0) << std::endl; std::cout << "acos(1) = " << std::acos(1.0) << std::endl; std::cout << "atan(1) = " << std::atan(1.0) << std::endl; } Output: sin(1) = 0.841471 cos(1) = 0.540302 tan(1) = 1.55741 asin(1) = 1.5708 acos(1) = 0 atan(1) = 0.785398
#include<iostream> #include<sstream> int input_num (std::string prompt) { int num = 0; while (1) { std::cout<<prompt<<": "; std::string input=""; getline (std::cin, input); std::stringstream ss (input); if (ss>>num) break; std::cout<<"Invalid input.\n"; } return (num); } int main() { while (1) { int num = input_num ("Enter a number (0 to exit)"); if (!num) break; if (num%7) std::cout << "The number is not divisible by 7.\n" << std::endl; else std::cout << "The number is divisible by 7.\n" << std::endl; } }
Example Code#include#includeunsigned input_num (){unsigned num = 0;while (1){std::cout > num)break;std::cout
Chlamydia is the most common bacterial STD in the US, and the most common reportable STD. You should know that not all STDs are reportable, so the CDC doesn't get direct reports about HPV or trichomonas.
#include<iostream> int main (void) { std::cout << "1 22 333 444\n"; } To produce the text programmatically: #include<iostream> int main (void) { for (int num=1; num<=4; ++num) { // the number to be printed for (int rep=1; rep<=num; ++rep) { // the repetitions of that number std::cout << num; } std::cout << ' '; // space } std::cout << '\n'; // new line }
Trichomoniasis is the most common protozoan STD in the US. Aside from giardia, which is rarely sexually transmitted, it's the only protozoan STD.
#include<iostream> #include<iomanip> #include<map> #include<random> #include<time.h> std::default_random_engine generator; std::uniform_int_distribution<unsigned> distribution (1, 10); typedef std::map<unsigned, unsigned> freq_map; int main() { std::cout << "Program to generate 1,000,000 random numbers in the range 1 to 10\n"; std::cout << "and to print the frequency table for all numbers generated.\n\n"; generator.seed ((unsigned) time (NULL)); freq_map fmap; freq_map::iterator iter; for (unsigned count=0; count<1000000; ++count) { unsigned number = distribution (generator); iter = fmap.find (number); if (iter == fmap.end()) { fmap.insert (freq_map::value_type (number, 1)); } else { ++(iter->second); } } for (iter = fmap.begin(); iter != fmap.end(); ++iter) { std::cout << "The number" << std::setw(3) << iter->first; std::cout << " occurred" << std::setw(7) << iter->second << " times\n"; } std::cout << std::endl; }
The following example code demonstrates unary increment operator overloads. The code makes use of two classes, natural and prime, where prime inherits from natural, but where both specialise their increment operators. Note that natural numbers can never be zero, therefore if an increment causes an overflow, the number is set to 1. Prime numbers are also natural numbers but they must be greater than 1. The main function tests both the prefix and postfix increment operators for both classes. #include<iostream> class natural { private: unsigned m_data; public: natural(unsigned data=1): m_data(data?data:1) {} // prefix increment operator natural& operator++() { ++m_data; if(!m_data) m_data=1; return(*this); } // postfix increment operator natural operator++(int) { natural temp(*this); ++m_data; if(!m_data) m_data=1; return(temp); } // conversion operator operator unsigned(){ return(m_data); } }; std::ostream& operator<<(std::ostream& os, natural& num) { os<<(unsigned)num; return(os); } class prime: public natural { private: bool is_prime() { unsigned num = (unsigned)*this; if(num==1) return(false); if(num==2) return(true); if(num%2==0) return(false); unsigned max_factor = (unsigned)sqrt((double) num); for(unsigned factor=3; factor<max_factor; factor+=2) if(num%factor==0) return(false); return(true); } public: prime(): natural(2) {} prime& operator++() { do { natural::operator++(); } while(!is_prime()); return(*this); } prime operator++(int) { prime temp(*this); do { natural::operator++(); } while(!is_prime()); return(temp); } }; int main() { natural n; prime p; std::cout<<"The 1st natural number is "<<n<<std::endl; std::cout<<"The 2nd natural number is "<<++n<<std::endl; std::cout<<"The 3rd natural number is "<<++n<<std::endl; std::cout<<"The 4th natural number is "<<++n<<std::endl; std::cout<<"The 4th natural number was "<<n++<<" while the 5th is "<<n<<std::endl; std::cout<<"The 1st prime number is "<<p<<std::endl; std::cout<<"The 2nd prime number is "<<++p<<std::endl; std::cout<<"The 3rd prime number is "<<++p<<std::endl; std::cout<<"The 4th prime number is "<<++p<<std::endl; std::cout<<"The 4th prime number was "<<p++<<" while the 5th is "<<p<<std::endl; }
#include<iostream> #include<fstream> int main() { std::ifstream infile ("example.txt", std::ios::in); unsigned chars(0); unsigned words(0); unsigned spaces(0); std::string delim("\t\n "); char ch(0); char last(0); if (!infile.good()) { std::cerr << "The filename is invalid." << std::endl; return -1; } while (infile.get(ch)) { switch (ch) { case (' '): case ('\t'): ++spaces; case ('\n'): // only count words if the last char was not a word delimiter if (delim.find(last) == std::string::npos) ++words; default: ++chars; } last = ch; } infile.close(); std::cout << "Number of chars:\t" << chars << std::endl; std::cout << "Number of words:\t" << words << std::endl; std::cout << "Number of spaces:\t" << spaces << std::endl; }
#include<iostream> int main() { int num1, num2; std::cout << "C++ addition program" << std::endl; std::cout << "Enter a number: "; std::cin >> num1; std::cout << "Enter another number: "; std::cin >> num2; std::cout << "The sum is " << num1 + num2 << std::endl; }
#include<iostream> #include<string> int main() { std::string input, invert; bool ok = false; while (!ok) { std::cout << "Enter a 5-digit binary number:"; std::getline (std::cin, input); if (input.size()==5) { invert = input; ok = true; for (auto c = invert.begin(); c!=invert.end(); ++c) { switch (*c) { case ('1'): *c = '0'; break; case ('0'): *c = '1'; break; default: ok = false; } } } if (!ok) { std::cout << "Bad input\n"; invert.clear(); input.clear(); } } std::cout << "Input:\t" << input << std::endl; std::cout << "Invert:\t" << invert << std::endl; }