Multiplying a variable by itself(squaring) without using pow function

So my instructor for my c++ class wants us to make multiple functions that do various operations on an initial value given by the user. He told us to essentially create user-defined functions, even for operations that have predefined functions such as pow, abs, etc.
Unfortunately, I have never run into a problem where I have to multiply a variable by itself without using the pow function, so I am not sure how exactly to go about it.

Edit: the error message comes about when I attempt to multiple initial by initial and assign it to squared.

1
2
3
4
5
6
7
8
9
10
11
12
13
14

 #include "stdafx.h"
#include<iostream>
#include<iomanip>
#include<cmath>
#include<fstream>
using namespace std;
double amountOfValues, initial, interval, squared = 0.0;

double valueSquared(double initial, double squared) {
	double squared = double initial * double initial;
	//initial = squared;
	return squared;
}
Last edited on
double squared = double initial * double initial;
You're declaring doubles which are uninitialised, so when you do arithmetic on them, you will most likely end up with junk values, usually large negative/positive values.

1
2
3
double valueSquared(double initial) {
	return initial * initial;
}

I'm not sure what you intended the parameter squared to do. Maybe it's the exponent? Maybe you meant to assign initial2 to it? (you'll need to pass it by reference if you intend to do that)
Topic archived. No new replies allowed.