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

Please put your code in CODE-tags for the sake of readability for our member.

>>float array[DefaultSIZE];
That array can not be resized. If you want to resize it then you have to declare it as a pointer and allocate the size. float* array;

Thanks alot, I fixed it but now am having another problem... it finishes but it gets stuck with segmentation fault just before closing the file, any idea why is this happening?

#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 CreateFile(float *, int , string );

int main(){
string FileName;
string line;
int size; 
int iter; 
int DefaultSIZE = 15; 
float* array;
array = new float [DefaultSIZE];


  cout<<"Insert the name of the file:"<<endl; 
  cin >>FileName;  
    
    if (VerifyFile(FileName)==true) 
{
            
        fstream infile;
        size = 0;
        iter = 1;
        infile.open(FileName.c_str(),fstream::in);    
    
    
              
            while(!infile.eof()){
                infile >> line; 
                InsertElement(array,line,size,DefaultSIZE,&iter);
                size++;
        }  
               
infile.close(); //se cierra el file
        cout << size << endl;
        cout << "out of the while" << endl;


for (int i=0; i<size; i++)
  cout << *(array+i) << endl;
  SelectionSort(array,size);

    cout<<"This is the array in ascending order"<<endl;
               for(int cont = 0; cont < size ; cont++) 
               cout << "[ " << array[ cont ] << " ]";

       cout << endl;
       system("pause");
}     
         

    else if (VerifyFile(FileName)==false){
   
       cout<<"The file does not exists."<<endl;
       return main();
       }    
 CreateFile(array,size,FileName);
 

    return 0;
}

//FUNCTIONS

void InsertElement(float *array,string line, int size, int DefaultSIZE, int *iter) {

  
               float FloatReturn;
               int iter1 = *iter;
               
              
                if(size == DefaultSIZE*(iter1)) {


                ResizeArray(array, size, DefaultSIZE);
                  *iter = iter1+1;
              
                 
                }
              
                FloatReturn = GetFloat(line); 
                
             
                *(array+size) = FloatReturn; 
                
}                
                
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];


cout<<"beggining resize."<< endl;
for(int i = 0; i < size ; i++) {

 *(array2+i) = *(array+i);
}

delete [] array;

array = array2;



delete [] array2;
cout << "leaving resize"<< endl;
}


void SelectionSort(float *array, int size){

       int scan, minI;
       float minV;
  
       
       for (scan = 0; scan < (size - 1); scan++)
       {
           minI = scan;
           minV = array[scan];
           for(int j = scan + 1; j < size; j++)
           {
              if (array[j] < minV)
              {
                minV = array[j];
                minI = j;
}
}
                array[minI] = array[scan];
                array[scan] = minV;
}


      
}    

int CreateFile(float array[], int size, string FileName)
{

      ofstream infile;   
      string FinalName = "Sorted_";

      float array3[size];
          
      FinalName.append(FileName);
      
      infile.open(FinalName.c_str(),fstream::out);
      
      for(int i=0; i < size; i++){

       infile << array[i]<<endl;
       }
      
      
   
    
        infile.close();    
         return 0;


}

I put some couts to see where is running and were is not... and the cout just after I close the file in the main does not appear.. Any help is appreciated.thanks in advance

>>void ResizeArray(float *array, int size, int SIZE){

the first parameter has to have two stars, not one. With one star all you have is a local pointer which is a COPY of the original. In c++ you have two choices:
(1) void ResizeArray(float **array, int size, int SIZE){ // pointer to pointer

(2)void ResizeArray(float *& array, int size, int SIZE){ // reference to pointer

If you use the second option then you don't have to change the code in that function If you use the first option then some minor code changes will be necessary.

oooohhh ok, nice :)

and how can I call the function? because this way of doing it is new for me... well c++ is new for me

I know u are suppose to call it differently than this:

ResizeArray(**array, size, DefaultSIZE);

because it didn't work.. I'm looking through the web but I would really appreciate if you could tell me :)

If you use the second option, the one with two stars, then you have to pas a pointer to the pointer ResizeArray( &array, size, DefaultSIZE);

hey, :$.. umm im having trouble in the lines 140 and 145.. cannot convert float* to float or float** to float*. I've tried different things that jumped in my mind but is useless, I still don't know how to manage well the pointers..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.