Thread: help with 1 function

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    20

    help with 1 function

    Hello. I am having trouble with this program that I wrote. Just one function is giving me a problem, the error that I get is:binary '=' : no operator defined which takes a right-hand operand, you get the idea. Code posted below. The fuction that is giving me problems is FindLargest, but as you can see I set IndexOfLargest = (FindLargest, IntVec) See Below.
    Thanks
    Code:
    #include <iostream>
    #include <iomanip>
    #include <cstdlib>
    #include <ctime>
    #include <vector>
    
    using namespace std;
    
    double CreateVec(vector <int> &);
    void PrintVec(vector <int>);
    int FindLargest(vector <int>, int);
    double Average (vector <int>);
    vector <int> Exchange(vector <int>, const int);
    void FillAryVectors(vector <double> [], double);
    void PrintAryVectors(vector <double> []);
    
    int main (void)
    {
    	double Avg;
    	int IndexOfLargest;
    	vector <int> IntVec (100);
    	vector <double> DbArrayVec [10];
    
    	srand(time(NULL));
    
    	Avg = CreateVec (IntVec);
    
    	PrintVec (IntVec);
    
    	cout << "The average is " << setiosflags(ios::fixed) << setprecision(2) << 
    		Average (IntVec) << endl << endl;
    
    	IndexOfLargest = (FindLargest, IntVec);
    
    	Exchange (IntVec, IndexOfLargest);
    
    	FillAryVectors (DbArrayVec, 0.5);
    
    	PrintAryVectors (DbArrayVec);
    
    	return 0;
    }
    
    double CreateVec(vector <int> & ivec)
    {
    	int total = 0;
    	int size;
    
    	vector <int> MyVec (100);
    
    	if (ivec.size() != 0)
    	{
    		MyVec = ivec;
    		size = ivec.size();
    	}
    	else
    		size = 100;
    
    	for (int i = 0; i < size; i++)
    	{
    		MyVec[i] = rand() & 199 - 99;
    		total += MyVec[i];
    	}
    	ivec = MyVec;
    
    	return (double(total)) / ivec.size();
    }
    
    void PrintVec (vector <int> ivec)
    {
    	for (int index = 0; index < ivec.size(); index++)
    	{
    		cout << setw(5) << ivec[index];
    
    		if (index % 10 == 9)
    			cout << endl;
    	}
    
    	cout << endl;
    }
    
    void FillAryVectors(vector <double> aryv[10], double value)
    {
    	vector <double> TempVec (10, value);
    
    	for (int i = 0; i < 10; i++)
    		aryv[i] = TempVec;
    }
    
    void PrintAryVectors (vector <double> aryv[10])
    {
    	for (int row = 0; row < 10; row++)
    	{
    		for (int col = 0; col < 10; col++)
    			cout << setiosflags(ios::fixed) << setprecision(2) << setw(5) << aryv[row][col];
    		cout << endl;
    	}
    }
    
    double Average (vector <int> ivec)
    {
    	int sum = 0;
    	int size = ivec.size();
    
    	for (int index = 0; index < size; index++)
    		sum += ivec [index];
    
    	return (double (sum) / size);
    }
    int FindLargest(vector <int> ivec, int sizeX)
    {
    	int counter;
    	int maxIndex = 0;
    
    	for (counter = 1; counter < sizeX; counter++)
    		if (ivec[maxIndex] < ivec[counter])
    			maxIndex = counter;
    		
    		return maxIndex;
    }	
    vector <int> Exchange(vector <int> ivec, const int cnt)
    {
    	for (int i = 0; i < cnt; i++)
    		ivec [i] *= 2;
    
    	return (ivec);
    }
    Sorry about the length of it.

  2. #2
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    made IndexOfLargest = (FindLargest, IntVec);

    IndexOfLargest = (FindLargest, 100);

    100 is the size it was declared too, hope it fixes it, no errors anyway.

    Code:
    #include <iostream>
    #include <iomanip>
    #include <cstdlib>
    #include <ctime>
    #include <vector>
    
    using namespace std;
    
    double CreateVec(vector <int> &);
    void PrintVec(vector <int> );
    int FindLargest(vector <int>, int);
    double Average (vector <int> );
    vector <int> Exchange(vector <int>, const int);
    void FillAryVectors(vector <double> [], double);
    void PrintAryVectors(vector <double> []);
    
    int main (void)
    {
    	double Avg;
    	int IndexOfLargest;
    	vector <int> IntVec (100);
    	vector <double> DbArrayVec [10];
    
    	srand(time(NULL));
    
    	Avg = CreateVec (IntVec);
    
    	PrintVec (IntVec);
    
    	cout << "The average is " << setiosflags(ios::fixed) << setprecision(2) << 
    		Average (IntVec) << endl << endl;
    
    	IndexOfLargest = (FindLargest, 100);
    
    	Exchange (IntVec, IndexOfLargest);
    
    	FillAryVectors (DbArrayVec, 0.5);
    
    	PrintAryVectors (DbArrayVec);
    
    	return 0;
    }
    
    double CreateVec(vector <int> & ivec)
    {
    	int total = 0;
    	int size;
    
    	vector <int> MyVec (100);
    
    	if (ivec.size() != 0)
    	{
    		MyVec = ivec;
    		size = ivec.size();
    	}
    	else
    		size = 100;
    
    	for (int i = 0; i < size; i++)
    	{
    		MyVec[i] = rand() & 199 - 99;
    		total += MyVec[i];
    	}
    	ivec = MyVec;
    
    	return (double(total)) / ivec.size();
    }
    
    void PrintVec (vector <int> ivec)
    {
    	for (int index = 0; index < ivec.size(); index++)
    	{
    		cout << setw(5) << ivec[index];
    
    		if (index % 10 == 9)
    			cout << endl;
    	}
    
    	cout << endl;
    }
    
    void FillAryVectors(vector <double> aryv[10], double value)
    {
    	vector <double> TempVec (10, value);
    
    	for (int i = 0; i < 10; i++)
    		aryv[i] = TempVec;
    }
    
    void PrintAryVectors (vector <double> aryv[10])
    {
    	for (int row = 0; row < 10; row++)
    	{
    		for (int col = 0; col < 10; col++)
    			cout << setiosflags(ios::fixed) << setprecision(2) << setw(5) << aryv[row][col];
    		cout << endl;
    	}
    }
    
    double Average (vector <int> ivec)
    {
    	int sum = 0;
    	int size = ivec.size();
    
    	for (int index = 0; index < size; index++)
    		sum += ivec [index];
    
    	return (double (sum) / size);
    }
    int FindLargest(vector <int> ivec, int sizeX)
    {
    	int counter;
    	int maxIndex = 0;
    
    	for (counter = 1; counter < sizeX; counter++)
    		if (ivec[maxIndex] < ivec[counter])
    			maxIndex = counter;
    		
    		return maxIndex;
    }	
    vector <int> Exchange(vector <int> ivec, const int cnt)
    {
    	for (int i = 0; i < cnt; i++)
    		ivec [i] *= 2;
    
    	return (ivec);
    }
    Last edited by RoD; 02-18-2003 at 05:26 PM.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    20
    Ok, I fixed that. Now when I run the program it prints out a 10x10 vector, then tells me the average. but after the average it has 0.50 in a 10x 10 vector I need the floating point value to initialize the elements which should be passed as a parameter, any ideas on how to do this? And also my FindLargest function isn't working. Or at least it isn't printing out the largest element in the integer vector like I intended to. Thanks for the help.
    K

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM