The program is in C++. #include <iostream> #include <string> using namespace std
ID: 3747279 • Letter: T
Question
The program is in C++.
#include <iostream>
#include <string>
using namespace std;
//////////////////////////////////////////////////////
// Rules:
// The main function contains a bunch of tests for a
// set of functions that you are going to write.
// Everything in main is commented out because it
// won't compile (build) until you write the needed
// functions.
//
// Read the comments in main to see how the code
// should work once you add your functions.
//////////////////////////////////////////////////////
// YOUR FUNCTIONS HERE
// END YOUR FUNCTIONS - DO NOT EDIT MAIN (except to uncomment things to test)
int main()
{
//Implement min and max functions.
//cout << max(10, 2.2, 7); << endl; // 10
//cout << max(4.9, 5, 5); << endl; //5
//cout << max(8, 10, 20.3); << endl; //20.3
//cout << min(6, 20, 3) << endl; //3
//cout << min(3.14, 1.17, 8.3) << endl; //1.17
//Implement isEven, isOdd boolean functions
// hint: think division
//int n;
//cout << "Enter a number: " << endl;
//cin >> n;
//if (isEven(n))
//{
// cout << n << " is even." << endl;
//}
//if (isOdd(n))
//{
// cout << n << " is odd." << endl;
//}
//Add, multiply functions
//cout << add(5, 2) << endl; // 7
//cout << add(-4.2, 23) << endl; // 18.8
//cout << multiply(5, 3) << endl; // 15
//cout << multiply(7, 10) << endl; // 70
//Create a function that sums the numbers between the given range
//int total1 = sum(8, 20); //this should be 8+9+10+...+20 =
//int total2 = sum(9, 35);
//cout << "The sum from 8 to 20 is " << total1 << endl;
//cout << "The sum from 9 to 35 is " << total2 << endl;
//Create a function that calculates power (x raised to the y) WITHOUT using the built-in pow function
// (yes same as the hw, but lots of people just called pow which it tots cheats - if you didn't, you
// already know the answer here)
//cout << "4 raised to the 5 is " << power(4, 5) << endl; // 1024
//cout << "14 raised to the 3 is " << power(14, 3) << endl; // 2744
//////////////////////////////////////////////////////
// Okay, now something more interesting!
// Prime numbers cannot be evenly divided by any number smaller than them except 1
// hint: computers don't mind doing lots of divisions
//if (isPrime(n))
//{
// cout << n << " is prime." << endl;
//}
//////////////////////////////////////////////////////
// Write a function that lists all the prime numbers from 1 up to a certain number
// hint: Functions may call other functions, even other functions that *you* have written.
//listPrimes(10); //Should print: 2,3,5,7
//listPrimes(20); //Should print: 2,3,5,7,11,13,17,19
return 0;
}
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
//////////////////////////////////////////////////////
// Rules:
// The main function contains a bunch of tests for a
// set of functions that you are going to write.
// Everything in main is commented out because it
// won't compile (build) until you write the needed
// functions.
//
// Read the comments in main to see how the code
// should work once you add your functions.
//////////////////////////////////////////////////////
// YOUR FUNCTIONS HERE
double max(double a,double b,double c);
double min(double a,double b,double c);
bool isEven(int n);
bool isOdd(int n);
float add(float a,float b);
float multiply(float a,float b);
int power(int a,int b);
bool isPrime(int n);
void listPrimes(int n);
int sum(int a, int b);
// END YOUR FUNCTIONS - DO NOT EDIT MAIN (except to uncomment things to test)
int main()
{
//Implement min and max functions.
cout <<max(10,2.2,7)<<endl; // 10
cout <<max(4.9,5,5)<<endl; //5
cout <<max(8,10,20.3)<< endl; //20.3
// cout << min(6,20,3) << endl; //3
// cout << min(3.14,1.17,8.3) << endl; //1.17
//Implement isEven, isOdd boolean functions
// hint: think division
int n;
cout << "Enter a number: " << endl;
cin >> n;
if (isEven(n))
{
cout << n << " is even." << endl;
}
if (isOdd(n))
{
cout << n << " is odd." << endl;
}
// Add, multiply functions
cout << add(5, 2) << endl; // 7
cout << add(-4.2, 23) << endl; // 18.8
cout << multiply(5, 3) << endl; // 15
cout << multiply(7, 10) << endl; // 70
//Create a function that sums the numbers between the given range
int total1 = sum(8, 20); //this should be 8+9+10+...+20 =
int total2 = sum(9, 35);
cout << "The sum from 8 to 20 is " << total1 << endl;
cout << "The sum from 9 to 35 is " << total2 << endl;
//Create a function that calculates power (x raised to the y) WITHOUT using the built-in pow function
// (yes same as the hw, but lots of people just called pow which it tots cheats - if you didn't, you
// already know the answer here)
cout << "4 raised to the 5 is " << power(4, 5) << endl; // 1024
cout << "14 raised to the 3 is " << power(14, 3) << endl; // 2744
//////////////////////////////////////////////////////
// Okay, now something more interesting!
// Prime numbers cannot be evenly divided by any number smaller than them except 1
// hint: computers don't mind doing lots of divisions
if (isPrime(n))
{
cout << n << " is prime." << endl;
}
//////////////////////////////////////////////////////
// Write a function that lists all the prime numbers from 1 up to a certain number
// hint: Functions may call other functions, even other functions that *you* have written.
listPrimes(10); //Should print: 2,3,5,7
listPrimes(20); //Should print: 2,3,5,7,11,13,17,19
return 0;
}
double max(double a,double b,double c)
{
if(a>b && a>c)
return a;
else if(b>c)
return b;
else
return c;
}
double min(double a,double b,double c)
{
if(a<b && a<c)
return a;
else if(b<c)
return b;
else
return c;
}
bool isEven(int n)
{
if(n%2==0)
return true;
else
return false;
}
bool isOdd(int n)
{
if(n%2!=0)
return true;
else
return false;
}
float add(float a,float b)
{
return a+b;
}
float multiply(float a,float b)
{
return a*b;
}
int power(int a,int b)
{
int res=1;
for(int i=1;i<=b;i++)
{
res*=a;
}
return res;
}
bool isPrime(int n)
{
// If the user entered number is '2' return true
if (n == 1)
return false;
for (int i = 2; i * i <= n; i++)
{
if (n % i == 0)
return false;
}
return true;
}
void listPrimes(int n)
{
for(int i=2;i<=n;i++)
{
if(isPrime(i))
cout<<i<<" ";
}
cout<<endl;
}
int sum(int a, int b)
{
int sum=0;
for(int i=a;i<=b;i++)
{
sum+=i;
}
}
_______________
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.