Please help me with following problem

I have been trying to do this practical, but the problem is I don't understand what/how am I going to programme this scenario, I am new in C++ programming language. If anyone can explain to me the following problem I would really appreciate it. Thank you in advance, Here is the case study:

This practical will focus on the use of vectors. Create a menu-driven, library-based system which performs the following operations:
• All functions apart from main are stored in a programmer defined library called libPrac6 with corresponding interface and implementation files.
• All functions must reside in the VectorSpace namespace.
• Generates a vector of random integers using user-provided values for the maximum, minimum, and number of entries.
• Prints the contents of the vector to the standard output.
• Empties the contents of the vector .
• Calculates and displays the arithmetic mean .
• Calculates and displays the median value (the built-in sort function may be used for this)
• Calculates and displays the modal values (most frequently occurring values).
• Must make use of both pass by value and pass by reference functions
Start by creating the menu and doing just the first two functions - generate a vector of random numbers and print it out.

Once you have that working, add the other functions one at a time.

You can search this site for lots of examples of menu driven programs, vectors, random numbers, arithmetic mean and mode. There are fewer examples of using namespaces. Libraries are implementation dependent so you'll need to tell us about what computer you're writing on and what development environment you're using.
Thank you so much for your response Dhayden, it really helped me, well I am using codeblocks as my development environment and the computer I am using is Dell Inspiron i7 8th Generation.

I've managed to write the code for the aforementioned problem, I will post my work here so that if you have time you can just show me if I've done the right thing, once again thanks for your time.

#ifndef LIBPRAC6_H_INCLUDED
#define LIBPRAC6_H_INCLUDED

#include <ctime>
#include <cstdlib>
#include <iostream>
#include <vector>

const int SUCCESS = 0;

using namespace std;

namespace VectorSpace
{

int GenRandom(int,int);

int GetInt();

vector<int> GenVec(int,int,int);

void PrintVector(vector<int>);

vector<int> GenArithmeticMean(const vector<int>&vecNums);

vector<int> GenMedianValue(const vector<int>&vecNums);

vector<int> GenModalValues(const vector<int>&vecNums);


}




#endif // LIBPRAC6_H_INCLUDED

Part 2

#include "libPrac6.h"

namespace VectorSpace
{

int GenRandom(int intLower,int intUpper)
{
int intNum = 0;
int intRange = intUpper - intLower + 1;
intNum = rand()%intRange + intLower;
return intNum;
}

vector<int> GenVec(int intCount,int intLower,int intUpper)
{
vector<int> vecNums;
for(int n=0;n<intCount;++n)
{
vecNums.push_back(GenRandom(intLower,intUpper));
}
return vecNums;
}

void PrintVector(vector<int> vecNums)
{
for(int n:vecNums)
{
cout << n << " ";
}
cout << endl;
}


vector<int> GenArithmeticMean(const vector<int> &vecNums)
{
return vecArithmeticMean;
}

vector<int> GenMeianValue(const vector<int> &vecNums)
{
return vecMedianValue;
}


vector<int> GenModalValues(const vector<int> &vecNums)
{
return vecModalValues;
}


}


This is an excellent start. A few comments.

Never put using namespace xyz; in a header file. It causes that "using" functionality to spill over into any other header files that you may included after this one. The results can be really hard to debug. Instead, use the fully qualified names in headers and put the using namespace xyz; in the cpp file. Better still, use using std::vector; etc. so you're making visible only the symbols that you require.

Putting it all together (and into one file for my testing):
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
#ifndef LIBPRAC6_H_INCLUDED
#define LIBPRAC6_H_INCLUDED

#include <ctime>
#include <cstdlib>
#include <iostream>
#include <vector>

const int SUCCESS = 0;

namespace VectorSpace
{

    int GenRandom(int, int);
    int GetInt();
    std::vector < int >GenVec(int, int, int);

    void PrintVector(std::vector < int >);
    std::vector < int >GenArithmeticMean(const std::vector < int >&vecNums);
    std::vector < int >GenMedianValue(const std::vector < int >&vecNums);
    std::vector < int >GenModalValues(const std::vector < int >&vecNums);
}

#endif				// LIBPRAC6_H_INCLUDED

// #include "libPrac6.h"

using std::vector;
using std::cout;
using std::endl;

namespace VectorSpace
{

    int GenRandom(int intLower, int intUpper)
    {
	int intNum = 0;
	int intRange = intUpper - intLower + 1;
	intNum = rand() % intRange + intLower;
	return intNum;
    }

    vector < int >GenVec(int intCount, int intLower, int intUpper)
    {
	vector < int >vecNums;
	for (int n = 0; n < intCount; ++n) {
	    vecNums.push_back(GenRandom(intLower, intUpper));
	}
	return vecNums;
    }

    void PrintVector(vector < int >vecNums)
    {
	for (int n:vecNums) {
	    cout << n << " ";
	}
	cout << endl;
    }

    vector < int >GenArithmeticMean(const vector < int >&vecNums)
    {
	vector < int >result;
	cout << "GenArithmenticMean not yet implemented\n";
	return result;
    }

    vector < int >GenMedianValue(const vector < int >&vecNums)
    {
	vector < int >result;
	cout << "GenMedianValue not yet implemented\n";
	return result;
    }

    vector < int >GenModalValues(const vector < int >&vecNums)
    {
	vector < int >result;
	cout << "GenModalValues not yet implemented\n";
	return result;
    }
}

how do i calculate the mean, median and modal values?
mean is the average ... sum of everything divided by the number of things summed.
median is the sorted list's center element. ( list[sizeoflist/2])
mode is the value that appears the most time. you just have to count them after sorting. you can do that while you sum them for the average, to save a loop.

you can look this stuff up online for in depth answers ... there may be conventional rules on what choices to make for odd sized list on median or which of several equally frequent items to pick for mode, I don't recall the edge case rules.
Last edited on
... rules on what choices to make for odd sized list on median ...

Not to be too picky, but it's the even-sized lists that need rules about median values. The odd-sized lists have a "center element".

Now, back to your regularly scheduled program...
doh.
Topic archived. No new replies allowed.