Having trouble with arrays, Need help w/ assignment!

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

32 Replies - 1656 Views - Last Post: 16 December 2012 - 09:00 PM Rate Topic: -----

#1 4N63L-Z3R0  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 94
  • Joined: 19-October 12

Having trouble with arrays, Need help w/ assignment!

Posted 11 December 2012 - 10:13 AM

I am posting my assignment, code, and then following it are my questions.

Here is the gist of the assignment:

+ Ask user to enter seat prices for each row. The prices
can be stored in a separate array or read from an alt. file.

+ Then, display a seating chart. User may enter the row and
seat numbers for tickets being sold. Everytime a ticket or
group of tickets is purchased, display the total ticket
prices and update the seating chart.

+ Keep a total of all ticket sales. User should be given an
option of viewing this amount. (do-while loop?)

+ Also give the user an option to:
*see a list of how many seats have been sold
*how many seats are available in each row
*and how many seats are available in the entire
auditorium.

NO accepting of row or seat numbers that do not exist.
Make sure a particular seat is available before it is sold.

Here is my code SO FAR:
// Theater_Seating.cpp : Defines the entry point for the console application.

// This program brelongs to Nichole Moore.
#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
	const int ROWS = 15;
	const int SEATS = 30;
	char chart[ROWS][SEATS] = {'*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*',
							    '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*','*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*',
							    '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*','*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*',
							    '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*','*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*',
								'*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*','*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*',
								'*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*','*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*',
								'*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*','*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*',
								'*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*','*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*',
								'*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*','*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*',
								'*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*','*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*',
								'*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*','*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*',
								'*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*','*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*',
								'*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*','*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*',
								'*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*','*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*',
								'*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*','*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*'};
	int price[ROWS]; // Array for prices per row.
	int r, c;

	// Aske user for for prices per row.
	for (r = 0; r < ROWS; r++)
	{
		cout << "Enter price for row " << (r+1) << ": $";
		cin >> price[ROWS];
	}

	// Then display a seating chart where user can enter the row and
	// seat numbers
	// for tickets being sold.
	// While doing this, display the total ticket prices and 
	// update&display the seating
	// chart.

	// Display seating chart - first time around (all seats available)
	cout << "0|1|2|3|4|5|6|7|8|9|0|1|2|3|4|5|6|7|8|9|0|1|2|3|4|5|6|7|8|9\n";
	for (r = 0; r < ROWS; r++)
	{
		for (c = 0; c < SEATS; c++)
		{
			cout << chart[r][c];
		}
		cout << endl;
	}

	return 0;
}


I am having a really hard time trying to start this out. I need some advice. My first question is - is there any way to initialize my chart array to something more simple/useful? My second question is pretty much the same for how I am displaying my chart. I would like to use functions if it makes it any easier, but if anyone here is going to assist me, please leave out using pointers because we have not learned how to pass arrays through functions yet!

Any help is very, very much appreciated! I am losing my mind with this thing, already... (I already started it over once or twice)

P.S.! The program I have displayed above displays the chart, but I don't know if I am going to have trouble with it later on (in order to meet the requirements of the assignment) AND right after the chart is displayed, the program errors and forces me to Abort. And I am obviously not done writing out the code yet, I have only yet just begun... I would also like to be able to display the row number next to the row.

Again, any help is appreciated!

This post has been edited by 4N63L-Z3R0: 11 December 2012 - 10:14 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Having trouble with arrays, Need help w/ assignment!

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1047
  • View blog
  • Posts: 4,452
  • Joined: 09-June 09

Re: Having trouble with arrays, Need help w/ assignment!

Posted 11 December 2012 - 11:36 AM

Quote

I would like to use functions if it makes it any easier ...
please leave out using pointers because we have not learned how to pass arrays through functions yet

The only way to pass an array to a function is with a pointer; however, there is some array syntax that let's you avoid using pointer notation.

void printArray(int arr[], int size) {
   for(int i=0; i<size; i++)
      std::cout<<arr[i]<<std::endl;
}



this is code I'm sure is annoying to look at
char chart[ROWS][SEATS] = {'*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*',
							    '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*','*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*',
							    '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*','*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*',
							    '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*','*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*',
								'*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*','*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*',
								'*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*','*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*',
								'*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*','*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*',
								'*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*','*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*',
								'*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*','*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*',
								'*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*','*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*',
								'*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*','*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*',
								'*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*','*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*',
								'*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*','*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*',
								'*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*','*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*',
								'*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*','*', '*', '*', '*', '*', '*', '*', '*', '



The array can be initalized by calling the function std::fill for memset.

i.e.
#include <iostream>

using namespace std;

const int ROWS = 15;
const int SEATS = 30;

int main(int argc, char *argv[]) {
   char chart[ROWS][SEATS];
   memset(chart, '*', ROWS * SEATS); //initialize to '*'
   
   return 0;
}


This post has been edited by jjl: 11 December 2012 - 11:47 AM

Was This Post Helpful? 0
  • +
  • -

#3 4N63L-Z3R0  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 94
  • Joined: 19-October 12

Re: Having trouble with arrays, Need help w/ assignment!

Posted 11 December 2012 - 02:08 PM

Yes, it is very annoying to look at (the one I posted), I can't stand it! Haha.

I think I understand where you are going. The only thing is I don't see how I would be able to modify that display for the table as the user inputs what seats and rows need to be replaced.

"*" is for when it's empty, and once they pick a spot, I need to mark it with something like "#" to show that it is taken. So would the memset code you just showed me interfere with that?
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,753
  • Joined: 25-December 09

Re: Having trouble with arrays, Need help w/ assignment!

Posted 11 December 2012 - 02:20 PM

No the memset() just initializes all elements of the array to '*'.


You may want to study up on arrays if you don't know how to access a particular element of an array.


Jim
Was This Post Helpful? 0
  • +
  • -

#5 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1047
  • View blog
  • Posts: 4,452
  • Joined: 09-June 09

Re: Having trouble with arrays, Need help w/ assignment!

Posted 11 December 2012 - 03:44 PM

Quote

"*" is for when it's empty, and once they pick a spot, I need to mark it with something like "#" to show that it is taken.

You can access the array elements at specific indices to change there corresponding values.

i.e.
#include <iostream>

using namespace std;

const int ROWS = 2;
const int COLS = 2;

//pass 2d array to function and print it
void printChart(char chart[ROWS][COLS]) {
   for(int i=0; i<ROWS; i++) {
      for(int j=0; j<COLS; j++) {
         std::cout<<chart[i][j];
      }
      std::cout<<std::endl; //print a new line for every row
   }
   std::cout<<std::endl; //add an extra empty line
}
int main() {
   
   char chart[ROWS][COLS];
   memset(chart, '*',  ROWS * COLS);
   
   printChart(chart);
   chart[0][0] = '#'; //set element [0][0] to #
   printChart(chart);

   return 0;
}


Was This Post Helpful? 0
  • +
  • -

#6 4N63L-Z3R0  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 94
  • Joined: 19-October 12

Re: Having trouble with arrays, Need help w/ assignment!

Posted 12 December 2012 - 08:04 AM

For those of you trying to help me, sorry for my delay. I am working on my program.

With what I have come up with so far, I continue to have the same Run-Time error. The program will go through, but then after it displays the seating chart, it says Run-Time Check Failure #2 - Stack around the variable 'price' was corrupted.

I've already tried making it into a new/different array, and have put it into a new file several times, and everything. I am not sure what else to do to get this error to go away.


EDIT: Oh never mind, it was a simple mistake on my behalf! I fixed this issue. Just ignore this individual post, hah.

This post has been edited by 4N63L-Z3R0: 12 December 2012 - 08:08 AM

Was This Post Helpful? 0
  • +
  • -

#7 4N63L-Z3R0  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 94
  • Joined: 19-October 12

Re: Having trouble with arrays, Need help w/ assignment!

Posted 14 December 2012 - 01:23 PM

.

This post has been edited by 4N63L-Z3R0: 14 December 2012 - 02:11 PM

Was This Post Helpful? 0
  • +
  • -

#8 4N63L-Z3R0  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 94
  • Joined: 19-October 12

Re: Having trouble with arrays, Need help w/ assignment!

Posted 14 December 2012 - 02:12 PM

I got so far as displaying the table, entering what spots are full and etc.
// Theater_Seating_N.cpp : Defines the entry point for the console application.
	
// This program brelongs to Nichole Moore.
#include "stdafx.h"
#include <iostream>
using namespace std;

// Function prototype.
void displayChart();

const int ROWS = 15;
	const int SEATS = 30;
	int price[ROWS];
	const char FULL = '*';
	const char EMPTY = '#';
	char chart[ROWS][SEATS];
	int r, c;

int main()
{
	int row_num, seat_num;
	char cat = 'X';
	char approve;
	
	// Ask for prices per row.
	for (r = 0; r < ROWS; r++)
	{
		cout << "Enter price for row " << (r+1) << ": $";
		cin >> price[r];
	}
	
	// Create the seating chart (as EMPTY).
	for (r = 0; r < ROWS; r++)
	{
		for (c = 0; c < SEATS; c++)
			chart[r][c] = EMPTY;
	}

	// Function call.
	// Display the seating chart/updated seating chart.	
	displayChart();

	while (cat == 'X')
	{
		// Ask for rows and seats.
		cout << "Enter Row number: ";
		cin >> row_num;
		cout << "Enter Seat number: ";
		cin >> seat_num;
		
		for (r = 0; r < ROWS; r++)
		{
		cout << "Price for that seat is: " << "$" << price[row_num-1] << ". Approve? Y/N: ";
		cin >> approve;
		while (approve == 'N' || approve == 'n')
		{
			cout << "Enter a different Row number: ";
			cin >> row_num;
			cout << "Enter a different Seat number: ";
			cin >> seat_num;

			cout << "Price for that seat is: " << "$" << price[row_num-1] << ". Approve? Y/N: ";
			cin >> approve;
		}
		
		// Make the chosen row & seat FULL.
		if (chart[row_num-1][seat_num-1] == '#')
			chart[row_num-1][seat_num-1] = FULL;
		
		// Display updated chart.
		displayChart();
		}
	}
	
	return 0;
}

// Function header.
void displayChart()
{
	cout << "        123456789012345678901234567890\n";
	for (r = 0; r < ROWS; r++)
	{
		cout << "Row " << (r+1) << "\t";
		for (c = 0; c < SEATS; c++)
		{
			cout << chart[r][c];
		}
		cout << endl;
	}
}



Now the rest of the things that need to be done are:
Keep total of ticket sales
> Give user an option to view this total
Give user the option to see a list of:
> how many seats have been sold
> how many seats available in each row
> how many seats available in the entire auditorium
NO accepting of row or seat numbers that do not exist.
Make sure a particular seat is available before it is sold.

I have been fiddling with code to get all of these tasks and calculations done, but nothing has been working properly. Does anyone have any advice or calculations?

Any help is very much appreciated.

This post has been edited by 4N63L-Z3R0: 14 December 2012 - 02:13 PM

Was This Post Helpful? 0
  • +
  • -

#9 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Having trouble with arrays, Need help w/ assignment!

Posted 15 December 2012 - 04:08 AM

If your chart is correct, you should be able to work from that, for most of this. How many seats have been sold? How many seats are available? Or available seats in each row?

Count 'em.

What is the problem specifically, that you're stuck on? "Been fiddling" can cover a lot of ground, LOL! ;)
Was This Post Helpful? 0
  • +
  • -

#10 4N63L-Z3R0  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 94
  • Joined: 19-October 12

Re: Having trouble with arrays, Need help w/ assignment!

Posted 15 December 2012 - 09:48 AM

Lol! Yes I agree, it could cover a lot of ground! :) I believe when I wrote that post, I was so befuddled because I felt so close to being finished and couldn't figure it out. But what I was having trouble with the most were the calculations.

Now the only problems I am having is figuring out the calculation for how many seats are available in each row. Any ideas?

Here are all of the calculations I have so far, and the one I am having trouble with figuring out how to write (the "how many seats are available in each row" calculation) I set all as a comment in green so it is easier to find:
/********ENTER RESULTS HERE********/
	// Calculate & display total sales.
	cout << "\n\n*****************   THEATER STATISTICS   *****************\n\n";
	for (r = 0; r < ROWS; r++)
		for (c= 0; c < SEATS; c++)
			if (chart[r][c] == '*') 
				totalPrice += price[r];
			cout << "Total sales:   $" << totalPrice << endl;
	
	for (r = 0; r < ROWS; r++)
		for (c = 0; c < SEATS; c++)
			if (chart[r][c] == '*')
				totalSold++;
			cout << "Total seats sold:   " << totalSold << endl;
			
	/*cout << "Enter a row number to see the number of seats available in it: ";
	cin >> row_num2;
	for (r = 0; r < ROWS; r++)
		for (c = 0; c < SEATS; c++)
			if (chart[row_num2][c] == '#')
				totalAvailinROW = chart[row_num2][c];
				cout << "Total seats available in that row:   " << totalAvailinROW << endl;*/
			
	totalAvailable = (ROWS * SEATS) - totalSold;
	cout << "Total seats available in entire auditorium:   " << totalAvailable << endl;

This post has been edited by 4N63L-Z3R0: 15 December 2012 - 11:21 AM

Was This Post Helpful? 0
  • +
  • -

#11 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1047
  • View blog
  • Posts: 4,452
  • Joined: 09-June 09

Re: Having trouble with arrays, Need help w/ assignment!

Posted 15 December 2012 - 12:17 PM

const int ROWS = 15;
	const int SEATS = 30;
	int price[ROWS];
	const char FULL = '*';
	const char EMPTY = '#';
	char chart[ROWS][SEATS];
	int r, c;

int main()



Global constants are fine.
Global variables are bad practice.

You should not develop a habit of using global variables, you should define these variables in main and practice how to pass them to your functions.
Was This Post Helpful? 0
  • +
  • -

#12 4N63L-Z3R0  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 94
  • Joined: 19-October 12

Re: Having trouble with arrays, Need help w/ assignment!

Posted 15 December 2012 - 09:33 PM

Could you please explain to me why global variables are bad? People have only been telling me they're bad and not explaining why.

Plus I am still having the same problem with my calculation...
Was This Post Helpful? 0
  • +
  • -

#13 jimblumberg  Icon User is online

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,753
  • Joined: 25-December 09

Re: Having trouble with arrays, Need help w/ assignment!

Posted 15 December 2012 - 09:52 PM

Global variables are bad because it is hard to tell where this variable is being changed. Any function can modify this value and when your number of functions increase it will become harder and harder to keep track of these variables. When you learn to properly pass variables to and from your functions it will be easier to tell when variables change because unless you pass the variable into the function you know that only the variables passed can possibly be changed, no other variables will be affected.

Quote

Plus I am still having the same problem with my calculation...


What exactly is the problem with your calculation?

Jim
Was This Post Helpful? 0
  • +
  • -

#14 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Having trouble with arrays, Need help w/ assignment!

Posted 15 December 2012 - 09:59 PM

Your code has a bad variable for counting the number of seats available in a row, which I'll show you.

Sweet! ;)


//This block of code is correct:

10	    for (r = 0; r < ROWS; r++)
11	        for (c = 0; c < SEATS; c++)
12	            if (chart[r][c] == '*')
13	                totalSold++;
14	            cout << "Total seats sold:   " << totalSold << endl;
15	             
16	    /*cout << "Enter a row number to see the number of seats available in it: ";
17	    cin >> row_num2;

//This block of code is incorrect:

18	    for (r = 0; r < ROWS; r++) {
19	        for (c = 0; c < SEATS; c++)  {

//Right here: row_num2 - should be r, just like you used in the upper block of code

20	            if (chart[[color="#FF0000"]row_num2[/color]][c] == '#')
21	                totalAvailinROW = chart[row_num2][c];
               } //end of one row's counting
22             cout << "Total seats available in that row:   " << totalAvailinROW << endl;*/
               totalAvailinRow = 0;
            } //end of all counting

The if statement should be:

if(chart[r][c] == "#') 
   totalAvailinRow++;
/*
Then, outside of the column nested for loop (that is, after one row has been counted up, you print up the totalAvailinRow number AND reset it to 0, so it can count up the next row's available seats.

I've added curly braces to help - and you should use them, as well. 
*/




Give that a try! ;)
Was This Post Helpful? 0
  • +
  • -

#15 4N63L-Z3R0  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 94
  • Joined: 19-October 12

Re: Having trouble with arrays, Need help w/ assignment!

Posted 15 December 2012 - 10:01 PM

.

This post has been edited by 4N63L-Z3R0: 15 December 2012 - 10:01 PM

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3