I have what I assume is a very stupid/simple question regarding arrays. Yes, this is a homework assignment for a second semester programming class (1st semester was in Java, this one is C++).
We're supposed to create an array buffer class which will have 7 methods: copyFromArray(), print(), sum(), mean(), max(), min() and range(). The buffer class should also have three properties: a constant named 'BUFFER_SIZE', an array of ints called 'buffer' and a variable named 'length' for the amount of data in the buffer. Carrying out all of those methods is easy enough, no problem with those.
In our professor's documentation for the assignment, only the copyFromArray() method accepts arguments which are the array created in main() and it's length, none of the other methods accept any arguments. So, what I don't understand is how are any of those other methods supposed to get the data from the array to actually do any of the operations? I did a simple print() function that just loops through the elements of the array and prints them out to the console but if that loop isn't in the copyFromArray() function, it just prints random/garbage numbers, obviously because none of the elements in the array are actually initialized.
I suspect I'm missing something in the constructor for the 'buffer' class but I'm not sure what it is and I've scoured our book, the web and this forum for a clue but haven't found one yet. I've also emailed our professor but haven't heard back from him yet so I'm reaching out to all of you. Like most people here, I'm not looking for someone to write the code for me, I'm just looking for a hint so I can get over this hump.
This is main.cpp (from the professor's document, he didn't provide anything else)
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
|
#include <iostream>
#include "DataBuffer.h"
using std::cout;
using std::endl;
using std::string;
void testDataBuffer(int arr[], int length);
int main()
{
const int ARR0_LEN = 10;
int arr0[ARR0_LEN] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
testDataBuffer(arr0, ARR0_LEN);
return 0;
}
void testDataBuffer(int arr[], int length)
{
DataBuffer data;
data.copyFromArray(arr, length); // this works
data.print(arr, length); // this works
data.printBuffer(); // this outputs garbage
}
|
This is DataBuffer.h
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
|
#ifndef DATABUFFER_H
#define DATABUFFER_H
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;
using std::to_string;
class DataBuffer
{
public:
DataBuffer();
void copyFromArray(int arr[], int length);
void print(int arr[], int length);
void printBuffer(); // my testing function
int sum();
double mean();
int max();
int min();
int range();
private:
static const int BUFFER_SIZE = 256;
int buffer[];
int length;
int intSum;
};
#endif // DATABUFFER_H
|
And this is the implementation file DataBuffer.cpp
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
|
#include "DataBuffer.h"
DataBuffer::DataBuffer()
{
}
void DataBuffer::copyFromArray(int arr[], int length) // This works as expected
{
this->length = length;
int buffer[length];
for (int i = 0; i < length; i++)
{
buffer[i] = arr[i];
cout << buffer[i] << endl;
}
}
void DataBuffer::print(int arr[], int length) // This works as expected
{
this->length = length;
cout << "Length is: " << length << endl;
cout << "{" << arr[0];
for (int i = 1; i < length ; i++)
{
cout << ", " << arr[i];
}
cout << "}" << endl;
}
void DataBuffer::printBuffer() // This prints random ints, array elements not initialized
{
for (int i = 0; i < 10; i++)
{
cout << buffer[i] << endl;
}
}
int DataBuffer::sum()
{
intSum = 0;
for (int i = 0; i < length; i++)
{
intSum += arr[i];
}
return intSum;
}
double DataBuffer::mean()
{
double dblMean = 0;
return dblMean;
}
int DataBuffer::max()
{
int intMax = 0;
return intMax;
}
int DataBuffer::min()
{
int intMin = 0;
return intMin;
}
int DataBuffer::range()
{
int intRange = 0;
return intRange;
}
|
Again, I understand why the printBuffer() function prints garbage, I just don't understand how to make it get the correct buffer[] values since it doesn't accept any arguments. Is it even possible? Is it possible our professor's notes are purposely misleading so as to make us go "Hey, we need to pass arguments to this stuff in here"?
Thank you!