Hi. I need help with trying to figure out using this array across different functions for a class. Here is my task:
"Implement the class illustrated below, and a main function that demonstrates it
working. You may need to look up random number generation if you can't remember it
from coursework assignment 1. Implement each of the functions listed.
Tips:
•The constructor should set the size to 0 and initialise the list pointer to NULL
• The destructor should deallocate any memory allocated for the list of numbers
•The generate function should create a new array of integers of size n, and fill it
with random numbers between 0 and 100"
I have two private class members of int pointer list and int size. With what I have so far, I have managed to get the generate function to fill the array randomly but when I have the average, it is giving a very unexpected result. I would really appreciate some help with this. Thank you!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
#include "stdafx.h"
#include "RandomArray.h"
#include <stdlib.h>
#include <iostream>
RandomArray::RandomArray()
{
size = 0;
list = nullptr;
}
void RandomArray::generate(int n)
{
int* Array = new int[n];
for (int i = 0; i < n; i++)
{
Array[n] = rand() % 101;
std::cout << Array[n] << std::endl;
}
size = n;
list = Array;
}
void RandomArray::print()
{
}
void RandomArray::getAverage()
{
int sum = 0;
for (int i = 0; i < size; i++)
{
sum += list[i];
}
sum / size;
std::cout << sum << std::endl;
}
void RandomArray::getMin()
{
int current;
int min;
min = list[1];
for (int i = 0; i < size; i++)
{
current = list[i];
if (current < min)
{
min = current;
}
}
}
void RandomArray::getMax()
{
}
RandomArray::~RandomArray()
{
}
|