|
 |
|
 |
03-29-2007, 11:06 PM
|
#1 (permalink)
|
|
Recruit
Join Date: Mar 2007
Posts: 4
|
Methods for initialising 2D arrays
hi my name is hamid , i am doing degree in info sys engin . and this my first year , actually i have case study which e i spent 2 days trying to get solution but really i am having hard time because this is my first course in programing so may any body help me plzz
i need to write a program that assign a passenger seats in an
> airplane, assuming that a small airplane with seats numbered as
> follows:
>
> 1 A B C D
> 2 A B C D
> 3 A B C D
> 4 A B C D
> 5 A B C D
> 5 A B C D
> 6 A B C D
> 7 A B C D
>
> the program shld. display the seat pattern, marking with an "X" the
> seats already assigned. For example, after seats 1A, 2B, 4C are
> taken, the display shld. look like
>
> 1 X B C D
> 2 A X C D
> 3 A B C D
> 4 A B X D
> 5 A B C D
> 6 A B C D
> 7 A B C D
After displaying the seats available, the program prompts for the
seat desired, the user types in a seat, and then display of available
seats is updated. This continues until all seats are filled or until
the user signals that the program shld. end. If the user types in a
seat that is already assigned, the program shld. say that the seat is
occupied and ask for another choice. the program shld. take into
consideration the use of arrays and functions as well as the user
interface.
and here is my trying
#include <iostream>
using namespace std;
#define NumRows 7
#define Numcols 4
int main()
{
char Broad[7][4];
int rows;
int cols;
int Y;
cout << "wellcome to our online resrvation seats" << "please press Y to display the avalible seats" << endl;
cin >> Y;
for (int rows=0;rows<7; rows++)
{
for (int cols=0; cols<4; cols++)
{
// initialize the first row.
Broad[0][0]= 'A';
Broad[0][1]='B';
Broad[0][2]='c';
Broad[0][3]='D';
// initialize the second row.
Broad[1][0]= 'A';
Broad[1][1]='B';
Broad[1][2]='c';
Broad[1][3]='D';
// initilize the third row.
Broad[2][0]= 'A';
Broad[2][1]='B';
Broad[2][2]='c';
Broad[2]3]='D';
// intilize the fourth row.
Broad[3][0]= 'A';
Broad[3][1]='B';
Broad[3][2]='c';
Broad[3][3]='D';
// intialize the fivth row.
Broad[4][0]= 'A';
Broad[4][1]='B';
Broad[4][2]='c';
Broad[4][3]='D';
// intialize the sixth row.
Broad[5][0]= 'A';
Broad[5][1]='B';
Broad[5][2]='c';
Broad[5][3]='D';
// intialize the sventh row.
Broad[6][0]= 'A';
Broad[6][1]='B';
Broad[6][2]='c';
Broad[6][3]='D';
cout <<"(" << cols << rows << ")\t";
}
}
}
and this also
#include <iostream>
using namespace std;
#define NumRows 7
#define Numcols 4
int main()
{
char seats[7][4]= { { 'a', 'B', 'c', 'd' }, { 'A', 'B', 'c', 'D' },{ 'a', 'B', 'c', 'd' }, { 'A', 'B', 'c', 'D' },
{ 'a', 'B', 'c', 'd' }, { 'A', 'B', 'c', 'D' },{ 'a', 'B', 'c', 'd' }};
int Y;
int i;
int j;
cout << "wellcome to our online resrvation seats" << "please press Y to display the avalible seats" << endl;
cin >> Y;
for(i=0;i<7;i++)
{
for(j=0;j<4;j++)
{
seat = { { 'a', 'B', 'c', 'd' }, { 'A', 'B', 'c', 'D' },{ 'a', 'B', 'c', 'd' }, { 'A', 'B', 'c', 'D' },
{ 'a', 'B', 'c', 'd' }, { 'A', 'B', 'c', 'D' },{ 'a', 'B', 'c', 'd' }};
cout << seat;
}
}
}
|
|
|
03-30-2007, 05:51 AM
|
#2 (permalink)
|
|
C++ Beginner
Join Date: Jul 2005
Location: Ottawa
Posts: 73
|
Try using [ code ] [ / code ] between your code so that it's formatted in a way that's easy to read for everyone on the forum.
I'll try to list all the problems I find (everyone else feel free to contribute to anything I've wrote, or if it's incorrect or misleading to clarify).
Your array initialization is acceptable, you need 7 rows by 4 columns which you have, which by default, each column is assigned to A,B,C,D, however I am going to point out two things:
1) Instead of using a #define, use the const keyword. It has the same behaviour as #define. when you initialize your seats array, instead of specifying
Code:
char seats[7][4] ...etc
you can use the constants you declared here:
Code:
char seats[NumRows][NumCols] ....etc
2)
Code:
int Y;
cout << "welcome to our online reservation.... press Y to view seats " << endl;
cin >> Y;
Here you are using improper types. The same way that you used a character array to define the seat letters, you should use a character here if you are expecting a character as input. Now your loops were correct, however in the inner most loop what you are doing is reinitializing your character array, which was done correctly. Instead you want to display the character that you're at, so use a cout. At a first pass through, this is what you can use as a basis. Note however that as you specified you should make use of arrays (which you do), functions (not currently used). My suggestion would be to write a function display() or whatever you choose which will take as a parameter your array, and take care of the displaying there, so that if you want to display the updated seat pattern, once you've made modifications all you have to do is call (once the function is written) as opposed to having to re-write the code over and over. If you need an explanation of functions, feel free to ask. here is a point in the right direction as a basis for you to start:
Code:
#include <iostream>
using namespace std;
const int NumRows = 7; //const keyword means that you cannot modify this variable once it has been initialized
const int Numcols = 4;
int main()
{
char seats[7][4]= { { 'A', 'B', 'C', 'D' }, { 'A', 'B', 'C', 'D' },{ 'A', 'B', 'C', 'D' }, { 'A', 'B', 'C', 'D' },
{ 'A', 'B', 'C', 'D' }, { 'A', 'B', 'C', 'D' },{ 'A', 'B', 'C', 'D' } };
char Y;
int i;
int j;
cout << "welcome to our online resrvation seats" << "please press Y to display the avalible seats" << endl;
cin >> Y;
for(i=0;i< 7;i++)
{
for(j=0;j<4;j++)
{
cout << seats[i][j] << " ";
}
cout << endl;
}
return 0;
}
|
|
|
03-30-2007, 06:27 AM
|
#3 (permalink)
|
|
Senior Contributor
Join Date: Mar 2005
Posts: 676
|
Sounds like your teacher is crawling mail archives for his questions
http://newsarchiv.tugraz.at/browse/t.../msg00182.html
The discussion is in german and is about "why are school tasks so comedian"
__________________

UT: Ultra-kill... God like!
|
|
|
03-31-2007, 06:22 AM
|
#4 (permalink)
|
|
Recruit
Join Date: Mar 2007
Posts: 4
|
thank you very very much iam very thankfull and i will try to work on the rest of the code ...............thank you again and have nice day
|
|
|
03-31-2007, 06:35 AM
|
#5 (permalink)
|
|
Recruit
Join Date: Mar 2007
Posts: 4
|
hi again actually would you expline for me about the functions i would be so greatfull i f you do so
|
|
|
03-31-2007, 05:39 PM
|
#6 (permalink)
|
|
Recruit
Join Date: Mar 2007
Posts: 4
|
hi . how ar eyou ?
actually i am trying to call the function , how ever the things gets worse and worse .................................................. ..... i need help plz this project should be submitted this weeek
regards ...............hamid
|
|
|
04-01-2007, 01:54 PM
|
#7 (permalink)
|
|
C++ Beginner
Join Date: Jul 2005
Location: Ottawa
Posts: 73
|
go to google, or any other search forum and lookup how functions work. basically, you declare a prototype. for example, if I wanted a function that displays an integer, I would create a prototype (or declare it) like so:
Code:
void displayInt(int);
somewhere in my source, I would have to call the function like
and somewhere later in my source, I would write the definition for it, and it would look like this:
Code:
void displayInt(int myInteger) {
cout << "Integer: " << myInteger << endl;
}
where myInteger is just what I named the variable so I can use the variable I was given by the function. Note that the declaration and definition appear outside the main() function. only the function call itself appears in the main() program, and if there are parameters for your functions, they must match the prototype (or declaration). in this case it was int, but yours to display character array you would need char** as the parameter, and in the definition change int to char ** and now you display the array.
Here's a small program with a simple function
Code:
#include <iostream>
using namespace std;
void display(int);
void main() {
display(5);
}
void display(int num) {
cout << "number is: " << num << endl;
}
|
|
|
04-01-2007, 01:59 PM
|
#8 (permalink)
|
|
C++ Beginner
Join Date: Jul 2005
Location: Ottawa
Posts: 73
|
Quote:
Originally Posted by DJMaze
Sounds like your teacher is crawling mail archives for his questions
TU-Graz Newsarchiv - Re: WIESO???
The discussion is in german and is about "why are school tasks so comedian"
|
lol
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 06:32 AM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|