c++programming (functional decomposition)

Using functional decomposition, write program in C++ that arranged a list of songs in the order in which you want to play them. However, you would like to maximize
your use of space on the CD, which holds 80 minutes of music. So, you want to
figure out the total time for a group of songs and see how well they fit.

The data will be read from an input text file. The duration of each song record in second. Hence, if the data read from file is 310, it mean 310 seconds. you need to convert them to 5 mins and 10 sec.

as you read data, you need to keep a running total of all the duration of all the songs read. This running total will be displayed each time you read a new data value. Once completed reading the file, you will need to display how much space left in the CD in terms of minutes and seconds; remember that the total capacity is 80 minutes.

Input validation:

Songs should have a minimum of 1 minute and a maximum of 10 minutes. Hence test
for any values less than 60 seconds or more than 600 seconds. You donâ€™t need to
display any message for songs less than 1 minute or more than 10 minutes but your code must account for it by not including them in the calculation.

Here is my data.
310
482
601
259
983
273
567
-12
535
45
300

Here is my code: What i have got so far.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192`` ``````#include #include #include using namespace std; // Global variable #define totalTime 4800 // function prototype void openFile(ifstream &); void printInfo(); int main() { ifstream inData; int songNum; int current = 0; int currentMin = 0; int currentSec = 0; int used_Min = 0; int used_Sec = 0; int used_total = 0; int min_Left = 0; int sec_Left = 0; // Call function openFile(inData); printInfo(); // fisrt record inData>>current; for( songNum = 1;songNum< 10; songNum ++){ //calculate minutes and second. if(current >59 && current <601){ currentMin = (current/60); currentSec = (current%60); } //Calcualte time total time used_Min += currentMin; // minute used used_Sec += currentSec; // second used used_total = (used_Min * 60) + used_Sec; // Display result cout << fixed << showpoint << setprecision(2) << setw(7) << songNum << setw(18) << currentMin << setw(10) << currentSec << setw(18) << used_Min << setw(10) << used_Sec << setw(15) << endl; //Get another record inData >>current; } //Close file inData.close(); //Calculate remain time. min_Left = (totalTime - used_total) / 60; sec_Left = (totalTime - used_total) % 60; //Display the result. cout <

Here is my output.

 ```song song time total time # min sec min sec 1 5 10 5 10 2 8 2 13 12 3 8 2 21 14 4 4 19 25 33 . . and so on.... there are 17 min and 46 sec left in 80 min CD.```

Notice that on song # 3 its over the limit which it should be 10 min and 1 sec.
and i dont want to display it, and want it to skip to song # 4. My text file have 11 values but it should only display 8 value since 3 of them are not in range.

I think i do the input validation wrong but i still couldnt figure another way to do it.

I also want to read the text file into array and store them. I think it would be a little bit easy that way. But again i havent figure a way to do it yet.

any suggestion on how should i approach them. One more question, how should i do a prototype function for my input validation? any Idea?
Hi there,

Let's look at your input validation:

 ``3031323334353637383940414243444546474849505152535455`` ``````// fisrt record inData>>current; //why not do that at the top of the for-loop, makes more sense for( songNum = 1;songNum< 10; songNum ++){ inData>>current; //calculate minutes and second. if(current >59 && current <601){ //here you validate, but everything outside of the if-block currentMin = (current/60); //will still execute if validation fails currentSec = (current%60); } //Calcualte time total time used_Min += currentMin; // minute used used_Sec += currentSec; // second used used_total = (used_Min * 60) + used_Sec; // Display result cout << fixed << showpoint << setprecision(2) << setw(7) << songNum << setw(18) << currentMin << setw(10) << currentSec << setw(18) << used_Min << setw(10) << used_Sec << setw(15) << endl; //Get another record inData >>current; }``````

So you need to put the rest of your code within that validating if-block too.

 I also want to read the text file into array and store them. I think it would be a little bit easy that way. But again i havent figure a way to do it yet.

Since you don't know beforehand how many songs you will need to put into the array, you would have to use dynamic memory allocation or, preferably an STL container such as std::vector, which I fear might be out of the scope of the assignment. Also, I don't think you need to sort the data or anything, just calculate a running total, so you shouldn't need an array for as far as I can tell.

 One more question, how should i do a prototype function for my input validation? any Idea?

I don't think you need a separate input validation function seeing as the validation is only one if-statement anyway. Of course it wouldn't hurt and would allow for easier addition of validation afterwards. Such functions usually return a boolean type:

 ``12345678`` ``````bool validate(int input) { return (input > 58 && input < 601); } //... if (validate(current) == true) //... ``````

Hope that helps.

All the best,
NwN
Thanks! Thats helps alot.

Last edited on
Topic archived. No new replies allowed.