473,387 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Methods for initialising 2D arrays

hi
i just start studying programing and i actually i have problem with my code i would be happy if you help me out in this
i need to display the seat pattern in air plane like this
1 ABCD
2ABCD
3ABCD
4ABCD
5ABCD
6ABCD
6ABCD
7ABCD AND HER E IS MY TREYING
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3. #define NumRows 7
  4. #define Numcols 4 
  5. int  main()
  6. {
  7.     char Broad[7][4];
  8.  
  9.     int rows;
  10.     int cols;
  11.     int Y;
  12.  
  13.     cout << "wellcome to our online resrvation seats" << "please press Y to display the avalible seats" << endl;
  14.     cin >> Y;
  15.     for (int rows=0;rows<=7; rows++) 
  16.     {
  17.         for (int cols=0; cols<=4; cols++)
  18.  
  19.         {
  20.  
  21.  
  22.  
  23.             // initialize the first row.
  24.             Broad[0][1]= 'A';
  25.             Broad[0][2]='B';
  26.             Broad[0][3]='c';
  27.             Broad[0][4]='D';
  28.  
  29.             // initialize the second row.
  30.             Broad[1][1]= 'A';
  31.             Broad[1][2]='B';
  32.             Broad[1][3]='c';
  33.             Broad[1][4]='D';
  34.             // initilize the third row.
  35.             Broad[2][1]= 'A';
  36.             Broad[2][2]='B';
  37.             Broad[2][3]='c';
  38.             Broad[2][4]='D';
  39.             // intilize the fourth row.
  40.             Broad[3][1]= 'A';
  41.             Broad[3][2]='B';
  42.             Broad[3][3]='c';
  43.             Broad[3][4]='D';
  44.             // intialize the fivth row.
  45.             Broad[4][1]= 'A';
  46.             Broad[4][2]='B';
  47.             Broad[4][3]='c';
  48.             Broad[4][4]='D';
  49.             // intialize the sixth row.
  50.             Broad[5][1]= 'A';
  51.             Broad[5][2]='B';
  52.             Broad[5][3]='c';
  53.             Broad[5][4]='D';
  54.             //  intialize the sventh row.
  55.             Broad[6][1]= 'A';
  56.             Broad[6][2]='B';
  57.             Broad[6][3]='c';
  58.             Broad[6][4]='D';
  59.             cout <<"(" << cols << rows << ")\t";
  60.         }
  61.     }
  62. }
  63.  
Mar 29 '07 #1
10 4718
JosAH
11,448 Expert 8TB
Just a few observations:

1) you re-initialize the entire array everytime you're in the loop body;
2) column indexes also start at zero;
3) array initialization can be done like this:
Expand|Select|Wrap|Line Numbers
  1. char seats[7][4]= { { 'a', 'B', 'c', 'd' }, { 'A', 'B', 'c', 'D' }, 
  2.                   /* etc */ }};
kind regards,

Jos
Mar 29 '07 #2
Savage
1,764 Expert 1GB
Or you can use two for loops,like:

Expand|Select|Wrap|Line Numbers
  1. for(i=0;i<7;i++)
  2. {
  3.       for(j=0;j<4;j++) c=65+j,broad[i][j]=c;/*65 is ASCII for char A so if you add it the counter value it should increse up to 68 which is 'D'.*/
  4. }
Savage
Mar 29 '07 #3
Dear friend i want say thank you for you r fast reply .
i already did you have told me but the i want you tell me how do i call the array to be shown as i mentioned above coz
what i did befor i used an example for chess board
so i may expect more explain
thank you
Mar 29 '07 #4
Banfa
9,065 Expert Mod 8TB
Hi almulsi,

please read our Posting Guidelines

particularly

Use a Good Thread Title
How to ask a question
Posting coursework and homework questions

You have not
  • given the thread an adequate title
  • Used code tags round the code to preserve formatting
  • Asked an actual question or described your actual problem

Banfa
Administrator
Mar 29 '07 #5
o k , thank you for your guide line , and i am so sorry if i did not make my self clear to you . the question is can somebody help me with my case study??? coz iam really having a
> hard time doing this.. i am a beginner../
> 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.

tnx in advance.. have a nice day!!
Mar 30 '07 #6
Savage
1,764 Expert 1GB
o k , thank you for your guide line , and i am so sorry if i did not make my self clear to you . the question is can somebody help me with my case study??? coz iam really having a
> hard time doing this.. i am a beginner../
> 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.

tnx in advance.. have a nice day!!
Well,if we suppose that all seats won't be reservated at once(as they probably wouldn't) I get conclusion that you will need to write it to a file which will be opened and loaded once the programme is started

Secound,to make a user input it would take that him fill up both dimensions.There are two ways:

First way,is simpler for user but it might be complicated for you:

you could make a string of two caracters entered by once (like 1A)
and then break it so that first part is converted to int and fills up first dimenison,and secound is char and fills secound dimension.All this will be iniside do-while loop which will execute until there are free seats or until
user press ESC.

Secound way,is simpler to make but there is qestion of functionality and
user will be prompted twice instead of once(like I explained above).it's
to make first input for row,and secound input for seat char.

And third part was:How to update this array?

You will need to search your array until it matches with user input and then remember those coordinates and update it with x.


After all of this you must owerwrite/update your file and exit programme.



I hope this helps a bit.

When you have acomplished or/and have some troubles please post just that part(use also codetags #) and we will help you to fix it or to make it even more better!!!


Savage
Mar 30 '07 #7
Well,if we suppose that all seats won't be reservated at once(as they probably wouldn't) I get conclusion that you will need to write it to a file which will be opened and loaded once the programme is started

Secound,to make a user input it would take that him fill up both dimensions.There are two ways:

First way,is simpler for user but it might be complicated for you:

you could make a string of two caracters entered by once (like 1A)
and then break it so that first part is converted to int and fills up first dimenison,and secound is char and fills secound dimension.All this will be iniside do-while loop which will execute until there are free seats or until
user press ESC.

Secound way,is simpler to make but there is qestion of functionality and
user will be prompted twice instead of once(like I explained above).it's
to make first input for row,and secound input for seat char.

And third part was:How to update this array?

You will need to search your array until it matches with user input and then remember those coordinates and update it with x.


After all of this you must owerwrite/update your file and exit programme.



I hope this helps a bit.

When you have acomplished or/and have some troubles please post just that part(use also codetags #) and we will help you to fix it or to make it even more better!!!


Savage
well thank you for your reply but what i want to say is that my course still not coverd the ClASS ND HOW MAKE THW FILE so , i expect you give me way that i can apply straight forward , thank you and have nice day
Mar 30 '07 #8
Savage
1,764 Expert 1GB
Do you know how to use structures?
Mar 30 '07 #9
Do you know how to use structures?
well to be honest we have not covered this topic till now so why iam strugling doing this .please help me
Mar 30 '07 #10
Savage
1,764 Expert 1GB
So here are the consequences:

1. There is no way u can save data whitout using files which means that every time u start programm it will be "reseted". ( it will start with all seats empty )

2. It might(will) be harder to make it without using structures.

Still it can be done.Probably(Hopefully) your programm is meant ro be reseted upon each start.

This is left to be done:

1.You need to make up 2D array(try using my for loops or Jos's way to do it).

2.Realise user input(One of two wich i presented as ur choice in my secound post).

3.How to update it? Also look in my secound post.




Savage
Mar 30 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Bob Rock | last post by:
Hello, this may seem a strange question, but is there a way of being able to call methods of a class through an array of that class when not referencing a specific object in the array. In other...
8
by: Nick Keighley | last post by:
Hi, is there a way of initialising a vector in a similar fashion to initialising an array? consider: Shape* triangle = new Triangle (); Shape* square = new Square (); Shape* shape_arr =...
2
by: shan_rish | last post by:
Hi CLCers, In the below program the error message while compiling is /home1/murugan/prog/cprog >cc -o struct_eval struct_eval.c cc: "struct_eval.c", line 14: error 1549: Modifiable lvalue...
1
by: Serj Kondryukov | last post by:
I am novice in C#. I did write on C++ before. In my current program i need to use big constant table. Each string is a some struct what contain following fields: MyEnum f1; MyEnum f2; bool f3;...
6
by: Jeremy Targett | last post by:
Hello, I'm trying to initialise a two-dimensional array of characters, 12 by 60, with each entry maximum 6 characters wide. (It's a fix I'm making to an old program which I learned just enough C...
107
by: DaveC | last post by:
I always used to initialise variables at declaration, then a couple of colleagues started telling me it was bad practice and that the compiler should be left to spot the use of uninitilised...
33
by: Adam Chapman | last post by:
Hi, Im trying to migrate from programming in Matlab over to C. Im trying to make a simple function to multiply one matrix by the other. I've realised that C can't determine the size of a 2d...
20
by: cowboyrocks2009 | last post by:
Hi, I need help to automate my code to take data from input file. Also I need to create it as a function so that I can pass it to some other program. I am new to Java so having a bit limitation to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.