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

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2009
Posts: 32
Reputation: maverick405 has a little shameless behaviour in the past 
Solved Threads: 0
maverick405 maverick405 is offline Offline
Light Poster

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

 
0
  #1
26 Days Ago
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];
}

}
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 398
Reputation: jonsca is an unknown quantity at this point 
Solved Threads: 49
jonsca jonsca is offline Offline
Posting Whiz
 
0
  #2
25 Days Ago
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....
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 113
Reputation: abhi_elementx is an unknown quantity at this point 
Solved Threads: 6
abhi_elementx's Avatar
abhi_elementx abhi_elementx is offline Offline
Junior Poster
 
0
  #3
25 Days Ago
Hi.
From what i see,
  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.
PEACE !
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 113
Reputation: abhi_elementx is an unknown quantity at this point 
Solved Threads: 6
abhi_elementx's Avatar
abhi_elementx abhi_elementx is offline Offline
Junior Poster
 
0
  #4
25 Days Ago
BTW, cube of a number is different from its cube root, as pointed by jonsca.
PEACE !
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 398
Reputation: jonsca is an unknown quantity at this point 
Solved Threads: 49
jonsca jonsca is offline Offline
Posting Whiz
 
0
  #5
25 Days Ago
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 32
Reputation: maverick405 has a little shameless behaviour in the past 
Solved Threads: 0
maverick405 maverick405 is offline Offline
Light Poster

I tried and did this..

 
0
  #6
25 Days Ago
Originally Posted by jonsca View Post
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];
}

}
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 398
Reputation: jonsca is an unknown quantity at this point 
Solved Threads: 49
jonsca jonsca is offline Offline
Posting Whiz
 
0
  #7
25 Days Ago
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 398
Reputation: jonsca is an unknown quantity at this point 
Solved Threads: 49
jonsca jonsca is offline Offline
Posting Whiz
 
0
  #8
25 Days Ago
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; 25 Days Ago at 8:46 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 32
Reputation: maverick405 has a little shameless behaviour in the past 
Solved Threads: 0
maverick405 maverick405 is offline Offline
Light Poster

cube

 
0
  #9
25 Days Ago
Originally Posted by jonsca View Post
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..
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 398
Reputation: jonsca is an unknown quantity at this point 
Solved Threads: 49
jonsca jonsca is offline Offline
Posting Whiz
 
0
  #10
24 Days Ago
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
  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.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC