C++ buffer manipulation - c++

My professor gave us an example main\source.cpp file and wants us to create the necessary class\header file to make it work. He's the no example code vague "helpful hints" type so I'm lost.
I know this is a long post any input anyone may be willing to help me with at all would be a huge help.
Source.cpp he provided;
#include <iostream> // std::cout, std::endl
#include <iomanip> // std::setw
#include <string>
#include "DataBuffer.h"
using namespace std;
void testDataBuffer(int arr[], int length);
int main() {
const int ARR0_LEN = 2;
int arr0[ARR0_LEN] = { -2,-1 };
const int ARR1_LEN = 10;
int arr1[ARR1_LEN] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
const int ARR2_LEN = 25; int arr2[ARR2_LEN] = { 2, 4, 6, 8, 10, 12, 14, 16, 7, 6, 22, 8, 9, 16, 5, 2, 7, 8, 12, 2, 0, 14, 17, 19, 22 };
testDataBuffer(arr0, ARR0_LEN);
testDataBuffer(arr1, ARR1_LEN);
testDataBuffer(arr2, ARR2_LEN);
//hold console open
std::cin.get();
return 0;
}
void testDataBuffer(int arr[], int length) {
DataBuffer buf; buf.copyData(arr, length);
buf.print(); cout << "Sum " << buf.sum() << endl;
cout << "Min " << buf.minValue() << endl;
cout << "Max " << buf.maxValue() << endl;
cout << "Range " << buf.range() << endl;
cout << "Mean " << buf.mean() << endl;
}
My DataBuff.h
#include <iostream> // std::cout, std::endl
#include <iomanip> // std::setw
#include <string>
#pragma once
class DataBuffer {
static const int BUFFER_SIZE = 256;
int buffer[BUFFER_SIZE];
static const int length = 5;
int arr[length] = { 1,2,3,4,5 };
public:
bool copyData(int intArray, int length);
string print();
double mean(int sum);
int sum();
int maxValue();
int minValue();
int range(int small, int large);
};
My DataBuffer.cpp
#include <iostream> // std::cout, std::endl
#include <iomanip> // std::setw
#include <string>
#include <array> // .sizeof()
#include "DataBuffer.h"
using namespace std;
bool DataBuffer::copyData(int arr, int length) {
for (int i = 0; i < length; i++) {
arr = buffer[i];
}
cout << "Length of buffer is " << length;
if (sizeof(buffer) < length) {
return false;
}
else {
return true;
}
}
string DataBuffer::print() {
if (length <= 0) {
cout << "{}" << endl;
}
else {
cout << buffer[0];
for (int i = 1; i < length; i++) {
cout << setw(10) << buffer[i];
cout << endl;
}
}
}
int DataBuffer::sum()
{
int i, sum = 0;
for (i = 0; i < length; i++)
{
sum += buffer[i];
}
return sum;
}
double DataBuffer::mean(int sum) {
double mean = sum / length;
return mean;
}
int DataBuffer::maxValue() {
int i = 0;
int large = buffer[0];
for (i = 1; i < length; i++)
{
if (buffer[i] > large)
large = buffer[i];
}
return large;
}
int DataBuffer::minValue() {
int i;
int small = buffer[0];
for(i=1; i < length; i++)
{
if(buffer[i] < small)
small = buffer[i];
}
return small;
}
int DataBuffer::range(int small, int large)
{
int range = large - small;
return range;
}
Main errors I'm stuck on;
The professor states that range and mean should accept parameters, so why doesn't the method call in his cpp have parameters? I'm getting errors stating range doesn't take 0 arguments
Error C3646 'print': unknown override specifier AustinNorrisBuffer c:\users\austinn\downloads\austinnorrisbuffer\databuffer.h 19
Error C2039 'print': is not a member of 'DataBuffer' AustinNorrisBuffer c:\users\austinn\downloads\austinnorrisbuffer\source.cpp 29
Again long post I know, any help appreciated!

I suggest the following approach (so that you learn what your professor had in mind and keep your sanity along the way):
Comment everything out.
Uncomment selected pieces of code.
Use a search engine to research those specific errors. Fix the code/header and proceed.
If you hit a very specific issue and need help then you can post a well formed question here and you will get a specific answer quickly.
Good luck.
Here is what I had to comment out to get it to compile at least:
DataBuff.h
#include <iostream> // std::cout, std::endl
#include <iomanip> // std::setw
#include <string>
#pragma once
class DataBuffer {
static const int BUFFER_SIZE = 256;
int buffer[BUFFER_SIZE];
static const int length = 5;
int arr[length] ={1,2,3,4,5};
public:
bool copyData(int intArray,int length);
//string print();
double mean(int sum);
int sum();
int maxValue();
int minValue();
int range(int small,int large);
};
DataBuffer.cpp
#include <iostream> // std::cout, std::endl
#include <iomanip> // std::setw
#include <string>
#include <array> // .sizeof()
#include "DataBuff.h"
using namespace std;
bool DataBuffer::copyData(int arr,int length)
{
for (int i = 0; i < length; i++) {
arr = buffer[i];
}
cout << "Length of buffer is " << length;
if (sizeof(buffer) < length) {
return false;
}
else {
return true;
}
}
//string DataBuffer::print()
//{
//if (length <= 0) {
// cout << "{}" << endl;
//}
//else {
// //cout << buffer[0];
// for (int i = 1; i < length; i++) {
// //cout << setw(10) << buffer[i];
// cout << endl;
// }
//}
//}
int DataBuffer::sum()
{
int i,sum = 0;
for (i = 0; i < length; i++) {
sum += buffer[i];
}
return sum;
}
double DataBuffer::mean(int sum)
{
double mean = sum / length;
return mean;
}
int DataBuffer::maxValue()
{
int i = 0;
int large = buffer[0];
for (i = 1; i < length; i++) {
if (buffer[i] > large)
large = buffer[i];
}
return large;
}
int DataBuffer::minValue()
{
int i;
int small = buffer[0];
for (i=1; i < length; i++) {
if (buffer[i] < small)
small = buffer[i];
}
return small;
}
int DataBuffer::range(int small,int large)
{
int range = large - small;
return range;
}
Source.cpp
#include <iostream> // std::cout, std::endl
#include <iomanip> // std::setw
#include <string>
#include "DataBuff.h"
using namespace std;
void testDataBuffer(int arr[],int length);
int main()
{
const int ARR0_LEN = 2;
int arr0[ARR0_LEN] ={-2,-1};
const int ARR1_LEN = 10;
int arr1[ARR1_LEN] ={1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
const int ARR2_LEN = 25; int arr2[ARR2_LEN] ={2, 4, 6, 8, 10, 12, 14, 16, 7, 6, 22, 8, 9, 16, 5, 2, 7, 8, 12, 2, 0, 14, 17, 19, 22};
testDataBuffer(arr0,ARR0_LEN);
testDataBuffer(arr1,ARR1_LEN);
testDataBuffer(arr2,ARR2_LEN);
//hold console open
std::cin.get();
return 0;
}
void testDataBuffer(int arr[],int length)
{
DataBuffer buf;
//buf.copyData(arr,length);
//buf.print(); cout << "Sum " << buf.sum() << endl;
//cout << "Min " << buf.minValue() << endl;
//cout << "Max " << buf.maxValue() << endl;
//cout << "Range " << buf.range() << endl;
//cout << "Mean " << buf.mean() << endl;
}

Related

error: prototype for ' ' does not match any in class ' ';

I recently started coding in c++ and was doing a project. Currently I'm using multiple files, one as a header, one for cpp code, and one as a main file. I've been stuck on a problem for a while now and I just cant figure out whats wrong. Whenever I compile the cpp file I get the error prototype for ‘GradeCalculator::GradeCalculator(const int*, const int&)’ does not match any in class ‘GradeCalculator’. I would really love to get some help and explanation on whats going wrong, here's my code:
//main file:
#include <iostream>
#include "GradeCalculator.h"
#define NUM_ASSIGNMENTS 100
int main() {
int points[NUM_ASSIGNMENTS];
int newsize = 0;
GradeCalculator gradecalculator(points, newsize);
std::cout << "Grade Calculator\n" << std::endl;
std::cout << "================\n" << std::endl;
std::cout << "This program will calculate your grade average on your assignments.\n" << std::endl;
std::cout << "Your lowest grade will be dropped.\n" << std::endl;
newsize = gradecalculator.addPoints(points);
gradecalculator.printResults(points, newsize);
return 0;
}
//header file:
#ifndef DEMO_GRADECALC_H
#define DEMO_GRADECALC_H
#include <iostream>
class GradeCalculator{
private:
int points[100];
int size;
public:
GradeCalculator(int* points, int size);
int addPoints(int*points);
int dropLowest(int* points, int size);
explicit GradeCalculator(const int* points, const int size);
void printResults(int* points, int size);
virtual ~GradeCalculator();
};
#endif
//cpp file:
#include <iostream>
#include "GradeCalculator.h"
GradeCalculator::GradeCalculator(const int* points, const int &size){
}
int GradeCalculator::addPoints(int* points){
int sizeofarray = 0;
for( int j = 0; points[j] = '\0'; j++){
std::cin >> points[j];
sizeofarray ++;
}
return sizeofarray;
}
int GradeCalculator::dropLowest(int* points, int size) {
int lowest = points[0];
int lowestIndex = 0;
for( int i = 0; i < size; i++ ) {
if( points[i] < lowest ) {
lowest = points[i];
lowestIndex = i;
}
}
for( int i = lowestIndex; i < size-1; i++ ) {
points[i] = points[i+1];
}
return size - 1;
}
void GradeCalculator::printResults(int* points, int size){
dropLowest(points, size);
int newtotal;
for(int k = 0; points[k] = '\0'; k++){
newtotal += points[k];
}
float avg = newtotal/size;
std::cout << "Total Points: " << newtotal << "\n" << std::endl;
std::cout << "Average: " << avg << "\n" << std::endl;
}

how to see number movements in c++ boolean?

I have a small program to sort random numbers into the right sequence number. Well, I want to see every random number move. what should be added to it?
#include <iostream>
using namespace std;
void pindah (int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
void tukar (int arr[], int n) {
int i, j;
bool trade;
for (i=0; i < n-1; i++) {
trade = false;
for (j=0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
pindah(&arr[j], &arr[j+1]);
trade = true;
}
}
if (trade == false)
break;
}
}
void cetakArray (int arr[], int n) {
int i;
for (i=0; i<n; i++) {
cout << arr[i] << " "; //tampil data
}
}
int main () {
int arr[] = {4, 6, 7, 2, 1, 5, 8, 10, 9, 3};
int n = sizeof(arr) / sizeof(int); //menghitung jumlah data array
cout << "setelah diurutkan : \n";
tukar (arr, n);
cetakArray (arr, n);
}
please help me
Assuming that by "into the right sequence" you mean from small to large, and assuming that you are willing to use C++11, we can easily obtain this with a lambda function. See the following example.
#include <vector>
#include <algorithm>
std::vector<int> values({4, 6, 7, 2, 1, 5, 8, 10, 9, 3});
int number_of_moves = 0;
std::sort(begin(values), end(values), [&](int lhs, int rhs)
{
if (lhs > rhs)
return false;
++number_of_moves;
std::cout << "I am going to swap " << lhs << " with " << rhs << '\n';
return true;
});
std::cout << "I took me " << number_of_moves << " move(s) to sort the vector\n";
Note: it is not clear to me what you mean by "movements in c++ boolean", so I chose to print the numbers that are going to be swapped.
EDIT: based on the comments I guess you want to count the number of movements, so I have added a counter. Note that a bool can only be true/false and cannot be used for counting.

How do i link the integer size of the function with the actual size of an array? C++

I'm trying to write a function that calculates the sum of an array, but when i declare int size = 0; , the function runs 0 times because i=0 ; i
int arraChec(int arra[]) {
int size = 0;
int sum = 0;
for (int i = 0; i < size; i++) {
sum = sum + arra[i];
}
return sum;
}
int main() {
int arra1[7] = { 2,3,5,7,8,9,1 };
cout << arraChec(arra1) << endl;
system("pause");
}
Pass in the array size as a parameter:
#include <iostream>
int arraChec(int arra[], int size) {
int sum = 0;
for (int i = 0; i < size; i++) {
sum += arra[i];
}
return sum;
}
int main() {
int arra1[7] = { 2, 3, 5, 7, 8, 9, 1 };
std::cout << arraChec(arra1, 7) << std::endl;
}
Or use std::vector:
#include <iostream>
#include <vector>
int arraChec(std::vector<int>& arra) {
int sum = 0;
for (int i = 0; i < arra.size(); i++) {
sum += arra[i];
}
return sum;
}
int main() {
std::vector<int> arra1 = { 2, 3, 5, 7, 8, 9, 1 };
std::cout << arraChec(arra1) << std::endl;
}
If you are referring to some C style (sizeof(arra) / sizeof(*arra)) construct I suggest you refrain from using it.
You need to pass two arguments to the function--either the beginning of the array plus the size, or the beginning and (one past the) end, as is conventional in C++:
int arraChec(int* begin, int* end) {
int sum = 0;
for (int* it = begin; it < end; ++it) {
sum += *it;
}
return sum;
}
int main() {
int arra1[7] = { 2,3,5,7,8,9,1 };
cout << arraChec(std::begin(arra1), std::end(arra1)) << endl;
system("pause");
}
Of course, you can implement is using the standard library:
cout << std::accumulate(std::begin(arra1), std::end(arra1), 0) << endl;
Use std::array instead of fixed size C-style array.
#include <iostream>
#include <array>
#include <numeric>
using namespace std;
int main() {
array<int, 7> arr = { 2, 3, 5, 7, 8, 9, 1 };
cout << accumulate(arr.begin(), arr.end(), 0) << endl;
return 0;
}
Output
35
Read more about std::accumulate.
Another way not mentioned yet is:
template<size_t N>
int arraChec(int (&arra)[N]) {
int sum = 0;
for (size_t i = 0; i < N; i++) {
sum = sum + arra[i];
}
return sum;
}

how to call the function in array?

I'm trying to call the function
int Even( int a[][MAX_COLUMNS], int length, int width)
inside the main to print the output to the user.
After running the program, i got the following error
`int Even(int,int,int) connot convert argument 1 from int [3][2] to int
Here's the code,
Thank you for taking the time to help me.
#include <iostream>
#define MAX_ROWS 3
#define MAX_COLUMNS 2
using namespace std;
int Even(int A, int length, int width);
int main(){
int A[MAX_ROWS][MAX_COLUMNS] = { { 3, 2 }, { 4, 5 }, { 2, 2 } };
cout << "There are " << Even(A,MAX_ROWS,MAX_COLUMNS) << " even numbers in the matrix." << endl;
cout << "Goodbye :-)" << endl;
system("PAUSE");
return 0;
}
int Even( int a[][MAX_COLUMNS], int length, int width){
int sum = 0;
int i;
int j;
for (i = 0; i < length, i++;){
for (j = 0; j < width, j++;){
if (a[i][j] % 2 == 0){
sum++;
}
}
}
return sum;
}
You should alwais avoid forward declaration (except special cases). At your Q it leaded you to error. Full code:
#include <iostream>
#define MAX_ROWS 3
#define MAX_COLUMNS 2
using namespace std;
int Even( int a[][MAX_COLUMNS], int length, int width){
int sum = 0;
int i;
int j;
for (i = 0; i < length, i++;){
for (j = 0; j < width, j++;){
if (a[i][j] % 2 == 0){
sum++;
}
}
}
return sum;
}
int main(){
int A[MAX_ROWS][MAX_COLUMNS] = { { 3, 2 }, { 4, 5 }, { 2, 2 } };
cout << "There are " << Even(A,MAX_ROWS,MAX_COLUMNS) << " even numbers in the matrix." << endl;
cout << "Goodbye :-)" << endl;
system("PAUSE");
return 0;
}

Converting 1D to 2D?

This might seem like a stupid question but it's really bugging me.
Basically, I have a 1D array that I need to convert into a 2D array. Basically, the size of the array is: 62017 now from this, I need to get the Rows and Cols of this. But, do this dynamically, so for example, it would take the number of say: 43101 and then establish the rows+cols and then re-size the vector accordingly.
I hope I've explained enough and hope someone can help, thanks :)
And here it is old school array style (since I can't access a C++0x compiler at the moment)
#include <iostream>
//#include <array>
#include <inttypes.h>
#include <math.h>
void calc_new_sizes(const size_t old_size, size_t& new_size1, size_t& new_size2)
{
new_size1 = 1;
new_size2 = 1;
size_t stop_at = (size_t)sqrt(old_size) + 1;
for (size_t i = 1; i<stop_at; i++)
{
if ( old_size % i == 0 )
{
new_size1 = i;
new_size2 = old_size / i;
}
}
}
template <class T>
T** twoDimensionify(T* p_old_array, const size_t old_size)
{
size_t new_size1=1, new_size2=1, old_i=0;
calc_new_sizes(old_size, new_size1, new_size2);
T** returnValue = new T*[new_size1];
for (size_t i=0; i<new_size1; i++)
{
returnValue[i] = new T[new_size2];
for (size_t j=0; j<new_size2; j++)
{
returnValue[i][j] = p_old_array[old_i];
old_i++;
}
}
return returnValue;
}
int main()
{
size_t old_size=20, new_size1=0, new_size2=0;
calc_new_sizes(old_size, new_size1, new_size2);
std::cout << "From " << old_size << " to " << new_size1 << "x" << new_size2 << std::endl;
int old_array[20] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
int **new_array = twoDimensionify<int>(old_array, 20);
for (size_t i=0; i<new_size1; i++)
{
for (size_t j=0; j<new_size2; j++)
{
std::cout << "new array[" << i << "," << j << "] = " << new_array[i][j] << std::endl;
}
}
// Clean up my memory. This is C++ afterall.
for (size_t i=0; i<new_size1; i++)
{
delete [](new_array[i]);
}
delete []new_array;
return 0;
}
I believe you're looking for something like this. Though with some challenges accessing a proper compiler I can't verify...
#include <iostream>
#include <array>
#include <inttypes.h>
#include <math.h>
void calc_new_sizes(const size_t old_size, size_t& new_size1, size_t& new_size2)
{
new_size1 = 1;
new_size2 = 1;
size_t stop_at = (size_t)sqrt(old_size) + 1;
for (size_t i = 1; i<stop_at; i++)
{
if ( old_size % i == 0 )
{
new_size1 = i;
new_size2 = old_size / i;
}
}
}
template <class T, size_t new_size_1, size_t new_size_2, size_t old_size>
std::array<new_size_1, std::array<T, new_size_2>> twoDimensionify(std::array<T, old_size> p_input)
{
std::array<new_size_1, std::array<T, new_size_2>> returnValue;
int old_i = 0;
for (int i=0; i<new_size_1; i++)
{
for (int j=0; j<new_size_2; j++)
{
returnValue[i][j] = p_input[old_i];
old_i++;
}
}
return returnValue;
}
int main()
{
size_t old_size=20, new_size1=0, new_size2=0;
calc_new_sizes(old_size, new_size1, new_size2);
std::cout << "From " << old_size << " to " << new_size1 << "x" << new_size2 << std::endl;
return 0;
}