-
alphabetizing
hi ,
i am trying to alphabetize a list of names...
the thing is that i am passing these students from an array inside a struct,
am i even close?
here's is where i am-
Code:
//my struct:
struct Student{
char lastname[20];
char firstname[20];
double exams[4];
double average;
char letgrade;
bool passed;};
//prototype:
void Alph(Student[], int ns)
//call:
Alph(ClassList, ns);
//Function C Alph============================================
void Alph(Student CL[], int ns){
int tempstu = 0;
for (int x = 0; x < ns; x++){
if( CL[x].lastname, CL[x+1].lastname >0);
//do swap
tempstu = CL[x].lastname;
CL[x].lastname = CL[x+1].lastname;
CL[x+1].lastname = tempstu;
}
}
}
-
Huh? Way off! Do yourself a favor and read those book that are gathering dust on your shelves! Anyway, for textual data, use strcmp() for string comparison, and strcpy for string copying.
Here's a simple bubble sort:
Code:
#define MAX_BUFF 50
struct Student{
char lastname[MAX_BUFF];
char firstname[MAX_BUFF];
double exams[4];
double average;
char letgrade;
bool passed;
};
void Alpha_Last(Student CL[], int ns){
char tempstu[MAX_BUFF];
int done = FALSE;
while( !done )
{
done = TRUE;
for(int x = 0; x < ns - 1; x++) //...ns-1 so we can safely compare...
{
if( strcmp(CL[x].lastname, CL[x+1].lastname) < 0)
{
done = FALSE;
strcpy(tempstu, CL[x].lastname);
strcpy( CL[x].lastname, CL[x+1].lastname);
strcpy(CL[x+1].lastname, tempstu);
}
}
}
}
-
I doubt he wants just the lastname to be switched. Probably the whole structure seeing as it represents a student.
Code:
// Student class declaration
class Student
{
public:
Student &operator=(const Student &st)
{
memcpy(reinterpret_cast<void*>(&exams), reinterpret_cast<const void*>(&st.exams), 4);
average = st.average;
letgrade = st.letgrade;
passed = st.passed;
firstname = st.firstname;
lastname = st.lastname;
return *this;
}
double exams[4];
double average;
char letgrade;
bool passed;
string firstname;
string lastname;
};
// Sorting function
void Alph(Student CL[], int ns)
{
Student tempstu;
for (int i = 0; i < ns; i++)
for (int j = i+1; j < ns; j++)
{
if (CL[i].lastname[0] > CL[j].lastname[0])
{
tempstu = CL[i];
CL[i] = CL[j];
CL[j] = tempstu;
}
}
}
-
Since you've made an effort and posted some code and this is the C++ board I will ask you if you have considered using the STL container templates and the built in sorting routines?
Code:
#include <vector>
#include <algorithm>
#include <string>
#include <iostream>
using namespace std;
struct Student
{
string lastname;
string firstname;
double exams[4];
double average;
char letgrade;
bool passed;
bool operator< (const Student& rhs) // Required to make 'sort' function call work
{
if( lastname < rhs.lastname ) return true;
else if( (lastname == rhs.lastname) && (firstname < rhs.firstname) ) return true;
else return false;
}
Student( const string& last, const string& first )
{
lastname = last;
firstname = first;
}
};
// Some code to demonstrate initializing and adding to the vector.
vector<Student> StudentArray;
vector<Student>::iterator itStudents;
// Push some Student objects onto the vector.
StudentArray.push_back( Student("Bbbbb","Ccccc") );
StudentArray.push_back( Student("Bbbbb","Bbbbb") );
StudentArray.push_back( Student("Bbbbb","Aaaaa") );
StudentArray.push_back( Student("Aaaaa","Ddddd") );
// Output vector contents prior to sorting.
cout << "Before sort:" << endl;
for( itStudents = StudentArray.begin(); itStudents != StudentArray.end(); ++itStudents )
cout << "LastName: " << itStudents->lastname << " FirstName: " << itStudents->firstname << endl;
// Call the sort function to sort the vector.
sort( StudentArray.begin(), StudentArray.end() );
// Output sorted contents.
cout << "After sort:" << endl;
for( itStudents = StudentArray.begin(); itStudents != StudentArray.end(); ++itStudents )
cout << "LastName: " << itStudents->lastname << " FirstName: " << itStudents->firstname << endl;
Should output something like this if I've done everything correctly and made no typos.
Before sort:
LastName: Bbbbb FirstName: Ccccc
LastName: Bbbbb FirstName: Bbbbb
LastName: Bbbbb FirstName: Aaaaa
LastName: Aaaaa FirstName: Ddddd
After sort:
LastName: Aaaaa FirstName: Ddddd
LastName: Bbbbb FirstName: Aaaaa
LastName: Bbbbb FirstName: Bbbbb
LastName: Bbbbb FirstName: Ccccc
-
sorting by just the first letter of the last name probably not adequate either, but is a step in the right direction, if you were using STL strings rather than Cstyle strings (Cstyle strings were used in the original post so using strcmp() to compare students by their last name and then switching entire students, not just the last name, is probably more helpful). If allowed to use the STL and/or the standard sort() routine, fine, but I suspect he/she needs to use Cstyle strings with strcmp() and create his/her own bubble sort (or other sorting routine).
-
thanks to all
<major sigh>
okay....
let's see
i do wish to sort the whole class based on last name...
i hope the examples indicate that.
i am unfamiliar with vectors...and was given this program to write after a half hour introduction to structs..that's where i get confused.
i can do a sort with numbers or characters but this is making me dizzy!
i have not done classes at ALL!
what are container templates and the built in sorting routines?
i'd like to post my entire program so that you guys can see what i have done so far but don't want to get jazzed on for doing so by the ppl that think i am looking for someone to wrote my homework for me..
but this forum has been my only support pretty much..
pease let me know?
i so get it when i am in class and i get here and feel really frustrated!
thanks alot all- please let me know
mouse
ps i am a she
-
My example would suffice for what you're attempting to do.
Sorry about the class I stuck in there (by the way, classes and structs are essentially the same) so you can replace the class keyword with struct if it makes you feel any better.
I dumbed my code down considerably, you should be able to understand most of it; if not then just ask.
Code:
struct Student
{
double exams[4];
double average;
char letgrade;
bool passed;
string firstname;
string lastname;
};
void Alph(Student CL[], int ns)
{
Student tempstu;
// Basic implementation of Bubble Sort, search google for more info
for (int i = 0; i < ns; i++)
for (int j = i+1; j < ns; j++)
{
if (CL[i].lastname[0] > CL[j].lastname[0])
{
tempstu = CL[i];
CL[i] = CL[j];
CL[j] = tempstu;
}
}
}
int main()
{
Student list[5];
// fill our list with some names to sort by
list[4].lastname = "evan";
list[3].lastname = "bob";
list[2].lastname = "nigel";
list[1].lastname = "alice";
list[0].lastname = "xavier";
Alph(list, 5);
// call our sorting function and print out the order our names are now in
for (int i = 0; i < 5; i++)
cout << list[i].lastname << endl;
cin.get(); // Pause the program
}
-
thanks so much
for trying to help me...
i don't understand your adding the strings into the struct...
my instructor went over what he expected for this function and it didn't modify my struct at all.
I am giving you my entir file as an attachment so you can see
everything i am doing...I am reading in students and then doing a calculation and alphabetizing etc..
THANK YOU so much for your help..
(i do get the sirt part...it's when the struct somes in i get messed up!)
thanks again, i really want to understand it
Mouse
-
//simple program to sort struct by embedded Cstyle string. Not compiled.
Code:
#include <iostream.h>
#include <string.h>
const int MAX = 3;
struct People
{
char lastName[30];
int age;
}
void sortPeople(People *, int);
int main()
{
int i;
People people[MAX];
strcpy(people[0].lastName, "Smith");
people[0].age = 18;
strcpy(people[1].lastName, "Jones");
people[1].age = 24;
strcpy(people[2].lastName, "Johnson");
people[2].age = 22;
cout << "Before sort" << endl;
for(i = 0; i < MAX; i++)
cout << people[i].lastName << " is " << people[i].age << endl;
sortPeople(people, MAX);
cout << endl << "After sort" << endl;
for(i = 0; i < MAX; i++)
cout << people[i].lastName << " is " << people[i].age << endl;
return 0;
}
void sortPeople(People * people, int MAX)
{
int i, j;
People temp;
//slightly different implementation of bubble sort than before
for(i = 0; i < MAX; i++)//how many times to go throgh loop
{
for(j = 0; j < MAX - i - 1; j++)//which structs to use
{
///comparison of lastNames
if(strcmp(people[j].lastName, people[j+1].lastName ) > 0)
{
//swap entire struct
temp = people[j + 1];
people[j + 1] = people[j];
people[j] = temp;
}
}
}
}