square, square root, functions

I'm just a few weeks into my C++ class, so I apologize for any lack of knowledge on my part, and for what, I'm sure, is terrible code.

I'm trying to create a program that outputs a number, that number squared, and that number's square root (example: 4, 16, 2). I need the program to complete this process for numbers 1 to 81 (example: Line 1: 1, 1, 1; Line 2: 2, 4, 1.41; etc.). Lastly, I can't use loops in this program, though right now, I'm most concerned about my math isn't coming out right.

I'm currently using a function within a function for the calculations in the first line, however, I'm interested in the idea of using if else statements to output all 81 lines. Or if someone has a more compact way of programming without loops, that would be great too. I'm also having several other problems with my current code, that I've outlined within the code.

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
30
31
32
33
34
35
36
#include <iostream>
#include <cmath> 
#include <math.h> //Not sure about including math.h
using namespace std;

int calc1 (int x){
	cout;
	x;
	return x;
}
double calc2 (double x){
	cout;
	sqrt(x*1.0);
	return sqrt(x*1.0);
}
int calc3 (int x){
	cout;
	pow(x, 2.0);
	return pow(x, 2.0);
}
int M1 (int x){
	cout << "Number: " << x << endl;
	x = calc1 (x);
	cout << "Number's square root: " << x << endl;
	x = calc2 (x); //Returns "Number". Why?
	cout << "Number squared: " << x << endl;
	x = calc3 (x); // Returns "square root". Why?
	cout << "_________" << endl;
	return x;
}
void main (){
	int y = 4; // Program currently uses 4 in calculations
	y = M1 (y);
	y = M1 (y);
	y = M1 (y);
}
First, use int main().

Ok, all of your cout; statements do nothing, just remove them. Same for the x; statement.

Lets step through the lines of M1:
22: Print out x (4)
23: Set x to be the result of calc1 (which does nothing, so it stays 4)
24: Print out x (still 4)
25: Set x to be the result of calc2 (which takes the square root, so it becomes 2)
26: Print out x (2)
27: Set x to be the result of calc3 (which squares it, so it becomes 4)
28: Random print statement
29: returns the number (now 4) which basically means this function doesn't change the number at all

So your output after the first call of M1 would be:
Number: 4
Number's square root: 4
Number squared: 2


Yes, the numbers are "wrong" but that is only because the text you put next to them is incorrect.
Thank you for your very speedy response, and I'm very sorry I didn't write back. I had no PC access until today. I'm grateful for and appreciative of any help, and I will respond back in a timely manner this time.

I removed the unnecessary statements. Thank you! As far as the math, I know that I could simply switch the text statements to correspond to the calculations except that, as far as I know, "pow (x, 2.0);" should multiply the value of x by itself, not square root the number, which is what's happening. I would like to actually understand my mistake, rather than accept it and switch the text around.

Lastly, I need the program to print out the number, square and square root for numbers 1 through 81 (example: 1, 1, 1; 2, 4, 1.41; 3, 9, 1.73; 4, 16, 2; etc.) not a number, its square root, and then its square root squared (4, 2, 4). I tried to accomplish by using the same value for x in each calculation, however it hasn't worked as of yet.

Current code--
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
#include <iostream>
#include <cmath> 
#include <math.h> //Not sure about including math.h
using namespace std;

int calc1 (int x){
	return x;
}
double calc2 (double x){
	return sqrt(x*1.0); //What's making this return 4?  I thought sqrt calculated a number's square root
}
int calc3 (int x){
	return pow(x, 2.0); //What's making this return 2, the square root?  I thought pow is used for exponents
}
int M1 (int x){
	cout << "Number: " << x << endl; //Should display 4
	x = calc1 (x);
	cout << "Number's square root: " << x << endl; //Should display 2
	x = calc2 (x); //Returns "Number". Why?
	cout << "Number squared: " << x << endl; //Should display 16
	x = calc3 (x); // Returns "square root". Why?
	cout << "_________" << endl;
	return x;
}
int main (){
	int y = 4; // Program currently uses 4 in calculations
	y = M1 (y);
}
Last edited on
Hello! I and a classmate fixed this code, and are including it, in case another programmer has a similar problem. I still need to figure out how to increment the number without just typing out new variables under "int main", but that's for another post.

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
#include <iostream>
#include <cmath> 
using namespace std;

int calc1 (int x){
	return x;
}
double calc2 (double x){
	return sqrt(x*1.0);
}
int calc3 (int x){
	return pow(x, 2.0);
}
int M1 (int x){
	cout << "Number: " << calc1 (x) << endl;
	cout << "Number's square root: " << calc2 (x) << endl;
	cout << "Number squared: " << calc3 (x) << endl;
	cout << "_________" << endl;
	return x;
}
int main (){
	int a = 1;
	a = M1 (a);
	int b = 2;
	b = M1 (b);
	int c = 3;
	c = M1 (c);
}
Last edited on
Hey dude i really like how this code is built

but I have found your little section in where the user can input there own code

your code is perfect till the int main (){ part

Here's what i have changed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
	...  // this is line 18 in your code above
	return x;
}
int main (){
	int a;
	cout << "Enter your First Number: ";
	cin >> a;
	a = M1 (a);
	int b;
	cout << "Enter your Second Number: ";
	cin >> b;
	b = M1 (b);	
	int c;
	cout << "Enter your Third Number: ";
	cin >> c;
	c = M1 (c);
}


you can use this little edit if u want but i would because it makes it more user frendly also try to find where the program will return to the start of the code ie: restart option
Last edited on
Topic archived. No new replies allowed.