I'm taking an Intro to C++ course and I'm having some difficultly with a lab I'm working on. I don't have any code yet. I've been working on my design file but don't know which direction to go in to solve the problem. I want to put the dataSheet into an array but since I don't know the size of the dataSheet I don't know how big to make the array. I've posted the assignment if that helps explain the problem.

A company uses facilities in 3 towns:  X, Y and Z.  

   Town X:  location of a Mine where ore is extracted
   Town Y:  location of a Warehouse where the ore is sold
   Town Z:  location of a Bank where money is stored

  A Train is used:
    - to move the ore from X to Y;
    - to move the money from Y to Z.

  Train route/schedule:
    1. First:  Train takes 3 hours to go from X to Y
    2. Next:   Train takes 5 hours to go from Y to Z
    3. Next:   Train takes 6 hours to go from Z to X
    4. Repeat steps 1 to 3.

  Load/Unload:  It takes 1 hour (total) in each town to load and/or
    unload the Train.

    Town X:  ore is loaded from Mine onto Train
    Town Y:  ore is unloaded from Train and put in Warehouse,
             90 percent of current money at Warehouse
             is loaded onto Train
    Town Z:  money is unloaded from Train and put in Bank

   Initial Setup:

     The Train is located in Town X.  The Train is already loaded
     with the maximum amount of ore that it can carry.  The Train
     is ready to start its route.


      The Bank pays the expenses for the company once per hour.
      Note:  a negative Bank balance is OK.
      These include:

        - a fixed expense
        - a variable expense


  Data from the various facilites is collected and put into a
  data sheet.  The program must read this data and prepare a report.
  All data except Item 1 on each line include fractional values.

    Filename:  dataSheet

      1st line:  initial data

        Item 1:  0 (data sheet is ready to be processed)
                 other (data sheet is not ready, do not process)
        Item 2:  initial tons of ore at Mine
        Item 3:  initial tons of ore at Warehouse
        Item 4:  initial amount of money at Warehouse
        Item 5:  initial amount of money in Bank
        Item 6:  maximum tons of ore that the Train can carry
        Item 7:  the amount of fixed expenses each hour

      variable number of lines:  data collected each hour

        Item 1:  hour (hour during which data is collected,
                       positive integer)
                 > hours will be consecutive, starting at 1
        Item 2:  tons of ore collected at Mine during this hour
        Item 3:  maximum tons of ore that could be sold at
                 Warehouse each hour (if ore is available)
        Item 4:  price of 1 ton of ore for this hour
        Item 5:  amount of expenses paid this hour

      last line: 0 0 0 0 0

     Program Output:

   Produce a Final Report that shows:

     1. The time during which data was collected in this data sheet.

        - number of complete days
        - hours for the last day

     2. The profit (or loss) during the period covered by the
        data sheet.

You have a few choices.

  1. Make an array that's sufficiently big to meet the likely amount of data
  2. Dynamically allocate the memory at run-time, using new
  3. Use std::vector to manage the memory allocation for you

I'd say that you probably need to use option 2 for this task, since it's a class assignment. (In practice I'd use std::vector, but you may not have covered that yet).

You should Google for "C++ new" and there will be a lot of things telling you about how to use the new operator to dynamically allocate memory.

Edited 4 Years Ago by ravenous

Alright. I know we have talked about new arrays so I thought that might be an option, but I wasn't sure. Thanks for your help.

This article has been dead for over six months. Start a new discussion instead.