Trying To Find The Square Root Of Each Number Of A Array

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

Join Date: Apr 2005
Posts: 4
Reputation: MrNiceGuy82 is an unknown quantity at this point 
Solved Threads: 0
MrNiceGuy82 MrNiceGuy82 is offline Offline
Newbie Poster

Trying To Find The Square Root Of Each Number Of A Array

 
0
  #1
May 2nd, 2005
I need a little assistance with finding the square roots of each number entered by the user in a array. I'll copy and paste what i have so far in my function, but i get an error when i try to run the program and it would say: "sqrt was not declared in the function"

void print_square_root (double values [size])
{
int index;
cout << "The square root of the numbers are:\n";
for (index = 0; index < size; index++)
{

values [index] = sqrt (values [index]);
cout << "\n\n";
cout << showpoint << fixed << setprecision (2);
cout << setw (9) << values [index];
}

Anyone with any suggestions on how i would get the square roots to work for each number in the array I would greatly appreciate it...Thank You
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,579
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Trying To Find The Square Root Of Each Number Of A Array

 
0
  #2
May 2nd, 2005
How about posting a compilable example? Like, with your header includes and driver and the complete function so we can do more than just guess?
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 4
Reputation: MrNiceGuy82 is an unknown quantity at this point 
Solved Threads: 0
MrNiceGuy82 MrNiceGuy82 is offline Offline
Newbie Poster

Re: Trying To Find The Square Root Of Each Number Of A Array

 
0
  #3
May 2nd, 2005
Ok here is what i believe your asking for. This is the rest of my program that i wrote along with the question that was asked of me to do.

1. Write a program that reads in 8 double values entered by the user. It should store the values in an array. It should print the original numbers, the squares of the original numbers and the square roots of the numbers. The values should be printed in width 9 with 2 digits after the decimal.

Define the following constant and functions to help solve the problem:

const int SIZE=8;

void read_numbers (double numbers [SIZE]);
void print_numbers (double numbers [SIZE]);
void print_squares (double numbers [SIZE]);
void print_square_roots (double numbers [SIZE]);

Here is what a sample run of the program would look like:

Enter 8 numbers:
8.32 9.55 6 2.34 15.4 24 6.5 3.1


The numbers entered were:
8.32 9.55 6.00 2.34 15.40 24.00 6.50 3.10

The squares of the numbers are:
69.22 91.20 36.00 5.48 237.16 576.00 42.25 9.61

The square roots of the numbers are:
2.88 3.09 2.45 1.53 3.92 4.90 2.55 1.76

What i have so far:
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6. const int size = 8;
  7. void square_array (double values [size]);
  8. void read_numbers (double values [size]);
  9. void print_numbers (double values [size]);
  10. void print_square_root (double values [size]);
  11.  
  12. int main(int argc, char *argv[])
  13. {
  14.  
  15. const int size = 8;
  16. double number;
  17.  
  18.  
  19.  
  20. double values [size];
  21.  
  22.  
  23.  
  24. read_numbers (values);
  25. print_numbers (values);
  26. square_array (values);
  27. print_square_root (values);
  28.  
  29. cout << "\n\n";
  30.  
  31.  
  32.  
  33.  
  34. system("PAUSE");
  35. return EXIT_SUCCESS;
  36. }
  37. void read_numbers (double values [size])
  38. {
  39. int index;
  40. cout << "Enter 8 numbers:\n";
  41. for (index = 0; index < size; index++)
  42. cin >> values [index];
  43. }
  44. void print_numbers (double values [size])
  45. {
  46. int index;
  47. cout << "The numbers entered were:\n";
  48. for (index = 0; index < size; index++)
  49. cout << setw (9) << values [index];
  50. cout << "\n\n";
  51. }
  52. void square_array (double values [size])
  53. {
  54. int index;
  55.  
  56.  
  57. cout << "The squares of the numbers are:\n";
  58. for (index = 0; index < size; index++)
  59. {
  60. values [index] = values [index] * values [index];
  61. cout << "\n\n";
  62. cout << showpoint << fixed << setprecision (2);
  63. cout << setw (9) << values [index];
  64. }
  65. }
  66. void print_square_root (double values [size])
  67. {
  68. int index;
  69. cout << "The square root of the numbers are:\n";
  70. for (index = 0; index < size; index++)
  71. {
  72.  
  73. values [index] = sqrt (values [index]);
  74. cout << "\n\n";
  75. cout << showpoint << fixed << setprecision (2);
  76. cout << setw (9) << values [index];
  77. }
  78.  
  79. }
Code tags added. -Narue
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,579
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Trying To Find The Square Root Of Each Number Of A Array

 
0
  #4
May 2nd, 2005
>along with the question that was asked of me to do
I couldn't care less what the question was. I already knew what your problem was, I just wanted to confirm it.

Add this:
  1. #include <cmath>
That's where sqrt is declared.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC