My program is supposed to take the names and 5 marks each for 10 students and output their corresponding average marks and grade. My functions avgmarks and avg_grade are supposed to calculate and return these, as arrays, to the main function. Using the statements return avg[10] and return gr[10] does not work. I know pointer variables can be used here, but i don't know how to implement them to make my program work correctly.
#include <iostream>
#include <string>
using namespace std;
void readnames(string p[]);
int readmarks(int p[10][5]);
int avgmarks(int p[10][5]);
char avg_grade(int p[10]);
void displayresults(string n[], int m[], char g[]);
int main()
{
string names[10];
readnames(names);
int marks [10][5];
readmarks(marks);
int avg_marks[10];
avg_marks[10]=avgmarks(marks);
char grade[10];
grade[10]=avg_grade(avg_marks);
displayresults(names, avg_marks, grade);
}
void readnames(string p[])
{
for (int i=0;i<10;i++)
{
cout<<"Enter student "<<i+1<<"'s name:\n";
cin>>p[i];
}
}
int readmarks(int p[10][5])
{
for (int i=0;i<10;i++)
{
cout<<"Enter student "<<i+1<<"'s marks:\n";
for (int j=0;j<5;j++)
{
cin>>p[i][j];
}
}
}
int avgmarks(int p[10][5])
{
int avg[10];
int sum;
for (int i=0;i<10;i++)
{
sum=0;
for (int j=0;j<5;j++)
{
sum=sum+p[i][j];
}
avg[i]=(sum/5);
}
return avg[10];
}
char avg_grade(int p[10])
{
char gr[10];
for (int i=0;i<10;i++)
{
if (p[i]>=90)
{
gr[i]='A';
}
if ((p[i]>=80)&&(p[i]<90))
{
gr[i]='B';
}
if ((p[i]>=70)&&(p[i]<80))
{
gr[i]='C';
}
if ((p[i]>=60)&&(p[i]<70))
{
gr[i]='D';
}
if ((p[i]>=50)&&(p[i]<60))
{
gr[i]='E';
}
if ((p[i]>=40)&&(p[i]<50))
{
gr[i]='F';
}
}
return gr[10];
}
void displayresults(string n[], int m[], char g[])
{
float ct;
float sum=0;
cout<<endl;
for (int i=0;i<10;i++)
{
cout<<"Name: "<<n[i]<<" Average Marks: "<<m[i]<<" Grade: "<<g[i];
cout<<endl;
}
for (int i=0;i<10;i++)
{
sum=sum+m[i];
}
ct=sum/10;
cout<<"The class average is "<<ct<<endl;
}
Arrays cannot be returned from functions. It's one good reason to use vectors instead of arrays (there are many more).
But if you want to carry on with arrays you should use pointers to simulate returning an array from your function.
In this example avg is a pointer to an array that will recieve the returned value
void avgmarks(int p[10][5], int* avg)
{
int sum;
for (int i=0;i<10;i++)
{
sum=0;
for (int j=0;j<5;j++)
{
sum=sum+p[i][j];
}
avg[i]=(sum/5);
}
}
You use it like this
int avg_marks[10];
avgmarks(marks, avg_marks);
PS. You seem to have the common (but to me bizarre) newbie misunderstanding that if you have an array of size 10 (say) then array[10] can be used to refer to the whole array. Please don't think that, if you have an array of size 10, then array[10] is just an error, because the valid indexes for an array of size 10 are 0 to 9.
This example changed every c-style array to std::array. Keep in mind, that it is the same code as yours with the arrays, so every programming error is still in there. And also arrays are returned by value, means that they are copied. Try to understand how to use std::array and you will get a better programmer.
#include <iostream>
#include <string>
#include <array>
using namespace std;
void readnames(std::array<string, 10>& p);
void readmarks(std::array<std::array<int,5>, 10>& p);
std::array<int,10> avgmarks(std::array<std::array<int,5>, 10>& p);
std::array<char,10> avg_grade(std::array<int,10>& p);
void displayresults(std::array<string, 10> n, std::array<int,10> m, std::array<char,10> g);
int main()
{
std::array<string, 10> names;
readnames(names);
std::array<std::array<int,5>, 10> marks;
readmarks(marks);
std::array<int,10> avg_marks;
avg_marks = avgmarks(marks);
std::array<char,10> grade;
grade = avg_grade(avg_marks);
displayresults(names, avg_marks, grade);
}
void readnames(std::array<string, 10>& p)
{
for (int i=0;i<10;i++)
{
cout<<"Enter student "<<i+1<<"'s name:\n";
cin>>p[i];
}
}
void readmarks(std::array<std::array<int,5>, 10>& p)
{
for (int i=0;i<10;i++)
{
cout<<"Enter student "<<i+1<<"'s marks:\n";
for (int j=0;j<5;j++)
{
cin>>p[i][j];
}
}
}
std::array<int,10> avgmarks(std::array<std::array<int,5>, 10>& p)
{
std::array<int,10> avg;
int sum;
for (int i=0;i<10;i++)
{
sum=0;
for (int j=0;j<5;j++)
{
sum=sum+p[i][j];
}
avg[i]=(sum/5);
}
return avg;
}
std::array<char,10> avg_grade(std::array<int,10>& p)
{
std::array<char,10> gr;
for (int i=0;i<10;i++)
{
if (p[i]>=90)
{
gr[i]='A';
}
if ((p[i]>=80)&&(p[i]<90))
{
gr[i]='B';
}
if ((p[i]>=70)&&(p[i]<80))
{
gr[i]='C';
}
if ((p[i]>=60)&&(p[i]<70))
{
gr[i]='D';
}
if ((p[i]>=50)&&(p[i]<60))
{
gr[i]='E';
}
if ((p[i]>=40)&&(p[i]<50))
{
gr[i]='F';
}
}
return gr;
}
void displayresults(std::array<string, 10> n, std::array<int,10> m, std::array<char,10> g)
{
float ct;
float sum=0;
cout<<endl;
for (int i=0;i<10;i++)
{
cout<<"Name: "<<n[i]<<" Average Marks: "<<m[i]<<" Grade: "<<g[i];
cout<<endl;
}
for (int i=0;i<10;i++)
{
sum=sum+m[i];
}
ct=sum/10;
cout<<"The class average is "<<ct<<endl;
}
I think this will work:
#include <iostream>
#include <string>
using namespace std;
void readnames(string p[]);
void readmarks(int p[10][5]);
int* avgmarks(int p[10][5]);
char* avg_grade(int p[10]);
void displayresults(string n[], int m[], char g[]);
int main()
{
string names[10];
readnames(names);
int marks [10][5];
readmarks(marks);
int *avg_marks;
avg_marks=avgmarks(marks);
char *grade;
grade=avg_grade(avg_marks);
displayresults(names, avg_marks, grade);
}
void readnames(string p[])
{
for (int i=0;i<10;i++)
{
cout<<"Enter student "<<i+1<<"'s name:\n";
cin>>p[i];
}
}
void readmarks(int p[10][5])
{
for (int i=0;i<10;i++)
{
cout<<"Enter student "<<i+1<<"'s marks:\n";
for (int j=0;j<5;j++)
{
cin>>p[i][j];
}
}
}
int* avgmarks(int p[10][5])
{
static int avg[10];
int sum;
for (int i=0;i<10;i++)
{
sum=0;
for (int j=0;j<5;j++)
{
sum=sum+p[i][j];
}
avg[i]=(sum/5);
}
return avg;
}
char* avg_grade(int *p)
{
static char gr[10];
for (int i=0;i<10;i++)
{
if (p[i]>=90)
{
gr[i]='A';
}
if ((p[i]>=80)&&(p[i]<90))
{
gr[i]='B';
}
if ((p[i]>=70)&&(p[i]<80))
{
gr[i]='C';
}
if ((p[i]>=60)&&(p[i]<70))
{
gr[i]='D';
}
if ((p[i]>=50)&&(p[i]<60))
{
gr[i]='E';
}
if ((p[i]>=40)&&(p[i]<50))
{
gr[i]='F';
}
}
return gr;
}
void displayresults(string n[], int m[], char g[])
{
float ct;
float sum=0;
cout<<endl;
for (int i=0;i<10;i++)
{
cout<<"Name: "<<n[i]<<" Average Marks: "<<m[i]<<" Grade: "<<g[i];
cout<<endl;
}
for (int i=0;i<10;i++)
{
sum=sum+m[i];
}
ct=sum/10;
cout<<"The class average is "<<ct<<endl;
}
Basically, to return an array from a function in C/C++, you declare it as "static", and you make the function return the pointer.
EDIT: As many others have noted, this won't work in multi-threaded programs.
Related
Run the code to see where the problem lies.
[Error] 'd' does not name a type
I tried declaring the variable directly in public but it kept popping up with more errors.
There seems to be apparently no fix, since googling didn't help much in the process.
It'd be super helpful if someone could help me figure out where the problem is.
#include<iostream>
using namespace std;
class array1{
protected:
static int a[50];
public:
int n1;
void getNum1(void)
{
cout<<"how many numbers?"<<endl;
cin>>n1;
for(int i=0;i<n1;i++)
{
cout<<"enter le number"<<endl;
cin>>a[i];
}
}
int* retNum1(void)
{
return a;
}
};
class array2{
protected:
static int b[50];
public:
int n2;
void getNum2(void)
{
cout<<"how many numbers?"<<endl;
cin>>n2;
for(int i=0;i<n2;i++)
{
cout<<"enter le number"<<endl;
cin>>b[i];
}
}
int* retNum2(void)
{
return b;
}
};
class conarray:public array1, public array2{
private:
int c[100],d;
//int d;
public:
d=0;
void disp()
{
for(int i=0;i<5;i++)
{
cin>>c[i];
}
for(int i=0;i<5;i++)
{
cout<<c[i]<<endl;
}
cout<<d;
}
//int d = 0;
/*void merge(void)
{
int *nn1 = retNum1();
int *nn2 = retNum2();
for(int i=0;i<n1;i++)
{
c[d]=nn1[i];
d++;
}
for(int i=0;i<n2;i++)
{
c[d]=nn2[i];
d++;
}
}
void display(void)
{
cout<<"NUMBERS IN ARRAY:"<<endl;
for(int i=0;i<d;i++)
{
cout<<c[i]<<endl;
}
}*/
};
int main()
{
conarray a;
//a.getNum1();
//a.getNum2();
//a.merge();
//a.display();
a.disp();
return 0;
}
I need help... appropriate questions have been asked in the comments. The programs has zero compiler errors and warnings!! I have concerns with calling a member function from another member function using function pointers. (To be precise, setMatrixto() is trying to call setElement() function using function pointer)
Plus somehow the "hello there" is not being printed to the console. I was expecting it to show up as output.Maybe the setMatrixto() is not getting called at all!!
Header File definition
#ifndef MATRIXOPERATIONS_H
#define MATRIXOPERATIONS_H
class MatrixOperations;
typedef int (MatrixOperations::*INTFUNC)(int,int);
typedef void (MatrixOperations::*VOIDFUNC)(int,int,int);
class MatrixOperations
{
public:
MatrixOperations();
MatrixOperations(int size);
~MatrixOperations();
//diagonal matrix funtions
void displayMatrixOf(INTFUNC f);
void setMatrixTo(VOIDFUNC f);
int getElement(INTFUNC from, int i, int j);
void setElement(VOIDFUNC to,int i ,int j, int value);
int fromDiagonalArray(int i, int j);
void toDiagonalArray(int i, int j, int value);
protected:
private:
int size;
int* a;
};
#endif // MATRIXOPERATIONS_H
CPP Implementation File
#include "MatrixOperations.h"
#include <iostream>
using namespace std;
MatrixOperations::MatrixOperations()
{
//ctor
size = 3;
a = new int[size];
}
MatrixOperations::MatrixOperations(int size)
{
//ctor
this->size = size;
a = new int[size];
}
MatrixOperations::~MatrixOperations()
{
//dtor
delete[] a;
}
///////////////////FUCNTION POINTER SECTION///////////////////////////////////
int MatrixOperations::getElement(INTFUNC from, int i, int j)
{
return (this->*from)(i,j);
}
void MatrixOperations::setElement(VOIDFUNC to,int i ,int j, int value)
{
(this->*to)(i,j,value);
}
/////////////////////////////////DIAGONAL ARRAY OPERATIONS/////////////////////////////////////////////////
int MatrixOperations::fromDiagonalArray(int i, int j)
{
if(i==j)
{
return a[i];
}
else
{
return 0;
}
}
void MatrixOperations::toDiagonalArray(int i, int j, int value)
{
a[i] = value;
}
///////////////////////////////////////////////////////////////////
void MatrixOperations::displayMatrixOf(INTFUNC f)
{
for(int i{0}; i < size; i++)
{
for(int j{0}; j < size; j++)
{
cout << getElement(f,i,j) << "\t"; //is this the correct way to send the function pointer?
}
cout << endl;
}
}
void MatrixOperations::setMatrixTo(VOIDFUNC f)
{
cout << "Hello there!!"; //not getting this output.. whats wrong??
for(int i{0}; i < size; i++)
{
int value {};
cout << "Enter value diagonal element " << i << " : ";
cin >> value;
setElement(f,i,i,value); //is this the correct way to send the function pointer?
}
}
///////////////////////////////////////////////////////////////////////////////
Main File
#include <iostream>
#include "MatrixOperations.h"
typedef MatrixOperations MATRIX;
using namespace std;
int main()
{
MATRIX m1;
m1.setMatrixTo(MATRIX::toDiagonalArray); //was expecting a "Hello there!" but i am not getting that output either
return 0;
}
EDIT2: I added all the class definitions and main function in one single file. SURPRISINGLY!! this works . I am confused??!!!
#include <iostream>
using namespace std;
class MatrixOperations;
typedef void (MatrixOperations::*VOIDFUNC)(int,int,int);
typedef MatrixOperations MATRIX;
class MatrixOperations
{
public:
MatrixOperations();
MatrixOperations(int size);
~MatrixOperations();
//diagonal matrix funtions
void setMatrixTo(VOIDFUNC f);
void setElement(VOIDFUNC to,int i ,int j, int value);
void toDiagonalArray(int i, int j, int value);
private:
int size;
int* a;
};
MatrixOperations::MatrixOperations()
{ //ctor
size = 3;
a = new int[size];
}
MatrixOperations::MatrixOperations(int size)
{ //ctor
this->size = size;
a = new int[size];
}
MatrixOperations::~MatrixOperations()
{
//dtor
delete[] a;
}
void MatrixOperations::setElement(VOIDFUNC to,int i ,int j, int value)
{
(this->*to)(i,j,value);
}
/////////////////////////////////DIAGONAL ARRAY OPERATIONS/////////////////////////////////////////////////
void MatrixOperations::toDiagonalArray(int i, int j, int value)
{
a[i] = value;
}
///////////////////////////////////////////////////////////////////
void MatrixOperations::setMatrixTo(VOIDFUNC f)
{
cout << "Hello there!!" << endl;
for(int i{0}; i < size; i++)
{
int value {};
cout << "Enter value diagonal element " << i << " : ";
cin >> value;
setElement(f,i,i,value);
}
}
int main()
{
MATRIX m1;
m1.setMatrixTo(MATRIX::toDiagonalArray);
return 0;
}
There is nothing wrong with the code in both cases. Its just my debugger was not running in admin mode. I got error code 740. So I launched my IDE in admin mode and voila it worked.
Here is my code for quick sort. I am a beginner kindly please help.
#include<iostream>
using namespace std;
class quick
{
private:
int n,left,right,i,j;
float a[55];
public:
void getdata();
void sort(float[],int,int);
void putdata();
};
void quick::getdata()
{
cout<<"Enter how many elements you want to enter:";
cin>>n;
for(int k=0;k<n;k++)
{
cout<<"Enter percentage of students:"<<k+1<<":";
cin>>a[k];
}
left=0;
right=n-1;
}
void quick::putdata()
{
for(int k=0;k<5;k++)
{
cout<<"\nSorted marks are:"<<a[k]<<endl;
}
}
void quick::sort(float a[],int left,int right)
{
if(left<right)
{
int i=left;
int j=right+1;
float pivot=a[left];
do{
do{
i++;
}while((a[i]<pivot)&& left<right);
do{
j--;
}while(a[j]>pivot);
if(i<j)
swap(a[i],a[j]);
}while(i<j);
a[left]=a[j];
a[j]=pivot;
sort(a,left,j-1);
sort(a,j+1,right);
}
}
int main()
{
quick obj;
obj.getdata();
obj.sort(a[],left,right);
obj.putdata();
return (0);
}
It is giving me error in int main() function:
a is not declared in this scope.
expected primary expression before ']'.
As the answer is given by #Shubham Khatri. Here is the corrected code.
#include<iostream>
using namespace std;
class quick
{
public: int n,left,right,i,j;
float a[55];
public:
void getdata();
void sort(float[],int,int);
void putdata();
};
void quick::getdata()
{
cout<<"Enter how many elements you want to enter:";
cin>>n;
for(int k=0;k<n;k++)
{
cout<<"Enter percentage of students:"<<k+1<<":";
cin>>a[k];
}
left=0;
right=n-1;
}
void quick::putdata()
{
for(int k=0;k<n;k++)
{
cout<<"\nSorted marks are:"<<a[k]<<endl;
}
}
void quick::sort(float a[],int left,int right)
{
if(left<right)
{
int i=left;
int j=right+1;
float pivot=a[left];
do{
do{
i++;
}while((a[i]<pivot)&& left<right);
do{
j--;
}while(a[j]>pivot);
if(i<j)
swap(a[i],a[j]);
}
while(i<j);
a[left]=a[j];
a[j]=pivot;
sort(a,left,j-1);
sort(a,j+1,right);
}
}
int main()
{
quick obj;
obj.getdata();
obj.sort(obj.a,obj.left,obj.right);
obj.putdata();
return (0);
}
As it mentions you have not declared a as a variable inside the int main(). Rather it is an object of quick. In a function you don't pass array like a[] rather as a only .since a, left, right are a private variable of a class you can't access it from the object directly. Declare it as public and use it as obj.a, obj.left, obj.right inside sort function.
Complete code:
#include<iostream>
using namespace std;
class quick
{
public:
int n,left,right,i,j;
float a[55];
void getdata();
void sort(float[],int,int);
void putdata();
};
void quick::getdata()
{
cout<<"Enter how many elements you want to enter:";
cin>>n;
for(int k=0;k<n;k++)
{
cout<<"Enter percentage of students:"<<k+1<<":";
cin>>a[k];
}
left=0;
right=n-1;
}
void quick::putdata()
{
for(int k=0;k<5;k++)
{
cout<<"\nSorted marks are:"<<a[k]<<endl;
}
}
void quick::sort(float a[],int left,int right)
{
if(left<right)
{
int i=left;
int j=right+1;
float pivot=a[left];
do{
do{
i++;
}while((a[i]<pivot)&& left<right);
do{
j--;
}while(a[j]>pivot);
if(i<j)
swap(a[i],a[j]);
}while(i<j);
a[left]=a[j];
a[j]=pivot;
sort(a,left,j-1);
sort(a,j+1,right);
}
}
int main()
{
quick obj;
obj.getdata();
obj.sort(obj.a,obj.left,obj.right);
obj.putdata();
return (0);
}
Header File (IntegerSet.h)
#include<iostream>
#include<string>
using namespace std;
class IntegerSet{
public:
unsigned int set[15];
unsigned int empty_set[15];
IntegerSet();
IntegerSet(int[],int);
IntegerSet unionOfsets(IntegerSet);
IntegerSet intersectionOfSets(IntegerSet);
void insertElement(int);
void deleteElement(int);
void printSet();
bool isEqualTo(IntegerSet);
void emptySet();//Set all elements of set to 0
void inputSet();//Reads values from the user into set
bool validEntry(int);//Determines a valid entry to the set
};
Implementation File (IntegerSet.cpp)
//Class implementation file
#include "IntegerSet.h"
using namespace std;
IntegerSet::IntegerSet(){
for(int i=0;i<100;i++){
empty_set[i]=0;
}
}
IntegerSet::IntegerSet(int arr[],int size){
int min;//Use to hold the value for the sorting algorithm
int counter;//Used to count how many values need to be removed from the new array
for(int i=0;i<size-1;i++){//Nested for loop used to sort the array in ascending order
min=arr[i];
if(arr[i]<0||arr[i]>100){//If statement used to count how many numbers are <0 or >100
counter++;
}
for(int k=i+1;k<size;k++){
if(arr[k]<min){
arr[i]=arr[k];
arr[k]=min;
min=arr[i];
}
}
}
int *newSet=new int[size-counter];
for(int j=0;j<size;j++){
if(arr[j]>100||arr[j]<0){
//Do nothing
}else{
newSet[j]=arr[j];
}
}
delete newSet;
}
bool IntegerSet::validEntry(int a){
if(a<0||a>100){
return false;
}
return true;
}
void IntegerSet::inputSet(){
int a;
for(int i=0;i<100;i++){
cout<<"Enter an element(-1 to end)";
cin>>a;
if(a==-1){
cout<<"Entry complete";
return;
}
this->set[i]=a;
}
}
void IntegerSet::emptySet(){
for(unsigned int i=0;i<sizeof(this->set);i++){
this->set[i]=0;
}
}
IntegerSet IntegerSet::unionOfsets(IntegerSet a){
unsigned int *Union=new unsigned int[sizeof(this->set)+sizeof(a.set)];
for(unsigned int i=0;i<sizeof(this->set);i++){
*(Union+i)=this->set[i];
for(unsigned int h=0;h<sizeof(this->set);h++){
*(Union+(h+i))=a.set[h];
}
}
for(unsigned int j=0;j<sizeof(*Union);j++){
for(unsigned int z=j+1;z<sizeof(*Union);z++){
if(*(Union+j)==*(Union+z)){
*(Union+z)=0;
}
}
}
unsigned int newUnion[sizeof(*Union)];
for(unsigned int y=0;y<sizeof(*Union);y++){
if(*(Union+y)!=0&&y<sizeof(newUnion)){
newUnion[y]=*(Union+y);
}
}
IntegerSet c;
for(unsigned int w=0;w<sizeof(*Union);w++){
c.set[w]=newUnion[w];
}
delete Union;
return c;
}
IntegerSet IntegerSet::intersectionOfSets(IntegerSet a){
unsigned int *Intersect=new unsigned int[sizeof(this->set)+sizeof(a.set)];
int counter=0;
for(unsigned int i=0;i<sizeof(this->set);i++){
for(unsigned int j=0;j<sizeof(a.set);j++){
if(this->set[i]==a.set[j]){
*(Intersect+counter)=a.set[j];
counter++;
}
}
}
IntegerSet c;
for(unsigned int w=0;w<sizeof(*Intersect);w++){
c.set[w]=*(Intersect+w);
}
delete Intersect;
return c;
}
void IntegerSet::printSet(){
unsigned int min;
for(unsigned int i=0;i<sizeof(this->set)-1;i++){
min=this->set[i];
for(unsigned int k=i+1;k<sizeof(this->set);k++){
{
if(this->set[k]<min){
this->set[i]=this->set[k];
this->set[k]=min;
min=this->set[i];
}
}
}
cout<<"{";
for(unsigned int h=0;h<3;h++){
if(h==sizeof(this->set)-1){
cout<<this->set[h]<<"}";
}else{
cout<<this->set[h]<<",";
}
}
}
bool IntegerSet::isEqualTo(IntegerSet a){
unsigned int counter=0;
if(sizeof(a.set)==sizeof(this->set)){
for(unsigned int i=0;i<sizeof(this->set);i++){
for(unsigned int f=0;f<sizeof(a.set);f++){
if(this->set[i]==a.set[f]){
counter++;
}
}
}
}else{
return false;
}
if(counter==sizeof(this->set)){
return true;
}
return false;//Used to make sure this method always has something to return
}
void IntegerSet::insertElement(int a){
unsigned A=(unsigned)a;
if(!this->validEntry(a)){
cout<<"Invalid Insertion Attempt!";
}
unsigned int Inserted[sizeof(this->set)+1];
Inserted[sizeof(this->set)]=A;
for(unsigned int w=0;w<sizeof(Inserted);w++){
this->set[w]=Inserted[w];
}
}
void IntegerSet::deleteElement(int a){
unsigned A=(unsigned) a;
unsigned int *Delete=new unsigned int[sizeof(this->set)-1];int test=0;
if(!this->validEntry(a)){
cout<<"No value of: "<<a<<" exists in the set";
}
for(unsigned int z=0;z<sizeof(this->set);z++){
if(this->set[z]==A){
test++;
}
}
if(test==0){
cout<<"No value of: "<<a<<" exists in the set";
}
for(unsigned int i=0;i<sizeof(this->set)-1;i++){
*(Delete+i)=this->set[i];
}
for(unsigned int w=0;w<sizeof(*Delete);w++){
this->set[w]=*(Delete+w);
}
delete Delete;
}
And then there is my tester file, which is what allowed me to find the error
(IntSet.cpp)
//Driver program for class IntegerSet
#include <iostream>
using namespace std;
#include "IntegerSet.h"
int main(){
IntegerSet a,b,c,d;
cout<<"Enter set A:\n";
a.inputSet();
cout<<"\nEnter set B:\n";
b.inputSet();
c=a.unionOfsets(b);
d=a.intersectionOfSets(b);
cout<<"\nUnion of A nd B is:\n";
c.printSet();
cout<<"Intersection of A nd B is:\n";
d.printSet();
//Test if set A is equal to set B
if(a.isEqualTo(b)){
cout<<"Set A is equal to set B\n";
}else{
cout<<"Set A is not equal to set B\n";
}
//test insertion
cout<<"\nInserting 77 into set A...\n";
a.insertElement(77);
cout<<"Set A is now:\n";
a.printSet();
const int arraySize=10;
int intArray[arraySize]={25,67,2,9,99,105,45,-5,100,1};
//Use construct that receive an array of ints
//and the size of that array to create a set object
IntegerSet e(intArray,arraySize);
cout<<"\nSet e is:\n";
e.printSet();
cout<<endl;
}
Personally I feel like what caused this error was some sort of memory leak, although the glib c error isn't exactly telling me where it would be coming from like the compiler does. Also this error occurs right after the function b.inputSet() exits.
What the error says (it alternates between two messages I've noticed):
***glibc detected*** ./a.out: free(): invalid next size (normal):
***glibc detected*** ./a.out: double free or corruption (!prev):
You're doing lots of bad here.
Here's your definition of empty_set.
unsigned int empty_set[15];
And your default constructor accesses outside these bounds.
IntegerSet::IntegerSet(){
for(int i=0;i<100;i++){
empty_set[i]=0;
}
}
Your other constructor doesn't actually change anything inside the class.
Also this code
IntegerSet IntegerSet::unionOfsets(IntegerSet a){
unsigned int *Union=new unsigned int[sizeof(this->set)+sizeof(a.set)];
looks remarkably suspicious as sizeof(set) is not the number of elements in set, but the array length in bytes (15*sizeof(float)) - but if you switch to a variable length array it wont even be that... You'll need to track the set length separately. Or use a std::vector<int>, or better yet - why reinvent the wheel and use std::set<int>?
I don't know how to call my class functions into printData(Testscore&) and readData(TestScore).
Also, could someone tell me why my Average() isn't being called to the main? I just learned about using static member variables and static member functions and was wondering if I am using them incorrectly.
The readData function:
Does not use copy constructor.
Reads all instance variables like the student's names and all their
grades.
Uses functions to store the variables, name, element of each grade of array pointed to private pquiz, and static member
grades of how many grades to read.
The printData function:
Writes the name and average grade of the quizzes.
Uses copy constructor.
This is my program so far:
#include <iostream>
#include <string>
using namespace std;
class TestScore {
private:
static int grades;
string name;
double *pquiz;
double average;
public:
TestScore();
~TestScore();
void setName(string);
static void setGrades(int);
void setPquiz(double *);
void setAverage(double);
string getName();
static int getGrades();
double getPquiz();
void readData(TestScore &);
void printData(TestScore);
double Average(double *, int);
static void Grade(int);
};
TestScore::TestScore() {
name="";
pquiz=new double[grades];
average=0;
}
void TestScore::setName(string name1) {
if(name1!="1") {
name=name1;
}
}
void TestScore::setPquiz(double *pquiz1) {
if(pquiz>=0) {
pquiz=pquiz1;
}
}
void TestScore::setGrades(int grades1) {
if(grades1>=0) {
grades=grades1;
}
}
void TestScore::setAverage(double average1) {
if(average1>=0) {
average=average1;
}
}
string TestScore::getName() {
return name;
}
int TestScore::getGrades() {
return grades;
}
double TestScore::getPquiz() {
return *pquiz;
}
double Average(double *pquiz, int grade) {
int count;
double total=0;
double average=0;
for(count=0; count<grade; count++) {
total+=pquiz[count];
}
average=total/grade;
return average;
}
void readData(TestScore&) {
}
void printData(TestScore) {
}
TestScore::~TestScore() {
delete [] pquiz;
pquiz=0;
}
int TestScore::grades=0;
void TestScore::Grade(int a) {
grades+=a;
}
int main() {
const int grades = 3;
const int students = 4;
TestScore exam;
string student;
int grade;
double *pquiz;
double average;
for(int i=0; i<students; i++) {
cout<<"Student "<<(i+1)<<": ";
cin>>student;
exam.setName(student);
cout<<endl;
for(int count=0; count<grades; count++) {
cout<<"Quiz "<<(count+1)<<": ";
cin>>pquiz[count];
exam.setPquiz(pquiz);
exam.getPquiz();
while(pquiz[count]<0) {
cout<<"Error, invalid test score, please try again."<<endl;
cout<<"Quiz "<<(count+1)<<": ";
cin>>pquiz[count];
}
}
exam.setAverage(average);
cout<<exam.getName()<<" average is "<<Average(pquiz, grade)<<endl<<endl;
}
readData(exam);
printData(exam);
return 0;
}
Don't use static anywhere, at least not for now. You have too many variables of the same name, scattered all over the place. Try to clean them up.
TestScore::TestScore()
{
name = "";
//pquiz = new double[grades];//#grades is undefined
pquiz = NULL;
average = 0;
}
grades is not defined yet, it could be zero, or it could be -817. You should just remove that line, or you can put something like pquiz = new double[10] that's if you are sure the number of quiz will not exceed 10.
TestScore::~TestScore()
{
if (pquiz) delete[] pquiz;
pquiz = NULL;
}
delete pquiz only if it is not NULL
int main() {
const int grades = 3;
const int students = 4;
TestScore exam;
string student;
int grade;
double *pquiz;
...
This is a different pquiz, it is a pointer which points to nothing, it doesn't really exist, you can't use it like that.