EECE 230 Homework with Solution, Fall 2010
American University of Beirut · Introduction to Programming · 53 pages · Prof. Hassan Artail · with solution
Showing the first 10 of 53 pages. Download the PDF to see all of it.
More EECE 230 files
Show the text of this homework
EECE 230 Introduction to Programming
Lab Assignment # 10
Objectives: Classes and dynamic arrays
1. Write a class dateType that has day, month and year as private data members,
provide a constructor with default values, a set and print function. The set
function should check if the day is between 1 and 31, month between 1 and 12
and year between 1900 and 2010.
2. Write a class person which has a name (string), a dynamic array of dop (date
of payments) as dateType (composition), a dynamic array of payments
(double), and the maxSize (int) of the arrays. Other than the needed
accessor(set and get) functions, provide the following member functions:
-
Default constructor, initializes the name to John Smith and the
maxSize to 10.
-
Parameterized constructor.
-
addPayment, which adds a payment and its date in the proper
locations in the arrays.
-
totalPersonPayments, which returns the total amount paid by one
person.
-
checkPersons, which returns a Boolean on whether two persons are
identical in names and payments.
-
printAll, which prints the name and the payments made by a person.
3. Write a main program that creates a dynamic array of any given number of
persons. Fill few persons and some payments for each. Print all information of
all persons sorted in ascending order according to totalPersonPayments.
EECE 230 Computers and Programming
Lab Assignment # 1
Objectives:
To learn about C++ environment, iostream, cmath, expressions
and static_cast.
1.
Write a C++ program that will accept the radius of the circle and the program
will print the circumference and the area of the circle.
2.
Write a C++ program to enter a four digit integer and print the sum of the
digits. Example, if the number entered is 3256 then the output will be 16.
3.
Write a C++ program to enter 3 grades of type integer, and then find their
average. Notice that you have to use static_cast.
4.
Write a C++ program named SellStocks that calculates the value of a stock
sale. The user will be prompted to enter the stock price, the number of shares
to be sold, and the commission rate. The program will calculate the value of
shares by multiplying the stock price by the number of shares. It will also
calculate the commission (the value of shares multiplied by the commission
rate) and the net proceeds (the value of the shares minus the commission). The
following example shows what the user will see on the screen:
This program calculates the net proceeds from a sale of stock.
Enter stock price: 10.125
Enter number of shares: 11
Value of shares: $111.38
Enter commission rate (as a percentage): 1.5
Commission: $1.67
Neat proceeds for XYZ is: $109.71
EECE 230 Computers and Programming
Lab Assignment # 2
Objectives:
To practice if, switch, rand, and while statements.
1. Write a C++ program to allow the user to enter two numbers and one of the
operations: +, -, *, /. Your program will print the result of the expression.
Notice that division by zero is not allowed and the result of integer division
can be with decimals. Use nested ifs.
2. Write a C++ program to allow the random number generator to guess a
number between 750 and 1000 inclusive. Then ask the user to guess that
number. The program will check the answer and give the user one of three
following messages: a- Guess higher, b- Guess lower, c- Correct, number of
guesses is X. Example: if the random generator chooses 830 and the user
guessed 900. The program will answer Guess lower. Then the user guessed
800 and the program answer Guess higher…. Finally the program will print
the number of guesses it took the user to guess the right number.
3. Write a C++ program that uses a while loop and a switch statement to enter a
given number of grades then print how many grades there are in the range 90-
100 and in the range 80-89 and so on.
Example run would be:
Enter number of grades: 5
Enter grade 1: 83
Enter grade 2: 91
Enter grade 3: 86
Enter grade 4: 65
Enter grade 5: 82
The output will be:
Range 90-100: 1
Range 80-89: 3
Range 60-69: 1
EECE 230 Computers and Programming
Lab Assignment # 3
Objectives:
To practice for statement and functions.
1. Write a C++ program to prompt the user for the width and the length of a
rectangle then pass the two numbers to a function that will return the area of the
rectangle to be printed in the main.
2. Write a C++ program to prompt the user to enter a number then pass it to a
function that will return a Boolean specifying whether the number entered is a
prime number or not. The main will print a corresponding message (prime or not
prime). Note that a prime number is a number that divides by 1 and itself only.
3. Write a C++ program to prompt the user for two fraction numbers and one of the
arithmetic operations + or *. Your main will call one of the two functions:
addFractions() or multiplyFractions() according to the operation entered. Each
function will return the result of the expression to be printed in the main.
Example: if the user enters 2 3 3 5 * This means he/she wants to multiply
(2/3)*(3/5). You pass the four numbers to the multiplyFractions() and return the
result in fraction form 6 and 15(no simplification is needed) to be printed in the
main as 6/15.
4. Write a C++ program to allow the user to enter a number between 10 and 80
inclusive. You have to keep asking the user to enter a number in that range if he
doesn’t (use do-while). Then the program will list all the combinations of two
integers (other than 1*the number itself) that their multiplication is equal to the
entered number. Example; if the user entered 36 then your output will be: <2, 18>
<3, 12> <4, 9> <6, 6>.
EECE 230 Computers and Programming
Lab Assignment # 4
Objectives:
To practice functions and strings.
1. Write a function to keep track of max and min of any given sequence of numbers.
The main will call the function as many times as the user wants, the input will end
with -999 as a sentinel. Finally, the main will print the max and the min values.
Example: if the user enters the following numbers: 3, -2, 5, 6, 4, -999
Then the output will be: The max is: 6
The min is: -2
2. Write a program to replace all occurrences of one text by another in an original
string.
Example: if the string is “this is a test, nothing but a test”
and text to replace is: “test”
and text to replace it is: “quiz”
Then the output will be:
this is a quiz, nothing but a quiz
3. Write a program to count the number of occurrences of any given substring in any
given string.
Example: if the user enters “this is a test, nothing but a test” as a string
and enters “test” as a substring to search for
Then the output will be: test occurred 2 time(s)
EECE 230 Computers and Programming
Lab Assignment # 5
Objectives:
to practice one dimensional array.
1. Write a program to enter 20 numbers, between 40 and 100 inclusive, from the
random number generator into a 20 element 1D array such that the odd number will
be put in the next available odd index and the even number will be put in the next
available even index. Then write a function that will take the array and returns its
smallest odd number and its largest even number. The smallest odd and the largest
even have to print in the main.
2. Write a program to enter an array of 10 numbers. Send the array to a recursive
function that will return a Boolean according to whether the array is sorted in
ascending order or not. Example; if the numbers entered are: 1 5 6 12 17 22 54 55 55
59 the function will return true, while if the numbers are: 1 5 6 6 5 12 13 14 15 17 the
function will return false.
3. Use a single-subscripted array to solve the following problem. A company pays its
salespeople on a commission basis. The salespeople receive $200 per week plus 9
percent of their gross sales for that week. For example, a salesperson who grosses
$5000 in sales in a week receives $200 plus 9 percent of $5000, or a total of $650.
Write a program that determines how many of the salespeople earned salaries in each
of the following ranges (assume that each salesperson’s salary is truncated to an
integer amount):
a) $200–$299
b) $300–$399
c) $400–$499
d) $500 and over
Your program should use two functions:
wages(…) that computes the salaries of the salespeople and counts how many
employees earned salaries in the above ranges, then returns these results to the
main program.
display(…) that displays these results
A sample run would look like:
Enter employee gross sales (-1 to end): 600
Employee commission is: $254
Enter employee gross sales (-1 to end): 10000
Employee commission is: $1100
Enter employee gross sales (-1 to end): 5000
Employee commission is: $650
Enter employee gross sales (-1 to end): -1
Employees in the range:
$200-$299 : 1
Over $500 : 2
EECE 230 Computers and Programming
Lab Assignment # 6
Objectives:
1D, 2D arrays and c-string.
1) Write a program to print the substring which forms the maximum number of
consecutive characters in any given c-string.
Example if the user enters "abc is a grand store" in char s[100]
Then "abc" and "st" have consecutive characters but "abc" has more
characters, so you print abc.
If you have two substrings having the same maximum size then print the
first one in sequence.
2) Write a program to check if any two given arrays have the same content or not?
You can assume that the two arrays have the same size but you can not sort the
arrays.
Example1: if the elements in the first array are: 5 7 3 5
and the elements in the second array are: 3 5 7 5
then you print: equal
Example2: if the elements in the first array are: 6 2 9 2
and the elements in the second array are: 2 9 6 3
then you print: not equal
3) Write a program to allow the user to enter a 4X4 square 2Darray then swap the
values of diagonals and then check if the sum of the diagonals is equal or not?
Note that you have to use loops because your program has to work for any array
size.
Example; if the array entered is:
1 2 3 4
16 2 3 13
5 6 7 8
5 11 10 8
9 10 11 12
9 7 6 12
13 14 15 16
4 14 15 1
Then the out is: Yes, the sum of the diagonals is equal.
EECE230 Fall01-11
Assignment 7
Objectives: I/O files, pointers.
1.
A telephone directory is stored in a text file”Directory.txt”. It has the following format:
Sami
2
01-111111
03-333333
Hani
1
01-444444
Rami
2
01-555555
03-666666
.
.
.
The file has the name followed by the number of telephones he/she has followed by the
telephone numbers (a name can have 1 or 2 telephone numbers only). You have to read till the
end of the file because we don’t know how many names we have but you can assume that they
are not more than 100.
Write a program that reads the names and the telephone numbers from the file into a 100X3
two dimensional array of strings. Then call a function that returns the array sorted by name in
ascending order. Finally, start a loop, for as long as the user wants, that calls a binary search
function that accepts the array, the number of names in the array, and a name to search for.
The function will return the index where the name is found to display the telephone numbers
that belongs to that name or returns -1 to display “Name not found” message. Write the names
that we search for and their telephone numbers to a text file for record.
2.
Write a program that takes the content of two text files and concatenate them in a new text file.
3.
Write a program that accepts from the user two integer arrays of any given size then pass them
as pointers to a function that will combine them (the elements of the first array followed by the
elements of the second array) in a third array. The main will print the combined array. The
prototype of the function:
void combine(int *a1, int size1, int *a2, int size2, int *a3);
The function doesn’t use the subscripts of the arrays; it uses rather a pointer to the elements of
the arrays.
EECE 230 Fall 10-11
Lab. Assignment # 8
Objectives:
C-strings, recursive functions, I/O files, and classes.
1. A palindrome is a string that reads as forward as backward. Example: “step on
no pets”. Write a program to prompt the user for a text of C-string type and send
it to a recursive function. The function will return a Boolean to the main that
will print palindrome or not palindrome.
2. Write a program that reads a text file “input.txt” and print the number of
characters, the number of words, and the number of lines in the text file.
Example; if the text file is:
“This is a test,
nothing but a test.”
Your output will be: there are 41 characters
8 words
3 lines
Notice the extra spaces between the words and the empty lines.
3. Design a class personType that includes the following:
a.
Data members: firstName, lastName, noOfCredits.
b. Methods:
1. Default and non-default constructors.
2. Set the first name only
3. Set the last name only
4. Set the noOfCredits
5. calculateTuition( double priceOfCredit)
6. print (firstName, lastName, tuitionAmount) this method will call
calculateTuition.
7. Check whether a given last name is the same as the last name of this
person
Also, write a main method to test the functionality of the methods of the class.
Good Luck
EECE 230 Computers and Programming
Lab Assignment # 9
1)
Implement class Point that contains: the coordinates of a point x and y. It should
provide the following operations:
a)
Constructor that takes x and y as arguments
b)
Default constructor that initializes x and y to random values
c)
double distanceTo(Point p) that returns the distance between two points
2)
Implement class Circle that contains: its center point, and its radius. It should provide
the following operations:
a)
Constructor that takes a center point (of type Point) and radius
b)
double area()
c)
double perimeter()
d)
bool intersects(Circle b)
e)
bool contains(Circle b)
3)
Implement class Stock that describes a stock owned by a customer. It should contain:
number of shares, ticker symbol (of type string), buy price, current price. In addition to a
constructor provide accessors methods.
4)
Implement a class StockAccount that helps maintain the stocks of a customer in a
brokerage firm. It should contain:
a)
Name of customer
b)
Number of owned stocks (initialized to 0)
c)
List of owned stocks implemented as an array of Stock objects (initialized to be of
size 100)
StockAccount must support the following operations:
a)
Constructor that takes: name of customer
b)
double currentValue(): returns current values of all stocks
c)
double currentProfit(): returns current profit based on current/buy prices of
all stocks
d)
void buy(int numShares, string ticker, double currentPrice)
e)
void updatePrice(string ticker, double newPrice)
f)void printReport(): sensible summary for each stock and all stocks combined
(including profits, losses etc.)
Provide appropriate driver to test StockAccount thoroughly. You can use the following
scenario:
Create account for John Lucky
Buy 100 shares of GE at 13.5
Buy 25 shares of GOOG at 110.1
Buy 97 shares of IBM at 23.3
Buy 250 shares of MSFT at 2.5
Buy 200 shares of YHOO at 11.8
Update GE to 45.5
Update GOOG to 700.1
Update IBM to 86.5
Update MSFT to 24.8
Print current value of account
Print profit
Print report summary
#include <iostream>
using namespace std;
const double pie = 3.14;
int main ()
{
double radius;
cout << "\nEnter the radius of the circle: ";
cin >> radius;
cout << "\nThe area of the circle is: " << radius*radius*pie << endl;
cout << "\nThe circumference of the circle is: " << 2*radius*pie << endl
<<endl;
return 0;
}
#include <iostream>
using namespace std;
int main ()
{
int integer, first, second, third, fourth;
cout << "\nEnter a four digit integer: ";
cin >> integer;
first = integer % 10;
second = (integer - first) % 100 / 10;
third = (integer - first - second) % 1000 / 100;
fourth = (integer - first - second - third) % 10000 / 1000;
cout << endl << first + second + third + fourth << endl << endl;
return 0;
}
#include <iostream>
using namespace std;
int main ()
{
double grade1, grade2, grade3, average;
cout << "Enter the 3 grades: " << endl;
cin >> grade1 >> grade2 >> grade3;
average =
( static_cast<int>(grade1)+static_cast<int>(grade2)+static_cast<int>(grade3)
)/3;
cout << "The average is: " << average << endl << endl;
return 0;
}
#include <iostream>
using namespace std;
int main ()
{
double stockPrice, numberOfShares, commissionRate, valueOfShares,
commission;
cout << "This programme calculates the net proceeds from a sale of
stock."<< endl << endl;
cout << "Enter stock price: ";
cin >> stockPrice;
cout << "\nEnter the number of shares: ";
cin >> numberOfShares;
cout << "\nValue of shares: $" << stockPrice * numberOfShares << endl <<
endl;
valueOfShares = stockPrice * numberOfShares;
cout << "Enter commission rate (as a percentage): ";
cin >> commissionRate;
cout << "\nCommission: $" << commissionRate * valueOfShares << endl;
commission = commissionRate*valueOfShares;
cout << "\nNeat proceeds: " << valueOfShares - commission << endl <<endl;
return 0;
}
Build Log
Build started: Project: HW 10, Configuration: Debug|
Win32
Command Lines
Creating temporary file "\\win2k.aub.edu.lb\files\HomeS\shk15\my documents\Visual Studio
[
/OUT:"\\win2k.aub.edu.lb\files\HomeS\shk15\my documents\Visual Studio 2005\Projects\HW 10
odbc32.lib odbccp32.lib
".\debug\dateType.obj"
".\debug\Person's functions.obj"
".\debug\Problem 2 solution.obj"
".\Debug\HW 10.exe.embed.manifest.res"
]
Creating command line "link.exe @"\\win2k.aub.edu.lb\files\HomeS\shk15\my documents\Visua
Creating temporary file "\\win2k.aub.edu.lb\files\HomeS\shk15\my documents\Visual Studio
[
/out:".\debug\HW 10.exe.embed.manifest" /notify_update /manifest
".\debug\HW 10.exe.intermediate.manifest"
]
Creating command line "mt.exe @"\\win2k.aub.edu.lb\files\HomeS\shk15\my documents\Visual
Creating temporary file "\\win2k.aub.edu.lb\files\HomeS\shk15\my documents\Visual Studio
[
@echo Manifest resource last updated at %TIME% on %DATE% > ".\debug\mt.dep"
]
Creating command line """\\win2k.aub.edu.lb\files\HomeS\shk15\my documents\Visual Studio
Output Window
Linking...
Embedding
manifest...
Results
Build log was saved at "file://\\win2k.aub.edu.lb\files\HomeS\shk15\my documents\Visual S
BuildLog.htm"
HW 10 - 0 error(s), 0 warning(s)
#include <iostream>
#include <string>
using namespace std;
#include "Person.h"
Person::Person()
{
name = "John Smith";
maxSize = 10;
dop = new dateType [maxSize];
payments = new double [maxSize];
for ( int i =0; i < maxSize; i++)
{
dop [i];
payments [i] = -5.0;
}
sum =0;
}
Person::Person (string n, int max)
{
name = n;
maxSize = max;
dop = new dateType [max];
payments = new double [max];
for ( int i =0; i < max; i++)
{
dop [i];
payments [i] = -5.0;
}
sum =0;
}
void Person::addPayments(dateType dopay, double pay)
{
for ( int i = 0; i < maxSize; i++)
{
if ( payments [i] == -5.0)
{
payments [i] = pay;
dop [i].copy (dopay);
break;
}
}
}
bool Person::checkPersons(Person per1)
{
if (per1.name== name)
{
for ( int j=0; j < maxSize && j < per1.maxSize; j++)
{
if ( per1.payments [j] != payments [j])
return false;
}
return true;
}
else
return false;
}
void Person::get(string n, int &size)
{
n = name;
dop = new dateType[size];
payments = new double [size];
maxSize = size;
}
void Person::printALL()
{
cout << "Name: " << name << endl;
for ( int j=0; j < maxSize; j++)
{
if ( payments [j] == -5.0)
break;
cout << "Payments: " << payments[j] << endl;
}
}
void Person::set(string nam)
{
name = nam;
}
Person::~Person()
{
delete []dop;
delete []payments;
}
double Person::totalPersonPayments()
{
int x;
for ( x=0; x < maxSize; x++)
{
if (payments [x] == -5.0)
break;
sum += payments[x];
}
return sum;
}
int Person::getmax()
{
return maxSize;
}
#include <iostream>
#include <string>
using namespace std;
#include "dateType.h"
class Person {
private: string name;
dateType *dop;
double *payments;
int maxSize;
double sum;
public:
Person ();
Person (string, int);
void set (string);
void get (string, int &);
int getmax ();
void addPayments (dateType , double);
double totalPersonPayments ();
bool checkPersons (Person);
void printALL();
~Person ();
};
#include <iostream>
#include "dateType.h"
using namespace std;
void main()
{
dateType today;
dateType someday;
dateType anotherday;
int d,m,y;
cout << "Enter the date, month and year: " << endl;
cin >> d >> m >> y;
today.setDate(d, m, y);
today.printDate();
someday.setDate (40, 13, 1799);
someday.printDate();
anotherday.setDate (8, 11, 1766);
anotherday.printDate();
}
#include <iostream>
#include <string>
using namespace std;
#include "Person.h"
const int size = 99999;
void sort (Person p[], int s);
void swap (Person p1[], int a, int b);
void main ()
{
system("cls");
Person *persons;
persons = new Person [size];
int i =0, j=0;
int day, month, year;
dateType dates;
double payments = 0;
for (j =0; j < size; j++)
{
string name1;
getline(cin, name1);
persons [j].set(name1);
for (int z =0; z < persons [j].getmax(); z++)
{
if ( payments == -1)
break;
cin >> payments;
persons [j].addPayments (dates, payments);
}
if ( name1 == "0")
break;
}
sort (persons, j);
for ( int z=0; z < j; z++)
{
persons [z].printALL();
}
string name;
Person Me ("Sami Kobayter", size);
Person Him ("a", size);
cout << "Enter your name please: ";
getline (cin, name);
Him.set (name);
for ( i =0; i < size; i++)
{
cout << "Enter the date (day/month/year) of the payments: " << endl;
cin >> day;
if (day == -1)
break;
cin >> month >> year;
dates.setDate (day, month, year);
cin >> payments;
Him.addPayments (dates, payments);
Me.addPayments ( dates, payments);
}
Him.printALL ();
Me.printALL();
cout << Me.totalPersonPayments() << endl;
if (Me.checkPersons(Him) == true)
cout << "The 2 persons are identical" << endl;
else
cout << "not the same person" << endl;
}
void sort (Person p[], int s)
{
for (int i=0; i<s-1; i++)
for (int j=s-1; j>i; j--)
if ((p[j].totalPersonPayments() <p[j-1].totalPersonPayments()))
swap(p, j, j-1);
}
void swap (Person p1[], int a, int b)
{
Person temp;
temp = p1[a];
p1[a] = p1[b];
p1[b] = temp;
}
#include <iostream>
#include "dateType.h"
using namespace std;
dateType::dateType ()
{
day = 1;
month = 1;
year = 1900;
}
void dateType::setDate (int D, int M, int Y)
{
if ( (D < 1) || (D > 31))
D = 1;
if ((M < 1) || (M > 12))
M = 1;
if ((Y < 1900) || (Y > 2010))
Y = 1900;
day = D;
month = M;
year = Y;
}
void dateType::printDate()
{
if ((day < 10) && (month < 10))
cout << "0" << day << "/" << "0" << month << "/" << year << endl;
else if ( month < 10)
cout << day << "/" << "0" << month << "/" << year << endl;
else if ( day < 10)
cout << "0" << day << "/" << month << "/" << year << endl;
else
cout << day << "/" << month << "/" << year << endl;
}
void dateType::copy (dateType date)
{
day = date.day;
month = date.month;
year = date.year;
}
#include <iostream>
using namespace std;
class dateType {
private: int day;
int month;
int year;
public: dateType ();
void setDate ( int, int, int);
void printDate ();
void copy ( dateType);
};
#include <iostream>
using namespace std;
int main ()
{
int num1, num2;
double result=0.0;
char operation;
cout << "Enter two numbers:"<< endl << endl;
cin >> num1;
cout << endl;
cin >> num2;
cout << "\nChoose the type of operation you want\n" << endl;
cin >> operation;
if (operation =='*')
{
result = num1*num2;
cout << "\nThe result is " << result << endl << endl;
}
else if ( operation == '/')
{
if ( num2 == 0)
cout << "\nAbort the mission" << endl << endl;
else
{
result = (double) num1/num2;
cout << "\nThe result is: " << result <<endl << endl;
}
}
else if ( operation == '+')
{
result = num1+num2;
cout << "\nThe Result is: " << result << endl << endl;
}
else if ( operation == '-')
{
result = num1-num2;
cout << "\nThe Result is: " << result << endl << endl;
}
else
cout << "\nThe operation is invalid\n" << endl << endl;
cout << "Enter any number to finish: ";
cin >> num1;
cout <<endl;
return 0;
}
#include <iostream>
using namespace std;
int main ()
{
int guess = 0, count = 0;
int randnum = rand()%250 +750;
cout << "Guess a number between 750 and 1000" << endl << endl;
while ( randnum != guess )
{
cin >> guess;
cout << endl;
count++;
if ( guess < randnum )
cout << "Guess Higher" << endl << endl;
else if ( guess > randnum )
cout << "Guess Lower" << endl << endl;
else
cout << "Correct, the number of guesses is " << count << endl
<< endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int X, grade, numberofgrades, counter = 0;
int Range1 = 0, Range2 = 0, Range3 = 0, Range4 = 0, Range5 = 0, Range6 =
0, Range7 = 0, Range8 = 0, Range9 = 0,Range10 = 0;
cout << "Enter the number of grades ";
cin >> numberofgrades;
cout << endl << endl;
while ( counter != numberofgrades)
{
counter++;
cout << "Enter grade " << counter << ": ";
cin >> grade;
cout << endl;
X = grade/10;
switch (X)
{
case 0 : Range1++;
break;
case 1 : Range2++;
break;
case 2 : Range3++;
break;
case 3 : Range4++;
break;
case 4 : Range5++;
break;
case 5 : Range6++;
break;
case 6 : Range7++;
break;
case 7 : Range8++;
break;
case 8 : Range9++;
break;
case 9 :
case 10:
Range10++;
break;
}
}
if (Range1 > 0)
cout << "Range 0-9: " << Range1 << endl;
if (Range2 > 0)
cout << "Range 10-19: " << Range2 << endl;
if (Range3 > 0)
cout << "Range 20-29: " << Range3 << endl;
if (Range4 > 0)
cout << "Range 30-39: " << Range4 << endl;
if (Range5 > 0)
cout << "Range 40-49: " << Range5 << endl;
if (Range6 > 0)
cout << "Range 50-59: " << Range6 << endl;
if (Range7 > 0)
cout << "Range 60-69: " << Range7 << endl;
if (Range8 > 0)
cout << "Range 70-79: " << Range8 << endl;
if (Range9 > 0)
cout << "Range 80-89: " << Range9 << endl;
if (Range10 > 0)
cout << "Range 90-100: " << Range10 << endl;
return 0;
}
#include <iostream>
using namespace std;
char addFractions ( int a, int b, int c, int d);
char multiplyFractions ( int w, int x, int y, int z);
int main ()
{
int num1, num2, num3, num4;
char operation;
cout << "Enter the 4 numbers: " << endl;
cin >> num1 >> num2 >> num3 >> num4;
cout << "Choose one of the operations '+' or '*'\n";
cin >> operation;
if (operation == '+')
{
cout << "The result is: \n";
cout << addFractions(num1, num2, num3, num4) << endl;
}
else if (operation == '*')
{
cout << "The result is: \n";
cout << multiplyFractions (num1, num2, num3, num4) << endl;
}
else
cout << "The operation is invalid\n" << endl;
return 0;
}
char addFractions (int a, int b, int c, int d)
{
cout << d*a + c*b << "/" << b*d << endl;
return ' ';
}
char multiplyFractions ( int w, int x, int y, int z)
{
cout << w*y << "/" << z*x << endl;
return ' ';
}
#include <iostream>
using namespace std;
void printcombinations (int X);
int main ()
{
int num;
cout << "Enter a number between 10 and 80:" << endl;
do
cin >> num;
while ((num < 10) || (num > 80));
printcombinations (num);
return 0;
}
void printcombinations(int X)
{
int divider = 2, remainder;
while (divider < X)
{
remainder = X % divider;
if (remainder == 0)
{
int a,b;
a= divider;
b= X/divider;
if (a <= b)
{
cout << "<" << a << "," << b << ">" << endl;
}
}
divider++;
}
}
#include <iostream>
using namespace std;
double area ( double x, double y);
int main ()
{
double length, width;
cout << "Enter the length of the rectangle: ";
cin >> length;
cout << "\nEnter the width: ";
cin >> width;
cout << endl;
cout << "The area of the rectangle is: " << area (length, width) << endl;
return 0;
}
double area (double x, double y)
{
return x*y;
}
#include <iostream>
using namespace std;
bool prime (int X);
int main()
{
int number;
bool Prime;
cout << "Enter a number to check wether it's prime or not: ";
cin >> number;
cout << endl;
Prime = prime (number);
if (Prime == true)
cout << "The number is prime\n" << endl;
else
cout << "The number is not prime\n" << endl;
return 0;
}
bool prime (int X)
{
int divider = 2;
while (divider < X )
{
if ( X % divider == 0)
return false;
divider++;
}
return true;
}
#include <iostream>
using namespace std;
const int sentinel = -999;
void compare ( int a, int & min, int & max);
int main ()
{
int number, Min, Max;
cout <<"Please enter as many numbers as you want to find the maximum and
mimum of them: ";
cin >> number;
Min = number;
Max = number;
while ( number != sentinel)
{
compare ( number, Min, Max);
cin >> number;
}
cout << "The max is: " << Max << endl;
cout << "The min is: " << Min << endl;
return 0;
}
void compare ( int a , int & min, int & max)
{
if ( a <= min)
min = a;
if ( a >= max)
max = a;
}
#include <iostream>
#include <string>
using namespace std;
void overwrite ( string & str1, string str2, int index);
int main ()
{
string statement, replacement, word;
int length, position;
char ch;
cout << "Enter the statement: ";
getline(cin, statement);
cout <<"Enter the word you want to replace: ";
cin >> word;
cin.get (ch);
cout <<"Enter the new word: ";
cin >> replacement;
cin.get (ch);
length = statement.length();
position = statement.find (word);
while ( (position >=0) && (position < length))
{
overwrite ( statement, replacement, position);
position = statement.find (word, (position+word.length()));
}
cout << "The new statement is: " << statement << endl;
return 0;
}
void overwrite ( string & str1, string str2, int index)
{
for( int i=0; i < str2.length(); i++)
{
str1[index+i] = str2[i];
}
}
#include <iostream>
#include <string>
using namespace std;
int main ()
{
int length, occurences=0, index;
string str1, sub;
char ch = 'A';
cout << "Enter a string: ";
getline (cin, str1);
cout << "Enter the substring you want to find: ";
cin >> sub;
cin.get (ch);
length = str1.length();
index = str1.find (sub);
while ((index >= 0) && (index <length))
{
occurences++;
index = str1.find (sub, (index+ sub.length()));
}
cout << "The number of occurences of the substring " << sub << " is: " <<
occurences << endl;
return 0;
}
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
const int size = 20;
int smallodd ( int arr [], int size, int min);
int largeven ( int arr [], int size, int max);
int main ()
{
int assignedNum[size];
int num, min=0, max=0, s=0;
srand((unsigned)time(0));
for ( int i=0; i <20; i++ )
{
if ( i%2 ==0 )
{
num = rand()% 59 + 40;
if ( num % 2 ==0)
assig