Simple C++ Program

Pages: 12345
a_value isnt right. But im trying to print the one value in the file.... what would i use to do that? v?
oh no problem. Im not sure let me look at that real quick

edit: what do you mean print the one value in the file. Your fillvector should read it into the vector and it should then be in the vector and that loop would print it??
Last edited on
i think it has to do with the while statement in the fill vector. i think it is accidently reading in garbage to the vector or something for when there are no numbers or 1 number.
Say theres one value in the text file. The current function that prints the values to the screen will always do it once then want to do it again. If there is no other value it errors because its looking for one that isnt there right? If thats correct I think we need to put something in the loop that protects from having one number in the file like
1
2
3
4
		if (v <= 0)
		cout << "Values entered: " << v;
		cout << "Sorted values: " << v;
		cout << "Median = " << v;
opps, if (v <= 1)
v is a vector. You mean to say v.size(). the size of the vector at minimum can be zero. Even if this were the case it should still have no problem printing nothing to the screnn because this loop would be perfectly ok

for(int i = 0; i < v.size(); i++) // this will fail the first test and thats fine theres no numbers in the vector.
Ya your right. So you think its in the while loop reading garbage then.. ill look at that.
wait dont change anythng yet. I just commented out everything all the prints and only read in the vector and got no errors etc. so it might be the prints. Im bout to comment out sections to see where the problem stems from
ok the problem is in the median code i think. and it has to do with the first if statement
Ok, ill try that aswell.
Just change the template to this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
template <typename T>
double median(const vector<T> & v)
{
	int length = v.size();
	double median = 0; // set to 0 in case no number is in file
	if(length == 1)
		return v[length-1];// if size ==1 v[1] is accessing out of range 
	else if( length % 2 == 0 ) // even
	{
		median = (v[length/2] + v[(length/2)-1]) / 2; 
	}
	else // odd
	{
		median = v[length/2];
	}
	return median;
}
Oh sweet. How did you figure that out so fast. haha
divide and conquer my friend. divide and flippin conquer. I just commented out most of the code until it worked then I started uncommenting parts. and the median line was giving issues. then i just went there and examined it.
Well thanks for all the help. Most likely couldn't have done this with out your help. Anyways I need to get some sleep cause i have a final in less then 6 hours!

Again thanks for all the time you spent on this.
No problem. Good luck on the final. I had mine last week. Aced that mother. hopefully you will do the same

1
2
3
4
5
6
template <typename T>
double median(const vector<T> & v)
{
	T summa = std::accamulate(v.begin(), v.end(),  T(0));
        return v.empty() ? 0.0 : double(summa / v.size());
}

Last edited on
Example whole programe
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

#include <numeric>
#include <iostream>

#include <vector>

template <typename T>
double median(const std::vector<T> & v)
{
    T summa = std::accumulate(v.begin(), v.end(),  T(0));
    return v.empty() ? 0.0 : (static_cast<double>(summa) / static_cast<double>(v.size()));
}

int main()
{
    std::vector<int> ints;

    for(int i = 0; i < 10; ++i)
    {
        ints.push_back(i);
    }

    std::cout << median(ints);

    return 0;
}
New implementation, here you can use other stl container type, not only vector, see new example
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
#include <numeric>
#include <iostream>
#include <vector>
#include <list>

template <typename ContainerT>
double median(const ContainerT& container)
{
    typedef typename ContainerT::value_type ElemT;
    ElemT summa = std::accumulate(container.begin(), container.end(),  ElemT(0));
    return container.empty() ? 0.0 : (static_cast<double>(summa) / static_cast<double>(container.size()));
}

int main()
{
    std::vector<int> ints;
    std::list<double> doubles;

    for(int i = 0; i < 10; ++i)
    {
        ints.push_back(i);
        doubles.push_back(static_cast<double>(i));
    }

    std::cout << median(ints) << '\n';
    std::cout << median(doubles) << '\n';

    return 0;
}
Last edited on
Your task fully
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
#include <numeric>
#include <iostream>
#include <vector>
#include <list>
#include <fstream>
#include <algorithm>

template <typename ContainerT>
void readFromFile(const char* fileName, ContainerT& destination)
{
    std::ifstream fs(fileName);

    if (!fs.is_open())return;

    while(!fs.eof())
    {
        typename ContainerT::value_type shouldBeAdded;
        fs >> shouldBeAdded;
        destination.push_back(shouldBeAdded);
    }

}

template <typename ContainerT>
void printContainer(const ContainerT& cont)
{
    std::copy(cont.begin(), cont.end(), std::ostream_iterator<typename ContainerT::value_type>(std::cout, " "));
}

template <typename ContainerT>
double median(const ContainerT& container)
{
    typedef typename ContainerT::value_type ElemT;
    ElemT summa = std::accumulate(container.begin(), container.end(),  ElemT(0));
    return container.empty() ? 0.0 : (static_cast<double>(summa) / static_cast<double>(container.size()));
}

int main(int argc, char* argv[])
{
    if(!argv[1])
    {
        std::cout << "You did not specify file\n";
        return 0;
    }
    std::vector<int> ints;
    //read file, fit the container and show content of container
    readFromFile(argv[1], ints);
    std::cout << "integer values from file:\n";
    printContainer(ints);

    //sort container and show content again
    std::sort(ints.begin(), ints.end());
    std::cout << "\nsorted integer values from file:\n";
    printContainer(ints);

    //show avarage values of file content
    std::cout << "\nAvarage value from file:\n";
    std::cout << median(ints) << '\n';

    return 0;
}
I've updated reading file function, so if file has double values you can write it to int container

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
#include <numeric>
#include <iostream>
#include <vector>
#include <fstream>
#include <algorithm>
#include <string>
#include <sstream>

template <typename ContainerT> void readFromFile(const char* fileName, ContainerT& destination)
{
    std::ifstream fs(fileName);
    while(!fs.eof())
    {
        typename ContainerT::value_type shouldBeAdded;
        std::string str;
        fs >> str;
        std::stringstream ss(str);
        ss >> shouldBeAdded;
        destination.push_back(shouldBeAdded);
    }

}
template <typename ContainerT> void printContainer(const ContainerT& cont)
{
    std::copy(cont.begin(), cont.end(), std::ostream_iterator<typename ContainerT::value_type>(std::cout, " "));
}
template <typename ContainerT> typename ContainerT::value_type median(const ContainerT& container)
{
    typedef typename ContainerT::value_type ElemT;
    ElemT summa = std::accumulate(container.begin(), container.end(),  ElemT(0));
    return container.empty() ? ElemT(0) : (summa / static_cast<ElemT>(container.size()));
}
int main(int argc, char* argv[])
{
    if(!argv[1])
    {
        std::cout << "You did not specify file\n";
        return 0;
    }
    std::vector<int> content;
    //read file, fit the container and show content of container
    readFromFile(argv[1], content);
    std::cout << "values from file:\n";
    printContainer(content);

    //sort container and show content again
    std::sort(content.begin(), content.end());
    std::cout << "\nsorted values from file:\n";
    printContainer(content);

    //show avarage values of file content
    std::cout << "\nAvarage value from file:\n";
    std::cout << median(content) << '\n';

    return 0;
}
Pages: 12345