vijayan121 1,152 Posting Virtuoso

u could

std::set<int> unique_ints ;
// N == number of elements
// MAX_VALUE == max value of an element
while( unique_ints.size() < N )
   unique_ints.insert( std::rand()%MAX_VALUE ) ;
//iterate from unique_ints.begin() to 
// unique_ints.end() and populate the array
vijayan121 1,152 Posting Virtuoso

a. you should be able to get a more accurate output by using cout << fixed << setprecision(5).

b. the accuracy of the mantissa of a floating point value on a c++ implementation can be gauged from std::numeric_limits<float>::digits.
rough accuracy for reasonable small values would be within += std::numeric_limits<float>::epsilon()

vijayan121 1,152 Posting Virtuoso

istream::getline (both the two arg and the three arg versions) fill a c-style array with characters read from an input stream. characters are extracted upto a maximum of n-1 (where n is the second arg) or the delimiter (defaults to a newline in the two arg version, the third arg in the three arg version) is encountered. if the delimiter is found, it is read and discarded (ie. the next char read would be the one after the delimiter). an ending null character that signals the end of a c-string is automatically appended.

if the stream encounters an error during input (typically eof), the state of the stream is set to some not good state. getline returns a reference to the stream; so
while(!inf.getline(name, 30, '|').eof())
means 'while the state of the stream is not eof'.
while( inf.getline(name, 30, '|') ) would have been more accurate; this would mean
'while the state of the stream is good'

vijayan121 1,152 Posting Virtuoso

modify the function from

int compare_suitor(suitorData *a, suitorData *b)
{   
    int result;            //result of comparsion 
    if (a->height < b->height) 
    // ...

to

int compare_suitor(const void*aa, const void* bb)
{   
   const suitorData* a = static_cast<const 
suitorData*>(aa) ;
   const suitorData* b = static_cast<const 
suitorData*>(bb) ;
    int result;            //result of comparsion 
    if (a->height < b->height) 
    // ...
vijayan121 1,152 Posting Virtuoso

for aan excellent library for serialization, see
http://www.boost.org/libs/serialization/doc/index.html

vijayan121 1,152 Posting Virtuoso

alternatively,

std::ifstream file("filename") ;
std::istream_iterator<std::string> begin(file), end ;
if( find( begin, end, "Computer" ) != end )
   // found
vijayan121 1,152 Posting Virtuoso

qsort would work with std::vector (if the elements are POD).
qsort( &vec.begin(), ...

vijayan121 1,152 Posting Virtuoso

if there is only one variable length string in the class we could

#include <cstring>
#include <cstdio>
using namespace std;
struct some_struct
{
  static some_struct* construct const char* s )
  {
    int n = strlen(sz) ;
    some_struct* pss = static_cast<some_struct*>(
          new char[sizeof(some_struct)+n] ) ;
    pss->string_sz = n ;
    strcpy( pss->str, s ) ;
    return pss ;
  }
  static void destroy( some_struct* pss ) { delete[] (char*)pss ; }
  // if required, implement these too
  static some_struct* copy_construct( some_struct* pss ) ;
  static some_struct* assign( some_struct* lvalue, const some_struct* rvalue );

  // now we can perform binary i/o as follows
  void write( FILE* file, const some_struct* object )
  {
     fwrite( &object->string_sz, sizeof(object->string_sz), 1, file ) ;
     fwrite( object, sizeof(some_struct)+object->string_sz, 1, file ) ;
  }
  static some_struct* read( FILE* file )
  {
      int n ;
      fread( &n, sizeof(n), 1, file ) ;
      some_struct* pss = static_cast<some_struct*>( new 
                                                                char[sizeof(some_struct)+n] ) ;
      fread( pss, sizeof(some_struct)+n, 1, file ) ;
      return pss ;
  }

  private:
  // other members
  int string_sz ;
  enum { ANY_SIZE = 1 } ; 
  char str[ANY_SIZE] ; // must be the last instance member var.

  // disable value semantics
  some_struct() {}
  some_struct( const some_struct& ) ;
  void operator= ( const some_struct& ) ;
};

however, this is not a good idea. text output is both more transparent
and more portable. (no surprise that xml, soap etc. are increasingly used.)

vijayan121 1,152 Posting Virtuoso

the compare function should have this signature:

int (*)(const void*, const void*)

a. declare the function with the right signature.
b. in the definition, cast the args to const suitorData*, then compare.

vijayan121 1,152 Posting Virtuoso

it is much easier if u use the c++ std library.

#include <iostream>
#include <fstream>
#include <iterator>
#include <map>
#include <algorithm>
#include <cctype>
using namespace std;

struct print_it
{
   inline void operator() ( const pair<char,int>& p ) const
   { cout << p.first << " occurs " << p.second << " times\n" ; }
};

int main()
{
   const char* const file_name = "data.txt" ;
   ifstream file(file_name) ;
   if( !file ) return 1 ;
   const char apostrophe = '\'' ;
   map<char,int> char_cnt ;
   char c ;
   while( (file>>c) && ( isalpha(c) || (c==apostrophe) ) ) ++char_cnt[c] ;
   for_each( char_cnt.begin(), char_cnt.end(), print_it() ) ;
   return 0 ;
}
vijayan121 1,152 Posting Virtuoso
#include <vector>
#include <algorithm>

struct some_struct { int key ; /* ... */ };

struct has_same_key
{
  explicit ( const some_struct& s ) : what(s) {}
  bool operator() ( const some_struct& elem ) const
  { return elem.key == what.key ; }
  const some_struct& what ;
};

inline std::vector<some_struct>::const_iterator 
   do_find( const std::vector<some_struct>& vec, const some_struct& what )
{
  return std::find_if( vec.begin(), vec.end(), has_same_key(what) ) ;
}
vijayan121 1,152 Posting Virtuoso
#include <iostream>
#include <fstream>
#include <map>
#include <algorithm>
using namespace std;

struct print_it
{
  void operator() ( const pair<char,int>& p ) const
  {
      cout << p.first << " occurs " << p.second << " times\n" ;
  }
};

int main( int argc, char** argv )
{
  if( argc != 2 )
  {
     cout << "usage: " << argv[0] << "file_name\n" ;
     return 1 ;
  }
  ifstream file( argv[1] ) ;
  if( !file )
  {
     cout << "could not open file " << argv[1] << '\n' ;
     return 2 ;
  }
  map<char,int> char_count ;
  char c ;
  while( file >> c ) ++char_count[c] ;
  for_each( char_count.begin(), char_count.end(), print_it() ) ;
  return 0 ;
}
vijayan121 1,152 Posting Virtuoso

and first download the windows platform sdk if u already
do not have it. http://www.microsoft.com/downloads/details.aspx?FamilyId=A55B6B43-E24F-4EA3-A93E-40C0EC4F68E5&displaylang=en