pinsickle 17 Junior Poster in Training

just take the series scores you output at compare them for the max and out that.

pinsickle 17 Junior Poster in Training

Maybe my code is wrong, but when I add that it throws off my code an causes it to output garbage values.

weird i just copied you program on my computer and tried my code and it worked. Comment you code out and copy and paste this. if you works you know it was a typo some where

for (i= 0; i < numofplayersonteam; i++)
  
      {
  
      for (j = 0; j < numofgamesinseries; j++)
  
      {
  
      cout << "Enter player " << i+1 << "'s score " << j+1 << ": ";
  
      cin >> score[i][j];
  
       if (score[i][j] < 0 || score[i][j] > 300)
       {
            cout << "Score must be between 0 and 300, please try again.\n";
            j--;
       }
  
      }
pinsickle 17 Junior Poster in Training
for (j = 0; j < numofgamesinseries; j++) { 
cout << "Enter player " << i+1 << "'s score " << j+1 << ": "; 
cin >> score[i][j]; 
if (score[i][j] < 0 || score[i][j] > 300)
{
cout << "You entered an invaild score of : " << score[i][j] << "\n"
<< "Scores must be between 0 and 300, please try again\n"
j-- // back up j by one (so the loop will run again rewriting old data)
}
}

probably a better way to do it but that should work without altering your code

pinsickle 17 Junior Poster in Training

Which would be considering a better coding style, this is for a merge sort in case you are wondering what I am doing with this line of code

int j = 0; // used to track for the temp array earlier in my code
              // reset it to be reused as opposed to creating another int
for (int k = first; k < last; k++)
{
     data[k] = merged[j];
     j++;
}

//versus......

for (int k = first, int m = 0; k < last; k++, m++)
{
     data[k] = merged[m];
     m++;
}

just curious if it is bad coding style to have multiple declarations in my for loop.

pinsickle 17 Junior Poster in Training

Hello, I am working on a merge sort but I keep getting the following error : ds07(6443) malloc: *** error for object 0x100100098: incorrect checksum for freed object - object was probably modified after being freed. I don't see what my error is with the creation and deletion of my dynamic memory. Any help would be appreciated

void Array::mergeSort (void) 
    {
        mergeSort (0, size-1); // call to private : merge
    }
    
    void Array::mergeSort (int first, int last)
    {
            if (last > first)// more than one in subarray
            {
                int mid = (first + last) / 2;
                mergeSort (first, mid);
                mergeSort (mid+1, last);
                merge (first, last);
            } 
    }
    
    void Array::merge (int first, int last)
    {
        int* merged = new int [last-first+1];
        int mid = (first + last) / 2;
        int secList = mid + 1; // beginning of second list
        int firstList = first; //beginning of first list
        int j = first; // used for array index assignment

       while ( (firstList <= mid) && (secList <= last) )// both lists #1 & #2 are not exhausted
        {
            if (data[firstList] < data[secList])// data in list #1 < data in list #2
            {
                merged[j] = data[firstList];// <- data in list #1
                firstList++;
            }   
            else
            {
                merged[j] = data[secList]; // <- data in list #2
                secList++;
            }
            j++;
        } // end while
        
        if (firstList > mid)
        {
            for (int q = secList; q <= last; q++){
                merged[j] = data[q];
                j++;
            }
        }
        else
        {
            for (int r = firstList; r <= mid; r++)
            {
                merged[j] = data[r];
                j++;
            }
        }
      
        for (int i = first; i <= last; i++)
        {
            data[i] = merged[i];// overwrite original positions with "merged"
        }
       delete [ ] merged; //commented out due to segmentation fault
       merged = NULL;
    }
pinsickle 17 Junior Poster in Training

thanks for all of the help but i just missed the obivious. Since Queues is a child of SinglyLinkedList I could just use

outfile << queues[i];
pinsickle 17 Junior Poster in Training

I understand that old code can be added to, I'd love to just add a get head access method to SinglyLinkedList and be done with it. However, we cant use our own actual old .h and .o files we have to use our professors. So I can't really make any changes. Here is the actual write method. Once again here is the basic problem, Queue is the child of SinglyLinkedList. The queue or array of them in my case hold LinkedPQNodes, which is a child of PriorityQueueNode (pure virtual) and SinglyLinkedNode. I can't seem to point to the head of each queue. I'm getting error cant convert (class)* to Queue. Maybe there is a different approach I am missing?

void PriorityQueueA::write (ostream& outfile) const{
        LinkedPQNode* current = NULL;
        outfile << "array-based priority queue\n";
        outfile << setiosflags(ios::left) << setw (9) << "pId" << setw(9) << "arrive"
               << setw(9) << "priority" << setw(9) << "duration" << setw(9) << "next\n";
        
        for (int i = 0; i < numberOfLevels; i++){
            if (queues[i].SinglyLinkedList::isNotEmpty()){
                outfile << "level[" << i << "]\n";
                current = queues[i]; // error is here 
                while (current !=NULL){
                    current -> write(outfile);
                    //current = current -> dynamic_cast <LinkedNode*> (SinglyLinkedNode::getNext ());
                }
            }
            else {
                outfile << "level[" << i <<"]\n" << "empty\n";
            }
        }
pinsickle 17 Junior Poster in Training

A matrix class.

In that case you could do something like.

class Matrix {
public:  
        Matrix ();
        ~Matrix();
        int getVal (int row, int col)const;
   .........
private:
int ArrayName[what ever the row size][whatever the col size];
};

int Matrix::getVal (int row, int col)const {
       return ArrayName[row][col];
}
pinsickle 17 Junior Poster in Training

Are you talking a matrix Class or just making a matrix out of any array in main and using c style functions?

pinsickle 17 Junior Poster in Training

One second thought I realized how confusing the above sounds without being to see it so here are the header files

class PriorityQueue {

    virtual bool add (ifstream& infile) = 0;
    virtual bool remove (PriorityQueueNode& highestPriority) = 0;
    virtual bool isEmpty (void) const = 0;
    virtual void write (ostream& outfile) const = 0;
    
    };

class LinkedPQNode : public PriorityQueueNode, public SinglyLinkedNode {

    public:
        LinkedPQNode (int processId, int arrivalTime, int priority, int duration);
        LinkedPQNode* getNext  () const;
        void	write (ostream& outfile) const;
        
    };

class PriorityQueueA : public PriorityQueue {

    public:
        PriorityQueueA (void);
        bool	add (ifstream& infile);
        bool	remove (PriorityQueueNode& highestPriority);
        bool	isEmpty (void) const;
        void	write (ostream& outfile) const;
        
        private:
            Queue*	queues;	// pointer to array
            int	numberOfLevels; // size of array
    };
    
    ostream& operator << (ostream& outfile, const PriorityQueueA& pQ);

class SinglyLinkedList
{
   public:
                        SinglyLinkedList
                           (void);

                        ~SinglyLinkedList
                           (void);

      bool              isEmpty
                           (void) const;

      bool              isNotEmpty
                           (void) const;

      bool              addAtHead
                           (SinglyLinkedNode* newHead);

      SinglyLinkedNode* removeFromHead
                           (void);

      bool              addAtTail
                           (SinglyLinkedNode* newTail);

      void              write
                           (ostream& outfile, char separator = ' ') const;
   protected:
      SinglyLinkedNode* head;


      SinglyLinkedNode* tail;
};

      ostream&          operator <<
                           (ostream& outfile, const SinglyLinkedList& list);



class SinglyLinkedNode : public LinkedNode
{
   public:
                        SinglyLinkedNode
                           ();
      virtual
                        ~SinglyLinkedNode
                           (void);

      void              setNext
                           (SinglyLinkedNode* newNext);

      SinglyLinkedNode* getNext
                           (void) const;
};

class LinkedNode : public Node
{
   public:
                   LinkedNode
                      (int numberOfLinks);
      virtual
                   ~LinkedNode
                      (void);
   protected:
      void         setLink
                      (int i, LinkedNode* nodeAddr);

      LinkedNode*  getLink
                      (int i) const;

      int          getNumberOfLinks
                      (void) const;
   private:
      int          numberOfLinks;


      LinkedNode** links;
};
pinsickle 17 Junior Poster in Training
current = queues[i];

This line of code is killing me. I am suppose to be doing a write method for an array based priority queue. However I cant seem to get the first node on the give part of the array. queues is of the Queue class which is a child of SinglyLinkedList. The array holds LinkedAQNodes which is a child of SinglyLinkedNode and PriorityQueueNode (this one is pure virtural) I can't seem to get the current pointer to take the assignment. It gives me the error can't convert Queue to Any Pointer I try. Finally, we are suppose to reuse our old object files so I can't put any kind of access method in singilyLinkedList Any ideas would be appreciated. If you want I will post the actual code, just didn't want to sling a lot of code up here unless it is actually needed.

pinsickle 17 Junior Poster in Training

lol how did i not notice that ..... wait why didn't my compiler notice that? Thanks for the help!!! Nothing more irritating then breezing through a homework assignment just to have a typo to stop you in your tracks. Thanks again

pinsickle 17 Junior Poster in Training

I also thought it could have been an inheritance problem but, I just finished another program in which I inherited from the same files as I am inheriting from in this program

pinsickle 17 Junior Poster in Training

What is the message with the error that it is giving you?

I'm getting the 40 paragraph long error message saying it almost matches this method on this method etc.... well here i'll just paste it

main.cpp:33: error: no match for 'operator<<' in 'std::operator<< [with _Traits 
= std::char_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&)(
(std::basic_ostream<char, std::char_traits<char> >*)((std::basic_ostream<char, s
td::char_traits<char> >*)std::operator<< [with _CharT = char, _Traits = std::cha
r_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&)((std::basi
c_ostream<char, std::char_traits<char> >*)std::operator<< [with _Traits = std::c
har_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&)((std::ba
sic_ostream<char, std::char_traits<char> >*)((std::basic_ostream<char, std::char
_traits<char> >*)std::operator<< [with _CharT = char, _Traits = std::char_traits
<char>](((std::basic_ostream<char, std::char_traits<char> >&)((std::basic_ostrea
m<char, std::char_traits<char> >*)std::operator<< [with _Traits = std::char_trai
ts<char>](((std::basic_ostream<char, std::char_traits<char> >&)(& std::cout)), (
(const char*)"** add #")))), std::setw(5)))->std::basic_ostream<_CharT, _Traits>
::operator<< [with _CharT = char, _Traits = std::char_traits<char>](n))), ((cons
t char*)": ")))), std::setw(5)))->std::basic_ostream<_CharT, _Traits>::operator<
< [with _CharT = char, _Traits = std::char_traits<char>](i))), ((const char*)" *
*\012")) << list'
/usr/include/c++/4.2.1/ostream:112: note: candidates are: std::basic_ostream<_Ch
arT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostrea
m<_CharT, _Traits>& (*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = ch
ar, _Traits = std::char_traits<char>]
/usr/include/c++/4.2.1/ostream:121: note:                 std::basic_ostream<_Ch
arT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_C
harT, _Traits>& (*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Tra
its = std::char_traits<char>]
/usr/include/c++/4.2.1/ostream:131: note:                 std::basic_ostream<_Ch
arT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*
)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2.1/ostream:169: note:                 std::basic_ostream<_Ch
arT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _
CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2.1/ostream:173: note:                 std::basic_ostream<_Ch
arT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int
) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2.1/ostream:177: note:                 std::basic_ostream<_Ch
arT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _Char
T = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2.1/bits/ostream.tcc:92: note:                 std::basic_ost
ream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short …
pinsickle 17 Junior Poster in Training

I have << overloaded to write to a file, however I get an error when I try to use it in main. I tried calling the actual write method. Is the SpareTable that operator that is failing. I already checked the .h and .cpp parameters and that all my #includes where in place. Thanks for the help

static ofstream lout ("lists.txt");
srand (2231);
SkipList list;
int i, compares;
try{
    for (int n = 1; n <= 5; n++){
         i = rand();
         compares = list.add (i);
         updateStats (n, 5, compares);
         
        if (compares == -1) {
            cout << "memory overflow!\n";
            exit (1);
            }
       
        lout << "** add #" << setw(5) << n << ": " << setw(5) << i << " **\n"; //<< list; // this gives error
        list.write(lout); // yet this works fine
void SkipList::write (ofstream& outfile)const {
        SkipListNode* current = heads[0];
 
        while (current != NULL){
            current -> write (outfile);
            current = current -> getNext(0);
        }
        outfile <<"\n";
    }
    
    ofstream& operator << (ofstream& outfile, const SkipList& list){
        list.write(outfile);
        return outfile;
    }
void SkipListNode::write (ofstream& outfile) const{

        outfile << value << " ";
        for (int i = 0; i < getNumberOfLinks(); i++){
            if (getNext(i)){
                outfile << "( " << getNext(i) -> getValue() << ") ";
            }
            else {
                outfile << "( / )";
            }
        }
        outfile << endl;
    }

    ofstream& operator << (const SkipListNode& node, ofstream& outfile){
        node.write(outfile);
        return outfile;
    }
/*******master.h*********/
#ifndef MASTER_H
#define MASTER_H
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::ostream;
using std::ios; …
pinsickle 17 Junior Poster in Training

I wanted to get some opinions on a good visual c++ book to buy. I am intermediate level when it comes to programming. I have already been through a class on C and almost done with my class on C++. So I want a book that is heavy on the actual visual part, I am not concerned about stacks, binary trees etc as I already know how those work. Any suggestions would be appreciated.

Thanks

pinsickle 17 Junior Poster in Training

whoops, forgot to add the lowerleft values to my midX and midY that would make my code only work when the lower left corner of the triangle was at (0, 0). Anyone notice any other mistakes or redundancies?

pinsickle 17 Junior Poster in Training

I am currently working on polymorphism in class, part of my homework is the classic point inside a shape test. I always have access to the lower left point of the triangle and it is always assumed to be isosceles. Last year I solved this problem using linear inequalites but I found out that method can fail under certain situations. So I am trying the cross product method. It seems to work at least with the dozen or so tests I have preformed. I just wanted a second pair of eyes on it in case there is a situation I may not be considering. Thanks in advance.

bool Triangle::contains (double pX, double pY){
   double lowLeftX;
   double lowLeftY;
   double midX;
   double midY;
   double lowRightX;
   double lowRightY;
   bool inside = false;
   
   location (lowLeftX, lowLeftY); // access method to get points from 
                                             // shape ancestor
   midX = .5 * base;             // finding top (midX and midY)
   midY = height;                 // and lower Right triangle points
   lowRightX = lowLeftX + base;
   lowRightY = lowLeftY;
   
   double crossProductA = (pX *(lowLeftY - midY)) + (pY * (midX - lowLeftX)) +
                           (lowLeftX * midY  - midX * lowLeftY);
   double crossProductB = (pX *(midY - lowRightY)) + (pY * (lowRightX - midX)) +
                           (midX * lowRightY  - lowRightX * midY);
   double crossProductC = (pX *(lowLeftY - lowRightY)) + (pY * (lowRightX - lowLeftX)) +
                           (lowLeftX * lowRightY  - lowRightX * lowLeftY);
   crossProductA = .5 * abs(crossProductA);
   crossProductB = .5 * abs(crossProductB);
   crossProductC = .5 * abs(crossProductC);

   if (crossProductA + crossProductB + crossProductC == (.5 * base * height)){
      inside = true;
   }
   return inside;
   
}
pinsickle 17 Junior Poster in Training

Ok guys, i'll say this first. I got it 99% working (100 % working if I test it with the much shorter name list that we are suppose to use for the homework.) For some reason the code skips the very last name. Honestly, I think I am over thinking the problem and making my code overly complex. Any help would be appreciated. Posted below is my code and the File I am reading from.

void BinarySearchTree::writeIteratively (ostream& outfile) const {
   TemplateStack <Node*> writeStack;
   Node* current = root; 
      
      while (current -> getLeft () != 0){ // loop to find first (farthest left) name
         writeStack.push(current);
         current = current -> getLeft ();
      }
      outfile << *current << endl; // displaying farthest left name
      do {
         
         while (current -> getRight () != 0){ // since 'smallest' name has been found starting to traverse right
            current = current -> getRight ();
            while (current -> getLeft () != 0){ // smaller name is tree found so traverse left
               writeStack.push (current);
               current = current -> getLeft();
            }
            outfile << *current << endl;
         }
         writeStack.pop (current);
         outfile << *current << endl;
         
         if (*current == *root){ // back at the top of the tree
            while (current -> getRight () != 0){ // repeat process from above to traverse right side of tree
            current = current -> getRight ();
            while (current -> getLeft () != 0){
               writeStack.push (current);
               current = current -> getLeft();
            }
            outfile << *current << endl;
         }
            writeStack.pop (current);
            outfile << *current << endl;
         } …
pinsickle 17 Junior Poster in Training

I am writing to a file the ios fixed and show point works fine through out the loop but ios left only works on my file from the second set of data on. The first set of data still stays shifted right.
Here is the code, thanks in advance.

void StudentArrayV4::write(ofstream& outfile){
   
   if(!outfile){
      cout << "File error data could not be saved!\n";
   }
   else{
       outfile << setw(40) << "scores\n" << setiosflags (ios::left)
       << setw(10) <<"name" << setw(11) << "ID  "
      << setw(8) << "1" << setw(10)  << "2" << setw(10)  << "3"
      << setw(13) << "avg  " << setw(11) << "grade\n" << resetiosflags(ios::left);
      for (int j =0; j < numberOfStudents; j++){
         members[j] -> write(outfile); // student::write
      }
   }
}

void Student::write(ofstream& outfile){
         outfile << setiosflags(ios::left) << setw(10) <<  name << setw(10)
         << idNumber << setw(10) << scores[0] << setw(10) << scores[1]
         << setw(10) << scores[2] << setw(10) << setprecision(2)
         << setiosflags(ios::fixed | ios::showpoint) << setprecision(2) << average
         << setw(10) << grade << resetiosflags(ios::fixed | ios::left | ios::showpoint) <<"\n";
}
pinsickle 17 Junior Poster in Training

wow that is fantastic thanks

pinsickle 17 Junior Poster in Training

Thanks for the help. The names are one word, the professor wanted it way to make it more simplistic. I couldn't have studentArrayV4 read the file as it is a dynamic array of pointers that points to the students as they are created by the Student Constructor. numberOfstudents is a counter, starts at zero and is incremented for every student made. physical size, which is currently set at 3, helps test the size of the array that way if a student is called to be construted and there is not enough room a new larger array can be created filled with the current student info and then the new student can be added. i.e. the array starts blank and has three elements. If a fourth student is called for then a new array is created that has six elements, the data from the 3 element array is copied over and the 3 element array is deleted. Finally the student** (pointer to array of pointers) is redirected to the new array. The process can be repeated as many times as needed.

pinsickle 17 Junior Poster in Training

Hello,

To start thanks in advance for any help. Now I will note this question is about homework so I just want help to get started not everything laid out for me. Ok here is the deal. I have a class called StudentArrayV4, it is a dynamic array of pointers. I also have the class Student that actually holds the student information and is pointed to by StudentArrayV4. I added the ability to save the student data as well as load it from a file. I made sure the code worked by using (!infile.eof) as my test to find the end of file. However, i know .eof can be unreliable so I don't want to use it, not to mention I'd probably lose points on my homework too. The way I was required to set it up was by having the ifstream opened in main and then passed by reference to StudentArrayV4 which then eventually passes it to the Student Constructor. Any ideas on how I can know when I hit the end of the file without using .eof Here is the code for my various methods.

void  StudentArrayV4::read (ifstream& infile){
   StudentArrayV4 loadedFile;
   if (!infile){
      cout << "Error no file found!\n";
   }
   else{
      while (!infile.eof )
      add(infile);
   }
}

void StudentArrayV4::add (ifstream & infile){
   Student **temp = new Student*[numberOfStudents + physicalArraySize];
      for (int j = 0; j < numberOfStudents; j++){
         temp[j] = members[j];
      }   
         delete [] members;
   physicalArraySize += 3;
   members = temp;
   members[numberOfStudents] = new Student(infile);
   numberOfStudents++; …
pinsickle 17 Junior Poster in Training

forget i said anything. I totally forgot that I could pass streams through a function parameter list.

pinsickle 17 Junior Poster in Training

First of all, let me say this. I just started object oriented programming in college and I am already way ahead of the subject matter being taught. Wow, that came off as cocky, I mean this is not one of those OMG my homework is due tomorrow type situations. So if possible just nudge me in the right direction instead of flat out telling me what to do. That being said here is my problem.

I have a program, that contains a class called Student. The student class stores the student's name, scores, ID number etc. I set the program up to save the student's info onto a file. The problem is when I tried to load the student's data from the file (now is a good time to mention that is an array of students) every element in the array gets the information of the same student (the first in the file.)

I overloaded the saveStudent function so it would accept all the student data. I am planning on trying to overload the loadStudent function by opening the ifstream in the first loadStudent function and setting up infile.close () to trigger after the last student is loaded in the second function. Would that work or will the infile close after the function closes? Is there a better way to approach this?

Sorry for being long winded, just want to make sure enough program background is there to get an opinion on what to do.

Thanks …