Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Why Doesn't This Work?

Name: Anonymous 2009-03-16 23:03

include<iostream>
#include<fstream>
using namespace std;

#define MAX_ITEMS  20

template<class ItemType>
class SortedList
{
private:
    int length;
    ItemType values[MAX_ITEMS];
    int currentPos;
public:
    SortedList();                     // default constructor: lenght=0, currentPos=-1
    void MakeEmpty();                 // let length=0
    void InsertItem(ItemType x);     // insert x into the list    
    void DeleteItem(ItemType x);     // delete x from the list
    bool IsFull();                     // test if the list is full
    int Lengthls();                     // return length
    void RetrieveItem(ItemType &x, bool &found);    // retrieve x from the list, the boolean result is stored in found
    void ResetList();                 // currentPos=-1
    void GetNextItem(ItemType &x);   // get the next element from the list with respect to the currentPos
};

int numCharElement(ifstream &x);

int numFloatElement(ifstream &x);

int main()
{
    ifstream inChar,inFloat ;
    char charHolder;
    float floatHolder;
    int numOfChars,numOfFloats;

    inChar.open("char.dat");
    inFloat.open("float.dat");

    SortedList <char> letters;
    SortedList <float> numbers;
   
    numOfChars = numCharElement(inChar);
    numOfFloats = numFloatElement(inFloat);

    inChar.seekg(0,std::ios::beg);
    inChar.clear();

    inFloat.seekg(0,std::ios::beg);
    inFloat.clear();

        for(int a=0;a<=numOfChars;a++)
        {
            inChar>>charHolder;       
            letters.InsertItem(charHolder);
        }

    for(int b=0;b<=numOfFloats;b++)
        {
            inFloat>>floatHolder;           
            numbers.InsertItem(floatHolder);
        }

    cout<<endl<<endl;
   
    cout<<"There are "<<numOfChars<<" elements in the char.dat file."<<endl<<endl;

    cout<<"There are "<<numOfFloats<<" elements in the float.dat file."<<endl;


    cout<<"The elements in char.dat after being sorted are "<<endl;

    for(int c=0;c<=numOfChars;c++)
        {
            letters.GetNextItem(charHolder);
            cout<<charHolder<<endl;
        }

    cout<<endl<<"The elements in float.dat after being sorted are "<<endl;

    for(int d=0;d<=numOfFloats;d++)
        {
            numbers.GetNextItem(floatHolder);
            cout<<floatHolder<<endl;
        }

    return 0;

}



int numCharElement(ifstream &x)
{
    char z;
    int count = 0;

    x>>z;
    cout<<z<<endl; //delete
    while(!x.eof())
        {
            x>>z;
            cout<<z<<endl; //delete
            count++;
        }

    return count;
}



int numFloatElement(ifstream &x)
{
    float y;
    int count = 0;

    x>>y;
    cout<<y<<endl; //delete

    while(!x.eof())
        {
            x>>y;
            cout<<y<<endl; //delete
            count++;
        }

    return count;
}



template<class ItemType> SortedList<ItemType>::SortedList()   
{
    length = 0;
    currentPos = -1;
}



template<class ItemType> void SortedList<ItemType>::MakeEmpty()
{
    length = 0;
}



template<class ItemType> void SortedList<ItemType>::InsertItem(ItemType x)
{
    int location = 0;
    int end = length - 1;
    bool moreToSearch;

    moreToSearch = (location<length);

    while(moreToSearch)
        {
            int midPoint = (location + end) / 2;

            if(x>values[midPoint] && location<=end)
                {
                    location = midPoint + 1;
                }   
            else if(x<values[midPoint] && location<=end)
                {
                    end = midPoint - 1;
                }
            else
                moreToSearch = false;
        }

    for(int index=length;index<location;index--)
        values[index] = values[index-1];

    values[location]=x;
    length++;
}



template<class ItemType> void SortedList<ItemType>::DeleteItem(ItemType x)
{
    int location = 0;
    int end = x.length - 1;

    while(location<=end)
        {
            int midPoint = (start + end) / 2;

            if(x>values[midPoint])
                location = midPoint + 1;
            else if(x<values[midPoint])
                end = midPoint - 1;
            else
                {
                    for(index = location + 1;index < length; index++)
                        {
                            values[location-1] = values[location];
                        }
                    length--;
                }
        }
}



template<class ItemType> bool SortedList<ItemType>::IsFull( )
{
    return (length == MAX_ITEMS);
}



template<class ItemType> int SortedList<ItemType>::Lengthls( )
{
    return length;
}



template<class ItemType> void SortedList<ItemType>::RetrieveItem(ItemType &x, bool &found)
{
    int location = 0;
    int end = x.length - 1;

    while(location<=end)
        {
            int midPoint = (location + end) / 2;

            if(x>values[midPoint])
                location = midPoint + 1;
            else if(x<values[midPoint])
                end = midPoint - 1;
            else
                {
                    found = true;
                    x = values[midPoint];
                    break;
                }
        }

}



template<class ItemType> void SortedList<ItemType>::ResetList( )
{
    currentPos = -1;
}



template<class ItemType> void SortedList<ItemType>::GetNextItem(ItemType &x)
{
    x = values[++currentPos];
}





And How do I fix it?

Name: Anonymous 2009-03-16 23:05

There is a # in front of include<iostream>

Name: Anonymous 2009-03-16 23:17

Because you are using Coot.

Name: Anonymous 2009-03-16 23:24

Your program is too long.  The compiler probably runs out of memory trying to parse it.

Name: Anonymous 2009-03-17 22:53

>>1

Your program is a fucking tragedy. I will not debug it for you until its 1/4th the size and written in java.

Name: Anonymous 2009-03-17 22:56

>>5
as huge as it is, it'd probably be 4000 times the size in java.

Name: Anonymous 2009-03-17 22:59

Try a Python compiler

Name: Anonymous 2009-03-17 23:02

Okay, THIS IS WHAT YOU GET FOR HELPING THEM!!!  Prepare to get a new thread about a question working with binary trees or bubble sort every time they get a new CS101 assignment.

Name: Anonymous 2009-03-17 23:21

>>8
I'm getting awfully tired of your drivel. There is no official rule written by the 4chan staff that says you can't help another person with an issue on something in /prog/

Also, IHBT

Name: Anonymous 2009-03-17 23:34

>>9
DON'T HELP HIM!! isn't one person.

Name: Anonymous 2009-03-17 23:37

>>10
YHBMT

Name: Anonymous 2009-03-18 0:55

>>11
E

Name: Anonymous 2009-03-18 2:08

>>9
[b][i]Doesn't get /prog/

Name: Anonymous 2009-03-18 6:07

Resurrection bump

Name: Anonymous 2009-03-18 13:45

What an eyesore.

Name: ​​​​​​​​​​ 2010-10-24 0:06

Don't change these.
Name: Email:
Entire Thread Thread List