Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 03-29-2007, 11:06 PM   #1 (permalink)
almulsi
Recruit
 
Join Date: Mar 2007
Posts: 4
almulsi is on a distinguished road
Smile 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;
}
}
}
almulsi is offline   Reply With Quote
Old 03-30-2007, 05:51 AM   #2 (permalink)
Deliverance
C++ Beginner
 
Join Date: Jul 2005
Location: Ottawa
Posts: 73
Deliverance is on a distinguished road
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
Code:
 display(seats);
(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;
}
Deliverance is offline   Reply With Quote
Old 03-30-2007, 06:27 AM   #3 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 676
DJMaze is on a distinguished road
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!
DJMaze is offline   Reply With Quote
Old 03-31-2007, 06:22 AM   #4 (permalink)
almulsi
Recruit
 
Join Date: Mar 2007
Posts: 4
almulsi is on a distinguished road
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
almulsi is offline   Reply With Quote
Old 03-31-2007, 06:35 AM   #5 (permalink)
almulsi
Recruit
 
Join Date: Mar 2007
Posts: 4
almulsi is on a distinguished road
hi again actually would you expline for me about the functions i would be so greatfull i f you do so
almulsi is offline   Reply With Quote
Old 03-31-2007, 05:39 PM   #6 (permalink)
almulsi
Recruit
 
Join Date: Mar 2007
Posts: 4
almulsi is on a distinguished road
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
almulsi is offline   Reply With Quote
Old 04-01-2007, 01:54 PM   #7 (permalink)
Deliverance
C++ Beginner
 
Join Date: Jul 2005
Location: Ottawa
Posts: 73
Deliverance is on a distinguished road
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
Code:
displayInt(5);
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;
}
Deliverance is offline   Reply With Quote
Old 04-01-2007, 01:59 PM   #8 (permalink)
Deliverance
C++ Beginner
 
Join Date: Jul 2005
Location: Ottawa
Posts: 73
Deliverance is on a distinguished road
Quote:
Originally Posted by DJMaze View Post
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
Deliverance is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Python - Multidimensional arrays? QUantumAnenome All Other Coding Languages 6 02-05-2007 03:27 PM
Dynamic Arrays, the 'right' way. Mr.Anderson Standard C, C++ 10 10-05-2004 06:00 AM
Methods of moving a database... DarkTwilkitri PHP 8 11-19-2003 05:02 AM


All times are GMT -8. The time now is 06:32 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting