Answer to Question #5721 in C++ for Naveed Qurban

Question #5721
Perform the following tasks.


a) Make an array of size 10.


b) Put random data in it.


c) Find Minimum


d) Find Maximum


e) Find Average


f) Find Standard-deviation


g) Find even values in array.


h) Find odd values in array.


i) Find prime number in array


j) Find co-prime number in array


k) Find values greater than k, where k is entered by the user.
1
Expert's answer
2011-12-22T08:45:59-0500
#include <iostream>
#include <cstdlib>
#include
<cmath>
using namespace std;

int main(int argc, char*
argv[]){
int const ARRAY_SIZE = 10;
int data[ARRAY_SIZE];

int min,max,k;
double avg = 0, std = 0;
cout << "Created
data: " << endl;
for(int i = 0; i < ARRAY_SIZE; ++i){

data[i] = (int)rand(); // random data
cout << data[i] <<
' ';
avg += data[i]; // sum of all elements
}
avg /=
ARRAY_SIZE; // average
cout << endl << "Average: "
<< avg << endl;
min = data[0];
max = data[0];

for (int i = 1; i < ARRAY_SIZE; ++i){
if (data[i] > max) max =
data[i]; // maximum
if (data[i] < min) min = data[i]; //
minimum
}
cout << "Maximum: " << max <<
endl;
cout << "Minimum: " << min << endl;
//
standart deviation calculation begin
for (int i = 0; i < ARRAY_SIZE;
++i){
std += pow((data[i] - avg),2.0);
}
std /=
(ARRAY_SIZE - 1);
std = sqrt(std);
// end
cout <<
"Standart devation: " << std << endl;

// even
numbers
cout << "Even numbers: ";
for (int i = 0; i <
ARRAY_SIZE; ++i){
if ((data[i] % 2) == 0) cout << data[i]
<< ' ';
}
// end

// odd numbers
cout
<< endl << "Odd numbers: ";
for (int i = 0; i <
ARRAY_SIZE; ++i){
if ((data[i] % 2) != 0) cout << data[i]
<< ' ';
}
// ned

// prime numbers
cout
<< endl << "Prime numbers: ";
for (int i = 0; i <
ARRAY_SIZE; ++i){
int j;
for(j = 2; j <= (data[i] - 1);
++j){
if((data[i] % j) == 0) break;
}
if
(j == data[i]) cout << data[i];
}
// end

//
co-prime numbers
cout << endl << "Co-prime numbers: ";

for (int i = 0; i < ARRAY_SIZE - 1; ++i){
int a = data[i];

int b = data[i+1];
int res = 0;
while(a != b){

if (a > b) a -= b;
else b -= a;
}

if(a == 1) cout << '(' << data[i] << ", " <<
data[i+1] << ") ";
}
// end

// greater then
k
cout << endl << "Please, enter k: ";
cin >>
k;
cout << "Numbers greater then k: ";
for (int i = 0; i
< ARRAY_SIZE; ++i){
if(data[i] > k) cout << data[i]
<< ' ';
}
// end

cout << "\n\n\nEnter any
number to exit : ";
cin >> k;

}

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
APPROVED BY CLIENTS