Hello, please help me I am trying to write program that allows a user to enter 10 numbers, stores in an array and then displays each with it's square (no * no) and cube (no * no * no), I am able to get square but cube in not working, it gives me output my that not cube for the number please help me to solve this also in output I want user input number, square and cube the below is my code.

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <cmath>  


      using namespace std;
      const int size = 10;
      void square_array (double values [size]);
      void read_numbers (double values [size]);
      void print_cube_root (double values [size]);
      int main(int argc, char *argv[])
{
      const int size = 10;
      double number;
      double values [size];
      read_numbers (values);
      square_array (values);
      print_cube_root (values);

cout << "\n\n";
system("PAUSE");
return EXIT_SUCCESS;
}
void read_numbers (double values [size])
     {
int index;
    cout << "Enter 10 numbers:\n";
    for (index = 0; index < size; index++)
    cin >> values [index];
    }
void square_array (double values [size])
     {

int index;
    cout << "\nThe square of the numbers are:\n";
    for (index = 0; index < size; index++)
    {
    values [index] = values [index] * values [index];
    cout << "\n\n";
    cout << values [index];
}
      }
  void print_cube_root (double values [size])  //this part is not //worknig
        {
        int index;
        cout << "\n\nThe cube root of the numbers are:\n";
        for (index = 0; index < size; index++)
        {
          values [index] = values [index] * values [index];
        cout << "\n\n";
        cout << values [index];
}

      }

Edited 3 Years Ago by Nick Evan: Fixed formatting

Before the cubing step values[] = original value squared, so saying cubed = values[]*values[] = originalvalue squared * original value squared = original value to the fourth. Also in your parlance, cube_root is the same as cubed which it is not (e.g., cube root of 27 = 3). So, ultimately I think the way you have it laid out is fairly confusing. You could pass in a second result array to each of the functions and leave your original one intact, depending on your specification....

Hi.
From what i see,

void square_array (double values [size])
{

int index;
cout << "\nThe square of the numbers are:\n";
for (index = 0; index < size; index++)
{
values [index] = values [index] * values [index];
cout << "\n\n";
cout << values [index];
}
}
void print_cube_root (double values [size]) //this part is not //worknig
{
int index;
cout << "\n\nThe cube root of the numbers are:\n";
for (index = 0; index < size; index++)
{
values [index] = values [index] * values [index];
cout << "\n\n";
cout << values [index];
}

}

both the functions are identical. Looks like you duplicated one of the functions, but forgot to change the cube-root function.

I see what the OP thought they were doing, they were thinking that after squaring it they were going to multiply in one more of the original arrays to get a cube, but since the square came back by reference it turns out to be to the 4th power. But you are correct that they are identical.

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <cmath>  


      using namespace std;
      const int size = 10;
      void square_array (int values [size]);
      void read_numbers (int values [size]);
      void cube_array (int values [size]);
      int main(int argc, char *argv[])
{
      const int size = 10;
       int values [size];
       int i [size];
       read_numbers (values);
       square_array (values);
       cube_array (values);

cout << "\n\n";
system("PAUSE");
return EXIT_SUCCESS;
}
void read_numbers (int values [size])
     {
int index;
    cout << "Enter 10 numbers:\n";
    for (index = 0; index < size; index++)
    cin >> values [index];
    }
void square_array (int values [size])
     {

int index;
    cout << "\nThe square of your input numbers are:\n";
    for (index = 0; index < size; index++)
    {
    values [index] = values [index] * values [index];
    cout << "\n\n";
    cout << "Square is ---> " << values [index];
}
      }
  void cube_array ( int values [size])
        {
        int i;
        cout << "\n\nThe cube root of the numbers are:\n";
        for (i = 0; i < size; i++)
        {
          values [i] = values [i] * values [i];
        cout << "\n\n";
        cout << values [i];
}

      }

Edited 3 Years Ago by Nick Evan: Fixed formatting

Code didn't seem to change too much as it is essentially the same thing. Remember your arrays are being passed as double * so once your functions are returning to main you have changed value arrays. Put some intermediate couts in there to convince yourself. You may have to store the array you get back from read_numbers() into another array and pass that into your cube function.

I should say pass it into your modified cube function, one that had values[index] = values[index]*values[index]*values[index]; not as you have it.

(also please use code tags with your code the next time)

Edited 7 Years Ago by jonsca: n/a

Code didn't seem to change too much as it is essentially the same thing. Remember your arrays are being passed as double * so once your functions are returning to main you have changed value arrays. Put some intermediate couts in there to convince yourself. You may have to store the array you get back from read_numbers() into another array and pass that into your cube function.

Can you please give me example how to do this.. I am really confuse and dont have much idea about arrays..

Ok. Do you understand about pointers? When you are passing in an array it's (to the best of my knowledge) converted to a type * at compile time to be passed into a function. When your array is being passed into your function it is being changed within the function, so that when you pass it into the next call in main() it is something different.

So basically change your cubing function to values[index] = values[index]*values[index]*values[index]; and in main() work off this pseudocode

declare values array;
declare tempvals (same size)
read_numbers (values);
copy values to tempvals
square_array (values);
cube_array (tempvals);

If you're having trouble with arrays there are tons (and tons) of writeups on the web about them.

This should work now. You gotta learn arrays, passing arrays to functions, returning arrays, etc. profoundly in order to master arrays.

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <cmath> 


using namespace std;
const int size = 10;
void square_store (int [], int []);
void read_numbers (int []);
void cube_store (int [], int []);

int main(int argc, char *argv[]){
	const int size = 10;
	static int values[size], square_array [size], cube_array[size];
	int i [size];
	
	read_numbers (values);
	square_store (square_array, values);
	cube_store   (cube_array, values);

	cout << "\n\n";
	system("PAUSE");
	return EXIT_SUCCESS;
}//main() ends

void read_numbers (int values[]){
	int index;
	cout << "Enter 10 numbers:\n";
	for (index = 0; index < size; index++)
		cin >> values [index];
}//read_numbers() ends

void square_store (int square_array[], int values[]){

	int index;
	cout << "\nThe square of your input numbers are:\n";
	for (index = 0; index < size; index++){
		square_array [index] = values [index] * values [index];
		cout << "\n\n";
		cout << "Square is ---> " << square_array [index];
	}
}//square_store() ends

void cube_store (  int cube_array[], int values[]){
	int i;
	cout << "\n\nThe cubes of the numbers are:\n";
	for (i = 0; i < size; i++){
		cube_array [i] = values [i] * values [i] * values [i];
		cout << "\n\n";
		cout << "Cube is ---> " << cube_array[i];
	}
}//cube_store() ends

As pointed by jonsca, there are many tuts/ebboks on the 'firehose of information'(the Internet).

Cheers

This article has been dead for over six months. Start a new discussion instead.