average mins and secs separately

So i have this program and i can't think of how is it possible to count average and then write minutes and seconds separately. the numbers go like this and i have them in separate arrays. any help is appreciated
4 32
4 52
3 30
3 23
2 41
2 49
3 28
4 33
3 22
i tried with this code i get 33minutes 10seconds but i still have to divide by 9. what can i do else to write them separately again?
1
2
3
4
5
6
7
8
9
10
11
for (int i =0;i<n;i++)
{
            vid = D[i].m + vid;
            vids = D[i].s + vids;
           
            if (vids >=60)
            {
                vids = vids - 60;
                vid = vid + 1;
            }
}
Last edited on
One way would be to
-- convert minutes and seconds to seconds
-- add up the seconds
-- find the average seconds
-- convert average seconds back to minutes and seconds

So if you have a total of 33 minutes and 10 seconds
-- total seconds is 60 * 33 + 10
-- average seconds is total seconds / number of items
-- converting back to minutes and seconds....
---- minutes is average seconds / 60
---- seconds is average seconds % 60
Last edited on
Assuming the data is stored in a file, read the file into a struct, get total secs and from that avg time:
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
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <utility>

struct Time
{
    unsigned int m_mins = 0;
    unsigned int m_secs = 0;
};

int returnSecs(const Time& t)
{
    return (t.m_mins * 60 + t.m_secs);
}

int main()
{

    std::ifstream inFile {"F:\\test1.txt"};
    std::vector<Time> myTimes{};
    while (inFile)
    {
        Time temp{};
        inFile >> temp.m_mins >> temp.m_secs;
        if(inFile)
        {
            myTimes.push_back(std::move(temp));
        }
    }

   int totalSecs{};
   for (const auto& elem : myTimes)totalSecs += returnSecs(elem);

   std::cout << "Average time (mins/secs): " << (totalSecs/myTimes.size()) / 60
                << ":" << (totalSecs/myTimes.size())%60 << "\n";

}



thank you guys, doing it with % worked, would't have thought about it.
Topic archived. No new replies allowed.