+ All Categories
Home > Documents > The following list represents the written codes in order · 7 #include using namespace std; int...

The following list represents the written codes in order · 7 #include using namespace std; int...

Date post: 16-Mar-2020
Category:
Upload: others
View: 10 times
Download: 0 times
Share this document with a friend
27
1 The following list represents the written codes in order:
Transcript
Page 1: The following list represents the written codes in order · 7 #include using namespace std; int sum(int, int); // function prototype void main() { //without using

1

The following list represents the written codes in order:

Page 2: The following list represents the written codes in order · 7 #include using namespace std; int sum(int, int); // function prototype void main() { //without using

2

#include<iostream> #include<cmath> using namespace std; void main() { double x; double y; char ch; while (true) { cout << "Enter a value of x: "; cin >> x; cout << "round(" << x << ") = " << round(x) << endl; cout << "ceil( " << x << ") = " << ceil(x) << endl; cout << "floor(" << x << ") = " << floor(x) << endl; cout << "fabs (" << x << ") = " << fabs(x) << endl; cout << "sqrt(" << x << ") = " << sqrt(x) << endl; //square root cout << "Enter a value of y: "; cin >> y; cout << "pow(" << x << "," << y << ") = " << pow(x,y) << endl; //power y = sqrt(pow(x, 3) + 2); // equation cout << y << endl; cout << "Exit (y/n):"; cin >> ch; ch = (char)tolower(ch); if (ch == 'y') break; } system("pause"); }

Page 3: The following list represents the written codes in order · 7 #include using namespace std; int sum(int, int); // function prototype void main() { //without using

3

#include<iostream> #include<algorithm> using namespace std; void main() { double x, y; cout << "Enter 2 numbers: "; cin >> x >> y; cout << "max(" << x << "," << y << ") = " << max(x, y) << endl; cout << "min(" << x << "," << y << ") = " << min(x, y) << endl; system("pause"); }

Page 4: The following list represents the written codes in order · 7 #include using namespace std; int sum(int, int); // function prototype void main() { //without using

4

#include<iostream> #include<iomanip> using namespace std; void main() { char letter = 'A'; char nCh = 97; cout << setw(15) << "letter = " << letter << endl; cout << setw(15) << "next letter = " << ++letter << endl; //Every character has a unique ASCII code between 0 & 127 cout << "ASCII of " << letter << " is " << (int)letter << endl; cout << "ASCII of " << letter << " is " << static_cast<int>(letter) << endl; cout << "nCh " << nCh << endl; int i = '2' + '3' + 'A' + 4; cout << "\'2\' + \'3\' + \'A\' + 4 = " << i << endl; cout << "Enter a Character : "; cin >> letter; if (isdigit(letter)) cout << "It is a digit." << endl; if (isspace(letter)) cout << "It is a space." << endl; if (isalpha(letter)) cout << "It is an alpha." << endl; if (islower (letter)) cout << "It is a lowercase letter, the uppercase is " << (char)toupper(letter) << endl; if (isupper(letter)) cout << "It is an uppercase letter, the lowercase is " << static_cast<char>(tolower(letter)) << endl; system("pause"); }

Page 5: The following list represents the written codes in order · 7 #include using namespace std; int sum(int, int); // function prototype void main() { //without using

5

#include<iostream> #include<string> using namespace std; void main() { string s1 = "Introdution "; string s2 = "to Programming in C++"; cout << "S1: " << s1 << endl; cout << "Length of S1 is " << s1.length() << endl << endl; cout << "S2: " << s2 << endl; cout << "Length of S2 is " << s2.length() << endl << endl; string s3 = s1 + s2; int L = s3.length(); cout << "S3 (Concatenating s1 and s2) : " << s3 << endl; cout << "Length of S3 is " << L << endl << endl; string str = "ABCDE"; str[0] = 'M'; cout << "str = " << str << endl; char c = str[3]; cout << "str[3] " << c << endl; c = str.at(4); cout << "str.at(4) " << c << endl; cout << endl; string input; cout << "Enter a string: "; cin >> input >> skipws; cout << "The entered string using cin is " << input << endl; cin.ignore(INT_MAX, '\n'); cout << "Enter a new string: "; getline(cin, input); cout << "The entered string using getline is " << input << endl; string name = "Mohamed Bassam"; for (int i = 0; i < name.length(); i++) name[i] = toupper(name[i]); cout << "To Uppercase string: " << name << endl; if (input == name) cout << "This is my name." << endl; else cout << "This is not my name." << endl; if (input.compare(name)) cout << "This is my name." << endl; else cout << "This is not my name." << endl; system("pause"); }

Page 6: The following list represents the written codes in order · 7 #include using namespace std; int sum(int, int); // function prototype void main() { //without using

6

#include<iostream> #include<cstdlib> #include<ctime> using namespace std; void main() { int i = rand(); cout << "Random number: " << i << endl; cout << "\nGenerate a random number between 0 and 5 " << rand() % 6; cout << "\nGenerate a random number between 10 and 50 " << 10 + rand() % 41; while (true) { i = rand(); cout << "Random number: " << i << endl; cout << "Generate a random number between 0 and 5 " << rand() % 6 << endl; cout << "Generate a random number between 10 and 50 " << 10 + rand() % 41 << endl; _sleep(1000); if (rand() % 16 == 0) break; } while (true) { int seed; cout << "Enter seed: "; cin >> seed; srand(seed); i = rand(); cout << "Random number after srand(seed): " << i << endl; if (seed == 0) break; } while (true) { srand(time(0)); i = rand(); cout << "Random number srand(time(0)): " << i << endl; _sleep(2000); } cout << endl; system("pause"); }

Page 7: The following list represents the written codes in order · 7 #include using namespace std; int sum(int, int); // function prototype void main() { //without using

7

#include<iostream> using namespace std; int sum(int, int); // function prototype void main() { //without using function! cout << "without using function!\n"; int s = 0; cout << "The sum of integers from 1 to 10 is "; for (int i = 1; i <= 10; i++) s += i; cout << s << endl; s = 0; cout << "The sum of integers from 10 to 100 is "; for (int i = 10; i <= 100; i++) s += i; cout << s << endl; s = 0; cout << "The sum of integers from 33 to 99 is "; for (int i = 33; i <= 99; i++) s += i; cout << s << endl; //define and invoke functions cout << "Using Functions\n"; cout << "The sum of integers from 1 to 10 is " << sum(1, 10) << endl; cout << "The sum of integers from 10 to 100 is " << sum(10, 100) << endl; cout << "The sum of integers from 33 to 99 is " << sum(33, 99) << endl; cout << endl; system("pause"); } int sum(int x, int y) { int result = 0; for (int i = x; i <= y; i++) result += i; return result; }

Page 8: The following list represents the written codes in order · 7 #include using namespace std; int sum(int, int); // function prototype void main() { //without using

8

#include <iostream> using namespace std; // Return the max between two numbers int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; return result; } int main() { int i = 5; int j = 2; int k = max(i, j); cout << "The maximum between " << i << " and " << j << " is " << k << endl; system("pause"); return 0; }

Page 9: The following list represents the written codes in order · 7 #include using namespace std; int sum(int, int); // function prototype void main() { //without using

9

#include <iostream> using namespace std; // Print grade for the score void printGrade(double score) { if (score >= 90.0) cout << 'A' << endl; else if (score >= 80.0) cout << 'B' << endl; else if (score >= 70.0) cout << 'C' << endl; else if (score >= 60.0) cout << 'D' << endl; else cout << 'F' << endl; } int main() { cout << "Enter a score: "; double score; cin >> score; cout << "The grade is "; printGrade(score); system("pause"); return 0; }

Page 10: The following list represents the written codes in order · 7 #include using namespace std; int sum(int, int); // function prototype void main() { //without using

10

#include <iostream> using namespace std; // Return the grade for the score char getGrade(double score) { if (score >= 90.0) return 'A'; else if (score >= 80.0) return 'B'; else if (score >= 70.0) return 'C'; else if (score >= 60.0) return 'D'; else return 'F'; } int main() { cout << "Enter a score: "; double score; cin >> score; cout << "The grade is "; cout << getGrade(score) << endl; system("pause"); return 0; }

Page 11: The following list represents the written codes in order · 7 #include using namespace std; int sum(int, int); // function prototype void main() { //without using

11

#include <iostream> #include <iomanip> using namespace std; // Check whether number is prime bool isPrime(int number) { for (int divisor = 2; divisor <= number/2 ; divisor++) { if (number % divisor == 0) { // If true, number is not prime return false; // number is not a prime } } return true; // number is prime } void printPrimeNumbers(int numberOfPrimes) { const int NUMBER_OF_PRIMES_PER_LINE = 10; // Display 10 per line int count = 0; // Count the number of prime numbers int number = 2; // A number to be tested for primeness // Repeatedly find prime numbers while (count < numberOfPrimes) { // Print the prime number and increase the count if (isPrime(number)) { count++; // Increase the count if (count % NUMBER_OF_PRIMES_PER_LINE == 0) { // Print the number and advance to the new line cout << setw(4) << number << endl; } else cout << setw(4) << number; } // Check if the next number is prime number++; } } int main() { cout << "The first 50 prime numbers are \n"; printPrimeNumbers(50); system("pause"); return 0; }

Page 12: The following list represents the written codes in order · 7 #include using namespace std; int sum(int, int); // function prototype void main() { //without using

12

#include <iostream> using namespace std; // Return the max between two int values int max(int num1, int num2) { if (num1 > num2) return num1; else return num2; } // Find the max between two double values double max(double num1, double num2) { if (num1 > num2) return num1; else return num2; } // Return the max among three double values double max(double num1, double num2, double num3) { return max(max(num1, num2), num3); } int main() { // Invoke the max function with int parameters cout << "The maximum between 3 and 4 is " << max(3, 4) << endl; // Invoke the max function with the double parameters cout << "The maximum between 3.0 and 5.4 is " << max(3.0, 5.4) << endl; // Invoke the max function with three double parameters cout << "The maximum between 3.0, 5.4, and 10.14 is " << max(3.0, 5.4, 10.14) << endl; system("pause"); return 0; }

Page 13: The following list represents the written codes in order · 7 #include using namespace std; int sum(int, int); // function prototype void main() { //without using

13

#include <iostream> using namespace std; // Function prototype int max(int num1, int num2); double max(double num1, double num2); double max(double num1, double num2, double num3); int main() { // Invoke the max function with int parameters cout << "The maximum between 3 and 4 is " << max(3, 4) << endl; // Invoke the max function with the double parameters cout << "The maximum between 3.0 and 5.4 is " << max(3.0, 5.4) << endl; // Invoke the max function with three double parameters cout << "The maximum between 3.0, 5.4, and 10.14 is " << max(3.0, 5.4, 10.14) << endl; system("pause"); return 0; } // Return the max between two int values int max(int num1, int num2) { if (num1 > num2) return num1; else return num2; } // Find the max between two double values double max(double num1, double num2) { if (num1 > num2) return num1; else return num2; } // Return the max among three double values double max(double num1, double num2, double num3) { return max(max(num1, num2), num3); }

Page 14: The following list represents the written codes in order · 7 #include using namespace std; int sum(int, int); // function prototype void main() { //without using

14

#include <iostream> using namespace std; void printArea(double = 1); int main() { printArea(); printArea(4); system("pause"); return 0; } // Display area of a circle void printArea(double radius) { double area = radius * radius * 3.14159; cout << "area is " << area << endl; }

Page 15: The following list represents the written codes in order · 7 #include using namespace std; int sum(int, int); // function prototype void main() { //without using

15

#include <iostream> using namespace std; // Display area of a circle void printArea(double radius = 1) { double area = radius * radius * 3.14159; cout << "area is " << area << endl; } int main() { printArea(); printArea(4); system("pause"); return 0; }

Page 16: The following list represents the written codes in order · 7 #include using namespace std; int sum(int, int); // function prototype void main() { //without using

16

#include <iostream> using namespace std; inline void f(int month, int year) { cout << "month is " << month << endl; cout << "year is " << year << endl; } int main() { int month = 10, year = 2008; f(month, year); // Invoke inline function f(9, 2010); // Invoke inline function system("pause"); return 0; }

Page 17: The following list represents the written codes in order · 7 #include using namespace std; int sum(int, int); // function prototype void main() { //without using

17

#include <iostream> using namespace std; void t1(); // Function prototype void t2(); // Function prototype int main() { t1(); t2(); system("pause"); return 0; } int y; // Global variable, default to 0 void t1() { int x = 1; cout << "x is " << x << endl; cout << "y is " << y << endl; x++; y++; } void t2() { int x = 1; cout << "x is " << x << endl; cout << "y is " << y << endl; }

Page 18: The following list represents the written codes in order · 7 #include using namespace std; int sum(int, int); // function prototype void main() { //without using

18

#include <iostream> using namespace std; int v1 = 10; int main() { int v1 = 5; cout << "local variable v1 is " << v1 << endl; cout << "global variable v1 is " << ::v1 << endl; system("pause"); return 0; }

Page 19: The following list represents the written codes in order · 7 #include using namespace std; int sum(int, int); // function prototype void main() { //without using

19

#include <iostream> using namespace std; void t1(); // Function prototype int main() { t1(); t1(); system("pause"); return 0; } void t1() { static int x = 1; int y = 1; x++; y++; cout << "x is " << x << endl; cout << "y is " << y << endl; }

Page 20: The following list represents the written codes in order · 7 #include using namespace std; int sum(int, int); // function prototype void main() { //without using

20

#include <iostream> using namespace std; void increment(int n) { n++; cout << "\tn inside the function is " << n << endl; } int main() { int x = 1; cout << "Before the call, x is " << x << endl; increment(x); cout << "after the call, x is " << x << endl; system("pause"); return 0; }

Page 21: The following list represents the written codes in order · 7 #include using namespace std; int sum(int, int); // function prototype void main() { //without using

21

#include <iostream> using namespace std; void increment(int& n) { n++; cout << "n inside the function is " << n << endl; } int main() { int x = 1; cout << "Before the call, x is " << x << endl; increment(x); cout << "After the call, x is " << x << endl; system("pause"); return 0; }

Page 22: The following list represents the written codes in order · 7 #include using namespace std; int sum(int, int); // function prototype void main() { //without using

22

#include <iostream> using namespace std; int main() { int count = 1; int& r = count; cout << "count is " << count << endl; cout << "r is " << r << endl; r++; cout << "count is " << count << endl; cout << "r is " << r << endl; count = 10; cout << "count is " << count << endl; cout << "r is " << r << endl; system("pause"); return 0; }

Page 23: The following list represents the written codes in order · 7 #include using namespace std; int sum(int, int); // function prototype void main() { //without using

23

#include <iostream> using namespace std; // Attempt to swap two variables does not work! void swap(int n1, int n2) { cout << "\tInside the swap function" << endl; cout << "\tBefore swapping n1 is " << n1 << " n2 is " << n2 << endl; // Swap n1 with n2 int temp = n1; n1 = n2; n2 = temp; cout << "\tAfter swapping n1 is " << n1 << " n2 is " << n2 << endl; } int main() { // Declare and initialize variables int num1 = 1; int num2 = 2; cout << "Before invoking the swap function, num1 is " << num1 << " and num2 is " << num2 << endl; // Invoke the swap function to attempt to swap two variables swap(num1, num2); cout << "After invoking the swap function, num1 is " << num1 << " and num2 is " << num2 << endl; system("pause"); return 0; }

Page 24: The following list represents the written codes in order · 7 #include using namespace std; int sum(int, int); // function prototype void main() { //without using

24

#include <iostream> using namespace std; // Swap two variables void swap(int& n1, int& n2) { cout << "\tInside the swap function" << endl; cout << "\tBefore swapping n1 is " << n1 << " n2 is " << n2 << endl; // Swap n1 with n2 int temp = n1; n1 = n2; n2 = temp; cout << "\tAfter swapping n1 is " << n1 << " n2 is " << n2 << endl; } int main() { // Declare and initialize variables int num1 = 1; int num2 = 2; cout << "Before invoking the swap function, num1 is " << num1 << " and num2 is " << num2 << endl; // Invoke the swap function to attempt to swap two variables swap(num1, num2); cout << "After invoking the swap function, num1 is " << num1 << " and num2 is " << num2 << endl; system("pause"); return 0; }

Page 25: The following list represents the written codes in order · 7 #include using namespace std; int sum(int, int); // function prototype void main() { //without using

25

#include <iostream> #include <iomanip> using namespace std; // Function prototypes void printMonth(int year, int month); void printMonthTitle(int year, int month); void printMonthName(int month); void printMonthBody(int year, int month); int getStartDay(int year, int month); int getTotalNumberOfDays(int year, int month); int getNumberOfDaysInMonth(int year, int month); bool isLeapYear(int year); int main() { // Prompt the user to enter year cout << "Enter full year (e.g., 2001): "; int year; cin >> year; // Prompt the user to enter month cout << "Enter month in number between 1 and 12: "; int month; cin >> month; // Print calendar for the month of the year printMonth(year, month); system("pause"); return 0; } // Print the calendar for a month in a year void printMonth(int year, int month) { // Print the headings of the calendar printMonthTitle(year, month); // Print the body of the calendar printMonthBody(year, month); } // Print the month title, e.g., May, 1999 void printMonthTitle(int year, int month) { printMonthName(month); cout << " " << year << endl; cout << "-----------------------------" << endl; cout << " Sun Mon Tue Wed Thu Fri Sat" << endl; } // Get the English name for the month void printMonthName(int month) { switch (month) { case 1:

Page 26: The following list represents the written codes in order · 7 #include using namespace std; int sum(int, int); // function prototype void main() { //without using

26

cout << "January"; break; case 2: cout << "February"; break; case 3: cout << "March"; break; case 4: cout << "April"; break; case 5: cout << "May"; break; case 6: cout << "June"; break; case 7: cout << "July"; break; case 8: cout << "August"; break; case 9: cout << "September"; break; case 10: cout << "October"; break; case 11: cout << "November"; break; case 12: cout << "December"; } } // Print month body void printMonthBody(int year, int month) { // Get start day of the week for the first date in the month int startDay = getStartDay(year, month); // Get number of days in the month int numberOfDaysInMonth = getNumberOfDaysInMonth(year, month); // Pad space before the first day of the month int i = 0; for (i = 0; i < startDay; i++) cout << " "; for (i = 1; i <= numberOfDaysInMonth; i++) { cout << setw(4) << i; if ((i + startDay) % 7 == 0) cout << endl; }

Page 27: The following list represents the written codes in order · 7 #include using namespace std; int sum(int, int); // function prototype void main() { //without using

27

} // Get the start day of the first day in a month int getStartDay(int year, int month) { // Get total number of days since 1//1//1800 int startDay1800 = 3; int totalNumberOfDays = getTotalNumberOfDays(year, month); // Return the start day return (totalNumberOfDays + startDay1800) % 7; } // Get the total number of days since January 1, 1800 int getTotalNumberOfDays(int year, int month) { int total = 0; // Get the total days from 1800 to year - 1 for (int i = 1800; i < year; i++) if (isLeapYear(i)) total = total + 366; else total = total + 365; // Add days from Jan to the month prior to the calendar month for (int i = 1; i < month; i++) total = total + getNumberOfDaysInMonth(year, i); return total; } // Get the number of days in a month int getNumberOfDaysInMonth(int year, int month) { if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) return 31; if (month == 4 || month == 6 || month == 9 || month == 11) return 30; if (month == 2) return isLeapYear(year) ? 29 : 28; return 0; // If month is incorrect } // Determine if it is a leap year bool isLeapYear(int year) { return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0); }


Recommended