944,073 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2546
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 8th, 2009
0

to calculate square and cube form given input using arrays..

Expand Post »
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];
}

}
Similar Threads
Reputation Points: 3
Solved Threads: 0
Light Poster
maverick405 is offline Offline
34 posts
since Oct 2009
Nov 9th, 2009
0
Re: to calculate square and cube form given input using arrays..
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....
Sponsor
Featured Poster
Reputation Points: 1165
Solved Threads: 578
Quantitative Phrenologist
jonsca is offline Offline
4,271 posts
since Sep 2009
Nov 9th, 2009
0
Re: to calculate square and cube form given input using arrays..
Hi.
From what i see,
C++ Syntax (Toggle Plain Text)
  1. void square_array (double values [size])
  2. {
  3.  
  4. int index;
  5. cout << "\nThe square of the numbers are:\n";
  6. for (index = 0; index < size; index++)
  7. {
  8. values [index] = values [index] * values [index];
  9. cout << "\n\n";
  10. cout << values [index];
  11. }
  12. }
  13. void print_cube_root (double values [size]) //this part is not //worknig
  14. {
  15. int index;
  16. cout << "\n\nThe cube root of the numbers are:\n";
  17. for (index = 0; index < size; index++)
  18. {
  19. values [index] = values [index] * values [index];
  20. cout << "\n\n";
  21. cout << values [index];
  22. }
  23.  
  24. }

both the functions are identical. Looks like you duplicated one of the functions, but forgot to change the cube-root function.
Reputation Points: 11
Solved Threads: 7
Junior Poster
abhi_elementx is offline Offline
118 posts
since Dec 2007
Nov 9th, 2009
0
Re: to calculate square and cube form given input using arrays..
BTW, cube of a number is different from its cube root, as pointed by jonsca.
Reputation Points: 11
Solved Threads: 7
Junior Poster
abhi_elementx is offline Offline
118 posts
since Dec 2007
Nov 9th, 2009
0
Re: to calculate square and cube form given input using arrays..
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.
Sponsor
Featured Poster
Reputation Points: 1165
Solved Threads: 578
Quantitative Phrenologist
jonsca is offline Offline
4,271 posts
since Sep 2009
Nov 9th, 2009
0

I tried and did this..

Click to Expand / Collapse  Quote originally posted by jonsca ...
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....

#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];
}

}
Reputation Points: 3
Solved Threads: 0
Light Poster
maverick405 is offline Offline
34 posts
since Oct 2009
Nov 9th, 2009
0
Re: to calculate square and cube form given input using arrays..
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.
Sponsor
Featured Poster
Reputation Points: 1165
Solved Threads: 578
Quantitative Phrenologist
jonsca is offline Offline
4,271 posts
since Sep 2009
Nov 9th, 2009
0
Re: to calculate square and cube form given input using arrays..
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)
Last edited by jonsca; Nov 9th, 2009 at 8:46 pm.
Sponsor
Featured Poster
Reputation Points: 1165
Solved Threads: 578
Quantitative Phrenologist
jonsca is offline Offline
4,271 posts
since Sep 2009
Nov 9th, 2009
0

cube

Click to Expand / Collapse  Quote originally posted by jonsca ...
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..
Reputation Points: 3
Solved Threads: 0
Light Poster
maverick405 is offline Offline
34 posts
since Oct 2009
Nov 9th, 2009
0
Re: to calculate square and cube form given input using 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
C++ Syntax (Toggle Plain Text)
  1. declare values array;
  2. declare tempvals (same size)
  3. read_numbers (values);
  4. copy values to tempvals
  5. square_array (values);
  6. cube_array (tempvals);

If you're having trouble with arrays there are tons (and tons) of writeups on the web about them.
Sponsor
Featured Poster
Reputation Points: 1165
Solved Threads: 578
Quantitative Phrenologist
jonsca is offline Offline
4,271 posts
since Sep 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: const char array problem
Next Thread in C++ Forum Timeline: Internet Provider Switch Program Help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC