squaring and cubing numbers- I have fixed some stuff

First I would like to know if there is a place where you post examples of how to write certain codes?

Second I am supposed to write a program that asks the user to enter 3 numbers. First one is how many numbers are to be in a table of numbers that is used for math problems. Second number is the number that I start the table with. Third number is the increments between the numbers. I am supposed to take the number that I start with and square it then square root it then cube it and then cube root it. I changed some stuff. This is my new one.
This is what I have so far but I know it is not right.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;
int main( )
{
int numValue;
double value, value2;

cout << "How many values should the table contain ( 1-25)? " << endl;
cin >> numValue;
cout << "What is the number you would like to start with ( -1000 - +1000)? "<< endl;
cin >> value;
cout << "What number represents the increment between successive values in the table ( 1-20)? " << endl;
cin >> value2;

double x, y, z, y2, z2;
for ( int i=0; i <= 25; i++)
{
x = numValue;
}
for (double i=0; i<=1000; i++ )
{
x = value;
y = ( value * value );
z = sqrt(y);
y2= ( value * value * value);
z2 = cbrt(y2);
}
cout << "Value=" << x << ", \tSquare=" << y << ", \tRoot2=" << z << ", \tCube=" << y2 << ", \tRoot3=" << z2 << endl;

system("pause");
return 0;
}
I cannot get the cube root to work. Any help would be great.
Last edited on
return x;

This leaves the function it is in, which is the main function, so the program ends here. I think you don't mean this.
Is this what you mean to do?

int num = y;
for (int i = 1; i <= x; i++)
{
num += z;
cout << num << " squared =" << pow(num, 2);
.....etc.....
}

It looks like you're re-using the variables, which can be confusing.
Last edited on

Here is what I have so far for my program of table of numbers. I have got it to work fine on the square, and the square root. But I can not figure out how to do a cube root. Could someone look at this and assist me in any way. Thanks!

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
 int main( )
{

	int numValue;
	double value, value2;

	cout << "How many values should the table contain (1-25)? " << endl;
	cin >> numValue;
	cout << "How many values should the table contain (-1000 - +1000)? " << endl;
	cin >> value;
	cout << "What number represents the increment between successive values in the table (1 - 20)? " << endl;
	cin >> value2;
	double x, y, z, y2,z2;
	for ( int i=0; i <= 25; i++)
	{
		x = numValue;
	}
	for ( i=0; i<=1000; i++ )
	{
		x = value;
		y = ( value * value);
		z = sqrt(y);
		y2 = ( value * value * value);
		z2 = cbrt(y2);  // It keeps underlining the cbrt. I have even tried cubeit.
	}

	cout << "i=" << i << ", \tx=" << x << ", \ty=" << y << ", \tz=" << z << endl;


system("pause");
return 0;
	}
// It keeps underlining the cbrt. I have even tried cubeit.


cbrt was added to C99, but is apparently not in the <cmath> library you're using.
You could use:
pow(x, 1.0 / 3.0)
Hey that worked thank you. Can I ask one more question? Now it doesn't seem to go to the next increment and work through the loop again. I tried (i + value2) but that did not work.

I tried (i + value2) but that did not work


That's not a complete statement. If you want to change the value of i, it needs to be on the left side of an assignment statement.
 
i = i + value2;

You don't need this:
1
2
3
4
for ( int i=0; i <= 25; i++)
{
	x = numValue;
}

This would work better:
1
2
3
4
5
6
7
8
9
10
11
12
        int x = value;
	for ( i=1; i<=numValue; i++ ) //not 25, the user picks the number of values
	{
		y = pow(x, 2)
		z = sqrt(y);
		y2 = pow(x, 3);
		z2 = pow(x, (1/3))
                //You need to print out the values inside the for loop or you'll 
                //only print the values for the last x.
                cout << "i=" << i << ", \tx=" << x << ", \ty=" << y << ", \tz=" << z << endl; 
                x += value2; //Adds value2 to the current x
	}

You seem to be getting confused with what your variables mean. Try naming them something more descriptive. Ex: value could be startingValue. value2 could be increment. x could be currentValue. y could be squared... etc.
Last edited on
I have a quick question. I am supposed to save the calculations in a set of arrays, one for each calculated value. Read a series of three-number sets from a data file named triples.txt and create a table of numbers corresponding to each three-number set. Save the resulting tables of numbers to a single text file named numbers.csv in comma-separated-value format.
These are supposed to be two different things that I am supposed to add to my program. I am wondering if they are supposed to go together or are they separate? and I really don't know how to start with the arrays. Thanks for all the info!
Topic archived. No new replies allowed.