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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>                
#include <fstream>    
#include <iomanip>
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 <<endl<< "There are "<< min_Left << " minutes and ";
    cout <<sec_Left << " seconds left of space on the 80 minute CD.";
    cout <<endl<<endl;
    
    system ("PAUSE");
    return 0;
}

// To open file
void openFile(ifstream &inData)
{ 
     inData.open("c:\\CSP31A\\songs.txt");
     if ( !inData )
          cout <<"Unable to open the file songs.txt";
}

// to display columns and headline.
void printInfo()
{
     cout << setw(8) << "Song" << setw(28)
    << "Song Time" << setw(28)
    << "Total Time" << setw(25)
    << "Number" << setw(20)
    << "Minutes" << setw(10)
    << "Seconds" << setw(18)
    << "Minutes" << setw(10)
    << "Seconds" << setw(22)
    <<endl<< endl;
}


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,

Welcome to the forums - I believe you are the third person this week to ask about this specific assignment.

Let's look at your input validation:

30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// 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:

1
2
3
4
5
6
7
8
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.