Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hey guys. I\'m getting a segmentation fault when I run two different functions t

ID: 3633116 • Letter: H

Question

Hey guys.
I'm getting a segmentation fault when I run two different functions together and I have no idea why. When Bubblesort(); and histogram(); are both running at the same time, I get a segmentation fault. Any help is much appreciated! Thanks!

*************************Assignment3.cpp*************************

#include "3.h"
#include "Assignment3func.h"

int main (int argc, char *argv[]){

Stats hist;


hist.input(argc, argv);
hist.Bubblesort();
hist.histogram();
hist.output(argv);
}



*************************3.h*************************






#include <iostream>
#include <cstdlib>
#include <fstream>
#include <cmath>
#include <new>
using namespace std;

class Stats
{
public:
// Input + Output
ifstream inputfile;
ofstream outputfile1;
ofstream outputfile2;
ofstream outputfile3;
void input(int argc, char *argv[]);
void output(char *argv[]);
void Bubblesort();
void average();
void variance();
void standarddeviation();
void histogram();
private:
// Data Members

double * A;
double * bin;
double * temp;
double sum,ave,stddev,min,max,binS,var;
int Asize,i,J,j,l,binN,N,I,hold, hold2;


};


*****************Assignment3func.h*****************

/*

Assignment 3



*/


void Stats::Bubblesort(){

bool swapped = true;

while (swapped) {
swapped = false;
j++;
for (i = 0; i< Asize - j; i++) {
if (A[i] > A[i+1]) {
hold = A[i];
A[i] = A[i+1];
A[i+1] =hold;
swapped = true;
}}}}


void Stats::average(){

sum = 0;

for (i = 0; i < Asize; i++)(
sum = sum + A[i]);

ave = sum/Asize;

}



void Stats::variance(){

double var = 0;

for (i = 0; i < Asize; i++)(
var = var + pow(A[i] - A[i + 1], 2));

}



void Stats::standarddeviation()
{
stddev = sqrt(var);
}

void Stats::histogram()
{
binN = ceil(sqrt(Asize));
binS = (abs(max - min)/Asize);

bin = new double[i];
i=0;
bin[i] = 0;


for (int i = 0; i < Asize; i++)
{ //loop to go through A
if ( A[i] < (min+binS) )
{ //compare the value in A to the bin
bin[i]+=1; //increases number in bin if value fits in bin
}
else
{
min+=binS; //if value isn't in bin, changes bin
// cout << "min = " << min << << endl;
i++; //shifts to next bin
bin[i]=0; //initiate the value in bin to 0
i--; //makes up for offset of a value not in bin of loop
}
}
}


void Stats::input(int argc, char *argv[])
{

switch (argc)
{

case 2:
cerr << "Error: Please provide 3 output filenames" << endl;
exit(1);
case 3:
cerr << "Error: Please provide 2 more output filenames" << endl;
exit(1);
case 4:
cerr << "Error: Please provide 1 more output filename" << endl;
exit(1);
case 5:
break;
default:
cerr << "Error: Please provide 1 input and 3 output filenames respectively as command line arguments" << endl;
exit(1);}


inputfile.open(argv[1], ios::in);

outputfile1.open(argv[2], ios::out);

outputfile2.open(argv[3], ios::out);

outputfile3.open(argv[4], ios::out);

if(inputfile.bad()){
cerr << "Error opening input file." << endl;
exit(1);}

if(outputfile1.bad()){
cerr << "Error opening output file 1." << endl;
exit(1);}

if(outputfile2.bad()){
cerr << "Error opening output file 2." << endl;
exit(1);}

if(outputfile3.bad()){
cerr << "Error opening output file 3." << endl;
exit(1);}



Asize=1;
A = new double[Asize];
int ii = 0;

//Loop over all the values in the input file
while (inputfile.eof() != 1)
{
if (ii >= Asize) //inspired by http://www.fredosaurus.com/notes-cpp/newdelete/55dynexample.html
{
Asize++;
double *temp = new double[Asize]; //Resizes the array if it needs to be larger
for (int jj=0; jj<ii; jj++)
{
temp[jj] = A[jj]; //Puts the old values into the new array
}
delete [] A;
A = temp;
}
inputfile >> A[ii]; // puts the value into the array
ii++; }




inputfile.close();
Asize--;

cout << "Asize" << endl;
for (i=0; i<Asize; i++){
cout <<A[ii] << " ";}
cout << endl;

outputfile1.close();
outputfile2.close();
outputfile3.close();

}




void Stats::output(char *argv[])
{






min = A[0];
cout << "Minimum = " << min << endl;


outputfile1.open(argv[2], ios::out);


outputfile2.open (argv[3], ios::out);


outputfile3.open (argv[4], ios::out);

//min = A[0];
//outputfile1 << "Minimum = " << min << endl;

max = A[Asize-1];
outputfile1 << "Maximum = " << max << endl;

average();
outputfile1 << "Mean = " << ave << endl;

variance();
outputfile1 << "var = " << var << endl;

standarddeviation();
outputfile1 << "Standard Deviation = " << stddev << endl;


outputfile1 << "Size of bins = " << binS << endl;

binN = ceil(sqrt(Asize));
outputfile1 << "Number of bins = " << binN << endl;

outputfile3 << "The sorted A is: " << endl;
for (i = 0; i < Asize; i++)
{
outputfile3 << A[i] << " ";
}
outputfile3 << endl;

outputfile2 << "MIN = " << min << endl;
outputfile2 << "MAX = " << max << endl;
outputfile2 << "Number of Bins = " << binN << endl;
outputfile2 << "Size of Bins = " << binS << endl;
outputfile2 << "The histogram is: " << endl;
for (l = 0; l < binN; l++){
outputfile2 << A[l]<< " ";
outputfile2 << endl;}


//delete[] bin;
//delete[] temp;
//temp = NULL;
//A = NULL;
//bin = NULL;

outputfile1.close();
outputfile2.close();
outputfile3.close();

}



Assignment3func.h

Explanation / Answer

Check whether all values are properly initialized or not........