Solved

Arrays

Posted on 2000-05-04
8
219 Views
Last Modified: 2010-04-02
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
0
Comment
Question by:raheelahmadkhan
8 Comments
 
LVL 2

Expert Comment

by:abesoft
ID: 2778615
You need to figure out an algorithm to do the task.  One approach is to "do it on paper".  Then, write a program that uses a similar approach to the operations that you did to do the task manually.

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.
0
 
LVL 1

Expert Comment

by:tvanceplus
ID: 2778706
The experts exchange policy is that we can not do a homework assignment for you. We can, however, help you with specific problems and setbacks that you encounter. This is so that all of us that belong to experts exchange can become better programmers.

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
0
 
LVL 1

Expert Comment

by:issamwd
ID: 2780769
Ok, the solution would be so easy, but first tell me what do you want exactly?

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
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:raheelahmadkhan
ID: 2781815
Could someone give me an example with C++ on how to do it....Your response will be greatly appreciated.
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 50 total points
ID: 2783589
This code uses a 2-d array to represent the seats of the testing room.  The function FindAvailableSeat(pY,pX) locates a seat that is unoccupied and has nobody in any neighboring seat and sets the caller's X,Y with the found location.  THe main() fn sets that location to a number (the student number).  

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][CNUM_ArrayClms];


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);
}
0
 

Author Comment

by:raheelahmadkhan
ID: 2813958
I fixed the program a little bit.  Right now, when we tell the program the number of tests, the program just arranges the tests so no two of them are near each other.  We  want to make the program in such a way that the program keeps on looping around when we input the number of tests and number of students, it loops until all of the tests are filled in.  If you have any more questions, please feel free to ask.  I hope you reply soon.



#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][CNUM_ArrayClms];


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);
}
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 2816041
Yes, raheelahmadkhan, I have two  questions:

Does the solution that I provided answer your question?  Is there some flaw in the program that keeps you from awarding me the points you offered?

I hope you reply soon.

-- dan

0
 

Author Comment

by:raheelahmadkhan
ID: 2817593
Youre program was excellent but there is more to it...You have to input the number of tests and the number of students taking each test...then you arrange the seating so that no two people sit next to each other...i'll reward you as soon as you can do this
thanks
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question