cant use sqrt(x) function !!! please help me

Apr 26, 2008 at 2:44pm
Hello, I'm getting nervous at my school assignment. Can you please check and find what errors I'm supposed to fix ? Please, hope you'd not mind giving your precious time to C++ beginners like me. Appreciate for any advice !!!

The Question is -

An n * n matrix that is filled with the numbers 1, 2, 3,…, n2 is a magic square if the sum of the elements in each row, in each column, and in the two diagonals is the same value. For example,
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1
(so you see whether sum up diagonally, vertically or horizontally, the result is same)

Write a program that reads in n2 values from the keyboard and tests whether they form a magic square when put into array form. You need to test three features:
• Did the user enter n2 numbers for some n?
• Does each of the numbers 1,2,…, n2 occur exactly once in the user input?
• When the numbers are put into a square, are the sums of the rows, columns, and diagonals equal to each other?

I already got some advice from my friends but still couldn't solve this problems. I'm writing codes in VS .Net 2003 and here's my coding.

#include "stdafx.h" //must include this if write in VS .NET 2003 compiler
//don't know why
#include <iostream>
#include <vector>
#include <cmath> //to use sqrt() but getting this error:

/**
error C2668: 'sqrt' : ambiguous call to overloaded function
*/

using namespace std;

int main()
{
vector<int> square_input;

cout << "Enter values:\n ";
bool more = true;
while (more)
{
int input;
cin >> input;
if (cin.fail())
more = false;
else
square_input.push_back(input);
}

/* determine if n * n entries were entered
*/
int n = static_cast<int>(sqrt(square_input.size()));
if (n * n != square_input.size())
{
cout << "Not a square matrix.\n";
return 0;
}

/* determine if each of the numbers 1,2, ... n*n occur exactly once
*/
int i;
int j;
for (i = 1; i <= n * n; i++)
{
bool found = false;
for (j = 0; !found && j < square_input.size(); j++)
if (square_input[j] == i) found = true;
if (!found)
{
cout << i << " is missing from the matrix.\n";
return 0;
}
}

/* construct a matrix out of the square_input vector */

const int MAX_N = 20;
if (n > MAX_N)
{
cout << "Sorry; can only process up to "
<< MAX_N << " by " << MAX_N << " squares.\n";
return 1;
}

int magic_square[MAX_N][MAX_N];

for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
magic_square[i][j] = square_input[i * n + j];

int sum = 0;

/* sum up each row */
for (i = 0; i < n; i++)
{
int total = 0;
for (int j = 0; j < n; j++)
total = total + magic_square[i][j];
if (i == 0) sum = total;
else if (sum != total)
{
cout << "Not a magic square.\n";
return 0;
}
}

/* sum up each column */
for (i = 0; i < n; i++)
{
int total = 0;
for (int j = 0; j < n; j++)
total = total + magic_square[j][i];
if (sum != total)
{
cout << "Not a magic square.\n";
return 0;
}
}

/* sum up the first diagonal */
int total = 0;
for (i = 0; i < n; i++)
total = total + magic_square[i][i];
if (sum != total)
{
cout << "Not a magic square.\n";
return 0;
}
/* sum up the second diagonal */
total = 0;
for (i = 0; i < n; i++)
total = total + magic_square[i][n - 1 - i];
if (sum != total)
{
cout << "Not a magic square.\n";
return 0;
}

cout << "It is a magic square.\n";
return 0;
}

I tried thinking other way instead of using sqrt() to check user inputs are correct or not. Can you please give some advice for me ?

Thanks in advance
Apr 26, 2008 at 6:40pm
closed account (z05DSL3A)
There are a number of 'versions' of sqrt() that take different input types. In your code you pass the result of square_input.size() into sqrt() however the type that is returned dose not match any of the versions, so the complier does not know wich to use hence the ambiguous call to overloaded function error.

To solve this you will need to cast the returned value of the square_input.size() call to a type that sqrt uses in the input (double, float...)

BTW
1
2
#include "stdafx.h" //must include this if write in VS .NET 2003 compiler
//don't know why 
this is only needed if you are using precompiled headers.
Last edited on Apr 27, 2008 at 6:57pm
Apr 27, 2008 at 1:53am
Thanks Grey Wolf but I think I already call static cast to change its type.

int n = static_cast<int>(sqrt(square_input.size()));

if (n * n != square_input.size())

I honestly want to know why I'm getting stacked here.
Thanks for your suggestion
Apr 27, 2008 at 3:59am
You didn't put one in the right spot. :P
int n = static_cast<int>(sqrt(static_cast<double>(square_input.size())));

You need to cast square_input.size() to a double (or float or long double) before you call sqrt(). std::vector::size() returns an unsigned int, so it doesn't know which floating-point type to convert to. Casting to one of the floating-point types like I did should fix things (if I'm interpreting the problem correctly... :P)
Apr 27, 2008 at 8:09am
Wow....thanks so much rpgfan3233....you did solve my problem...I don't know much about sqrt() function and this is the first time I ever try to use it. By the way, do you know where can I get references(Books or Sites) about C++ so next time I won't get much troubles again ?

Appreciate your help!!!
Apr 27, 2008 at 5:24pm
This very site has an /excellent/ reference. Just check it out at http://www.cplusplus.com/reference/

It starts with a map of the IOstreams library, then it goes to the C++ versions of the C headers, followed by the std::string class (it gets its own section) and an STL reference.
Topic archived. No new replies allowed.