Problem with Sqrt fx.

Hi guys. I am trying to do this one problem: Find the Fifth root of the sum of the squares of the first 100 ODD numbers only. (Beginner). I got everything working but when I get to the square root part I got a compiling error. Please help.

I am using MinGW Developer Studio 2.05.
This is the compiling error:
--------------------Configuration: TestingLop - Debug--------------------
Compiling source file(s)...
Test.cpp
Test.cpp: In function `int main()':
Test.cpp:27: error: call of overloaded `sqrt(int&)' is ambiguous
C:\MinGWStudio\MinGW\include\math.h:166: error: candidates are: double
sqrt(double)
C:\MinGWStudio\MinGW\include\c++\3.3.1\cmath:550: error: long
double std::sqrt(long double)
C:\MinGWStudio\MinGW\include\c++\3.3.1\cmath:546: error: float
std::sqrt(float)

TestingLop.exe - 4 error(s), 0 warning(s)
--------------------------------------------------


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
#include <iostream>
#include <string>
#include <math.h>
 
using namespace std;
inline int OddOrEven(int input){ //check to see if number is odd or even. 
	int remain = input % 2; 
		return(remain); 
}

int main(){
	cout << "Please input a number: "; 
	int input(0), total(0), result(0), check; 
		cin >> input; 
	int A_input[input], A_inputToBeSquare[input]; 
	
for(int i = 0; i < input; i++){
	check = OddOrEven(i);   
	if(check == 1){
		A_input[i] = i; //collect all the odd number. 
		A_inputToBeSquare[i] = A_input[i] * A_input[i]; //squaring all the odd number.  
		total += A_inputToBeSquare[i]; //adding all the squaring number up. 
	}//if  
}//for

result = sqrt(total); //attempting to sqrt the total. << error here somewhere. 
cout << result; 

}//main 
Try making your total variable into a long double, and maybe result as well. I don't know if that will work though as I can't test it right now.

Also the 5th root is different than the square root.

e.g.

5\/32 = 2 because 25 = 32

You can use pow() to do this. Raise your total to the 1/5 or .2 power and you'll get the same result.

EDIT:

So I got to test it now, I didn't get any errors with your code. I did get a warning: converting double to int. I'm using Dev-C++. Try what I said and if it still doesn't work than I don't know what else to say.

I do see some logic errors that you should work out.

1.)First off you say you want to find blah blah blah of the first 100 odd numbers. If I enter 100, I only get the sum of the odd numbers underneath 100. i.e. 1, 3, 5 ... 99. That's a bit unclear so maybe you want to do something that if you enter 100, it will sum up 100 odd numbers not just up to 99.

2.)You're only filling half of your array. To be exact your only entering values into A_input[1], A_input[3], A_input[5]....which is something else you want to fix.

3.)Last thing, if I entered 3, you would think it would include 3. But it won't because when i gets to 3, i < input will no longer be true and will end the loop. Meaning it only evaluated 1 number, 1. I would change the < to <=.

3 1/2.)Theres also no need to store the odd numbers in an array. You only need to store the squares of the numbers. That's not really important but you should look for these type of things.

3 3/4.) One more thing =) You could make your function return bool and use that for your if on line 19. Make it return true if its odd and false if not. And you could get rid of that check int. Then do

1
2
3
4
if(OddOrEven(i))
{
    //blah blah blah
}


Sorry if you feel like I'm massacring your code but I'm not, I'm a beginner as well and I know its good not to get into bad habits early as they are harder to quit later. Hopefully I'm clear and you understand the reasons for me saying this stuff. Also this is good practice for me =)

Edit again.... I just read that some cmath functions do not allow integral parameters as sometimes that would not make sense. If you haven't read about overloaded functions, do a little reading, it's a pretty easy concept to understand.

Change total to anything besides an int and it should work.

'Ambiguous' means "open to more than one interpretation" meaning the compiler does not know whether to return a double, long double, or a float by the way. (Only reason I included this was because I didn't know what ambiguous meant >.<)
Last edited on
Yep. I fixed all of those thing. hehe. It turn out if you want to use the square root, all of the number involves have to be something other than an int type. Something to do with the require precision I guess.

Yep. normally I would just use the <vector> type instead of an array. But I thought I should have some practice with array also. I tested all the number on my TI calculator, and it work out pretty well now. Thx.
No problem. I don't know much about vectors but I am pretty good with arrays so maybe I'll do something like yours with vectors =P

Glad to help.
Hey man, I am just following a series of problems, or programming ideas I put together from all over the internet. I share it with you. I just put them together, but I am on number 11. hehe.

Beginner
1. Write a program which finds the factorial of a number entered by the user. (check for all conditions) (Beginner).

2. Develop a program to convert currency X to currency Y and visa versa (beginner).

3. Write a program that print out the following, user will input the top number:
*****
****
***
**
*

4. Write a program that print out the following, user will input the bottom number: *
**
** *
****
******

5. Write a program that pring out the following triangle, the user will input the bottom number:
*
***
*****
*******

6. Develop a program that uses a randomly generated number to select 1 of 3(or more)
functions to show the user. (Beginner)

7. Guessing game. Write a c++ game that asks the user to guess a number between 1 and a 100. If you guessed correctly, it will say you win. If your too high or too low it will also let you know. (Beginner)

8. Create a program which generates Fibonacci series till a number 'n' where 'n' is entered by the user. For eg. if the user enters 10 then the output would be: 1 1 2 3 5 8 (Beginner)

9. Determine how many of the characters are vowels and how many are consonants in a given line of text. Also terminate the string when the input character encountered is other than the alphabets. (Beginner).

10. Find the Fifth root of the sum of the squares of the first 100 ODD numbers only. (Beginner)

11. Perform 4-letter WORD UNSCRAMBLING i.e. List all possible combinations of 4-letters in a word. Ex: The word 'TEST' can be unscrambled as TEST,TETS,TSET,TSTE,TTSE,TTES,etc. (Beginner)

12. Make a program that allows the user to input either the radius, diameter, or area of the circle. The program should then calculate the other 2 based on the input. (Beginner)

13. READ a line of text and WRITE it out BACKWARDS using RECURSIVE Function. (Beginner)

14. Write a program to SWAP the contents of 3 variables without using the temporary (or extra) variables. (Beginner)

15. Write a program which reverses the numerals in an integer, that is 326 becomes 623, etc.. (Beginner)

16. The program will tell the user each time whether he guessed high or low. The user WINS the game when the number guessed is same as randomly generated number. (Beginner)

17. Write a program to simulate a simple calculator. It should accept two number from the user along with the required operation to be performed. Addition, Subtraction, Division and Multiplication are the basic operations that should be implemented. Feel free to implement the other operations (Beginner)

18. Determine how much money is in a piggy bank that contains several 50 paise coins, 25 paise coins, 20 paise coins, 10 paise coins and 5 paise coins. Use the following values to test your program : Five 50 paise coins, Three 25 paise coins, Two 20 paise coins, One 10 paise coin and Fifteen 5 paise coins. (Beginner)

19. Create a simple Palindrome checker program. The program should allow the user to enter a string and check whether the given string is a palindrome or not. Only digits and alphabets should be considered while checking for palindromes -- any other characters are to be ignored. (beginner)

20. Write a program that allows you to input students' scores and weights. The program should then calculate a weighted average and score based on the data inputted by the user. (Beginner)
Intermediate

21. Write a program which will print all the pairs of prime numbers whose sum equals the number entered by the user. ( suggested by Aniseed ) (Intermediate)

22. Develop a animal classification program base on the animal kingdom. (for practice the use of inhabitant classes).

23. Write a quiz program, which retrieve question, and answer from a file. Then allow the user to take the quiz/count point and return score. (Intermediate)

24. Write a program that accepts XHTML, parses and removes the tags. Then it prints out the remaining text. (Intermediate)

25. Write a program which performs addition, subtraction, multiplication of matrices. The dimensions of both the matrices would be specified by the user (dynamic memory allocation required). Use of structure or a class to define the matrix would be a good idea. (Intermediate)

26. Write a program which will perform the job of moving the file from one location to another. The source and destination path will be entered by the user. Perform the required error checking and handle the exceptions accordingly. (Intermediate)

27. Create a sophisticated linked list class. You should be able to insert and delete nodes anywhere in the list, and the nodes should have pointers to nodes both in front and behind them. (Intermediate)

28. Create a program that implements a database in C++. The fields are hard-coded, and the data is saved in a binary file. Although this isn't really flexibility, you aren't relying on any external libraries or functions. (Intermediate)

Expert
1. Implement your own strstr.

2. Write a program which acted like a personal planner. In which a user can input in event, note of thing to-do on certain date.

3. Tic Tac Toe program.

4. Simple file encryption (using something simple like ROT13.

5. Write a phone/address book program, with data save in binary files. The users should be able to add/delete/change the data.

6. Write a simple payroll program, that would include pay rates, and hours work for employees.

7. Create a few classes that model playing cards. Then use this framework to create your favorite card game. Blackjack..etc(expert)

8. Create a few classes that model chess pieces. Then develop a real chess game.(Expert)

9. Create a binary tree which has search and sorting functions. (Expert)

10. Create a Quine, that is, a program that prints out its own source code. (Expert)

11. Implement your own version of the Standard Template Library. (Expert)

Graphics
1. Write a program to draw a rectangle, ellipse, square, circle, point and line based on user input. (Beginner)

2. Write a program to emulate Microsoft Paint. It should be possible to switch between different tools (circle, rectangle, eraser...) using pre-defined key strocks. – Intermediate

3. Write a program to plot a simple x-y graph for a harcoded function (e.g. y=cos(x)). It should be possible to zoom in on any part of the graph. - Intermediate.

4. Write a program to plot a graph of given equation of form y=f(x) and a range for x as command line arguments. (e.g. my_graph_plotter -eq="y=x*x" -xmin=-10, -xmax=10) - Expert. (PS: more to do with equation solving than graphics)

5. Write the classic brick-break-out game. E.g. see DX Ball. – Expert.

Reference:
http://projecteuler.net/index.php?section=problems
http://www.cprogramming.com
http://www.cplusplus.com

Last edited on
Topic archived. No new replies allowed.