# HELP with ARRAYS in C++



## newbieangel (Sep 14, 2005)

hello, im new to the forum and i wanted to help with my c++ program. 
the teacher wants us to design a progrm that would keep track of airplanes awaitin landing at an airport. the program will maintain a queue of flights numbers.

the program will be abel to do the following:

Add a new flight number to the end of the queue (got it done)
LAnd the plane at the front of the queue - problems wit it-
display the queue - got it done
seach for a specific flight number in queue ( didn't get there yet)
move a flight number one one position in the queue to another ( didn't get there yet)

this is what i have so far. it runs but something is wrong and i don't know what it is.

#include <iostream>
#include <iomanip>
using namespace std ;

const int MAXQUEUE = 100 ; // maximum number of flights in the queue

void displayQueue (int[], int) ;
void getChoice ();
void addPlane (int[], int&) ;
int checkPlane (int[], int, int) ;

int main ()
{
int queue[MAXQUEUE] ; // array of incoming flight numbers
int qsize = 0 ; // number of flights in the queue
char choice; // user's choice of next operation

do
{ // start menu loop
displayQueue (queue, qsize) ;
getChoice();
switch (choice)
{ // start switch
case 'S' : cout << "under construction" << endl; break ;
case 'L' : cout << "under construction" << endl; break ;
case 'A' : addPlane (queue, qsize) ; break ;
case 'M' : cout << "under construction" << endl; break ;
case 'Q' : cout << "program ended" << endl ;
} // end switch
} while (choice != 'Q');	// end menu loop

return 1 ;
}

//============displayQueue=====================
// task - display the position and flight number of each flight in the array
// pre - given an array of flight numbers and number of flights in the array
// post - nothing

void displayQueue (int list[], int lsize)
{ // start function
int w ;
cout << endl << endl << endl ;
if (lsize == 0)
cout << "no flights awaiting landing at this time" << endl ;
else
{ // start listing
cout << "Queue size = " << lsize << endl << endl ;
cout << "Position Flight#" << endl ;
for (w = 0; w < lsize; w++)
cout << setw(5) << w + 1 << setw(12) << list[w] << endl ;
} // end listing
} // end function

//============getChoice========================
// task - obtain a choice code from the user
// pre - nothing
// post - a valid, uppercase code is returned

void getChoice()
{ // start function
char holdCode;
char pick;


if(pick == holdCode) 
{
cout << endl << " to search for a flight number, enter S" ;
cout << endl << " to add a flight to the queue, enter A" ;
cout << endl << " to land the next flight, enter L" ;
cout << endl << " to move a flight in the queue, enter M" ;
cout << endl << " to quit the program, enter Q" ;
cout << endl << " enter your choice " ;
cin >> holdCode ;
}
else
{
cout << " not a valid choice "<<endl;
}
holdCode = toupper(holdCode) ; // convert to upper case
} // end function


//======================addPlane===========================
// task - add a new flight to the queue
// pre - given an array of flight numbers and number of flights in array
// post - size of array is increased, new flight number added to end

void addPlane (int list[], int& lsize)
{ // start function
int newFlight ; // flight number to be added to queue
int w;
char pause ;
cout << endl << "Enter flight number to add : " ;
cin >> newFlight;



if (lsize == MAXQUEUE)
{ // start queue is full
cout << endl << "EMERGENCY! queue is full, cannot add flight" << newFlight ;
cout << endl << "press any key to continue" ;
cin.get(pause) ;
return ;
} // end queue is full
//list[lsize] = newFlight ; // add flight to end of list
lsize++ ; // increase list size

for (w = 0; w < lsize; w++)
if (newFlight == list[w])
return ;
} // end function


//=====================checkPlane=============================
// task - search the list of flights for a given flight number
// pre - given an array of flight numbers, number in array, and target flight
// post - return queue position if target is found in array, else return -1

int checkPlane (int list[], int lsize, int target) 
{ // start function
int w ;
for (w = 0; w < lsize; w++)
if (target == list[w])
return w + 1;
return -1 ;
} // end function




any help would be appreciated. thanks-

Joyce


----------



## ricer333 (Sep 17, 2004)

*answer*

Joyce
Although i feel your pain, I'm not in the habit of writing code. However, I will help you with some of the concepts. At a quick (and I mean quick) look, I see your 'addPlane' function checks to see if the 'queue' is full, if not, then adds the new plane to the end. That's fine, however, you will definetly want a function that updates the queue when you remove a plane. Maybe a removePlane(), that removes the first AND (this is important) shifts all the other planes behind it into the spot above them. You've got to keep in mind that the queue is always updating whenever something is added, or removed. If you do not do this, then you will keep adding planes, and eventually exceed your size, even if planes should have landed and exited the queue successfully. One way to do this is to have 2 pointers. One pointing to the beginning (aka the head) of the queue (so if you are using a static array, that's fine too) and one that points to the end (aka the tail) of the queue. 

So, I would have a static array of WHATEVER_SIZE declared, have a head pointer (type int) and a tail pointer (type int) so that I could reference the array's indecies easily. Every time a new plane comes in update the tail pointer (you're kinda doing that with the lsize (i just wouldn't use lsize as the name)). When you remove a plane, pop it off the list by updating the head pointer or shifting everything up one space.

--------------------
| plane 1 | plane 2 |
--------------------
| index 0 | index 1 |
--------------------
|**tail**|**head*|
--------------------

after plane 1 removed:
--------------------
| plane 2 | ***** |
--------------------
| index 0 | index 1 |
--------------------
| tail/head|******|
--------------------


----------



## mgoldb2 (Dec 16, 2004)

while ricer way would work but I guessing you in a first level c++ class and proberly not so familar with using pointers so I going to show a example with out using pointers. Here a program that does what you want that I wrote very quickly that should show you what you need to do and just uses basic array and function rules. 


```
#include<iostream>

using namespace std;

int main()
{
	void add(int[],int&);
	void display(int[],int&);
	void land(int[],int&);
	int search(int[],int&,int);
	void s(int[], int&);


	const int max=100;
	int size=0;
	int queue[max];
	int c;
	int t;

	while(c!=6)
	{
		c=0;

		while(c<1 || c>6)
		{
			cout<<"1. Add new flight to end of queue"<<endl;
			cout<<"2. Land plane at front of queue"<<endl;
			cout<<"3. Display queue"<<endl;
			cout<<"4. Search for specific flight in queue"<<endl;
			cout<<"5. Switch two flights in queue"<<endl;
			cout<<"6. Exit program"<<endl;
	
			cout<<endl<<"? ";
			cin>>c;

			if(c<1 || c>6)
			{
				cout<<"invalid responce choose again"<<endl;
			}
		}

	    switch (c)
		{
			case  1: 
				add(queue,size);
				break ;
			case  2:
				land(queue,size); 
				break ;
			case  3: 
				display(queue,size);
				break ;
			case  4:
				int f;
				cout<<"search for flight number? ";
				cin>>f;
				t=search(queue,size,f);
				if(t!=-1)
				{
					cout<<"found"<<endl<<endl;
				}
				else
				{
					cout<<"not in array"<<endl<<endl;
				}
				break ;
			case  5:
				s(queue,size);
				break;
			case  6:
				cout<<"program exit"<<endl;
		}

		
	}

	return 0;
}
```
Pretty much provide a menu of your options. The main only operates as the interface and dont acturally do any of the work except for allowing you to choose what you want to do then do the function. I broke this rule slightly for case 4 only because I wanted to be able to reuse the function for the switching flights which mean i had to make it as simple and broad as possiable.


```
void add(int q[], int& s)
{
	int f;
	cout<<"enter flight number to be added?";
	cin>>f;

	if(s==100)
	{
		cout<<"array is full"<<endl<<endl;
	}
	else
	{

		if (s==0)
		{
			q[0]=f;
		}
		else
		{
			int x=s-1;
			while(x>=0)
			{
				q[x+1]=q[x];
				x--;
			}
			
			q[0]=f;
		}

		s++;
	}
}
```
For this function I have everything added to the front of the array which makes removing the first in really simple. This is a matter of perference I rather rearrange on the input other might rather rearrange on the output. If you have any questions on how the function work just ask.


```
void display(int q[], int& s)
{
	
	cout<<"display array"<<endl<<endl;
	int x=s;
	while(x>0)
	{
		cout<<q[x-1]<<endl;
		x--;
	}
	cout<<endl<<endl;
}
```
pretty basic circle through the array to display everthing


```
void land(int q[], int& s)
{	
	if(s>0)
	{
		s--;
	}
	else
	{
		cout<<"there are no planes to land"<<endl<<endl;
	}
}
```
Since the whole program work using size just decreasing the size make it like the last one never exist.


```
int search(int q[], int& s, int f)
{
	int x=s-1;
			
	while(x>=0)
	{
		if(q[x]==f)
		{
			return x;
		}

		x--;
	}
		
		return -1;
}
```
circle through array. if found flight you looking for it returns the array index(I use this fact for my advantage in the last function) otherwise it just return not found.


```
void s(int q[], int& s)
{
	int fa;
	int fb;

	cout<<"enter flight";
	cin>>fa;
	cout<<"enter flight to switch first flight with";
	cin>>fb;

	int a = search(q,s,fa);
	int b = search(q,s,fb);

	if(a!=-1 && b!=-1)
	{
	int c=q[a];
	q[a]=q[b];
	q[b]=c;
	}
	else
	{
		cout<<"one of the flights not in the array"<<endl<<endl;
	}
}
```
uses search to determine the 2 index of the flights you want to switch. then it switch the content of the 2 spots in the array.


If you got any questions on how it work feel free to ask. I did it this way because I found it faster then playing around with your code. You can look at what I did to help you figure out how to get your code to work. If you really want to learn c++ it best you use my code to learn and fix your code and not just turn in my code to your teacher because you wont learn anything that way.


----------

