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 05-Dec-2008, 12:10
Crytek Crytek is offline
New Member
 
Join Date: Dec 2008
Posts: 3
Crytek is on a distinguished road

Sorting an array of structs


Hello, I am in need of serious help for this assignment. The program calls to read and write data from a in file to an out file. The program does that successfully. I need help adding the sort array function for the "lnames", "exams", "avg" that is inside the struct Grades. Here is the code.
CPP / C++ / C Code:
#include<iostream>
#include<string>
#include<iomanip>
#include<fstream>
#include<cstdlib>
using namespace std;

#define outFile "output.txt"
#define inFile "studInfo.txt"

struct Grades
{
	float exams[3];
	float avg;
	string lname;
	char grade;
};

char lGrade(float avg);
void write(ofstream& out, Grades);
int read(ifstream & in, Grades studs[]);
int search(const Grades studs[], int numberUsed, string target);

int main()
{
	//Variable declaration
	Grades studs[30];//Array of structures
	float avg, total = 0.0;
	int numStuds, index;
	string target;
	ofstream out;
	ifstream in;
	
	out.open(outFile);
	if (out.fail())
	{
		cerr<<"***ERROR: Cannot open " <<outFile
			<<" for output." << endl;
		out.close();
		return EXIT_FAILURE; 
   }

   in.open(inFile);
   if (in.fail())
   {
	   cerr<<"***ERROR: Cannot open " <<inFile
		   <<" for input." << endl;
	   in.close();
	   return EXIT_FAILURE; 
   }

   //Variable initialization
   int ch;

   //Dynamic entry of the data
   numStuds = read(in, studs);
   for(int i = 0; i < numStuds; i++)
   {
	   total = 0;
		for (int j = 0; j < 3; j++)
			{
				//calculation
				total += studs[i].exams[j];
			}
		studs[i].avg=total / 3.0;
		avg = studs[i].avg;
		//letter grade: call the lGrade function
		
		studs[i].grade = lGrade(avg);
		//display the results
		write(out,studs[i]);	
   }
	out.close();
	in.close();

	//Search section
	cout<<"Please enter the student's last name: ";
	cin>>target;

	//Call the function
	index = search(studs, numStuds, target);
	if (index != -1)
		cout<<"The letter grade for "<<target<<" is "<<studs[index].grade<<endl;
	else
		cout<<"The student is not enrolled in the course."<<endl;

	for (int pass = 1; pass < 30; pass++) 
	{
		for (int i=0; i < 30-pass; i++) 
		{
			if (studs[i] > studs[i+1]) 
			{
               int temp = studs[i]; studs[i] = studs[i+1]; studs[i+1] = temp;
			}
		}
	}
	cout<<temp<<endl;
	return 0;
}
int read(ifstream &in, Grades studs[])
{
	int ch; int i = 0;

	while(ch = in.peek() != EOF)
	{
		in >> studs[i].lname;
	
		for(int a = 0; a < 3; a++)
			in >> studs[i].exams[a];
		i++;
	}
	return(i);
}
char lGrade(float avg)
{
	//Declare variable
	char grade;

	//Calculate letter grade
	if(avg >= 90)
		grade = 'A';
	else if (avg >= 80)
		grade = 'B';
	else if (avg >= 70)
		grade ='C';
	else if (avg >= 60)
		grade = 'D';
	else
		grade = 'F';
	return grade;
}

void write (ofstream& out, Grades studs)
{
	out<<setiosflags(ios::fixed)<<setprecision(2);
	out<<"Name: "<<studs.lname<<endl
		<<"Average: "<<studs.avg<<endl
		<<"Grade: "<<studs.grade<<endl<<endl;	
}
int search(const Grades studs[], int numberUsed, string target)
{
	int index = 0;
	bool found = false;
	while ((!found) && (index < numberUsed))
		if (target == studs[index].lname)
			found = true;
		else
			index++;

		if (found)
			return index;
		else 
			return -1;
}


The input file is studinfo.txt and here it is.
Code:
stud1 90 90 90 stud2 80 80 80 stud3 80 80 80 stud4 70 70 70 stud5 85 85 85
  #2  
Old 05-Dec-2008, 16:14
L7Sqr L7Sqr is online now
Member
 
Join Date: Jul 2005
Location: constant limbo
Posts: 234
L7Sqr is a jewel in the roughL7Sqr is a jewel in the rough

Re: help with sorting an array of structs


Well, you could just supply operator< to struct Grades. Then you could do something like
CPP / C++ / C Code:
Grades g1, g2;
// ... fill in values ...
if (g1 < g2) {
   // do something
} else {
   // do something else
}
__________________
My personal site: Utilities for text processing, debugging, testing and plotting
  #3  
Old 06-Dec-2008, 18:42
Crytek Crytek is offline
New Member
 
Join Date: Dec 2008
Posts: 3
Crytek is on a distinguished road

Re: Sorting an array of structs


Can't do that haha. The sorting part has to have its owns function like
void sortArray or along those lines. I can't decide how to do it. Which do you prefer is easier bubble or selection sort?
  #4  
Old 06-Dec-2008, 23:16
L7Sqr L7Sqr is online now
Member
 
Join Date: Jul 2005
Location: constant limbo
Posts: 234
L7Sqr is a jewel in the roughL7Sqr is a jewel in the rough

Re: Sorting an array of structs


Given
CPP / C++ / C Code:
struct Grades
{
	float exams[3];
	float avg;
	string lname;
	char grade;
};
you can implement a bubble sort like so:
CPP / C++ / C Code:
// this is pseudo code
void bubbleSort(Array of Grades) {
   for each element in the array grades
      for each element in the array grades starting at above index
         if comparator(grade1, grade2)
            swap(grade1, grade2)
}

int comparator(Grade, Grade) {
   if Grade.avg < Grade.avg
      // do something
   //...
}
__________________
My personal site: Utilities for text processing, debugging, testing and plotting
  #5  
Old 07-Dec-2008, 18:21
Crytek Crytek is offline
New Member
 
Join Date: Dec 2008
Posts: 3
Crytek is on a distinguished road

Re: Sorting an array of structs


Hmm...Okay well in the comments of your answer what would I put? do i put a for statement? If i do can i get an example as a way to sort it from low to high? Sorry if i am annoying, but im bad with sorting functions lol.
 
 

Recent GIDBlogOnce again, no time for hobbies 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
where is the problem and can you fix it (php) oggie MySQL / PHP Forum 8 14-Apr-2008 16:08
Getting a line error in register oggie MySQL / PHP Forum 5 13-Apr-2008 17:16
How to sort in C++ alphabetically wilen C++ Forum 5 20-Apr-2007 15:43
Need help deleting the last element in the array headphone69 C++ Forum 2 15-Mar-2006 20:31
sorting a two-dimensional array using qsort Hyuga C Programming Language 20 24-Aug-2005 09:10

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

All times are GMT -6. The time now is 14:55.


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