hey, I need to make a program that
• Define a default array size. the size HAS to be 15.
• Define a function “ResizeArray” which virtually increase the size of an array, by creating a new array with the size of the old array plus the default array size and copying the content of the old array to the new array, finally returning the new array. This is a function that receives an old array, the size of the old array, and the default size.
• You will need track of the position of the last number inserted on the array, such that
if you reach the size of the array then you need to call the function ResizeArray. For
this define a function called “InsertElement”, which inserts a number into the array and
checks if it has reached the array size.
• Your program should sort the array using selection sort.
well... in my program it works perfectly with files that has less than 41 numbers... when i open a file with 41 numbers or more.. it causes a segmentation faul...this is weird.. I have no Idea whats wrong with my program... I put the options to print the size and the array in which the info is being stored to see if that is working correctly and I noticed is in the size 41 that starts the problem.. this is my code:
#include<iostream>
#include<fstream>
#include<string>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
//prototypes
void InsertElement(float *,string , int , int , int *);
bool VerifyFile(string);
float GetFloat(string);
void ResizeArray(float [], int , int);
void SelectionSort(float[], int );
int main(){
string FileName;
string line;
int size; //tamaño del arreglo que se ira incrementando a medida que se lea un valor del archivo
int iter; //iteraciones ... las veces que se añaden 15 (en este caso)
int DefaultSIZE = 15;
float array[DefaultSIZE];
cout<<"Insert the name of the file:"<<endl; //se pide nombre de file al usuario
cin >>FileName; //usuario entra el nombre
if (VerifyFile(FileName)==true) //verifica si el file existe, si existe entonces.
{
fstream infile;
size = 0;
iter = 1;
infile.open(FileName.c_str(),fstream::in); //abre el file para leerlo
while(!infile.eof()){
infile >> line;
InsertElement(array,line,size,DefaultSIZE,&iter);
size++;
}
infile.close(); //se cierra el file
system("pause");
}
else if (VerifyFile(FileName)==false){
cout<<"The file does not exists."<<endl;
return main();
}
return 0;
}
//Functions
void InsertElement(float *array,string line, int size, int DefaultSIZE, int *iter) {
float FloatReturn;
int iter1 = *iter;
cout << size << endl;
if(size == DefaultSIZE*(iter1)) {
ResizeArray(array, size, DefaultSIZE);
*iter = iter1+1;
}
FloatReturn = GetFloat(line); //usamos para cambiar de string a float
array[size] = FloatReturn; //se asigna cada valor leido del file a cada valor de un arreglo
cout << array[size]<<endl; //despliega cada valor del arreglo
}
bool VerifyFile(string FileName)
{
fstream infile;
infile.open(FileName.c_str(),fstream::in);
if (infile.good())
{
infile.close();
return true ;
}
else return false;
}
float GetFloat(string line){
float floatReturn;
floatReturn = atof(line.c_str());
return(floatReturn);
}
void ResizeArray(float array[], int size, int SIZE){
float* array2;
array2 = new float [size + SIZE];
for(int i = 0; i < size ; i++) {
array2[i] = array[i];
}
*array = *array2;
delete [] array2;
}
please , it's driving me nuts.. any help is appreciated