Matching students to university problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • luap
    New Member
    • Mar 2007
    • 40

    #46
    I took the input file out simply because i wanted to tackle these code first. After that i will try to handle it.



    please i would like to know how to make max_average_gra de compactible with
    universities average_grade, should i max_average_gra de as an array?

    Code:
     :\dokumente und einstellungen\paul\eigene dateien\visual studio 2005\projects\matching\matching\matching.cpp(88) : error C2039: 'max_average_grade' : is not a member of 'universities'
    This is how my code looks as at the moment.

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    
    
    
    struct universities
          {
    	  string name;
    	  int places;
    	  double average_grade;
    	  int akzept;
    	  }university;
           
    
    struct students
     {
      int id;
      universities choice[6]; 
      universities average_grade;
      double my_average_grade;
    }student;
    
    
    
    struct matching_student{
    	   universities match;
    	   students matchd;
           }matching; 
       
    
    
    int _tmain(int argc, _TCHAR* argv[])
      
    {
      double max_average_grade;
      int i, j, k; 
    
     
      int student_no;
      int uni_start;
    
      const int no_of_uni=30;
      universities university[no_of_uni];
      const int no_of_student=50;
      students student[no_of_student]; 
    
    
     
     
    for(int i=0;i<=30;i++)
    {
     university[no_of_uni].name=uni_start;
     }	
    
    for(int j=0;j<50;j++)
    {
    	student[no_of_student].id=student_no;
    }
     
    
    
    
     for(int i=0;i<30;i++)
      {
        for(int j=i+1;j<30;i++)
        {
         if (university[i].average_grade < university[j].average_grade)
    	  {
    		  max_average_grade=university[j].average_grade;
    	      university[j].average_grade=university[i].average_grade;
    	      university[i].average_grade=max_average_grade;
    	  }
        } 
      }
    
     
    for(int i;i<=30;i++)
     {
      
    
    	 for(int j=0;j<=30;j++)
         {
    	  cout << university[j].max_average_grade;
    	  university[i].average_grade=university.[j].max_average_grade;
    	  
    	  while(university[i].places > 0 && university[i].average_grade)
    	  {
    	   for(int j=0;j<=50;j++)
    	    {
    		   for(int k=0;k<=student[j].choice[6];k++)
    		   {
    		     if(student[j].my_average_grade==university[j].average_grade && student[j].choice[k]==university[i].name)
    		       {
                   university[i].akzept=student[j].choice[k];
    		       }
    	      }
           }
    	 }
    	   --university[i].places;
      }  
      
     
      return 0;
      }
    Thanks.
    Last edited by Ganon11; Mar 22 '07, 12:06 PM. Reason: code tags added

    Comment

    • Roonie
      New Member
      • Mar 2007
      • 99

      #47
      Originally posted by luap
      I took the input file out simply because i wanted to tackle these code first. After that i will try to handle it.
      good idea.
      Originally posted by luap
      should i max_average_gra de as an array?
      AAAACK!!! that should not be your first impulse! arrays are horribly tricky things and you should use them only when you need to. to figure out what to do with it, first, please tell me again what exactly its purpose is?

      Comment

      • luap
        New Member
        • Mar 2007
        • 40

        #48
        i want to arrange the universities in this order: The university with the highest average grade. So my average_grade is to help me change with changing of position, so that after the for loop, the universities would be arraged in order of
        high their averge grade.

        Comment

        • Roonie
          New Member
          • Mar 2007
          • 99

          #49
          Code:
          for(int i=0;i<30;i++)
          {
          for(int j=i+1;j<30;i++)
          {
          if (university[i].average_grade < university[j].average_grade)
          {
          max_average_grade=university[j].average_grade;
          university[j].average_grade=university[i].average_grade;
          university[i].average_grade=max_average_grade;
          }
          } 
          }
          it looks like thats what youre doing here . . . is it possible you dont need max_average_gra de anywhere else? therefore it doesnt need to be in your structs or anywhere else in your code.

          what about akzept?

          Comment

          • luap
            New Member
            • Mar 2007
            • 40

            #50
            for my akzept i just want to allocate / match a student to a university when there is still a place aviable. This must hold that :

            1.The student has choosen the university and also have the the required grade.
            2. There must be a place free at the university.

            Thanks.

            Comment

            • Roonie
              New Member
              • Mar 2007
              • 99

              #51
              so is akzept supposed to be a function?

              it looks like this:
              Code:
              for(int i;i<=30;i++)
              {
              	for(int j=0;j<=30;j++)
              	{
              		cout << university[j].max_average_grade;
              		university[i].average_grade=university.[j].max_average_grade;
              		while(university[i].places > 0 && university[i].average_grade)
              		{
              			for(int j=0;j<=50;j++)
              			{
              				for(int k=0;k<=student[j].choice[6];k++)
              				{
              					if(student[j].my_average_grade==university[j].average_grade && student[j].choice[k]==university[i].name)
              					{
              						university[i].akzept=student[j].choice[k];
              					}
              				}
              			}
              		}
              		--university[i].places;
              	}
              }
              is where you are trying to do the matching. if that is your method of making matches and you want to clean it up, first try to come up with some ideas how you might store the matches you make. it looks like youre set up to store the matches in an array of struct matching_studen t. so instead of using akzept, just store the match there, right? (get rid of akzept.)

              try that and repost your code.

              Comment

              • luap
                New Member
                • Mar 2007
                • 40

                #52
                This how i think the problem should be solved. Because not all students will be matched, because of limited number of places available. And for a student to matched to a university, the conditions must be fulfiled 1. average_grade and choice of the universtiy.

                Code:
                //
                #include "stdafx.h"
                #include <iostream>
                #include <string>
                using namespace std;
                
                
                
                
                
                struct universities
                      {
                	  string name;
                	  int places;
                	  double average_grade;
                	  
                	  }university;
                       
                
                struct students
                 {
                  int id;
                  universities choice[6]; 
                  universities average_grade;
                  double my_average_grade;
                }student;
                
                
                
                struct matching_student{
                	   universities match;
                	   students matchd;
                       }matching; 
                   
                
                
                int _tmain(int argc, _TCHAR* argv[])
                  
                {
                  double max_average_grade;
                  int i, j, k; 
                
                 
                  int student_no;
                  int uni_start;
                
                  const int no_of_uni=30;
                  universities university[no_of_uni];
                  const int no_of_student=50;
                  students student[no_of_student]; 
                  const int no_of_matched_students;
                  matching_student matching[no_of_matched_students];
                
                
                 
                 
                for(int i=0;i<=30;i++)
                {
                 university[no_of_uni].name=uni_start;
                 }	
                
                for(int j=0;j<50;j++)
                {
                	student[no_of_student].id=student_no;
                }
                 
                
                
                
                 for(int i=0;i<30;i++)
                  {
                    for(int j=i+1;j<30;i++)
                    {
                     if (university[i].average_grade < university[j].average_grade)
                	  {
                		  max_average_grade=university[j].average_grade;
                	      university[j].average_grade=university[i].average_grade;
                	      university[i].average_grade=max_average_grade;
                	  }
                    } 
                  }
                
                 
                for(int i;i<=30;i++)
                 {
                  
                
                	 for(int j=0;j<=30;j++)
                     {
                	  cout << university[j].max_average_grade;
                	  university[i].average_grade=university.[j].max_average_grade;
                	  
                	  while(university[i].places > 0 && university[i].average_grade)
                	  {
                	   for(int j=0;j<=50;j++)
                	    {
                		   for(int k=0;k<=student[j].choice[6];k++)
                		   {
                		     if(student[j].my_average_grade==university[j].average_grade && student[j].choice[k]==university[i].name)
                		       {
                				   matching[no_of_matched_students]=student[j].choice[k];
                		       }
                	      }
                       }
                	 }
                	   --university[i].places;
                  }  
                  
                 
                  return 0;
                  }
                Thanks once again.
                Last edited by Ganon11; Mar 22 '07, 12:04 PM. Reason: code tags added

                Comment

                • Roonie
                  New Member
                  • Mar 2007
                  • 99

                  #53
                  well since your matching_studen t looks like this:
                  Code:
                  struct matching_student{
                  	   universities match;
                  	   students matchd;
                         }matching;
                  shouldnt your code to store the match look more like this:
                  Code:
                  matching_student[no_of_match].match=university[i];
                  matching_student[no_of_match].matchd=student[k];
                  ?

                  you also need to

                  1) find a length to initialize your matching_studen t[] to. i would suggest using an array the same size as student[]. (50.)

                  2) you also need to straighten out the variables you are using as iterators in your for loops. you cant use your iterator inside the loop for anything else but sequntial access (or sequential comparison . . .) specifically, in the last block of code, the second nested for loop needs a different iterator since j is already in use.

                  lets build a new for loop. why dont we begin with a basic method to find and assign your matches:
                  Code:
                  if(<the match of university[i] to student[k] is good>)
                  {
                  matching_student[no_of_match].match=university[i];
                  matching_student[no_of_match].matchd=student[k];
                  }
                  how would you write a conditional statement to see if the match is good?

                  Comment

                  • Roonie
                    New Member
                    • Mar 2007
                    • 99

                    #54
                    or like this, i mean:
                    Code:
                    if(<the match of university[i] to student[k] is good>)
                    {
                    matching[no_of_match].match=university[i];
                    matching[no_of_match].matchd=student[k];
                    }
                    sorry, youre just going to have to work with me here too.

                    Comment

                    • luap
                      New Member
                      • Mar 2007
                      • 40

                      #55
                      PS this how i think the the students could be properly matched. I am not really sure.

                      so i send my code.

                      Thanks
                      Code:
                      #include "stdafx.h"
                      #include <iostream>
                      #include <string>
                      using namespace std;
                      
                      
                      
                      
                      
                      struct universities
                            {
                      	  string name;
                      	  int places;
                      	  double average_grade;
                      	  }university;
                      	  
                             
                      
                      struct students
                       {
                        int id;
                        universities choice[6]; 
                        universities average_grade;
                        double my_average_grade;
                      }student;
                      
                      
                      
                      struct matching_student{
                      	   universities match;
                      	   students matchd;
                             }matching; 
                         
                      
                      
                      int _tmain(int argc, _TCHAR* argv[])
                        
                      {
                        double max_average_grade;
                        int i, j, k,c; 
                      
                       
                        int student_no;
                        int uni_start;
                      
                        const int no_of_uni=30;
                        universities university[no_of_uni];
                        const int no_of_student=50;
                        students student[no_of_student];
                        const int no_of_match=50;
                        matching_student[no_of_match].match=university[i];
                        matching_student[no_of_match].matchd=student[k];
                        
                        
                      
                       
                       
                      for(int i=0;i<=30;i++)
                      {
                       university[no_of_uni].name=uni_start;
                       }	
                      
                      for(int j=0;j<=50;j++)
                      {
                      	student[no_of_student].id=student_no;
                      }
                       
                      
                      
                      
                       for(int i=0;i<30;i++)
                        {
                          for(int j=i+1;j<30;i++)
                          {
                           if (university[i].average_grade < university[j].average_grade)
                      	  {
                      		  max_average_grade=university[j].average_grade;
                      	      university[j].average_grade=university[i].average_grade;
                      	      university[i].average_grade=max_average_grade;
                      	  }
                          } 
                        }
                      
                       
                      for(int i;i<=30;i++)
                       {
                        
                      
                      	 for(int j=0;j<=30;j++)
                           {
                      	  cout << university[j].max_average_grade;
                      	  university[i].average_grade=university.[j].max_average_grade;
                      	  
                      	  while(university[i].places > 0 && university[i].average_grade)
                      	  {
                      	   for(int c=0;c<=50;c++)
                      	    {
                      		   for(int k=0;k<=student[c].choice[6];k++)
                      		   {
                      		     if(student[c].my_average_grade==university[j].average_grade && student[c].choice[k]==university[j].name)
                      		       {
                      				 {
                                        matching_student[no_of_match].match=university[i];
                                        matching_student[no_of_match].matchd=student[c];
                                       }
                      		       }
                      	      }
                             }
                      	 }
                      	   --university[i].places;
                      }
                      for(j=0;j<=50;j++)
                      {
                      	if(matching_student[no_of_match].match->my_average_grade==matching_student[no_of_matcth.average_grade)
                      {
                      matching_student[no_of_match].match=university[i];
                      matching_student[no_of_match].matchd=student[k];
                      }
                        }  
                        
                       
                        return 0;
                        }

                      Comment

                      • Roonie
                        New Member
                        • Mar 2007
                        • 99

                        #56
                        ok.

                        were going to rebuild your program completely.

                        it looks like your structs are ok for now:
                        Code:
                        struct universities
                        {string name;
                        int places;
                        double average_grade;
                        }university;
                        
                        struct students
                        {int id;
                        universities choice[6]; 
                        //universities average_grade; //except for this.
                        double my_average_grade;
                        }student;
                        
                        struct matching_student
                        {universities match;
                        students matchd;
                        }matching;
                        now. in your main(), lets get your file input back in the picture, because i think it will be easier to get your program written if we can see what were doing to each line of input. (like you wanted, lets work on one thing at a time, but lets work on the input part first so we have the data accessible for the rest of the program) you were almost correct before . . . you want to start getting input after setting up your variables, and then put the rest of your code inside the file-access loop.

                        (these are your declaration/initializations - which are also good for the time being):
                        Code:
                        double max_average_grade;
                        int i, j, k, c; 
                        int student_no, uni_start;
                        
                        const int no_of_uni=30;
                        universities university[no_of_uni];
                        const int no_of_student=50;
                        students student[no_of_student];
                        const int no_of_matches=50;
                        matching_student match[no_of_matches];
                        so your program should look like something of the following:
                        Code:
                        //put your struct def's here
                        
                        int main()
                        {
                        //and put your declarations/initializations here
                        
                        //now you want to access the file and get your input:
                        
                        string inputData;
                        while (!in.eof())
                        {
                        	getline(data, inputData, '\n');
                        //getline() is a function that will read in a line from istream data until a '\n'
                        //is found and will put the line into the string inputData.
                        //see http://www.cprogramming.com/tutorial/string.html
                        
                        	<you should then take the data and store it in the appropriate struct arrays.>
                        } // now you are done getting your input.
                        
                        //next, you sort the university data and process matches.
                        try rewriting your code like that and then repost it.

                        Comment

                        • luap
                          New Member
                          • Mar 2007
                          • 40

                          #57
                          i am a bit confused, how to present the conditional statment.

                          I was thinking to compare the students average grade with the universities of their choice and if this true then the student should not be matched to any other university again, and the place of the university should be reduced by one.
                          Code:
                          for(j=0;j<=50;j++)
                          {
                          	if(matching_student[no_of_match].match->my_average_grade==matching_student[no_of_matcth.average_grade)
                          {
                          matching_student[no_of_match].match=university[i];
                          matching_student[no_of_match].matchd=student[k];
                          }
                          This is what i thank.

                          Thanks.

                          Comment

                          • Roonie
                            New Member
                            • Mar 2007
                            • 99

                            #58
                            well the way you described it earlier,
                            Code:
                            if(<the student has chosen the university> && <the students average is high enough> && <the university has spots left>)
                            {
                            <accept the student>
                            }
                            right?

                            Comment

                            • luap
                              New Member
                              • Mar 2007
                              • 40

                              #59
                              First of all thanks so much for the help.

                              I have made some changes to my code and this is how it looks like now.

                              Code:
                              #include "stdafx.h"
                              #include <iostream>
                              #include <string>
                              #include <fstream>
                              using namespace std;
                              
                              
                              
                              
                              
                              struct universities
                                    {
                              	  string name;
                              	  int places;
                              	  double average_grade;
                              	  }university;
                              	  
                                     
                              
                              struct students
                               {
                                int id;
                                universities choice[6]; 
                                universities average_grade;
                                double my_average_grade;
                              }student;
                              
                              
                              
                              struct matching_student{
                              	   universities match;
                              	   students matchd;
                                     }matching; 
                                 
                              
                              
                              int _tmain(int argc, _TCHAR* argv[])
                                
                              {
                                double max_average_grade;
                                int i, j, k,c; 
                              
                               
                                int student_no;
                                int uni_start;
                              
                                const int no_of_uni=30;
                                universities university[no_of_uni];
                                const int no_of_student=50;
                                students student[no_of_student];
                                const int no_of_match=50;
                                matching_student[no_of_match].match=university[i];
                                matching_student[no_of_match].matchd=student[k];
                              
                                string studentFile.txt;
                                string universityFile.txt;
                                
                              
                                while (!studentFile.eof())
                                   {
                              	  getline(cin, studentFile.txt '\n');
                                    for(int i=0;i<universityFile.txt.length(); i++)
                                        {
                                        cout << universityFile.txt[i];
                                        university[no_of_uni]=universityFile.txt[i];
                              	      } 
                                while(!universityFile.eof());
                                    getlint(cin, universityFile.txt 'n\');
                                    for(int i = 0; i < stdudentFile.txt.length(); i++)
                                        {
                                        cout<< studentFile.txt[i];
                              	      student[no_of_students]=studentFile.txt[i];
                                        }
                                  
                               for(int i=0;i<=30;i++)
                                {
                                university[no_of_uni].name=uni_start;
                                }	
                              
                              for(int j=0;j<=50;j++)
                              {
                              	student[no_of_student].id=student_no;
                              }
                               
                              
                              
                              
                               for(int i=0;i<30;i++)
                                {
                                  for(int j=i+1;j<30;i++)
                                  {
                                   if (university[i].average_grade < university[j].average_grade)
                              	  {
                              		  max_average_grade=university[j].average_grade;
                              	      university[j].average_grade=university[i].average_grade;
                              	      university[i].average_grade=max_average_grade;
                              	  }
                                  } 
                                }
                              
                               
                              for(int i;i<=30;i++)
                               {
                                
                              
                              	 for(int j=0;j<=30;j++)
                                   {
                              	  cout << university[j].max_average_grade;
                              	  university[i].average_grade=university.[j].max_average_grade;
                              	  
                              	  while(university[i].places > 0 && university[i].average_grade)
                              	  {
                              	   for(int c=0;c<=50;c++)
                              	    {
                              		   for(int k=0;k<=student[c].choice[6];k++)
                              		   {
                              		     if(student[c].my_average_grade>=university[j].average_grade && student[c].choice[k]==university[j].name)
                              		       {
                              				 {
                                                matching_student[no_of_match].match=university[i];
                                                matching_student[no_of_match].matchd=student[c];
                                               }
                              		       }
                              	      }
                                     }
                              	 }
                              	   --university[i].places;
                              }
                              for(j=0;j<=50;j++)
                              {
                               if(student[c].my_average_grade>=university[i].average_grade && student[c].choice[k]==university[j].name&&!( university[i].places)=0)
                              		       {
                                              matching_student[no_of_match].match=university[i];
                                              matching_student[no_of_match].matchd=student[k];
                                              }
                                }  
                                
                               
                                return 0;
                                }
                              And this is what the compiler tell me.
                              Code:
                              :\dokumente und einstellungen\paul\eigene dateien\visual studio 2005\projects\tutorials\tutorials\tutorials.cpp(56) : error C2143: syntax error : missing ';' before '['
                              c:\dokumente und einstellungen\paul\eigene dateien\visual studio 2005\projects\tutorials\tutorials\tutorials.cpp(56) : error C2337: 'no_of_match' : attribute not found
                              c:\dokumente und einstellungen\paul\eigene dateien\visual studio 2005\projects\tutorials\tutorials\tutorials.cpp(56) : error C2143: syntax error : missing ';' before '.'
                              c:\dokumente und einstellungen\paul\eigene dateien\visual studio 2005\projects\tutorials\tutorials\tutorials.cpp(57) : error C2143: syntax error : missing ';' before '['
                              c:\dokumente und einstellungen\paul\eigene dateien\visual studio 2005\projects\tutorials\tutorials\tutorials.cpp(57) : error C2337: 'no_of_match' : attribute not found
                              c:\dokumente und einstellungen\paul\eigene dateien\visual studio 2005\projects\tutorials\tutorials\tutorials.cpp(57) : error C2143: syntax error : missing ';' before '.'
                              c:\dokumente und einstellungen\paul\eigene dateien\visual studio 2005\projects\tutorials\tutorials\tutorials.cpp(59) : error C2143: syntax error : missing ';' before '.'
                              c:\dokumente und einstellungen\paul\eigene dateien\visual studio 2005\projects\tutorials\tutorials\tutorials.cpp(60) : error C2143: syntax error : missing ';' before '.'
                              c:\dokumente und einstellungen\paul\eigene dateien\visual studio 2005\projects\tutorials\tutorials\tutorials.cpp(63) : error C2039: 'eof' : is not a member of 'std::basic_string<_Elem,_Traits,_Ax>'
                                      with
                                      [
                                          _Elem=char,
                                          _Traits=std::char_traits<char>,
                                          _Ax=std::allocator<char>
                                      ]
                              c:\dokumente und einstellungen\paul\eigene dateien\visual studio 2005\projects\tutorials\tutorials\tutorials.cpp(63) : fatal error C1903: unable to recover from previous error(s); stopping compilation
                              Build log was saved at "file://c:\Dokumente und Einstellungen\Paul\Eigene Dateien\Visual Studio 2005\Projects\tutorials\tutorials\Debug\BuildLog.htm"
                              tutorials - 10 error(s), 0 warning(s)
                              ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

                              Once Thanks.

                              Comment

                              • Roonie
                                New Member
                                • Mar 2007
                                • 99

                                #60
                                Code:
                                const int no_of_match=50;
                                matching_student[no_of_match].match=university[i];
                                matching_student[no_of_match].matchd=student[k];
                                im sorry, i know i told you to initialize your matching_studen t array, i had a momentary lapse and forgot that that was your array type . . . you need a different name for the actual array. you should only initialize your array once like this:
                                Code:
                                const int no_of_match=50;
                                matching_student matches[no_of_match];
                                and then use matches[index] to access different match records.

                                and to open a file: (see http://www.cplusplus.com/doc/tutorial/files.html)
                                Code:
                                string filename = "studentFile.txt";
                                ifstream inFile(filename.c_str());
                                (remember that to open a file with ifstream, you need a const char[] and not a string, hence the <string>.c_str( ) to convert the string to a char array.) and you should also end your input loop before sorting the data now stored in your arrays.

                                Comment

                                Working...