• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 894
  • Last Modified:

is_double() and is_string()

I have an array. I need to check whether my array is a collection of double or string values. Are there functions in C++, something like is_double() or is_string()? If not, what is an easy way to implement them? Many thanks!
0
njava
Asked:
njava
  • 6
  • 4
1 Solution
 
esorfCommented:
What's the declaration of the array?

Even using RTTI, you're not going to be able to get very far unless the objects are closely related (like rooted in the same object hierarchy).
0
 
njavaAuthor Commented:
My array is filled during run-time and not fixed. Example of declaration:

double [100] array_num = {177, 165, 175, 185}; or
string [100] array_str = {"Gary", "Tony", "Brad", "Helen"};

Sorry, I am getting confused myself as to what I want to accomplish :) I am working on an assignment, in which I should declare a numerical vector array_num. This vector should have a name array_str (if I understood it correctly). Then the vector is tested whether it's numerical or string.
Now, when I had written this, I am totally puzzled that if i define my array as double, how I can write string values into it??

Perhaps you could shed some light as to what in C++ is called "the name of the vector"?
0
 
esorfCommented:
Note: arrays are not the same as vectors.  Arrays are a built in feature of the language whereas vectors are template classes that come with the standard template library.

If you have an array of doubles, there is no way of putting string values into it.

If you have array_num and array_str as defined above, what does the code look like that would need to test the type of array?  I don't think the problem makes sense in C++, because you cannot recover the type of something passed around as void*.

Are you supposed to make an array of unions that could contain either strings or doubles?  Do you have an example of the code that needs to figure out the type of the array?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
njavaAuthor Commented:
I think I should post the complete assignment so that it's clear what I am doing.
==========================================================

Many researchers find a statistics package to be particularly useful in their studies. Such a package allows them to enter data, and to analyze it in various ways. In this course, you are going to build a simple package for statistical analysis. For this assignment you are going to implement a C++ class for numerical vectors. A numerical vector is used in statistical packages to represent a single statistical variable whose values are real numbers. Therefore, a numerical vector is a collection of real values that a researcher has collected in their experiment.

For example, suppose a researcher wanted to investigate the relationship between a person's height and his/her final  grade. The researcher might survey the students in the class and collect the following data.

Student Surveyed  | Height | Grade
-------------------------------------------
 1                        | 177     | 83
 2                        | 165     | 98
 3                        | 175     | 91
 4                        | 185     | 68
 
Then, the researcher creates two numerical vectors, one for the height and one for the grade and stores the values there (in the order they appear in the table above). Later, the researcher can use some analysis tools to analyze the results. For instance, the researcher can compute the average height  and grade, and correlate the height and grade of the 216 students.

In this assignment, we will implement a class NumVector that implements the behaviour of a statistical numerical vector.  Usually, a vector has a name, a type ( i.e. numeric or string vector as we'll see later), a size ( number of values in the vector), and the maximum and minimum value allowed in it (for instance, for height, min could be 100cm and max 210cm).

Our NumVector class should provide the following operations:

NumVector( double our_min, double our_max )
an initializer that creates an empty numerical vector, sets its min and max allowed values to be the given values, and the vector's type to be "nv". Later we'll create vectors of other types, so we need a type variable to distinguish the different vectors.

void name( string aname)
sets the vector's name

string name()
returns the name of the vector

string type()
returns the vectors type

double min_limit( )
returns the smallest value allowed in the collection

double max_limit( )
returns the largest value allowed in the collection

int size( )
returns the vector's size (I.e. the number of values in the vector)

bool add( double value )
adds a value to the collection;  returns true if it is successful, false otherwise.

bool valid( double value )
returns true if value is between the vector limits, false otherwise

double get( int i )
returns the i-th value in the vector. i should be between 1 and the size of the vector, otherwise an error value (you decide on this) is returned.

double min()
returns the smallest value in the vector

double max()
returns the largest value in the vector

double mean()
returns the mean of the values stored in the vector

double variance()
returns the variance of the values stored in the vector. It is calculated as
mean of the squares of the vector values minus the square of their mean

void print()
prints all the vector values.

The implementation for the vector we will do for this assignment is the simplest possible. Use a simple array to implement our numerical vector. Your array should have a fixed length, but you should make it easy  for the user to change the size if it is necessary.
0
 
njavaAuthor Commented:
And here is what I did so far:
=================================================
// file: NumVector.h
// NumVector class header Version 1.1


#ifndef NUMVECTOR_H
#define NUMVECTOR_H
#include <string>
using namespace std;

class NumVector
{
      public:

            NumVector();
            //Default constructor
            //PRE: none
            //POST: Creates an empty NumVector

            
            NumVector(double our_min, double our_max);
            // Parameterised constructor
            // PRE: our_max and our_min are the lower and higher values in array
            // POST: Creates an empty NumVector

            void name(string aname, int index);
            // Sets the vector's name
            // PRE: none
            // POST: Vector is assigned a name

            string name(int index);
            // Returns the vector's name
            // PRE: none
            // POST: The vector's name is obtained

            string type();
            // Returns the vector's type
            // PRE: none
            // POST: The vector's type is obtained

            double min_limit();
            // Returns the smallest value allowed in the collection
            // PRE: none
            // POST: The smallest value of the vector is returned

            double max_limit();
            // Returns the size of the vector
            // PRE: none
            // POST: The vector size is displayed

            int size();
            // Returns number of elements in the vector
            // PRE: none
            // POST: number of elements is obtained
      
            bool add(double value);
            // Adds a value to the collection
            // PRE: The value is double
            // POST: Returns true if successful and false if not

            bool valid(double value);
            // Validates the value
            // PRE: The value is double
            // POST: Returns true if value is between the vector limits
            // and false otherwise

            double get(int i);
            // Returns the i-th value in the vector
            // PRE: i should be between 1 and the size of the vector
            // POST: if PRE: is not true, error value 1.0 is returned

            double min();
            // Returns the smallest value in the vector
            // PRE: none
            // POST: The minimum vector value is obtained

            double max();
            // Returns the largest value in the vector
            // PRE: none
            // POST: The maximum vector value is obtained

            double mean();
            // Returns the mean of the values stored in the vector
            // PRE: none
            // POST: The mean of the vector is calculated

            double variance();
            // Returns the variance of the values stored in the vector
            // PRE: none
            // POST: The variance of the vector is calculated

            void print();
            // Prints all the vector values
            // PRE: none
            // POST: The values in each vector are printed

      private:

      //defining capacity of the vector array and minimum and maximum
      //components for grade and height parts
      
            enum {
                        CAPACITY=100,
                        HEIGHT_MIN = 100,
                        HEIGHT_MAX = 210,
                        GRADE_MIN = 0,
                        GRADE_MAX = 100
            };

//defining minimum and maximum values in array
                      double min_value;
            double max_value;      
                        
            double data[CAPACITY];
            string vectorName[CAPACITY];
                      string nv;
};
#endif
=========================================================
// file: NumVector.cpp

#include <string>
#include <cmath>
#include "NumVector.h"

using namespace std;

NumVector::NumVector()
{
      min_value =0;
      max_value =0;

}

NumVector::NumVector(double our_min, double our_max)
{
      min_value = our_min;
      max_value = our_max;
             
}

//I have troubles understanding how to assign a name to the vector. As you can see, the function that I
//wrote is not what was requested.

void NumVector::name(string aname, int index)
{
      vectorName[index]="aname";

}

string NumVector::name(int index)
{
      string vName ="";
      vName = vectorName[index];

      return vName;
}


//Same with discovering the type of the vector. It's unclear how to implement this function.
string NumVector::type()
{
      
      if ((min_value >= HEIGHT_MIN) && (min_value <= HEIGHT_MAX))
      {
            nv ="height";
      }
      else if ((min_value >= GRADE_MIN) && (min_value <= GRADE_MAX))
      {
            nv="grade";
      }
      else
            nv="name";
      
      return nv;
}


double NumVector::min_limit()
{
      
      double minimum=0;

      if ((min_value >= HEIGHT_MIN) && (min_value <= HEIGHT_MAX))
      {
            minimum = 210;
      }
      else if ((min_value >= GRADE_MIN) && (min_value <= GRADE_MAX))
      {
            minimum=100;
      }

      return minimum;

      
}

double NumVector::max_limit()
{
      double maximum=0;

      if ((min_value >= 100) && (min_value <= 210))
      {
            maximum=210;
      }
      else if ((min_value >= 0) && (min_value <= 100))
      {
            maximum=100;
      }

      return maximum;
}



int NumVector::size(){

      return sizeof(data);

}

bool NumVector::add(double value)
{
      int current_index = size();
      bool success=true;
      
      if (sizeof(data) > CAPACITY){
                  return false;      
      }
      
      if ((value >= HEIGHT_MIN) && (value <= HEIGHT_MAX))
      {
                        height[current_index+1]=value;
                        grade[current_index+1]=0;
                        success = true;
      }

      else if ((value >= GRADE_MIN) && (value <= GRADE_MAX))
      {
                        grade[current_index+1]=value;
                        height[current_index+1]=0;
                        success = true;
      }
      else
      {
                        success = false;
      }

      return success;

}

bool NumVector::valid(double value)
{
      bool success = true;
      int i=0;

      if (((value >= HEIGHT_MIN) && (value <= HEIGHT_MAX)) ||
      
            ((value >= GRADE_MIN) && (value <= GRADE_MAX)))
      {
                  success = true;
      }
      else
      {
                  success = false;
      }
      
      return success;

}

double NumVector::get(int i)
{
            if ( (i > 0) && (i < size()) )
            {
                  return height[i];
            }
            else return -1;

}

double NumVector::min()
{
      int i=0;
      double min_value = height[0];

      for (i=0; i< size(); i++)
      {
            if (min_value >= height[i])
                        min_value = height[i];
      }
      return min_value;
}

double NumVector::max()
{
      int i=0;
      double max_value = height[0];

      for (i=0; i< size(); i++)
      {
            if (max_value <= height[i])
                        max_value = height[i];
            
      }
      return max_value;
}

double NumVector::mean()
{
      double mean_value = 0;
      double min_value = min();
      double max_value = max();

      mean_value = (min_value + max_value)/2;
      
      return mean_value;

}

double NumVector::variance()
{
      double square, vector_mean = 0;
      int i=0;
      double mean_value = mean();


      for (i=0; i < size(); i++)
      {
            square+=height[i]*height[i];      
      }

      vector_mean = square/size();

      return vector_mean - (mean_value*mean_value);


}
0
 
esorfCommented:
From the assignment it appears that

1. The name property is for the vector as a whole.  You're not naming each element.  So you don't need an array of names.  Just one for the vector will suffice.

2. The "type" attribute is not being used in this assignment.  Right now its value is hard coded to "nv" for all three vectors you're creating, because they're all Numeric Vectors.  "Later we'll create vectors of other types, so we need a type variable to distinguish the different vectors."

3. CAPACITY is a property of the vector (the maximum values it can hold).  But the other constants you've defined (height min/max and grade min/max) are properties of the application using the NumVector, so they shouldn't be defined within the NumVector.

Good luck with the assignment.
0
 
njavaAuthor Commented:
Thanks so much! I understand the assignment much better :)

I just have one more last question - how to implement  double max_limit() function?

My constructor is

NumVector::NumVector(double our_min, double our_max)
{
      min_value = our_min;
      max_value = our_max;
             
}

should I change it to

NumVector::NumVector(double our_min, double our_max)
{
      min_value = our_min;
      max_value = our_max;

               int collection_max;
int collection_max;       
}
0
 
njavaAuthor Commented:
I meant int collection_min. How would I initialize collection_max and collection_min??

NumVector Height(150, 200);

0
 
njavaAuthor Commented:
I figured it out:

NumVector::NumVector(double our_min, double our_max)
{
     min_value = our_min;
     max_value = our_max;
}

min_value and max_value are the low and high limits. I thought that there will be only two arrays, therefore, I defined hight/max/min and grade/max/min as constants.

Thanks for your help!
0
 
esorfCommented:
You're welcome.  Thanks for the points!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now