Sunday, January 23, 2011

C++ again and Dynamic 2D Arrays

I hope you remember my friend at the University of Sri Jayawardenapura. This time also he needed my help to do a programming assignment.

Before coming to the question, I'll tell you what I learned by doing the assignment.

Do you know how to create an array Dynamically ? Lets say you want the user to enter a size and according to that size, you create an array of the given size dynamically.

int size;
cout<<"Enter the size of the array : ";
cin>>size;
int *myarray = new int[size];

The above code is correct. But have you ever tried to create a 2D array dynamically. I wanted to create a 2D array of a given number of rows and 2 columns. I tried it this way.

int rows;
cout<<"Enter the rows you want : ";
cin>>rows;
int *myarray = new int[rows][2];

When I compiled the program, I got errors and I realized that the above method is incorrect. So I went through some C++ forums and finally found the solution. This is one of the correct ways to create a 2D array dynamically.

int (*myarray)[2] = new int[rows][2];

The the value for the variable 'rows' can be given either from a key board input or from assigning a value from some other variable (eg: using a count variable).

I hope now you know it. This is the problem I had to solve.

Question

Applicants who obtain over 75 marks for the test will be called for the first interview.
Applicants who obtain over 50 marks for the first interview will be called for the second interview.
Each selected applicant has to face the second interview.
First six applicants who obtain highest marks for the second interview will be selected for the management trainee position.

The bank has received 500 applications for the selection test and applicants have been numbered from 1 to 500.

Write a C++ program to do the following tasks:

->To define a one-dimensional array to store the marks obtained by each applicant for selection test.
->To input marks obtained by each candidate for the test.
->To define another one-dimensional array to store the applicant's number for those who have been selected for hte first interview.
->To display the applicant's number of those who have been selected for the first interview.
->To define a two-dimensional array to store the applicant's number and marks obtained by each applicant selected for the second interview.
->To input applicant's number and marks obtained by each applicant for the second interview.
->To display the numbers of those who have been selected for the management trainee position.

I drew a plan to understand the question.

Since I cannot check it for 500 marks, I gave 20 for the number of applicants.
Once the user inputs the marks of all the candidates, the program should select the candidates having more than 75 marks and should display their registration number. They are the candidates who have been selected for the 1st interview.

Then the user has to enter the marks obtained by the candidates from the 1st interview. The program should allow only to enter marks for those who have got more than 50 marks among the selected candidates for the 1st interview. The above image shows that. 

Once the user has input all the marks of the selected candidates for the 2nd interview, again needs to enter the marks obtained from the 2nd interview among those who got more than 50 marks from the 1st interview. Now the program should allow only to enter marks for those who have passed the test and also the 1st interview.
Then finally it should select the top six candidates based on their marks from the 2nd interview and display them.

Click Here to download the source code (106.99 KB).

P.S - This is the longest program I wrote in C++ without using any functions other than the main().

-Tharindu Edirisinghe-
-SLIIT 10'-

2 comments:

Post a Comment