0

For my homework I had to design an arraylist in c++ using only 1d arrays and pointers to make the array dynamic. I have done ample testing and my functions work correctly, but when I use the main that the teacher has provided me I get this floating point error. The point of this homework is to create a class that will work for the teachers main without changing any code in the main

here is the main:

#include "ArrayList.h"
#include <iostream>
using namespace std;

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

    for (int i=1;i<=50;i++)
    {
        arr.push_back(i);
    }

    cout << "Should contain numbers 1..50, is ";

    cout << arr.toString() << endl;

    for (int i=arr.size()-1;i>=1;i--)
    {
        arr.erase(arr[i]);
    }   

    cout << "Should contain only 1, is ";
    cout << arr.toString() << endl;

    arr.erase(arr[0]);

    for (int i=1;i<=50;i++)
    {
        if (i<=2)
            arr.push_back(i);
        else
        {
            int j=1;
            while ((j<arr.size()) && (i%arr[j]!=0))
                j++;

            if (j==arr.size())
            {
                arr.push_back(i);
            }
        }
    }

    cout << "Prime numbers between 1 and 50 are: " << arr.toString() << endl;

}

here is my cpp:

#include<iostream>
#include<string>
#include<sstream>
#include "ArrayList.h"

using namespace std;
void ArrayList:: intialArr(int arr[])
{
    for(int i = 0; i < length; i++)
    {
        arr[i] = 0;
    }
}

string ArrayList:: toString()
{
    std::ostringstream ss;
    for(int i = 0; i < capacity; i++)
    {
        if(arr[i]>0 || arr[i] <0)
        {
            ss << arr[i] << " ";
        }
    }
    return ss.str();
}

ArrayList::ArrayList()
{
    length = 1;
    capacity=0;
    arr = new int[length];
    intialArr(arr);
}

int& ArrayList:: operator[] (unsigned int i)
{
    return arr[i];
}

void ArrayList:: push_back(int m)
{
    if(capacity>=length)
    {   
       int oldlength = length;

       length = length*2;
       int* curArr = new int[length];
       intialArr(curArr);
       for (int i = 0; i < oldlength; i++)
       {
          curArr[i] = arr[i];
       }
       delete [] arr;
       arr = curArr;
    }

    arr[capacity] = m;
    capacity++;
}   


void ArrayList:: erase(int m)
{
        if(capacity == length/2)
        {   
            length = length/2;
            int* curArr = new int[length];
            intialArr(curArr);      
        for (int i = 0; i<capacity; i++)
        {
                curArr[i] = arr[i];

        }
        delete [] arr;
        arr = curArr;
        }

        for(int i = 0; i < capacity; i++)
        {
            if(arr[i]==m)
            {
                for(int j = i; j<length; j++)
                {
                    arr[j] = arr[j+1];
                }
                capacity--;
                break;
            }
    }
    cout << "length = " << length << " capacity = " << capacity << " capacity/length = " << capacity*2 << endl;
}

from what I have read online floating point exceptions are normally thrown when you try to divide by zero or an infinate value arises but I dont understand how I am getting either of these issues to arise.

My code get through the main where number 1-50 are added and deleted but I get the error once I go into setting up the array to hold prime numbers (after the arr.erase(arr[0]) in the main)

I just set a couple of tags in the main to find what my number look like going into the while ((j<arr.size()) && (i%arr[j]!=0))and i find that my numbers before the crash are

j = 1 and arr[j] = 2
i = 5 and arr.size() = 4
spstephens
  • 75
  • 1
  • 7

0 Answers0