Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ Homework
what's up guys?
ummm can u take a look to my program please
//bona.h

 
class SLList;
class student 
{
	char FullName [100];
	float FinalMark ;
	int ArrayOfWishes[3];
	student *next;
public:
	student();
	student(char[],float,int[]);
	student(const student&);
	~student();
	void set_name(char[]);
	void set_final_mark(float);
	void set_wishes(int,int,int );
	friend class SLList;
	friend void print(student&);
	friend void AccepteInWill(student&,int);
	const char* getName(){return FullName;}
	float getMark(){return FinalMark;}
	int getWish(int a){return ArrayOfWishes[a];}
 
};
class SLList
{
	student*head;
public:
	SLList();
	~SLList();
	void AddToList(student*);
	bool IsEmpty();
};
void AccepteInWill(student&,int);
void test_vality_of_wish(int,int,int,int,int,int,int,int,int,int);
//************************************************
// bona.cpp
#include"bona.h"
#include < iostream >
#include < cstring >
using namespace std;
student::student()
{
	FullName[100]=NULL;
	FinalMark=NULL;
	ArrayOfWishes[3]=NULL;
	next=NULL;
}
student::student(char name[],float mark,int a[])
{
	set_name(name);
	set_final_mark(mark);
	set_wishes(a[0],a[1],a[2]);
	next=NULL;
 
}
student::student(const student&s)
{
	strcpy(FullName,s.FullName);
	FinalMark=s.FinalMark;
	ArrayOfWishes[0]=s.ArrayOfWishes[0];
	ArrayOfWishes[1]=s.ArrayOfWishes[1];
	ArrayOfWishes[2]=s.ArrayOfWishes[2];
}
student::~student()
{
	for(int i=0;i<sizeof(fullname);i++)>
		FullName[i]=NULL;
	FinalMark=0;
	for(int i=0;i<3;i++)
		ArrayOfWishes[i]=0;
 
}
void student::set_name(char name[])
{
	strcpy(FullName,name);}
void student::set_final_mark(float mark)
{
	if(mark>=60&&mark<=100)
		FinalMark=mark;
	else
		throw invalid_argument("invalid mark");
}
void student::set_wishes(int a1,int a2,int a3)
{
	if((a1>=1&&a1<=8)&&(a2>=1&&a2<=8)&&(a3>=1&&a2<=8))
	{
		ArrayOfWishes[0]=a1;
		ArrayOfWishes[1]=a2;
		ArrayOfWishes[2]=a3;
	}
	else throw invalid_argument("wish number 1,wish number 2 or/and wish number 3 are invalid input");
}
SLList::SLList()
{
	head=NULL;
}
SLList::~SLList()
{
	student*i=head;
	while(!IsEmpty())
	{
		i=i->next;
		delete i;
		head=i;
	}
 
}
bool SLList::IsEmpty()
{
	if(head=NULL)
		return true;
	else
		return false;
}
 
void SLList::AddToList(student*s)
{
	if(IsEmpty())
		head=s;
	else
	{
		student*i,*j;
		i=head;
		j=i->next;
		while(j!=NULL)
		{
			if(((s->getMark()) >( i->getMark()) )&&((s->getMark()) <( j->getMark())))
			{
				s->next=j;
				i->next=s;
			}
		}
	}
}
void test_vality_of_wish(int u,int a,int b,int c,int d,int e,int f,int g,int h,int q,student& i)
{
	if(u<=3)
	{
		static int j1=1,j2=1,j3=1,j4=1,j5=1,j6=1,j7=1,j8=1,j9=1;
		switch(i.getWish(u))
		{
				case (1):
					if(j1<=a)
					{
 
							AccepteInWill(i,1);
							j1=j1+1;
					}
					else
						test_vality_of_wish(u+1,a,b,c,d,e,f,g,h,q,i);
					break;
				case(2):
					if(j2<=b)
					{
 
							AccepteInWill(i,2);
							j2=j2+1;
					}
					else
						test_vality_of_wish(u+1,a,b,c,d,e,f,g,h,q,i);
					break;
				case(3):	
					if(j3<=c)
					{
 
							AccepteInWill(i,3);
							j3=j3+1;
					}
					else
						test_vality_of_wish(u+1,a,b,c,d,e,f,g,h,q,i);
					break;
				case(4):
						if(j4<=d)
					{
 
							AccepteInWill(i,4);
							j4=j4+1;
					}
					else
						test_vality_of_wish(u+1,a,b,c,d,e,f,g,h,q,i);
					break;
				case(5):
					if(j5<=e)
					{
 
							AccepteInWill(i,5);
							j5=j5+1;
					}
					else
						test_vality_of_wish(u+1,a,b,c,d,e,f,g,h,q,i);
					break;
				case(6):
					if(j6<=f)
					{
 
							AccepteInWill(i,6);
							j6=j6+1;
					}
					else
						test_vality_of_wish(u+1,a,b,c,d,e,f,g,h,q,i);
					break;
				case(7):
					if(j7<=g)
					{
 
							AccepteInWill(i,7);
							j7=j7+1;
					}
					else
						test_vality_of_wish(u+1,a,b,c,d,e,f,g,h,q,i);
					break;
				case(8):
					if(j8<=h)
					{
 
							AccepteInWill(i,8);
							j8=j8+1;
					}
					else
						test_vality_of_wish(u+1,a,b,c,d,e,f,g,h,q,i);
					break;
	}
	}
}
 

	
/////****************************************////
//main.cpp
#include<iostream>
#include"bona.h"
using namespace std;
int main()
{
 
	int a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0,all_student=0;
	student*wishNumber1=new student[a];
	student*wishNumber2=new student[b];
	student*wishNumber3=new student[c];
	student*wishNumber4=new student[d];
	student*wishNumber5=new student[e];
	student*wishNumber6=new student[f];
	student*wishNumber7=new student[g];
	student*wishNumber8=new student[h];
	student*Denied_student=new student[(all_student-a-b-c-d-e-f-g)];
	return 0;
}
my problem is: as u can see the function test_vality_of_wish needs more than three parameters , and i tried to use the (extern) keyword but it didnt work
i'm kinda lost .
can anyone give any advice .
Posted 13-Dec-12 23:53pm
Edited 14-Dec-12 0:17am
nv330.5K
v2
Comments
Richard MacCutchan at 14-Dec-12 5:24am
   
Yes, test_vality_of_wish needs 10 parameters. Tried to use the extern keyword for what reason? You need to be far more clear about what you are trying to do and what problems you are seeing.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

If your only problem is passing that many values, why not pass them in an array? ...or how about a smart array container like std::vector or std::array (or any other container)? ..if that's not your question, you need to be more clear because it's hard to understand what you're asking.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

There are so many problems in your code that it is hard to explain them all. Let's start with the group of lines:
    student*wishNumber1=new student[a];
    student*wishNumber2=new student[b];
    student*wishNumber3=new student[c];
Do you realize that you are allocating arrays of objects with type student and not single instances? And as all your variables a, b, c, ... are zero, you are allocating zero objects each.
 
Next, let's take a look at the constructor of the student class:
student::student()
{
    FullName[100]=NULL;
    FinalMark=NULL;
    ArrayOfWishes[3]=NULL;
    next=NULL;
}
The first line is assigning a character code of NULL to element FullName[100]. An array boundary violation, as the highes addressable element is FullName[99]. You probably wanted to clear the array, which you could have done by either
    memset (FullName, 0, sizeof FullName);
or
    FullName[0] = '\0';
The destructor is also somewhat peculiar:
student::~student()
{
	for(int i=0;i<sizeof(fullname);i++)>
		FullName[i]=NULL;
	FinalMark=0;
	for(int i=0;i<3;i++)
		ArrayOfWishes[i]=0;
 
}
The object is about to go out of scope anyhow. So setting FullName, FinalMark, and ArrayOfWishes to zeros is absolutely pointless.
 
The destructor of SLList can't work:
SLList::~SLList()
{
	student*i=head;
	while(!IsEmpty())
	{
		i=i->next;
		delete i;
		head=i;
	}
 
}
it should probalby read:
SLList::~SLList()
{
    student*i=head;
    while(!IsEmpty())
    {
        i=i->next;
        delete head;
        head=i;
    }
 
}
The IsEmpty function is a sad affair:
bool SLList::IsEmpty()
{
    if(head=NULL)
        return true;
    else
        return false;
}
The single equal sign will assign to head instead of checking it. All you had to do was:
bool SLList::IsEmpty()
    {return head != NULL;}
I don't comment on the AddToList function, which obviously can't work.
 
Function test_validity_of_whish is in fact hard to understand. It's signature is
     void test_vality_of_wish (int u, 
                   int a, int b, int c, int d, int e, int f, int g, int h, int q,
                   student& i)
which makes me wonder how you are going to deliver the test result. What I guess that you actually wanted to do is: Check whether a student will fit into any of the courses that he has registered for in hiswish list. Argument u is the number of the students wish (0 ...2) and used for recursion. a to q are the maximum occupancies for the courses, and i is the student for which the course assignment is to be solved.
 
Please use speaking names for parameters and variables. You probably see how difficult it is to interpret your code with single letter names.
 
Solving that problem via recursion is not the easiest approach. A simple loop would have sufficed. And you fell into the trap of using static variables to keep track of how many students are already booked for which course:
    if(u<=3)
    {
	static int j1=1,j2=1,j3=1,j4=1,j5=1,j6=1,j7=1,j8=1,j9=1;
	switch(i.getWish(u))
	    {
Now consider that j1 ... j9 are initialize just once at the start of your program. You won't have a chance to reset them.
 
Here is my suggestion. Organize that course allocation like this:
    int maxOccupancies[9];
    int curOccupancies[9];
    // ... initialize them appropriately

    bool courseAssigned = false;
    for (int wishIdx = 0; wishIdx < 3; ++wishIdx)
    {
        int course = stud.getWish (wishIdx);
        if (curOccupancies[course] < maxOccupancies[course]
        {
             // grant the wish and put student in the course
             courseAssigned = course; 
             curOccupancies[course] += 1;
             stud.assignCourse (course);
             break;
        }
    }
    if (!courseAssigned)
        // ...
That all does not actually answer your question. But it tries to lead you on to the right way of programming. I'd suggest to pick up a good C++ text book and start from the very beginning. Once you get the basics correct, things will be much easier for you.
  Permalink  
v2
Comments
CPallini at 14-Dec-12 16:11pm
   
Awesome, 5++.
nv3 at 14-Dec-12 16:35pm
   
Thanks!
Ali++paco# at 20-Dec-12 11:27am
   
man .... I AM SO DUMB ... I WILL NEVER PASS THE NEXT EXAM ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 325
1 DamithSL 265
2 CPallini 235
3 Sergey Alexandrovich Kryukov 229
4 Maciej Los 190
0 OriginalGriff 5,455
1 DamithSL 4,422
2 Maciej Los 3,860
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,010


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 14 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100