Thread: Help with sending a vector to a function

  1. #1
    Registered User boojus's Avatar
    Join Date
    Oct 2003
    Posts
    9

    Help with sending a vector to a function

    my magic square program is almost working i think. I need help with sending the current vector to my bool type function "isMagicSquare(a[])" that decides whether it is a magic square or not.

    Code:
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    
    bool isMagicSquare(unsigned int a[]);
    
    int main()
    {
    
    	char input[10];
    	unsigned int numInput, i;
    	vector<int> a;
    
    	cout << "Enter a number ranging from 9-30: ";
    	cin.getline(input, 10);
    	numInput = atoi(input);
    	while(numInput < 9 || numInput >30)
    	{
    		cout << "Enter a number ranging from 9-30: ";
    		cin.getline(input,10);
    		numInput = atoi(input);
    	}
    
    	for(i=1; i<=numInput ;i++)
    		a.push_back(i);
    	
    	while(next_permutation(a.begin(), a.end()))
    	{
    		if(isMagicSquare(a[]))
    		{
    			for(i=0; i<9;i++)
    			cout << a[i] << " ";
    			//if(i/9==1)
    			//	cout<<endl;
    		}
    	}
    
    return 0;
    }
    
    bool isMagicSquare(unsigned int a[])
    {
    	bool torf=false;
        	
    	if(    a[0]+a[1]+a[2] == a[3]+a[4]+a[5] 
    		&& a[3]+a[4]+a[5] == a[6]+a[7]+a[8]
    		&& a[0]+a[3]+a[6] == a[1]+a[4]+a[7] 
    		&& a[1]+a[4]+a[7] == a[2]+a[5]+a[8]
    		&& a[0]+a[4]+a[8] == a[2]+a[4]+a[6]
    		&& a[0]!=a[1] && a[0]!=a[2]
    		&& a[0]!=a[3] && a[0]!=a[4]
    		&& a[0]!=a[5] && a[0]!=a[6]
    		&& a[0]!=a[7] && a[0]!=a[8]
    		&& a[1]!=a[2] && a[1]!=a[3]
    		&& a[1]!=a[4] && a[1]!=a[5]
    		&& a[1]!=a[6] && a[1]!=a[7]
    		&& a[1]!=a[8] && a[2]!=a[3]
    		&& a[2]!=a[4] && a[2]!=a[5]
    		&& a[2]!=a[6] && a[2]!=a[7]
    		&& a[2]!=a[8] && a[3]!=a[4]
    		&& a[3]!=a[5] && a[3]!=a[6]
    		&& a[3]!=a[7] && a[3]!=a[8]
    		&& a[4]!=a[5] && a[4]!=a[6]
    		&& a[4]!=a[7] && a[4]!=a[8]
    		&& a[5]!=a[6] && a[5]!=a[7]
    		&& a[5]!=a[8] && a[6]!=a[7]
    		&& a[6]!=a[8] && a[7]!=a[8])
    			torf=true;
    
    	return torf;
    }
    Last edited by boojus; 11-21-2003 at 10:22 PM.

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    Code:
    bool isMagic(const std::vector<int> &a) { ...

  3. #3
    Registered User boojus's Avatar
    Join Date
    Oct 2003
    Posts
    9
    what is the format for this part:
    Code:
    if(isMagicSquare(?)){...}

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Code:
    a
    gg

  5. #5
    Registered User boojus's Avatar
    Join Date
    Oct 2003
    Posts
    9
    oh, i had the wrong code in the declaration of my function. im awesome. thanks for helping me. it works now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Sending an Array to a function
    By bizounce in forum C Programming
    Replies: 2
    Last Post: 04-08-2003, 05:40 PM
  5. sending member function to function
    By Joe Monti in forum C++ Programming
    Replies: 6
    Last Post: 04-01-2003, 09:42 PM