----------------------------------------------------------------------------------------
main.cpp
----------------------------------------------------------------------------------------
#include "array.h"
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main() {
Array mArrayObj;
// display 20 random numbers
mArrayObj.display_num();
// sort 20 random numbers in
// asending order
mArrayObj.asending_sort_array();
cout << mArrayObj.get_min(); // get minimun number
cout << " " << mArrayObj.get_max() << endl; // get maximum number
cout << mArrayObj.get_mean();
cout << " " << mArrayObj.get_variance() << endl;
return 0;
}
----------------------------------------------------------------------------------------
array.h
----------------------------------------------------------------------------------------
#ifndef Array_H
#define Array_H
class Array {
private:
unsigned int random_num[20];
int get_array_length();
public:
Array();
void display_num();
int get_max();
int get_min();
float get_variance();
float get_mean();
void asending_sort_array();
};
#endif
----------------------------------------------------------------------------------------
array.cpp
----------------------------------------------------------------------------------------
#include "array.h"
#include <stdlib.h> /* srand, rand nummbers*/
#include <time.h>
#include <iostream>
#include <algorithm>
using std::cin;
using std::cout;
using std::endl;
Array::Array() {
srand(time(NULL));
// initialises all 20 numbers of array
for (int i = 0; i < get_array_length(); i++) {
random_num[i] = rand() % 100 + 0; // 0 to 99 random number
}
}
void Array::display_num() {
for (int i = 0; i < get_array_length(); i++) {
cout << random_num[i] << " ";
}
cout << endl;
}
int Array::get_max() {
return random_num[get_array_length() - 1];
}
int Array::get_min() {
return random_num[0];
}
float Array::get_variance() {
float square_value = 0, variance = 0;
for (int i = 0; i < get_array_length(); i++) {
square_value += (random_num[i]-get_mean())*(random_num[i]-get_mean());
}
variance = square_value / get_array_length();
return variance;
}
float Array::get_mean() {
float average = 0, sum = 0;
for (int i = 0; i < get_array_length(); i++) {
sum = sum + random_num[i];
}
average = sum / get_array_length();
return average;
}
int Array::get_array_length() {
int length = (sizeof(random_num) / sizeof(random_num[0]));
return length;
}
void Array::asending_sort_array() {
std::sort(random_num, random_num + get_array_length());
}
main.cpp
----------------------------------------------------------------------------------------
#include "array.h"
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main() {
Array mArrayObj;
// display 20 random numbers
mArrayObj.display_num();
// sort 20 random numbers in
// asending order
mArrayObj.asending_sort_array();
cout << mArrayObj.get_min(); // get minimun number
cout << " " << mArrayObj.get_max() << endl; // get maximum number
cout << mArrayObj.get_mean();
cout << " " << mArrayObj.get_variance() << endl;
return 0;
}
----------------------------------------------------------------------------------------
array.h
----------------------------------------------------------------------------------------
#ifndef Array_H
#define Array_H
class Array {
private:
unsigned int random_num[20];
int get_array_length();
public:
Array();
void display_num();
int get_max();
int get_min();
float get_variance();
float get_mean();
void asending_sort_array();
};
#endif
----------------------------------------------------------------------------------------
array.cpp
----------------------------------------------------------------------------------------
#include "array.h"
#include <stdlib.h> /* srand, rand nummbers*/
#include <time.h>
#include <iostream>
#include <algorithm>
using std::cin;
using std::cout;
using std::endl;
Array::Array() {
srand(time(NULL));
// initialises all 20 numbers of array
for (int i = 0; i < get_array_length(); i++) {
random_num[i] = rand() % 100 + 0; // 0 to 99 random number
}
}
void Array::display_num() {
for (int i = 0; i < get_array_length(); i++) {
cout << random_num[i] << " ";
}
cout << endl;
}
int Array::get_max() {
return random_num[get_array_length() - 1];
}
int Array::get_min() {
return random_num[0];
}
float Array::get_variance() {
float square_value = 0, variance = 0;
for (int i = 0; i < get_array_length(); i++) {
square_value += (random_num[i]-get_mean())*(random_num[i]-get_mean());
}
variance = square_value / get_array_length();
return variance;
}
float Array::get_mean() {
float average = 0, sum = 0;
for (int i = 0; i < get_array_length(); i++) {
sum = sum + random_num[i];
}
average = sum / get_array_length();
return average;
}
int Array::get_array_length() {
int length = (sizeof(random_num) / sizeof(random_num[0]));
return length;
}
void Array::asending_sort_array() {
std::sort(random_num, random_num + get_array_length());
}
No comments:
Post a Comment