Hello, I have this assignment I'm supposed to deliver tomorrow, I have most of it figured out, but there are certain bits and parts of the question I can't figure out... I'll show you what I mean, here's the entire question:

``````Develop two linked lists to manage the list of students at the uni as follows:
-	ApplicantsList stores information about students that have applied to the uni and still waiting for acceptance.
-	StudentsList stores information about students at the uni .

First of all, both lists are empty. An applicant is defined by the following information:FirstName, LastName, DateOfBirth, Status. The last variable status gives information whether the student is still on the waiting list or has been rejected/accepted

A student is defined by the following information:FirstName, LastName, DateOfBirth, ID.

1-	Start your program by adding 500 applicants to the list of ApplicantsList. It is your choice to add them at front or back.
2-	Go through the ApplicantsList from beginning to end. For every applicant, you have to make one of the following decisions:
a.	Reject with a probability of 30%. In this case, the applicant should be deleted from the list ApplicantsList.
b.	Keep the student waiting with a probability of 30%. This means the status of the applicant should be set to Waiting.
c.	Accept with a probability of 40%. In this case, the applicant should join the StudentsList and be given a random ID.
Attention: The StudentsList should be sorted in increasing order of ID.
The applicant should be deleted from the ApplicantsList.``````

I most of the program figured out but my question is, HOW do I make the code to "make a decision" to reject by a certain probability? What I don't get is how I can make the program "reject with a probability of 30%" and so on... Any thoughts?

## All 10 Replies

Hello doubleh,
If your linked lists are done by using classes, then you can use destructors to destroy the values of student with percentage less than 30.

Hello doubleh,
If your linked lists are done by using classes, then you can use destructors to destroy the values of student with percentage less than 30.

Hello, I don't think that's what I'm supposed to do, first of all we're not allowed to use classes, just structures, and second take a look at these:
" 1. Reject with a probability of 30%
2. Keep the student waiting with a probability of 30%
3. Accept with a probability of 40% "

My question is how do i make the program REJECT WITH A PROBABILITY... There are no percentages for students or grades or anything as you can see, ugh this is confusing!!

One more question, If i want to ask the user how many students are there, and make him input a certain number, then add the data according to that number.
For example :
cout << "how many: ";
cin >> number;

and according to that number, I want to make that many linked lists.. Usually we have a definite ammount so we declare ( LinkedList a1,a2,a3,a4; etc..) but what if we have no definite number? what to do ?

thanks!

I tried this method:

``````int size;
cout << "How many students: ";
cin >> size;
ApplicantsList a[size]; //ApplicantsList is a structure foremerly defined
ApplicantsList *ptr;

for(int i=1;i<=size;i++){
cout << "Applicant No." <<i << endl;
cout << "Enter first name: ";
cin >> a[i].FirstName;
cout << "Enter last name: ";
cin >> a[i].LastName;
cout << "Enter Date of Birth: ";
cin >> a[i].DateOfBirth;
cout << "Status (1 for accepted, 0 for rejected): ";
cin >> a[i].status;``````

but when I wanted to try this, it gave me a runtime error and crashed!

I did develop this to display the outcomes:

``````while(ptr){
cout << ptr->FirstName << " " << ptr->LastName << endl;
cout << ptr->DateOfBirth << endl;
if(ptr->status==1)
cout<< "Accepted" << endl;
else
cout<< "Rejected" << endl;
cout << endl;
cout << endl;

ptr=ptr->next;
}``````

but that's when we have defined number of students, what if there isn't? whats to be done?

It is not possible to declare an array without constant expression.

``````cin >> size;
ApplicantsList a[size];//<--``````

Give a constant in place of size(like 100 etc)

Well i tried that, when I ran the program and entered the first name, the moment I presed enter it displayed this error message for me:

Unhandled exception at 0x104f01be in asd.exe: 0xC0000005: Access violation writing location 0xcccccccc.

And the program halted.

could you give your code elaborately including the definition of structure ?

Back to probability. You need to use `srand()` and `rand()` to get your probability. Get a random number between 0 and 99. If the number is 30 or more the record is OK. If < 30, reject it.

Walt yes I did something similar to what you said, thanks.

Here's my code:

``````#include "stdafx.h"
#include <iostream>
#include "conio.h"
#include "string"
#include <cstdlib>

using namespace std;

struct ApplicantsList{
string FirstName;
string LastName;
string DateOfBirth;
int status;
ApplicantsList *next;
};

struct StudentsList{
string FirstName;
string LastName;
string DateOfBirth;
int id;
StudentsList *next;
};

int main(){
ApplicantsList a[4];
ApplicantsList *ptr;

for(int i=1;i<=4;i++){
cout << "Applicant No." <<i << endl;
cout << "Enter first name: ";
cin >> a[i].FirstName;
cout << "Enter last name: ";
cin >> a[i].LastName;
cout << "Enter Date of Birth: ";
cin >> a[i].DateOfBirth;
cout << "Status (1 for accepted, 0 for rejected): ";
cin >> a[i].status;
}

getch();
return 0;
}``````

I figured out why its doing this, its because of the char * declaration in the structs, i replaced it with 'string' types and the program still halts;
but it stores all the info till it reaches Birth date of the fourth student, it gives the same error...

I'm developing each part of the code alone, helps troubleshoot.

If you want to allow the user to choose the size of the array, you can't do this:

``ApplicantsList a[size];``

Instead, you need to dynamically allocate memory for an array like this:

``````ApplicantsList* a;
a = new ApplicantsList[size];``````

When you do this, you can use "a" as an array of applicantsList classes/structs(whichever they are).

However when you dynamically allocate the array, you should know that the pointer is the only thing that allows you to acess the array. Therefore you should NEVER set the pointer to null UNLESS another pointer is already pointing to it

For instance, you should NEVER do this (assuming there is no other pointer storing the memory inside of a1)

``a1 = null // NEVER do this!``

Doing so would cause a memory leak, meaning you have gotten rid of your only way to access the memory that you have dynamically allocated, which will cause you problems and could significantly slow down your computer if done repeativly.

This would be acceptable however as you still can acess your memory.

``````a2 = a1;
a1 = null``````

However, if you do not want to use the array you have dynamically allocated anymore, you can delete instead.(Which you will usally need to do in a destructor of a class if you have dynamically allocated memory in it) To do this, you use the "delete" keyword, but you also should set your pointer to null for good measure or you might cause something called a "dangling pointer" which is a pointer which points to an unknown piece of memory, altering anything with that pointer will unintetionally alter the memory in that location.(aka, BAD)

So to delete dynamically allocated memory, a good method would be like this:

``````delete [] a; // [] tells the program that the memory is an array, not a single piece
a = null // sets the pointer to null, to prevent creating a dangling pointer.``````

I hate to nit pick but isn't the assignment supposed to use linked list? An array is not the same as a linked list. For info on linked list check this out

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.