LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-19-2012, 10:16 PM   #1
animeresistance
Member
 
Registered: Feb 2004
Distribution: Slackware
Posts: 189

Rep: Reputation: 19
Unhappy linked list is not receiving multiple data (homework)


Hi.
Sorry for bothering you but i need help.

I have an assignment to make a program with a classes and calculate linear regression, but my linked list class is just receiving one value and i don't know why . The code compiles but it does not calculates the linear regression ...

Here is my code ...

Code:
#include<iostream>
#include<cmath>
#include<fstream>
#include<iomanip>
#include<istream>
#include<string>


float a,b,r;
float X[20],Y[20],n = 10, z;
int intAux = 0, intI;
//double dblSumX,dblSumY,dblSumX2,dblSumY2,dblSumXY;
float dblSumX,dblSumY,dblSumX2,dblSumY2,dblSumXY;
//double dblSXX,dblSYY,dblSXY;
float dblSXX,dblSYY,dblSXY;

using namespace std;

// Clase Nodo
//p.base=
class Nodo {
	float fltEstProxySize;
	float fltDevelHours;
	float fltEst;
	Nodo* next;

  public:
  	  Nodo() {};
  	  void SetVariables(float data)
  	  {
  	  	  fltEstProxySize = data;
  	  	  fltDevelHours = data;
  	  	  fltEst = data;
  	  }
  	  
  	  void SetData(float EPS, float DH, float ES)
  	  {  
  	  	  fltEstProxySize = EPS;
  	  	  fltDevelHours = DH;
  	  	  fltEst = ES;
  	  };
  	  
  	  void SetNext(Nodo* aNext) 
  	  { 
  	  	  next = aNext;
  	  };
  	  
  	  float Data() 
  	  {  
  	  	  return(fltEstProxySize, fltDevelHours, fltEst);
  	  };
  	  
  	  Nodo* Next() 
  	  { 
  	  	  return next; 
  	  };
};


//p.
//... Clase de la Lista ...
class Lista {
	Nodo *head;
  
  public:
  	  Lista() { head = NULL; };
  	  void Print();
  	  void Append(float fltEstProxySize, float fltDevelHours, float fltEst);
  	  void Delete(float fltEstProxySize, float fltDevelHours, float fltEst);
};


/**
 * ... Imprime el contenido de la lista
 */
 //i.
void Lista::Print() {

	// ... Apuntador temporal ...
	Nodo *tmp = head;

	// ... No hay Nodos ...
	if ( tmp == NULL ) {
		cout << "EMPTY" << endl;
		return;
	}
	
	// ... Existe un Nodo in the Lista ...
	if ( tmp->Next() == NULL ) {
		cout << tmp->Data();
		cout << " --> ";
		cout << "NULL" << endl;
	}
	else {
		// ... Recorre la lista y la imprime ...
		do {
			cout << tmp->Data();
			cout << " --> ";
			tmp = tmp->Next();
		}
		while ( tmp != NULL );
		
		cout << "NULL" << endl;
	}
}

//i.
// ... Agrega un nodo a la lista ...
void Lista::Append(float fltEstProxySize, float fltDevelHours, float fltEst){

	// ... Aqui crea a Nodo nuevo ...
	Nodo* newNodo = new Nodo();
	
	newNodo->SetData(fltEstProxySize, fltDevelHours, fltEst);
	newNodo->SetNext(NULL);
	
	// ... Crea un apuntador temporal ...
	Nodo *tmp = head;
	
	if ( tmp != NULL ) {
		// ... El Nodo esta en la Lista ...
		// ... Recorre la Lista ...
		while ( tmp->Next() != NULL ) {
			tmp = tmp->Next();
		}
		// ... El ultimo Nodo de la lista ...
		tmp->SetNext(newNodo);
	}
	else {
		// ... Nuevo Nodo de la lista ...
		head = newNodo;
	}
}



/**
 * ... Borra un Nodo de la Lista ...
 */
//i.
void Lista::Delete(float fltEstProxySize, float fltDevelHours, float fltEst){

	// ... Crea un Nodo temporal ...
	Nodo *tmp = head;
	
	// ... No hay Nodos ...
	if ( tmp == NULL )
		return;
	
	// ... ultimo Nodo de la Lista ...
	if ( tmp->Next() == NULL ) {
		delete tmp;
		head = NULL;
	}
	else {
		// ... Recorre los nodos ...
		Nodo *prev;
		do {
			//if ( tmp->Data() == data ) break;
			if(tmp->Data() == fltEstProxySize && tmp->Data() == fltDevelHours) break;
			prev = tmp;
			tmp = tmp->Next();
		} while ( tmp != NULL );

		// ... Ajusta los Nodos ...
		prev->SetNext(tmp->Next());
		
		// ... Borra el Nodo actual ...
		delete tmp;
	}
}



//i.
class RegresionLineal
{
	Nodo *head;
	int i;
	//double dblSumX=0,dblSumY=0,dblSumX2=0,dblSumY2=0,dblSumXY=0;
	//double dblSumX,dblSumY,dblSumX2,dblSumY2,dblSumXY;
	//double dblSXX,dblSYY,dblSXY;

public:
	RegresionLineal(){head = NULL;};
	void Calculo();
	
};

void RegresionLineal::Calculo()
{
	for (intI=0;intI < 20; intI++)
	{	
		//if (X[intI] != 0)
		//{
			dblSumX += X[intI];
			dblSumX2 += (X[intI] * X[intI]);
		//}
		
		//if(Y[intI] !=0)
		//{
			dblSumY += Y[intI];
			dblSumY2 += (Y[intI] * Y[intI]);
			dblSumXY += (X[intI] * Y[intI]);
		//}
	}

	dblSXX = dblSumX2 - dblSumX * dblSumX / n;
	dblSYY = dblSumY2 - dblSumY * dblSumY / n;
	dblSXY = dblSumXY - dblSumX * dblSumY / n;
	
	// ... pendiente infinita ...
	if (abs(dblSXX) == 0)
		//return 0;
		z=0;
		
	
	// ... calcula la pendiente ...
	b = dblSXY / dblSXX;
	a = dblSumY / n - (b) * dblSumX / n;
	
	// ... calcula el coeficiente de regresion ...
	if (abs(dblSYY) == 0)
		r = 1;
	else
		r = dblSXY / sqrt(dblSXX * dblSYY);
	
	
}


// ... ... ... ... ... ... ... ... ... ... ... ... ...
int main()
{
	// ... Variables auxiliares ...
	float fltMedia2 = 0;
	float fltDevStd1 = 0;
	float fltDevStd2 = 0;
	float fltSuma1 = 0;
	float fltSuma2 = 0;
	float fltCuenta = 0;
	int intF = 0;
	float fltAux1 = 0, fltAux4 = 0, fltMedia1, fltAux3;
	float fltAux2 = 0;
	

	Lista lista;
	RegresionLineal RL;
	
	// ... inicializa arreglo ...
	for (intI = 0; intI < 20; intI++)
	{
		X[intI] = 0;
		Y[intI] = 0;
	}
	
	// ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... 
	
	cout << "\nPlease introduce the values of table 1: (Note: to finish just type -1 and hit enter)\n";
		
	while(!cin.eof())
	{	//d.
		cout << "\nPlease introduce X: ";
		cin >> fltAux1;
		cout << "\nPlease introduce Y: ";
		cin >> fltAux2;
		cout << "\nPlease introduce the estimate: ";
		cin >> fltAux3;
		//d.
		//d.
		
		if ((fltAux1 == -1) || (fltAux2 == -1))
		{
			break;
		}
		else
		{
			lista.Append(fltAux1, fltAux2, fltAux3);
			X[intI] = fltAux1;
			Y[intI] = fltAux2;
			fltCuenta = fltCuenta + 1;
			intI++;
			cout << "fltAux1: " << fltAux1 << endl;
			cout << "fltAux2: " << fltAux2 << endl;
		}
	}
	
	lista.Print();
	// ... Calcula la regresión lineal ...
	RL.Calculo();
	cout << "\na: " << a << "\n" << endl;
	
	cout << "Suma X: " << dblSumX << endl;
	cout << "Suma Y: " << dblSumY << endl;
	cout << "Suma X2: " << dblSumX2 << endl;
	cout << "Suma Y2: " << dblSumY2 << endl;
	cout << "Suma X*Y:" << dblSumXY << endl;
	
	
	return 0;
}
Any ideas or suggestions ??
 
Old 09-19-2012, 11:39 PM   #2
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,300
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by animeresistance View Post
Any ideas or suggestions ??
The first thing you should do is read the forum rules:
Quote:
Originally Posted by jeremy
  • Do not post homework assignments verbatim. We're happy to assist if you have specific questions or have hit a stumbling point, however. Let us know what you've already tried and what references you have used (including class notes, books, and searches) and we'll do our best to help. Keep in mind that your instructor might also be an LQ member.
The second thing you need to do is look closely at the implementation of RegresionLineal.
 
Old 09-19-2012, 11:48 PM   #3
tc_
LQ Newbie
 
Registered: Sep 2010
Location: Germany
Distribution: Slackware
Posts: 27

Rep: Reputation: 30
Unhappy

You should actually use your Linked List in the Calculo() method:

Code:
void RegresionLineal::Calculo()
{
	for (intI=0;intI < 20; intI++)
	{	
	
	}

	dblSXX = dblSumX2 - dblSumX * dblSumX / n;
	dblSYY = dblSumY2 - dblSumY * dblSumY / n;
	dblSXY = dblSumXY - dblSumX * dblSumY / n;
	
	// ... pendiente infinita ...
	if (abs(dblSXX) == 0)
		z=0;
		
	
	// ... calcula la pendiente ...
	b = dblSXY / dblSXX;
	a = dblSumY / n - (b) * dblSumX / n;
	
	// ... calcula el coeficiente de regresion ...
	if (abs(dblSYY) == 0)
		r = 1;
	else
		r = dblSXY / sqrt(dblSXX * dblSYY);
	
}
There is not a single access to your list in the above method. Apart from that, you construct your RegressionLineal instance in main with default parameters
Code:
	Lista lista;
	RegresionLineal RL;
which means that the RegressionLineal in question only knows about the empty list.

Code:
class RegresionLineal
{
	Nodo *head;
public:
	RegresionLineal(){head = NULL;};
};
I also strongly suggest that you should not use any global variables like 'a' and the various 'sumXYZ'.

EDIT:
Sorry for posting homework "answers" just a second after being reminded of the forum rules

Last edited by tc_; 09-19-2012 at 11:52 PM.
 
Old 09-20-2012, 04:52 AM   #4
animeresistance
Member
 
Registered: Feb 2004
Distribution: Slackware
Posts: 189

Original Poster
Rep: Reputation: 19
Oopsss ... sorry my bad ...

It will not happen again, sorry i'm terribly sorry, please forgive me.

And thanks for the reply ...
 
  


Reply

Tags
c++, homework


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Linked list data structure in PHP? ivanatora Programming 6 08-24-2007 07:09 PM


All times are GMT -5. The time now is 01:19 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration