Function Call of Vectors

I know this code is longer than usual but I need help going through my function to trace my code andHow do i get the function established in enter to be placed in the other functions so that they are not empty? Everything is greatly appreciated.

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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <iostream>
#include <vector>
using namespace std;

void enter(vector<int>&num)//takes in the empty vector from main and has the user fill it
{
	
	int ber;
    const int MAX = 20;//const variable 
    for (int i = 0; i < MAX;i++) {
		num.push_back(ber);
		cout<<"enter numbers one at a time: (-1 to finish)";
		int input; // if statement combined with break to exit loop early when -1 is entered
		cin>>input;  
		if (input == -1)
			break;       
	}
}
void avg(vector<int>&avr)//finds the average 
{
	vector<int>numero(100);
	int counter=0, average=0, avgsum=0;
	for(int i=0; i<counter; i++)
        {
               int avgsum=avgsum+numero[i];
        }
        average=(avgsum/counter);
	
}
void maxi(vector<int>&mum)//finds the maximum number from the set
{  
    int size = mum.size(); // establish size of the vector
    int max = mum[0];       // start with first stored number
	for(int m = 1; m<size; m++)//compares the values to find the highest number
     {
          if(mum[m] > max)
                max=mum[m];
     }
     cout<<max; // return highest value
}
void range(vector<int>&nog)
{
	int ran=0;
    int size = mum.size(); // establish size of the vector
    int max = mum[0];       // start with first stored number
	for(int m = 1; m<size; m++)//compares the values to find the highest number
     {
          if(mum[m] > max)
                max=mum[m];
     }
	int min = nog[0];  // start with first stored num
     for(int l = nog.size(); l<nog.size(); l--)//goes down through the numbers 
     {
          if(nog[l] < max)
                min=nog[l];
	 }
	 cout<<max-min;//subtracts the maximum from the minimum to get the range
}
void mode(vector<int>&mo)
{
	int s, nu=0;
	int modenum, oldnum, cnum = -1, precount, count = 0;
	for(int t = 0; t<mo; t++)//t counts how many numbers are in mo
	{	}
	
	for(int e = 0; e < t; e++)//while the size is less than the counter
	{
		precount = 0;
		oldnum = (mo + e);
		for (int n = 0; n < t; n++)//
		{
			if(cnum == oldnum)
				oldnum = (mo + (e++));
		}

		for(int i = 0; i < t; i++)
		{
			if (oldnum == mo[i])
			{
				precount++;
			}
		}
		cnum = oldnum;
		if (precount > count)
		{
			count = precount;
			modenum = cnum;
		}
	}
	cout<<modenum;
}
	
void bar(vector<int>&num)
{
	 int one=0, two=0, three=0, four=0, five=0, six=0, seven=0, eight=0, nine=0, ten=0, number=0, counter=0;
	 for(int i=0; i<counter; i++)//goes through the shelves of the vectorto check them
        {
                if(num[i]>=1&&num[i]<=5)//goes through each number and stores in 1-10 which bracket they fall under 
                        one++;
                else if(num[i]>=6&&num[i]<=10)//goes through each number and stores in 1-10 which bracket they fall under 
                        two++;
                else if(num[i]>=11&&num[i]<=15)//goes through each number and stores in 1-10 which bracket they fall under 
                        three++;
                else if(num[i]>=16&&num[i]<=20)//goes through each number and stores in 1-10 which bracket they fall under 
                        four++;
                else if(num[i]>=21&&num[i]<=25)//goes through each number and stores in 1-10 which bracket they fall under 
                        five++;
                else if(num[i]>=26&&num[i]<=30)//goes through each number and stores in 1-10 which bracket they fall under 
                        six++;
                else if(num[i]>=31&&num[i]<=35)//goes through each number and stores in 1-10 which bracket they fall under 
                        seven++;
                else if(num[i]>=36&&num[i]<=40)//goes through each number and stores in 1-10 which bracket they fall under 
                        eight++;
                else if(num[i]>=41&&num[i]<=45)//goes through each number and stores in 1-10 which bracket they fall under 
                        nine++;
                else
                        ten++;//goes through each number and stores in 1-10 which bracket they fall under 
        }

	    cout<<"1-5: ";
        for(int i=0; i<one; i++)//it displays the number of astrisks for each set of numbers
        {
                cout<<'*';
        }
        cout<<endl<<"6-10: ";
        for(int i=0; i<two; i++)//it displays the number of astrisks for each set of numbers
        {
                cout<<'*';
        }

        cout<<endl<<"11-15: ";
        for(int i=0; i<three; i++)//it displays the number of astrisks for each set of numbers
        {
                cout<<'*';
        }

        cout<<endl<<"16-20: ";
        for(int i=0; i<four; i++)//it displays the number of astrisks for each set of numbers
        {
                cout<<'*';
        }

        cout<<endl<<"21-25: ";
        for(int i=0; i<five; i++)//it displays the number of astrisks for each set of numbers
        {
                cout<<'*';
        }

        cout<<endl<<"26-30: ";
        for(int i=0; i<six; i++)//it displays the number of astrisks for each set of numbers
        {
                cout<<'*';
        }

        cout<<endl<<"31-35: ";
        for(int i=0; i<seven; i++)//it displays the number of astrisks for each set of numbers
        {
                cout<<'*';
        }

        cout<<endl<<"36-40: ";
        for(int i=0; i<eight; i++)//it displays the number of astrisks for each set of numbers
        {
                cout<<'*';
        }

        cout<<endl<<"41-45: ";
        for(int i=0; i<nine; i++)//it displays the number of astrisks for each set of numbers
        {
                cout<<'*';
        }

        cout<<endl<<"46-50: ";//it displays the number of astrisks for each set of numbers
        for(int i=0; i<ten; i++)
        {
                cout<<'*';
        }
}

int main() 
{
	vector<int>begin();
	enter(begin());//passes an empty vector to enter() so it can be filled
	avg(begin);
	maxi(begin);
	range(begin);
	mode(begin);
	bar(begin);
	system("pause");
	return(0);
}
Last edited on
pass my functions back and forth through each other

This is terrible practice. Leads to insane code that is hard to follow.
if(num[i]>=1&&num[i]<=5)

All must become pointers


Most of the issues with my code are currently these please explain what they are and how I can fix them


 
for(t = 0; t < mo.size(); t++) 

must have a class

Last edited on
I'm sorry but I don't get what you're asking right there.
there is a line under num and other variables where i use vector/arrays it's is telling me there are improper conversions between the functions or expression must be a pointer to a complete object type.
Could you just give us a copy of the error messages?
------ Build started: Project: Ch 8 Ex 4, Configuration: Debug Win32 ------
Ch8 ex4.cpp
c:\users\gijessiebear\documents\visual studio 2010\projects\ch 8 ex 4\ch 8 ex 4\ch8 ex4.cpp(11): error C2064: term does not evaluate to a function taking 1 arguments
c:\users\gijessiebear\documents\visual studio 2010\projects\ch 8 ex 4\ch 8 ex 4\ch8 ex4.cpp(25): error C2660: 'enter' : function does not take 0 arguments
c:\users\gijessiebear\documents\visual studio 2010\projects\ch 8 ex 4\ch 8 ex 4\ch8 ex4.cpp(27): error C2228: left of '.size' must have class/struct/union
type is 'std::vector<_Ty> &(__cdecl *)(void)'
with
[
_Ty=int
]
c:\users\gijessiebear\documents\visual studio 2010\projects\ch 8 ex 4\ch 8 ex 4\ch8 ex4.cpp(30): error C2109: subscript requires array or pointer type
c:\users\gijessiebear\documents\visual studio 2010\projects\ch 8 ex 4\ch 8 ex 4\ch8 ex4.cpp(36): error C2660: 'enter' : function does not take 0 arguments
c:\users\gijessiebear\documents\visual studio 2010\projects\ch 8 ex 4\ch 8 ex 4\ch8 ex4.cpp(38): error C2228: left of '.size' must have class/struct/union
c:\users\gijessiebear\documents\visual studio 2010\projects\ch 8 ex 4\ch 8 ex 4\ch8 ex4.cpp(39): error C2109: subscript requires array or pointer type
c:\users\gijessiebear\documents\visual studio 2010\projects\ch 8 ex 4\ch 8 ex 4\ch8 ex4.cpp(43): error C2109: subscript requires array or pointer type
c:\users\gijessiebear\documents\visual studio 2010\projects\ch 8 ex 4\ch 8 ex 4\ch8 ex4.cpp(44): error C2109: subscript requires array or pointer type
c:\users\gijessiebear\documents\visual studio 2010\projects\ch 8 ex 4\ch 8 ex 4\ch8 ex4.cpp(50): error C2660: 'maxi' : function does not take 0 arguments
c:\users\gijessiebear\documents\visual studio 2010\projects\ch 8 ex 4\ch 8 ex 4\ch8 ex4.cpp(51): error C2109: subscript requires array or pointer type
c:\users\gijessiebear\documents\visual studio 2010\projects\ch 8 ex 4\ch 8 ex 4\ch8 ex4.cpp(53): error C2228: left of '.size' must have class/struct/union
type is 'std::vector<_Ty> &(__cdecl *)(void)'
with
[
_Ty=int
]
c:\users\gijessiebear\documents\visual studio 2010\projects\ch 8 ex 4\ch 8 ex 4\ch8 ex4.cpp(53): error C2228: left of '.size' must have class/struct/union
type is 'std::vector<_Ty> &(__cdecl *)(void)'
with
[
_Ty=int
]
c:\users\gijessiebear\documents\visual studio 2010\projects\ch 8 ex 4\ch 8 ex 4\ch8 ex4.cpp(55): error C2109: subscript requires array or pointer type
c:\users\gijessiebear\documents\visual studio 2010\projects\ch 8 ex 4\ch 8 ex 4\ch8 ex4.cpp(56): error C2109: subscript requires array or pointer type
c:\users\gijessiebear\documents\visual studio 2010\projects\ch 8 ex 4\ch 8 ex 4\ch8 ex4.cpp(58): error C2065: 'ran' : undeclared identifier
c:\users\gijessiebear\documents\visual studio 2010\projects\ch 8 ex 4\ch 8 ex 4\ch8 ex4.cpp(58): error C2563: mismatch in formal parameter list
c:\users\gijessiebear\documents\visual studio 2010\projects\ch 8 ex 4\ch 8 ex 4\ch8 ex4.cpp(59): error C2065: 'ran' : undeclared identifier
c:\users\gijessiebear\documents\visual studio 2010\projects\ch 8 ex 4\ch 8 ex 4\ch8 ex4.cpp(66): error C2228: left of '.size' must have class/struct/union
type is 'std::vector<_Ty> &(__cdecl *)(void)'
with
[
_Ty=int
]
c:\users\gijessiebear\documents\visual studio 2010\projects\ch 8 ex 4\ch 8 ex 4\ch8 ex4.cpp(68): error C2228: left of '.size' must have class/struct/union
type is 'std::vector<_Ty> &(__cdecl *)(void)'
with
[
_Ty=int
]
c:\users\gijessiebear\documents\visual studio 2010\projects\ch 8 ex 4\ch 8 ex 4\ch8 ex4.cpp(70): error C2109: subscript requires array or pointer type
c:\users\gijessiebear\documents\visual studio 2010\projects\ch 8 ex 4\ch 8 ex 4\ch8 ex4.cpp(70): error C2109: subscript requires array or pointer type
c:\users\gijessiebear\documents\visual studio 2010\projects\ch 8 ex 4\ch 8 ex 4\ch8 ex4.cpp(73): error C2109: subscript requires array or pointer type
c:\users\gijessiebear\documents\visual studio 2010\projects\ch 8 ex 4\ch 8 ex 4\ch8 ex4.cpp(74): error C2109: subscript requires array or pointer type
c:\users\gijessiebear\documents\visual studio 2010\projects\ch 8 ex 4\ch 8 ex 4\ch8 ex4.cpp(74): error C2109: subscript requires array or pointer type
c:\users\gijessiebear\documents\visual studio 2010\projects\ch 8 ex 4\ch 8 ex 4\ch8 ex4.cpp(75): error C2109: subscript requires array or pointer type
c:\users\gijessiebear\documents\visual studio 2010\projects\ch 8 ex 4\ch 8 ex 4\ch8 ex4.cpp(86): error C2109: subscript requires array or pointer type
c:\users\gijessiebear\documents\visual studio 2010\projects\ch 8 ex 4\ch 8 ex 4\ch8 ex4.cpp(86): error C2109: subscript requires array or pointer type
c:\users\gijessiebear\documents\visual studio 2010\projects\ch 8 ex 4\ch 8 ex 4\ch8 ex4.cpp(88): error C2109: subscript requires array or pointer type
c:\users\gijessiebear\documents\visual studio 2010\projects\ch 8 ex 4\ch 8 ex 4\ch8 ex4.cpp(88): error C2109: subscript requires array or pointer type
c:\users\gijessiebear\documents\visual studio 2010\projects\ch 8 ex 4\ch 8 ex 4\ch8 ex4.cpp(90): error C2109: subscript requires array or pointer type
c:\users\gijessiebear\documents\visual studio 2010\projects\ch 8 ex 4\ch 8 ex 4\ch8 ex4.cpp(90): error C2109: subscript requires array or pointer type
c:\users\gijessiebear\documents\visual studio 2010\projects\ch 8 ex 4\ch 8 ex 4\ch8 ex4.cpp(92): error C2109: subscript requires array or pointer type
c:\users\gijessiebear\documents\visual studio 2010\projects\ch 8 ex 4\ch 8 ex 4\ch8 ex4.cpp(92): error C2109: subscript requires array or pointer type
c:\users\gijessiebear\documents\visual studio 2010\projects\ch 8 ex 4\ch 8 ex 4\ch8 ex4.cpp(94): error C2109: subscript requires array or pointer type
c:\users\gijessiebear\documents\visual studio 2010\projects\ch 8 ex 4\ch 8 ex 4\ch8 ex4.cpp(94): error C2109: subscript requires array or pointer type
c:\users\gijessiebear\documents\visual studio 2010\projects\ch 8 ex 4\ch 8 ex 4\ch8 ex4.cpp(96): error C2109: subscript requires array or pointer type
c:\users\gijessiebear\documents\visual studio 2010\projects\ch 8 ex 4\ch 8 ex 4\ch8 ex4.cpp(96): error C2109: subscript requires array or pointer type
c:\users\gijessiebear\documents\visual studio 2010\projects\ch 8 ex 4\ch 8 ex 4\ch8 ex4.cpp(98): error C2109: subscript requires array or pointer type
c:\users\gijessiebear\documents\visual studio 2010\projects\ch 8 ex 4\ch 8 ex 4\ch8 ex4.cpp(98): error C2109: subscript requires array or pointer type
c:\users\gijessiebear\documents\visual studio 2010\projects\ch 8 ex 4\ch 8 ex 4\ch8 ex4.cpp(100): error C2109: subscript requires array or pointer type
c:\users\gijessiebear\documents\visual studio 2010\projects\ch 8 ex 4\ch 8 ex 4\ch8 ex4.cpp(100): error C2109: subscript requires array or pointer type
c:\users\gijessiebear\documents\visual studio 2010\projects\ch 8 ex 4\ch 8 ex 4\ch8 ex4.cpp(102): error C2109: subscript requires array or pointer type
c:\users\gijessiebear\documents\visual studio 2010\projects\ch 8 ex 4\ch 8 ex 4\ch8 ex4.cpp(102): error C2109: subscript requires array or pointer type
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Line 8: num is a vector, but you're calling it like it was some kind of function. Perhaps you meant num.push_back(0) by your comment?

Line 22: You are calling enter() with no arguments, but that function needs a vector as an argument.

There are a lot of problems here. Have you tried simply looking over your code and making sure what you are doing with variables and functions is actually even legal? i.e. on line 8 that calling of a vector like a function makes me think you don't know how they work.
How do i get the function established in enter to be placed in the other functions so that they are not empty?
EDIT: There were several posts made while I was replying.

The thing that stands out the most is your bar function.

Instead of having 10 variables - have an array of 10 elements. Lines 105 - 162 could be handled with nested for loops instead

In lines 82 to 102 there is a pattern in the way the numbers progress - you could use a variable to handle this better along with nested for loops.

On line 65, variable s is uninitialised.

On lines 63 & 65, it would be better to calc mo.size once before the for loops, rather than having a function call every time both loops end conditions are evaluated.

How does this function calculate the mode? It is doing some swapping, but returns the last number that was swapped - how is that related to being the mode of the set? I would have expected some counting to be happening.

How does the avg function calculate the average? I don't see any division - that will be a problem with an int return type.

On line 47, the value returned from the function call is not assigned to anything.

With this:

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
void enter(vector<int>&num)//takes in the empty vector from main and has the user fill it
{
	int ber=0;  //not needed as long as values are pushed into vector
        //following line is cute but pointless
	num(ber); //places the value 0 in vector num
	cout<<"enter numbers one at a time: ( 0 to begin -1 to finish)";
	cin>>ber;  //remove this
       
         //don't use magic number like 50, use a const variable instead
        const int MAX = 50;
        for (int i = 0; i < MAX;i++) {
             //see comment below for a better way
	//for (int i=1; ber<=50 && ber>=0; i++) //allows user to enter numbers from 0 to 50
	//{
		cout<<"enter numbers one at a time: (-1 to finish)" << endl;
       // use an if statement combined with break to exit loop early when -1 is entered
                int Input;
		cin>>Input;  
                if (Input == -1)
                       break;
        //need to put  Input into the array use push_back
                 num.push_back(Input)
       //resizing not need, setting the size less than how many number you have is ridiculous
		num.resize(i-1); //resizes function so the numbers are stored in different shelves
		
	}
}


I hope all this helps :)
Last edited on
yes thanks that helped a bit i'm having some trouble my first function is passed into the others through the main right?
..... my first function is passed into the others through the main right?


I think you mean a reference to the vector object is sent to other functions as an argument in function calls.

Have a go at fixing up the various things & post your code again.
One other main idea - declare your functions before main and put their definitions (hopefully in the order they are called ) after main. This way we don't have to scroll all the way down to see what main does.

I just saw some of the changes you have made:

Delete lines 8 & 11. Put num.push_back(input) after the break as I had in my example.

With your avg function - it is void and there is no reference to the avg variable so nothing is changed. The avg variable is an int so that won't work as integer division truncates. Either have the function return a double and assign this to something in main, or send the function a reference to an average variable that is declared in main, and assigned to in the function.

In the mode function, line 63 doesn't do what you think. Read this:

http://www.cplusplus.com/reference/vector/vector/size/


Also the mode function just prints the answer, it is not returned or assigned to a variable in the main scope, making it rather useless.

Are you having a go at the ideas I mentioned with your bar function?
Last edited on
Topic archived. No new replies allowed.