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

Code:

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

Dani AI

Generated

, the compile error is only one issue. Right now square_array mutates values, so print_square_root ends up taking the root of the squared numbers instead of the originals. That yields |x| and will also cause a domain problem if any input is negative. Compute squares and roots from the original array and qualify the call as std::sqrt so you get the right overload. See the standard behavior (domain error, NaN on negatives) here. std::sqrt reference.

Drop the in-place updates and print directly:

void print_squares(const double a[], std::size_t n) {
    std::cout << "The squares of the numbers are:\n"
              << std::fixed << std::setprecision(2) << std::showpoint;
    for (std::size_t i = 0; i < n; ++i)
        std::cout << std::setw(9) << (a[i] * a[i]);
    std::cout << "\n\n";
}

void print_square_roots(const double a[], std::size_t n) {
    std::cout << "The square roots of the numbers are:\n"
              << std::fixed << std::setprecision(2) << std::showpoint;
    for (std::size_t i = 0; i < n; ++i) {
        if (a[i] < 0) std::cout << std::setw(9) << "nan";
        else          std::cout << std::setw(9) << std::sqrt(a[i]);
    }
    std::cout << "\n\n";
}

A few cleanups that make your output match the sample and the intent, echoing ’s request for a minimal, clean example: set fixed/showpoint/setprecision(2) once before the loop (not inside it), keep a single SIZE constant (do not redeclare it in main), and avoid system("PAUSE"). fixed together with setprecision(2) ensures exactly two digits after the decimal. std::fixed std::setprecision.

Recommended Answers

All 3 Replies

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?

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 );
void print_numbers (double numbers );
void print_squares (double numbers );
void print_square_roots (double numbers );

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:

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

using namespace std;
const int size = 8;
void square_array (double values [size]);
void read_numbers (double values [size]);
void print_numbers (double values [size]);
void print_square_root (double values [size]);

int main(int argc, char *argv[])
{
    
    const int size = 8;
    double number;
    
    
    
    double values [size];
    
    
      
    read_numbers (values);
    print_numbers (values);
    square_array (values);
    print_square_root (values);
    
    cout << "\n\n";
    
    
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
void read_numbers (double values [size])
{
     int index;
     cout << "Enter 8 numbers:\n";
     for (index = 0; index < size; index++)
     cin >> values [index];
}
void print_numbers (double values [size])
{
     int index;
     cout << "The numbers entered were:\n";
     for (index = 0; index < size; index++)
     cout << setw (9) << values [index];
     cout << "\n\n";
}
void square_array (double values [size])
{
   int index;
   
   
   cout << "The squares of the numbers are:\n";
   for (index = 0; index < size; index++)
   {
       values [index] = values [index] * values [index];
    cout << "\n\n";
    cout << showpoint << fixed << setprecision (2);
    cout << setw (9) << values [index];
   }
}
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];
         }
         
}

Code tags added. -Narue

>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:

#include <cmath>

That's where sqrt is declared.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.