Answer to Question #201445 in C++ for shuvo

Question #201445
Do the following 5 tasks. Each task is worth 2 points.
(a) Define an array of integers called primes containing the first five prime numbers.
(b) What are the contents of the array primes[5] after executing the following loop?
for (int k = 0; k < 2; k++)
{
primes[4-k] = primes[k];
}
(c) Consider the array primes[5] defined in part a. What is its contents after executing
the following loop?
for (int j = 0; j < 5; j++)
{
primes[j]++;
}
(d) Define an array containing two strings, ”Yes” and ”No”.
(e) Given the array you modified in part (c) print in reverse the elements of the array
prime
1
Expert's answer
2021-06-01T05:14:35-0400
#include <iostream>
#include <string>


using namespace std;


int main()
{
	// a
	int primes[5] = { 2, 3, 5, 7, 11 };
	cout << "a) Primes: ";
	for (int i = 0; i < 5; i++)
	{
		cout << primes[i] << " ";
	}
	cout << endl;
	// b
	for (int k = 0; k < 2; k++)
	{
		primes[4 - k] = primes[k];
	}
	cout << "b) Primes: ";
	for (int i = 0; i < 5; i++)
	{
		cout << primes[i] << " ";
	}
	cout << endl;
	// c
	int primesC[5] = { 2, 3, 5, 7, 11 };
	for (int j = 0; j < 5; j++)
	{
		primesC[j]++;
	}
	cout << "c) Primes: ";
	for (int i = 0; i < 5; i++)
	{
		cout << primesC[i] << " ";
	}
	cout << endl;
	string options[2] = { "Yes", "No" };
	cout << "d) String array: " << options[0] << " " << options[1] << endl;
	cout << "e) Primes: ";
	for (int i = 4; i >= 0; i--)
	{
		cout << primesC[i] << " ";
	}
	cout << endl;
	system("pause");
	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog