Question: Please I need help wiht this assignment. Using this struct definition: struct number_properties { int number; bool prime, perfect, square, triangular; }; Create an array

Please I need help wiht this assignment.

Using this struct definition:

struct number_properties

{

int number;

bool prime, perfect, square, triangular;

};

Create an array of 100,001 of these structs and set the appropriate values (we create 100,001 because

we want to go up to 100,000 and we dont care about 0). The design of the program is left to you, but you

should probably write some functions!

For instance, if we create

?

number_properties np[100001];

?

then the values for

?

np[6]

?

will be set as

follows:

np[6].number = 6

np[6].prime = false

np[6].perfect = true

np[6].square = false

np[6].triangular = true

Following the population of the array, output the count of the number of primes, perfect numbers, square

numbers, and triangular numbers for the following ranges (inclusive):

1 to 100000

1 to 10000

1 to 20000

10001 to 20000

1 to 50000

50001 to 100000

For example your output should look something like this (these numbers are not correct, but this is how

your result should be formatted)

Range 1 to 100000:

5000 prime numbers

8 perfect numbers

100 square numbers

85 triangular numbers

Range 1 to 10000:

...

THIS IS MY CODE

#include #include #include

using namespace std;

/* Declaration of struct */ struct number_properties { int number; bool prime, perfect, square, triangular; };

bool isprime(int);/*This function finds prime numbers in the array */ bool isperfect(int);/*This function finds perfect numbers in the array */ bool issquare(int);/*This function finds squared numbers in the array */ bool istriangular(int);/*This function finds triangular numbers in the array */ void display(const number_properties &);/*This function displays results */ number_properties different_values(number_properties);/*This function changes the parameters in the array */

int main() { number_properties np; // declaring np to identify struct np.number[]; // declaring number in the struct is an array

/*This function counts the number of prime numbers within the parameter of the array */ bool prime[np.number];

/*On the following function if changing np.number by an actual number prints the correct amount of prime numbers within parameters*/ for (int i = 0; i < np.number; i++) prime[i] = isprime(i);

int prime_count = 0;

for (int i = 0; i < np.number; i++) if (prime[i]) prime_count++;

bool perfect[np.number];

/*On the following funciton even when changing np.number by an actual number amount of perfect numbers is incorrect*/

for (int i = 0; i < np.number; i++) perfect[i] = isperfect(i);

int perfect_count = 0;

for (int i = 0; i < np.number; i++) if (perfect[i]) perfect_count++;

bool square[np.number];

/*On the following function even when changing np.number by an actual number amount of square numbers is incorrect*/

for (int i = 0; i < np.number; i++) square[i] = issquare(i);

int square_count = 0;

for (int i = 0; i < np.number; i++) if (square[i]) square_count++;

bool triangular[np.number];

/*On the following function even when changing np.number by an actual number amount of triangular numbers is incorrect*/

for (int i = 0; i < np.number; i++) triangular[i] = istriangular(i);

int triangular_count = 0;

for (int i = 0; i < np.number; i++) if (triangular[i]) triangular_count++;

display(np); // displays struct. cout << endl;

display(different_values(np));//repeats display structs with new parameters. cout << endl;

return 0; }

bool isprime( int n) { if (n == 2) return true;

if (n < 2 || n % 2 == 0) return false;

for (int i = 3; i <= sqrt(n); i += 2) if ( n % i == 0) return false;

return true;

}

bool isperfect( int n) { int sum = 0;

for ( int i = 1; i < n; i++) { if (n % i == 0) sum += i;

if (sum == n) return true; else return false;

} } bool issquare(int n) { int sum = 0;

for (int i = 1; i < n; i++) { if ( n / i == i) { return true; sum += i; } else return false; }

} bool istriangular( int n) { int sum = 0;

for ( int i = 1; i < n; i++) { sum += i*(i+1)/2;

if ( sum == i) return true; }

return false; } void display(const number_properties &np) {

cout << "Range " << np.number[0] << " to " << np.number[1] << " :" << endl; cout << left << setw(6) << np.prime << " prime numbers" << endl; cout << left << setw(6) << np.perfect << " perfect numbers" << endl; cout << left << setw(6) << np.square << " square numbers" << endl; cout << left << setw(6) << np.triangular << " triangular numbers" << endl;

}

number_properties different_values(number_properties np) {

np.number[100001]; np.number[10000]; np.number[20000]; np.number[2]={10001, 20000}; np.number[50000]; np.number[2] = {50001,100000}; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!