imaculgy / cppexamples Goto Github PK
View Code? Open in Web Editor NEWExamples of C++ Programs to help out Beginners
License: MIT License
Examples of C++ Programs to help out Beginners
License: MIT License
Refer to the following program, "Classify Numbers"
#include <iostream>
#include <iomanip>
using namespace std;
const int N = 20;
//Function prototypes
void initialize(int & zeroCount, int & oddCount, int & evenCount);
void getNumber(int & num);
void classifyNumber(int num, int & zeroCount, int & oddCount,
int & evenCount);
void printResults(int zeroCount, int oddCount, int evenCount);
int main() {
//Variable declaration
int counter; //loop control variable
int number; //variable to store the new number
int zeros; //variable to store the number of zeros
int odds; //variable to store the number of odd integers
int evens; //variable to store the number of even integers
initialize(zeros, odds, evens); //Step 1
cout << "Please enter " << N << " integers." <<
endl; //Step 2
cout << "The numbers you entered are: " <<
endl;
for (counter = 1; counter <= N; counter++) //Step 3
{
getNumber(number); //Step 3a
cout << number << " "; //Step 3b
classifyNumber(number, zeros, odds, evens); //Step 3c
} // end for loop
cout << endl;
printResults(zeros, odds, evens); //Step 4
return 0;
}
void initialize(int & zeroCount, int & oddCount, int & evenCount) {
zeroCount = 0;
oddCount = 0;
evenCount = 0;
}
void getNumber(int & num) {
cin >> num;
}
void classifyNumber(int num, int & zeroCount, int & oddCount,
int & evenCount)
{
switch (num % 2) {
case 0:
evenCount++;
if (num == 0)
zeroCount++;
break;
case 1:
case -1:
oddCount++;
} //end switch
} //end classifyNumber
void printResults(int zeroCount, int oddCount, int evenCount) {
cout << "There are " << evenCount << " evens, " <<
"which includes " << zeroCount << " zeros" <<
endl;
cout << "The number of odd numbers is: " << oddCount <<
endl;
} //end printResults
As written, the program inputs the data from the
standard input device (keyboard) and outputs the results on the standard
output device (screen). The program can process only 20 numbers. Rewrite
the program to incorporate the following requirements:
a. Data to the program is input from a file of an unspecified length; that is, the
program does not know in advance how many numbers are in the file.
b. Save the output of the program in a file.
c. Modify the function getNumber so that it reads a number from the
input file (opened in the function main), outputs the number to the
output file (opened in the function main), and sends the number read to
the function main. Print only 10 numbers per line.
d. Have the program find the sum and average of the numbers.
e. Modify the function printResult so that it outputs the final results to
the output file (opened in the function main). Other than outputting the
appropriate counts, this new definition of the function printResult
should also output the sum and average of the numbers.
Notes:
The prompt more so involves the input and output devices used to run the program. You do not need to include the input/output files in the pull request, but please identify the file names one should use if they are running the program on their own client.
You may name the program as you please, for example, "ClassifyNumbers.cpp"
Please make sure the program compiles and runs as it should!
(You do not need to include the prompt before the code for this program, it is very long :( )
During the tax season, every Friday, J&J accounting firm provides assistance
to people who prepare their own tax returns. Their charges are as follows.
a. If a person has low income (<= 25,000) and the consulting time is less
than or equal to 30 minutes, there are no charges; otherwise, the service
charges are 40% of the regular hourly rate for the time over 30 minutes.
b. For others, if the consulting time is less than or equal to 20 minutes, there
are no service charges; otherwise, service charges are 70% of the regular
hourly rate for the time over 20 minutes.
(For example, suppose that a person has low income and spent 1 hour and 15 minutes, and
the hourly rate is $70.00. Then the billing amount is 70.00 x 0.40 x (45 / 60) = $21.00.)
Write a program that prompts the user to enter the hourly rate, the total consulting time,
and whether the person has low income. The program should output the billing amount.
Your program must contain a function that takes as input the hourly rate, the total
consulting time, and a value indicating whether the person has low income. The function
should return the billing amount. Your program may prompt the user to enter the
consulting time in minutes.
You may name the program as you please, for example, "TaxReturns.cpp"
Please make sure the program compiles and runs as it should!
Please refer to the program below, detailing a cell phone company and its services:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
char service;
int accnum;
double cost;
cout << "Enter account number: ";
cin >> accnum;
cout << "Enter 'r' for regular service or 'p' for premium service: ";
cin >> service;
cout << fixed << showpoint << setprecision(2);
if (service == 'r' || service == 'R')
{
int minutes;
cout << "Enter the number of minutes used: ";
cin >> minutes;
if (minutes <= 50)
minutes = 0;
else
minutes -= 50;
cost = 10 + minutes * 0.20;
cout << "Cost: $" << cost;
}
else if (service == 'p' || service == 'P')
{
int minDay, minNight;
double costDay, costNight;
cout << "Enter the number of minutes used at day and at night (separated by spaces): ";
cin >> minDay >> minNight;
if (minDay <= 75)
minDay = 0;
else
minDay -= 75;
costDay = 0.10 * minDay;
if (minNight <= 100)
minNight = 0;
else
minNight -= 100;
costNight = 0.05 * minNight ;
cost = 10 + costDay + costNight;
cout << "Cost: $" << cost;
}
else
cout << "Incorrect input. Try again later.";
return 0;
}
The two types of services this company officers are regular and premium, as shown above.
Its rates vary, depending on the type of service. The rates are computed as
follows:
Regular service: $10.00 plus first 50 minutes are free. Charges for
over 50 minutes are $0.20 per minute.
Premium service: $25.00 plus:
a. For calls made from 6:00 a.m. to 6:00 p.m., the first 75 minutes are free;
charges for more than 75 minutes are $0.10 per minute.
b. For calls made from 6:00 p.m. to 6:00 a.m., the first 100 minutes are
free; charges for more than 100 minutes are $0.05 per minute.
Rewrite the program so that it uses the following functions to calculate the billing
amount. Do NOT output the number of minutes during which the service is used.
a. regularBill: This function calculates and returns the billing amount
for regular service.
b. premiumBill: This function calculates and returns the billing amount
for premium service.
You may name the program as you please, for example, "CellPhoneCompany.cpp"
Please make sure the program compiles and runs as it should!
Task: Update the README.md file to include a definition of C++, including its history and origins. Include this information directly before the line stating "Contributions are welcome." You may use outside sources for this, such as Wikipedia, but do cite the sources you use.
P.S. If you see any grammar issues inside readme, you are more than welcome to fix those as well!
Write a value-returning function, isVowel, that returns the value true if a
given character is a vowel and otherwise returns false.
You may name the program as you please, for example, "VowelChecker.cpp"
Please make sure the program compiles and runs as it should!
The code didn't compile for me when I clicked download. How does it run? I'm using Samsung Smart Fridge running TIZEN 6.0.1003. Please help. I need to do my taxes
Consider the following C++ code:
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
void func1();
void func2(/*formal parameters*/);
int main()
{
int num1, num2;
double num3;
int choice;
cout << fixed << showpoint << setprecision(2);
do
{
func1();
cin >> choice;
cout << endl;
if (choice == 1)
{
func2(num1, num2, num3);
cout << num1 << ", " << num2 << ", " << num3 << endl;
}
}
while (choice != 99);
return 0;
}
void func1()
{
cout << "To run the program, enter 1." << endl;
cout << "To exit the pogram, enter 99." << endl;
cout << "Enter 1 or 99: ";
}
void func2(/*formal parameters*/)
{
//Write the body of func2.
}
The function func2 has three parameters of type int, int, and double, say a, b,
and c, respectively. Write the definition of func2 so that its action is as follows:
a. Prompt the user to input two integers and store the numbers in a and b,
respectively.
b. If both of the numbers are nonzero:
i. If a >= b, the value assigned to c is a to the power b, that is, ab.
ii. If a < b, the value assigned to c is b to the power a, that is, ba.
c. If a is nonzero and b is zero, the value assigned to c is the square root of
the absolute value of a.
d. If b is nonzero and a is zero, the value assigned to c is the square root of
the absolute value of b.
e. Otherwise, the value assigned to c is 0.
The values of a, b, and c are passed back to the calling environment.
After completing the definition of the func2 and writing its function
prototype, test run your program.
You may name the program as you please, for example, "FunctionTester.cpp"
Please make sure the program compiles and runs as it should!
Write a function, reverseDigit, that takes an integer as a parameter and returns the number with its digits reversed. For example, the value of reverseDigit(12345) is 54321; the value of reverseDigit(5600) is 65; the value of reverseDigit(7008) is 8007; and the value of reverseDigit(-532) is -235.
You may name the program as you please, for example, "DigitReverser.cpp"
Please make sure the program compiles and runs as it should!
Write a program to convert the time from 24-hour notation to 12-hour
notation and vice versa. Your program must be menu driven, giving the user
the choice of converting the time between the two notations. Furthermore,
your program must contain at least the following function: a function to convert
the time from 24-hour notation to 12-hour notation, a function to convert the
time from 12-hour notation to 24-hour notation, a function to display the
choices, function(s) to get the input, and function(s) to display the results. (For
12-hour time notation, your program must display AM or PM.)
You may name the program as you please, for example, "TimeConversion.cpp"
Please make sure the program compiles and runs as it should!
The population of a town A is less than the population of town B.
However, the population of town A is growing faster than the population of town B. Write a program that prompts the user to enter the
population and growth rate of each town. The program outputs after
how many years the population of town A will be greater than or equal
to the population of town B and the populations of both the towns at
that time. (A sample input is: Population of town A = 5000, growth
rate of town A = 4%, population of town B = 8000, and growth rate of
town B = 2%.)
You may name the program as you please, for example, "TownPopulation.cpp"
Please make sure the program compiles and runs as it should!
Consider the definition of the function main.
int main()
{
int x, y;
char z;
double rate, hours;
double amount;
.
.
.
}
The variables x, y, z, rate, and hours referred to in items a through f below
are the variables of the function main. Each of the functions described must
have the appropriate parameters to access these variables. Write the following
definitions:
a. Write the definition of the function initialize that initializes x and y
to 0 and z to the blank character.
b. Write the definition of the function getHoursRate that prompts the
user to input the hours worked and rate per hour to initialize the
variables hours and rate of the function main.
c. Write the definition of the value-returning function payCheck that calculates
and returns the amount to be paid to an employee based on the hours worked
and rate per hour. The hours worked and rate per hour are stored in the
variables hours and rate, respectively, of the function main. The formula for
calculating the amount to be paid is as follows: For the first 40 hours, the rate is
the given rate; for hours over 40, the rate is 1.5 times the given rate.
d. Write the definition of the function printCheck that prints the hours
worked, rate per hour, and the salary.
e. Write the definition of the function funcOne that prompts the user to
input a number. The function then changes the value of x by assigning
the value of the expression 2 times the (old) value of x plus the value of y
minus the value entered by the user.
f. Write the definition of the function nextChar that sets the value of z to
the next character stored in z.
g. Write the definition of a function main that tests each of these functions.
You may name the program as you please, for example, "PayCheck.cpp"
Please make sure the program compiles and runs as it should!
(You can split up this program in multiple PRs if need be)
Write a program that outputs inflation rates for two successive years and
whether the inflation is increasing or decreasing. Ask the user to input the
current price of an item and its price one year and two years ago. To
calculate the inflation rate for a year, subtract the price of the item for that
year from the price of the item one year ago and then divide the result by
the price a year ago. Your program must contain at least the following
functions: a function to get the input, a function to calculate the results, and
a function to output the results. Use appropriate parameters to pass the
information in and out of the function. Do not use any global variables.
You may name the program as you please, for example, "InflationRates.cpp"
Please make sure the program compiles and runs as it should!
Write a program that prints the day number of the year, given the date in the
form month-day-year. For example, if the input is 1-1-2006, the day
number is 1; if the input is 12-25-2006, the day number is 359. The program
should check for a leap year. A year is a leap year if it is divisible by 4, but not
divisible by 100. For example, 1992 and 2008 are divisible by 4, but not
by 100. A year that is divisible by 100 is a leap year if it is also divisible by
400. For example, 1600 and 2000 are divisible by 400. However, 1800 is not
a leap year because 1800 is not divisible by 400.
You may name the program as you please, for example, "DayNumber.cpp"
Please make sure the program compiles and runs as it should!
In a right triangle, the square of the length of one side is equal to the sum of
the squares of the lengths of the other two sides. Write a program that
prompts the user to enter the lengths of three sides of a triangle and then
outputs a message indicating whether the triangle is a right triangle.
You may name the program as you please, for example, "RightTriangleChecker.cpp"
Please make sure the program compiles and runs as it should!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.