link list size

problem: link list is reading in last value of textfile twice

Page 1 of 1

4 Replies - 1330 Views - Last Post: 23 February 2010 - 12:48 PM Rate Topic: -----

#1 Guest_LapBo*


Reputation:

link list size

Posted 21 February 2010 - 06:23 PM

I have to write a program that reads in data from two text files (there are 10 values in each file) and saves them in a link list. It then calculates the correlation of the data in the two text files. My link list for some reason is inserting the last value twice, making the size of the list 11 and not 10. This is messing up the calculations for my functions. I have written all of the code for the program and everything works fine, just my size is off. Can anyone help fix this?
Thanks so much!

#include <iostream>
#include <fstream>
#include <cmath>

using namespace std;
struct node
{
     public:
          int proxySize;
          double developmentHours;
          node *next;
};
class Numbers
{         
     private:
          int size1;
          int size2;
          node *head1;
          node *head2;
          int sumOfElements1;
          double sumOfElements2;
          int sumOfSquares1;     
          double sumOfSquares2;
          double sumOfProduct;  
          double correlationValue;
     public:
          Numbers();
          int count1();
          int count2();
          int insert1(int fileInput1);
          int insert2(double fileInput2);
          void display1();
          void display2();
          int sum1();
          double sum2();
          double product();
          int square1();
          double square2();
          double correlation();
          double squareCorrelation();
          
};

int main()
{
     Numbers numbers;
     ifstream f1("text1.txt");     
     ifstream f2("text2.txt");
     int fileInput1;  //variable to read in textfile1
     double fileInput2; //variable to read in textfile2

/*-------------------Readding in text file 1--------------------------*/
     if(! f1.is_open())
     {
          cout << "!!!There was an error opening the file!!!" << endl;
     }
     else
     {
          cout << "Reading in data for proxy size..." << endl;
          
          while(! f1.eof())
          {
               f1 >> fileInput1;
               numbers.insert1(fileInput1); 
          }
      }    

/*-------------------Readding in text file 2--------------------------*/
     if(! f2.is_open())
     {
          cout << "!!!There was an error opening the file!!!" << endl << endl;
     }
     else
     {
          cout << "Reading in data for development hours..." << endl << endl;
          
          while(! f2.eof())
          {
               f2 >> fileInput2;
               numbers.insert2(fileInput2); 
          }
      }
/*-----------Check to make sure all values are correct--------------     
     cout << "Numbers in list1: " << numbers.count1() << endl; 
     cout << "Numbers in list2: " << numbers.count2() << endl; 
     //numbers.display();
     cout << "Sum1: " << numbers.sum1() << endl;
     cout << "Sum2: " << numbers.sum2() << endl;
     cout << "Sum of squares1: " << numbers.square1() << endl;  
     cout << "Sum of squares2: " << numbers.square2() << endl;
     cout << "Sum of products: " << numbers.product() << endl;
-------------------------------------------------------------------*/

     numbers.count1();
     numbers.count2();
     numbers.sum1();
     numbers.sum2();
     numbers.square1();
     numbers.square2();
     numbers.product();

/*----------Print out the calculate correlation results-----------*/
     cout << "Calculating correlation . . ." << endl << endl << endl;   
     cout << "Correlation: " << numbers.correlation() << endl;
     cout << "Correlation Squared: " << numbers.squareCorrelation() << endl;
     
     return 0;
}

/*Initialize size and head pointer*/
Numbers::Numbers()
{
     size1 = 0;
     size2 = 0;
     head1 = NULL;
     head2 = NULL;
}
 
/*Return the size of the linked list1*/         
int Numbers::count1()
{
     return size1;
}

/*Return the size of the linked list2*/         
int Numbers::count2()
{
     return size2;
}

/*Insert data from textfile1 into the linked list*/
int Numbers::insert1(int fileInput)
{
     node *p = new node;
     
     p->proxySize = fileInput;
     p->next = head1;
     head1 = p;
     return size1++;
}

/*Insert data from textfile2 into the linked list*/
int Numbers::insert2(double fileInput)
{
     node *q = new node;
     
     q->developmentHours = fileInput;
     q->next = head2;
     head2 = q;
     return size2++;
}

/*Display the elements in the linked list*/
/*void Numbers::display()
{
     node *p;

     if(p == head)
          cout << "There is nothing in the list." << endl;
     else
     {
          for(p= head; p != NULL; p = p->next)
          {
               cout << p->data; 
               cout << endl;
          }
          cout << endl << endl;
     }    
}*/

/*Calculates the sum of the data in textFile1*/
int Numbers::sum1()
{
     node *p;
     sumOfElements1 = 0;
     
     for (p = head1; p != NULL; p = p->next)
     {
          sumOfElements1 = sumOfElements1 + p->proxySize;
     }
    
     return sumOfElements1;
}

/*Calculates the sum of the data in textFile2*/
double Numbers::sum2()
{
     node *p;
     sumOfElements2 = 0;
     
     for (p = head2; p != NULL; p = p->next)
     {
          sumOfElements2 = sumOfElements2 + p->developmentHours;
     }
    
     return sumOfElements2;
}

/*Calculates the sum of squared data in textFile1*/   
int Numbers::square1()
{
     node *p;
     sumOfSquares1 = 0;

     for (p = head1; p != NULL; p=p->next)
     {
          sumOfSquares1 = sumOfSquares1 + pow(p->proxySize, 2);
     }
     
     return sumOfSquares1;
}

/*Calculates the sum of squared data in textFile2*/ 
double Numbers::square2()
{
     node *p;
     sumOfSquares2 = 0;

     for (p = head2; p != NULL; p=p->next)
     {
          sumOfSquares2 = sumOfSquares2 + pow(p->developmentHours, 2);
     }
     
     return sumOfSquares2;
}

/*Calculates the sum of products from data in textFile1 and textFile2*/ 
double Numbers::product()
{    
     node *p;
     node *q;
     sumOfProduct = 0;

     p = head1;     
     q = head2;
       
     while (p != NULL && q != NULL)
     {
          sumOfProduct = sumOfProduct + (p->proxySize * q->developmentHours);
          p = p->next;
          q = q->next;
     }

     return sumOfProduct;
}

/*Calculates the correlation of the data in textFile1 and textFile2*/
double Numbers::correlation()
{
     double x = 0;  
     double y = 0;
     double z = 0;
     correlationValue = 0;
     
     x = size1 * sumOfProduct - (sumOfElements1 * sumOfElements2);
     y = size1 * sumOfSquares1 - pow(sumOfElements1, 2);
     z = size2 * sumOfSquares2 - pow(sumOfElements2, 2);
     correlationValue = x / (sqrt(y * z));  
     
     return correlationValue;
}
  
/*Calculates the correlation squared*/
double Numbers::squareCorrelation()
{
     return correlationValue * correlationValue;
}



Is This A Good Question/Topic? 0

Replies To: link list size

#2 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: link list size

Posted 21 February 2010 - 06:25 PM

Please read the excellent tutorial from our expert Bench.
Was This Post Helpful? 0
  • +
  • -

#3 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7197
  • View blog
  • Posts: 15,004
  • Joined: 16-October 07

Re: link list size

Posted 21 February 2010 - 08:07 PM

You're writing the same code over and over; don't do that. Use Numbers twice, once for each file. Write a method that takes two instance of Numbers for the combination stuff.

e.g.
class Numbers {         
private:
	int size;
	node *head;
	int sumOfElements;
	int sumOfSquares;     
public:
	Numbers();
	int count();
	int insert(int fileInput);
	void display();
	int sum();
	int square();
};

double product(const Numbers &numbers1, const Numbers &numbers2);


Was This Post Helpful? 0
  • +
  • -

#4 Guest_LapBo*


Reputation:

Re: link list size

Posted 23 February 2010 - 11:20 AM

baavgai,

I tried your solution and cleaned up my code and I have most of it working, however, I am stuck on my product function. I know it isn't working because in the function I use node *head, which is private in the link list, but I don't know how to get the function to work. I tried a couple of things but still cannot seem to get it to work. I am posting the code I have now. The only errors I am getting are that node* Numbers::head is private. Is there any way you could help me with this?

Thanks for the help.

/*---------------------------------------------*/
#include <iostream>
#include <fstream>
#include <cmath>

using namespace std;

struct node
{
     public:
          double data;
          node *next;
};

class Numbers {         
private:
        int size;
        node *head;
        int sumOfElements;
        int sumOfSquares;
          
public:
        Numbers();
        int count();
        int insert(int fileInput);
        void display();
        double sum();
        double square();
                  
};

double product(const Numbers &numbers1, const Numbers &numbers2);

int main()
{
     Numbers numbers1;
     Numbers numbers2;
     ifstream f1("text1.txt");     
     ifstream f2("text2.txt");
     int fileInput1;  //variable to read in textfile1
     double fileInput2; //variable to read in textfile2

/*-------------------Readding in text file 1--------------------------*/
     if(! f1.is_open())
     {
          cout << "!!!There was an error opening the file!!!" << endl;
     }
     else
     {
          cout << "Reading in data for proxy size..." << endl;
          
          while( f1 >> fileInput1 )
          {
               numbers1.insert(fileInput1); 
          }
      }    

/*-------------------Readding in text file 2--------------------------*/
     if(! f2.is_open())
     {
          cout << "!!!There was an error opening the file!!!" << endl << endl;
     }
     else
     {
          cout << "Reading in data for development hours..." << endl << endl;
          
          while( f2 >> fileInput2 )
          {
               numbers2.insert(fileInput2); 
          }
      }
/*-----------Check to make sure all values are correct--------------*/     
     cout << "Numbers in list1: " << numbers1.count() << endl; 
     cout << "Numbers in list2: " << numbers2.count() << endl; 
     //numbers.display();
     cout << "Sum1: " << numbers1.sum() << endl;
     cout << "Sum2: " << numbers2.sum() << endl;
     cout << "Sum of squares1: " << numbers1.square() << endl;  
     cout << "Sum of squares2: " << numbers2.square() << endl;
     cout << "Sum of products: " << product(numbers1, numbers2) << endl;
     //cout << "Average of list1: " << numbers.average1() << endl;
     //cout << "Average of list2: " << numbers.average2() << endl;

     return 0;
}

Numbers::Numbers()
{
     size = 0;
     head = NULL;
}

/*Insert data from textfile1 into the linked list*/
int Numbers::insert(int fileInput)
{
     node *p = new node;
     
     p->data = fileInput;
     p->next = head;
     head = p;
     return size++;
}

/*Return the size of the linked list1*/         
int Numbers::count()
{
     return size;
}

double Numbers::sum()
{
     node *p;
     sumOfElements = 0;
     
     for (p = head; p != NULL; p = p->next)
     {
          sumOfElements = sumOfElements + p->data;
     }
    
     return sumOfElements;
}

double Numbers::square()
{
     node *p;
     sumOfSquares = 0;

     for (p = head; p != NULL; p = p->next)
     {
          sumOfSquares = sumOfSquares + pow(p->data, 2);
     }
     
     return sumOfSquares;
}

double product(const Numbers &numbers1, const Numbers &numbers2)
{    
     node *p;
     node *q;
     double sumOfProduct = 0;

     p = numbers1.head;     
     q = numbers2.head;
       
     while (p != NULL && q != NULL)
     {
          sumOfProduct = sumOfProduct + (p->data * q->data);
          p = p->next;
          q = q->next;
     }

     return sumOfProduct;
}


Was This Post Helpful? 0

#5 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7197
  • View blog
  • Posts: 15,004
  • Joined: 16-October 07

Re: link list size

Posted 23 February 2010 - 12:48 PM

I would do this by making one object process the other. That way, you don't have to reveal the list implementations.

class Numbers {         
private:
	struct node {
		double data;
		node *next;
	};
	
	int size;
	node *head;
	// you really shouldn't use these vars, do it local to the method.
	int sumOfElements;
	int sumOfSquares;
          
public:
	Numbers();
	int count();
	int insert(int fileInput);
	void display();
	double sum();
	double square();
	int getProduct(const Numbers &other) const;
};



int Numbers::getProduct(const Numbers &other) const {    
	node *p = head;
	node *q = other.head;
	int sumOfProduct = 0;

	while (p != NULL && q != NULL) {
		sumOfProduct += (p->data * q->data);
		p = p->next; q = q->next;
	}
	
	// I added this, I think you missed it
	while (p != NULL) { sumOfProduct += p->data; p = p->next; }
	while (q != NULL) { sumOfProduct += q->data; q = q->next; }
	return sumOfProduct;
}

//...
cout << "Sum of products: " << numbers1.getProduct(numbers2) << endl;


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1