Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

For my C++ project, we are supposed to make a program that organizes a seating for the exam. No two people taking the exam can sit near each other. We are supposed to use arrays. Could you please help me out as I am a beginner with C++.

Thanks

Thanks

If you can't figure it out how to do it manually, then no amount of expert help will tell you how to write the program.

Try and put something together. Figure out what data you get to start with and then what conclusions you need to come up with.

I have always found the best way to figure out an algorithm is to get some sample input, then do what I want the computer to do manually, recording what I do. This helps you organize and know exactly what your program is supposed to do.

Great Luck!!

tvanceplus

as i understand the problem till this point is:

1) one testing room.

2) more than one test at the same time.

3) no two students having the same course to sit near each other.

first lets number the tables sequentially so you can use 1 dimentional arrays. (its easier)

the type of the array's elements is STRING data type. (each student would have a string that indicates his number and course ID)

if two tests to be held assign the odd numbered tables to those of test "A"

and the even to the others.

Many alternatives exists depending on your algorithm.

For more assistance please tell us what do you want exactly.

issamwd

#include <iostream.h>

#include <math.h>

#include <fstream.h>

#include <string.h>

#include <stdio.h>

#define CNUM_Rows 30

#define CNUM_Clms 20

#define CNUM_ArrayRows (CNUM_Rows+2)

#define CNUM_ArrayClms (CNUM_Clms+2)

#define CNUM_NumberOfTests 10

int aSeats[CNUM_ArrayRows][CNU

void InitSeatArray()

{

for (int j=0; j< CNUM_Rows; j++ ) {

for (int k=0; k< CNUM_Clms; k++ ) {

aSeats[j][k]= 0; // nobody is sitting there

}

}

}

bool FindAvailableSeat( int *pnRow, int* pnClm )

{

for (int nRow=1; nRow< CNUM_Rows; nRow++ ) {

for (int nClm=1; nClm< CNUM_Clms; nClm++ ) {

if ( (aSeats[nRow+0][nClm+0] == 0 ) // the seat itself

&& (aSeats[nRow+1][nClm+0] == 0 ) // the seat behind

&& (aSeats[nRow-1][nClm+0] == 0 ) // the seat in front

&& (aSeats[nRow+0][nClm-1] == 0 ) // to the left

&& (aSeats[nRow+0][nClm+1] == 0 ) // to the right

&& (aSeats[nRow+1][nClm+1] == 0 ) // diag right/behind

&& (aSeats[nRow+1][nClm-1] == 0 ) // diag left/behind

&& (aSeats[nRow-1][nClm+1] == 0 ) // diag right/ahead

&& (aSeats[nRow-1][nClm-1] == 0 ) // diag left/ahead

) {

*pnRow= nRow; *pnClm= nClm;

return( true );

}

}

}

return (false );

}

main()

{

InitSeatArray();

int fRet;

int nRow, nClm;

while (fRet!= false)

{

for (int j=1; j<= CNUM_NumberOfTests; j++ ) {

bool fRet= FindAvailableSeat( &nRow, &nClm );

if ( fRet == false ) {

printf("Only able to seat %d test-takers in the "

"room with %d rows and %d clms of seats.\n",

j-1, CNUM_Rows, CNUM_Clms

);

break;

}

else {

aSeats[nRow][nClm]= j; // put a student in the seat

}

}

printf( "Seating Chart\n" );

for ( nRow=1; nRow <CNUM_Rows; nRow++ ) {

for (nClm=1; nClm <CNUM_Clms; nClm++ ) {

printf(" %02.2d", aSeats[nRow][nClm] );

}

printf("\n");

}

}

return(0);

}

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.

This checks not only for and empty seta to the left and right plus empty infront and in bac, it ensures that the seats in the four diagonals are also empty.

When done, It prints a seating chart

#include <stdio.h>

#define CNUM_Rows 20

#define CNUM_Clms 10

#define CNUM_ArrayRows (CNUM_Rows+2)

#define CNUM_ArrayClms (CNUM_Clms+2)

#define CNUM_NumberOfTestTakers 20 // pepple to try to seat

int aSeats[CNUM_ArrayRows][CNU

void InitSeatArray()

{

for (int j=0; j< CNUM_Rows; j++ ) {

for (int k=0; k< CNUM_Clms; k++ ) {

aSeats[j][k]= 0; // nobody is sitting there

}

}

}

bool FindAvailableSeat( int *pnRow, int* pnClm )

{

// the outside of the matrix -- clm 0, clm 9, row 0, and row 20

// are a buffer to avoid using an index < 0)

// we later adjust by discarding these two rows and two clms

for (int nRow=1; nRow< CNUM_Rows; nRow++ ) {

for (int nClm=1; nClm< CNUM_Clms; nClm++ ) {

if ( (aSeats[nRow+0][nClm+0] == 0 ) // the seat itself

&& (aSeats[nRow+1][nClm+0] == 0 ) // the seat behind

&& (aSeats[nRow-1][nClm+0] == 0 ) // the seat in front

&& (aSeats[nRow+0][nClm-1] == 0 ) // to the left

&& (aSeats[nRow+0][nClm+1] == 0 ) // to the right

&& (aSeats[nRow+1][nClm+1] == 0 ) // diag right/behind

&& (aSeats[nRow+1][nClm-1] == 0 ) // diag left/behind

&& (aSeats[nRow-1][nClm+1] == 0 ) // diag right/ahead

&& (aSeats[nRow-1][nClm-1] == 0 ) // diag left/ahead

) {

*pnRow= nRow; *pnClm= nClm;

return( true );

}

}

}

// cycled through all seats and found no place to put

// another testee

return (false );

}

main()

{

InitSeatArray();

int nRow, nClm;

for (int j=1; j<= CNUM_NumberOfTestTakers; j++ ) {

bool fRet= FindAvailableSeat( &nRow, &nClm );

if ( fRet == false ) {

printf("Only able to seat %d test-takers in the "

"room with %d rows and %d clms of seats.\n",

j-1, CNUM_Rows, CNUM_Clms

);

break;

}

else {

aSeats[nRow][nClm]= j; // put a student in the seat

}

}

printf( "Seating Chart\n" );

for ( nRow=1; nRow <CNUM_Rows; nRow++ ) {

for (nClm=1; nClm <CNUM_Clms; nClm++ ) {

printf(" %02.2d", aSeats[nRow][nClm] );

}

printf("\n");

}

return(0);

}