GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 10-May-2005, 12:40
CT++ CT++ is offline
New Member
 
Join Date: May 2005
Posts: 7
CT++ is on a distinguished road

Trying to make a vector of structures, need help


Hello,

I am writing a program that runs a bunch of statistical tests on data that is read in from a file. What I am having trouble with is storing the data in structures.

There is a data file that has rows of scores.
Right now, the program reads in each row as a string, splits the strings up, and converts each value to a float. Each float is stored as a value in a vector.

So a string like:
"11 1 1 111 111 1"

Becomes a vector with 6 elements, each one holding a float.

What I ideally want is to have something like this.

DATASET
-GROUPS
-SCORES

CPP / C++ / C Code:
struct Group
{
	vector<float> scores;	//	all scores in group
	int n;					//	number of elements in group
	int k;					//	number of groups in DATASET 
	float mean;				//	group mean
	float sumX;				//	sum of all scores in group
	float sumX_SQRD;		//	(sum of all scores) squared
	float sum_XSQRD;		//	sum of all (scores squared)
};

struct Data
{
	vector<string> allRows;
	vector<Group> groups;
	int k;					// Number of groups in dataset
	int n;					// Number of all elements in all groups
	int dfW;				// Degrees of Freedom WITHIN groups
	int dfB;				// Degrees of Freedom BETWEEN groups
	float fComp, fCrit, cF, ssBetween, ssWithin, ssTotal, msBetween, msWithin;
};

This is the code that I have working for the string conversion to a float of vectors:

CPP / C++ / C Code:
void StripNums(Data data)
//  Does a bunch of crap
{
	Header("Strip Nums");
	string temp;
	vector<Group> newvect(data.k);		// makes a vector of groups to store temp data in
	
	for (int i=0; i < data.k; i++)						//	For every row of data in the dataset
	{													//
		cout << "Row " << i+1 << ": ";					//
		while (data.allRows[i].empty()!=true)			//	Until the string for this row is empty...
	   {												//
			temp.clear();									//	Clear the temp string
			int marker = 0;									//	Set marker to zero 
			marker= data.allRows[i].find(' ');				//	Set marker to the location of first blank char in the string
			if (marker < 1)									//	If there is no whitespace...
				marker=data.allRows[i].size();				//	...set marker to the end of the string
			temp = data.allRows[i].substr(0,marker);		//	Set temp string to all chars between the start of the string and the marker
			data.allRows[i].erase(0, temp.size()+1);		//	Wipe the chars that were copied from the string, as well as the leading blank char
			float moose = atof(temp.c_str());				//	Convert the snipped string into a float
			newvect[i].scores.push_back(moose);				//	Append float to vector
		}													//
		PrintGroup(newvect[i].scores);						//	Printout scores
		cout << endl;										//
	}
}

What I cannot figure out is how to assign the values from the newvect vector into a vector that is a member of a group structure, that is part of a vector of group structures, in the dataset structure.

I thought that I could do it like this:

CPP / C++ / C Code:
	for (int xx=0; xx < data.k; xx++)
	{
		Group data.groups[xx];
		data.groups[xx] = NewGroup();
		cout << data.groups[xx].n;
	}

..but that gives me an error saying "expected initializer before '.' token", which I don't understand. I had thought that it would step through the data.groups vector of group structures, and pass each one into the fuction I wrote that assigns values to the vector of floats, but it doesn't. I think maybe I am not initializing something right, but I don't know what

Any help would be sincerely appreciated!!
  #2  
Old 10-May-2005, 12:52
CT++ CT++ is offline
New Member
 
Join Date: May 2005
Posts: 7
CT++ is on a distinguished road
For reference, this is the function I wrote that passes values into the vector of floats of a group structure:

CPP / C++ / C Code:
Group NewGroup()
{
	Group temp;
	for (int i=0; i < 5; i++)
	{
		temp.scores.push_back(1);			//	all scores in group
	}
	return temp;
}

The iteration of five and value of one are arbitray, I was just using it for testing. This function works great if I run it like this:

CPP / C++ / C Code:
Group cow;
cow = NewGroup();

I will get a cow structure that gets the value of 1 assigned to vector elements 0 through 5.

What I cannot do is get it to "fill" a structure that is part of a vector of structures.

i.e.

CPP / C++ / C Code:
vector<Group> yourmomma;
yourmomma[0] = NewGroup();
cout << yourmomma[0].scores[0];

It will compile and run, but the code above doesn't execute, and I get an "exited due to signal 10 (SIGBUS)" error.
  #3  
Old 10-May-2005, 13:36
Sokar Sokar is offline
Member
 
Join Date: May 2005
Posts: 243
Sokar has a spectacular aura aboutSokar has a spectacular aura about
Quote:
Originally Posted by CT++
What I cannot do is get it to "fill" a structure that is part of a vector of structures.

i.e.

CPP / C++ / C Code:
vector<Group> yourmomma;
yourmomma[0] = NewGroup();
cout << yourmomma[0].scores[0];

It will compile and run, but the code above doesn't execute, and I get an "exited due to signal 10 (SIGBUS)" error.
This is a empty vector
CPP / C++ / C Code:
vector<Group> yourmomma;
No elements you must use push_back to add to that vector.
Maybe this,
CPP / C++ / C Code:
yourmomma.push_back(NewGroup());
or
If you have an idea of how many you need you could include that when make the vector.
CPP / C++ / C Code:
vector<Group> yourmomma(20);
That will have 20 element to start with.
Now you could do something like this for 20,
CPP / C++ / C Code:
yourmomma[0] = NewGroup();
  #4  
Old 10-May-2005, 14:11
CT++ CT++ is offline
New Member
 
Join Date: May 2005
Posts: 7
CT++ is on a distinguished road
Thank you!!!!

I realized I wasn't initializing the groups properly, and ended up doing this:

CPP / C++ / C Code:
void StripNums(Data data)
//  Does a bunch of crap
{
	Header("Strip Nums");								//	Label
	string tempStr;										//	Holds value snipped from beginning of string
	Group tempGrp;										//	Temp value to initialize group structures
	for (int i=0; i < data.k; i++)						//	For each row of data in dataset
	{													//
		data.groups.push_back(tempGrp);					//	Create a group structure to hold scores & other values
	}													//
	for (int i=0; i < data.groups.size(); i++)			//	For each group just created
	{													//
		cout << "Row " << i+1 << ": ";					//	Label
		while (data.allRows[i].empty()!=true)			//	Until the string for this row is empty...
	   {												//
			tempStr.clear();								//	Set temp string to null
			int marker = 0;									//	Set marker to zero 
			marker= data.allRows[i].find(' ');				//	Set marker to location of the first blank char in this string
			if (marker < 1)									//	If there is no whitespace...
				marker=data.allRows[i].size();				//	...set marker to the end of this string
			tempStr = data.allRows[i].substr(0,marker);		//	Set temp string to all chars between the start of the string and the marker
			data.allRows[i].erase(0, tempStr.size()+1);		//	Wipe the chars that were copied from the string, as well as the leading blank char
			float moose = atof(tempStr.c_str());			//	Convert the snipped string into a float
			data.groups[i].scores.push_back(moose);			//	Append float to vector
		}													//
		PrintGroup(data.groups[i].scores);					//	Printout scores by passing vector to function
	}
}	

The relevant part being:
CPP / C++ / C Code:
Group tempGrp;										//	Temp value to initialize group structures
	for (int i=0; i < data.k; i++)						//	For each row of data in dataset
	{													//
		data.groups.push_back(tempGrp);					//	Create a group structure to hold scores & other values
	}													//

I was totally flummoxed on using push_back to add a group, without having any values in it.

Thank you again
  #5  
Old 10-May-2005, 17:00
Sokar Sokar is offline
Member
 
Join Date: May 2005
Posts: 243
Sokar has a spectacular aura aboutSokar has a spectacular aura about
Quote:
Originally Posted by CT++
Thank you again
You are welcome.
 
 

Recent GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Photoshop Tutorial: Make An Inspirational/Mystical Picture ToddSAFM Graphics Forum 9 09-Aug-2005 21:32
Delete and diplaying probrlem with structures. glulu76 C++ Forum 2 09-May-2005 18:23

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 07:16.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.