### 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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191`` ``````#include #include using namespace std; void enter(vector&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&avr)//finds the average { vectornumero(100); int counter=0, average=0, avgsum=0; for(int i=0; i&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 max) max=mum[m]; } cout<&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 max) max=mum[m]; } int min = nog[0]; // start with first stored num for(int l = nog.size(); l&mo) { int s, nu=0; int modenum, oldnum, cnum = -1, precount, count = 0; for(int t = 0; t count) { count = precount; modenum = cnum; } } cout<&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=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; ibegin(); 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:

 ``123456789101112131415161718192021222324252627`` ``````void enter(vector&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.