1. Using vectors or arrays, write a function named word_rev() that reverse any g
ID: 3690150 • Letter: 1
Question
1. Using vectors or arrays, write a function named word_rev() that reverse any given word.
2. Write a program that will:
Ask the user to enter a word.
Save the entry as word_entered.
Call word_rev on the entry
Display the reversed entry (word_entered)
3. Write a function named prime() that determine whether or not a given number n (greater than
one) is prime. The algorithm:
If n is even then n is not a prime number except n = 2
For all odd numbers less or equal to the square root of n, if any of them divide n, then
n is not prime
4. Write a program that:
Asks the user to enter an integer
Saves the entry as var
Calls the function prime() on the entry
Displays whether or not the entry is prime
5. Write a function named eratos() that find all the prime number less than a given number, its
argument n
6. Write a program that:
Asks the user to enter an integer
Saves the entry as bound
Passes bound onto eratos()
Displays all the prime numbers less or equal to bound
## the code does not build
Explanation / Answer
1.
void word_REV(string& y)
{
string temp = "";
int len = y.length();
for(int i = len - 1; i >= 0; --i)
{
temp += y[i];
}
y = temp;
}
2.
#include <iostream>
#include <string>
using namespace std;
void word_REV(string& y)
{
string temp = "";
int len = y.length();
for(int i = len - 1; i >= 0; --i)
{
temp += y[i];
}
y = temp;
}
int main()
{
string word_entered;
cout<< "enter the word to be reversed";
cin>>word_entered
reverseStr(word_entered);
cout << "word_entered = " << x << endl;
return 0;
}
3.
int prime(double num)
{
int isprime = 0;
for(int i = 2; i <= sqrt(num); i += 2)
{
if(i % 2 == 0)
i++;
if((int(num)% i) == 0)
{
isprime = 1;
break;
}
}
return isprime;
}
4.
#include <math.h>
#include <iostream>
using namespace std;
using namespace System;
int prime(double num);
int prime(double num)
{
int isprime = 0;
for(int i = 2; i <= sqrt(num); i += 2)
{
if(i % 2 == 0)
i++;
if((int(num)% i) == 0)
{
isprime = 1;
break;
}
}
return isprime;
}
int main()
{
double var;
cin>>"enter number to determine";
cin>>var;
if(prime(var)==0)
cout>>"given integer is prime";
cout>>"given integer is not prime";
return(0);
}
5.
void eratos(int n)
{
bool prime[n+1];
memset(prime, true, sizeof(prime));
for (int p=2; p*p<=n; p++)
{
// If prime[p] is not changed, then it is a prime
if (prime[p] == true)
{
// Update all multiples of p
for (int i=p*2; i<=n; i += p)
prime[i] = false;
}
}
// Print all prime numbers
for (int p=2; p<=n; p++)
if (prime[p])
cout << p << " ";
}
6.
#include <bits/stdc++.h>
using namespace std;
void eratos(int n)
{
bool prime[n+1];
memset(prime, true, sizeof(prime));
for (int p=2; p*p<=n; p++)
{
// If prime[p] is not changed, then it is a prime
if (prime[p] == true)
{
// Update all multiples of p
for (int i=p*2; i<=n; i += p)
prime[i] = false;
}
}
// Print all prime numbers
for (int p=2; p<=n; p++)
if (prime[p])
cout << p << " ";
}
int main()
{
int bound;
cout<< "enter number ";
cin>>bound;
cout << "Following are the prime numbers smaller "
<< " than or equal to " << n << endl;
eratos(n);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.