Narue 5,707 Bad Cop Team Colleague

Pascal's triangle is boring. How about something more fractalish:

#include <iomanip>
#include <iostream>

using namespace std;

int main()
{
  int rows = 16;

  for ( int i = 0; i < rows; i++ ) {
    cout<< setw ( rows - i - 1 ) <<"";

    for ( int j = 0; j <= i; j++ )
      cout<< ( ( ~i & j ) ? ' ' : '*' ) <<' ';

    cout<<'\n';
  }

  cin.get();
}

>Here i am trying to design a custom pascal triangle where the user can
>specify the TOP ELEMENT as well as both the CORNER elements.
That's not very logical. Can you give a few examples?

>Err...
>Please elaborate if possible.
Take note of the ncr function:

#include <iostream>

using namespace std;

double factorial ( double n )
{
  return ( n > 1 ) ? n * factorial ( n - 1 ) : 1;
}

double ncr ( int n, int r )
{
  return factorial ( n ) / ( factorial ( r ) * factorial ( n - r ) );
}

int main()
{
  cout.setf ( ios::fixed, ios::floatfield );
  cout.precision ( 0 );

  for ( int i = 0; i < 15; i++ ) {
    for ( int j = 0; j <= i; j++ )
      cout<< right << ncr ( i, j ) <<' ';

    cout<<'\n';
  }

  cin.get();
}
Narue 5,707 Bad Cop Team Colleague

>What is the Importance and uses of data stuctures in Programming using C/C++ when using it?
That should be intuitively obvious. Data is critical to programming and storing that data in effective ways is equally critical.

>How Stack,Queue,and list works and give example problem that solve stacks
This reeks of homework. As such, I'll fall back on the usual answer of "show me yours and I'll show you mine".

Narue 5,707 Bad Cop Team Colleague

Because you're just swapping addresses, not values. You want to dereference the pointers:

void f( int *a, int *b)
{
  int temp;
  temp=*a;
  *a=*b;
  *b=temp;
}
Narue 5,707 Bad Cop Team Colleague

$ man stat

Narue 5,707 Bad Cop Team Colleague

Both the length and size member functions will give you the length of the actual string. Why are there two functions that do the same thing? That's the effect of taking an existing (pre-standard) class and forcing it to fit within the new standard definition of a container class. There are lots of ways the string class is poorly designed, and this is one of them. ;)

Narue 5,707 Bad Cop Team Colleague

>hey ..d ans is very simple d array shld be declare as array[4] nt as array[3] ..dis is solve it
Not only are your grammar and spelling atrocious, this thread is over two years old. Look at the date of the thread before you post in it, please. And keep in mind that not all of our members speak English fluently, and your silly abbreviations could be terribly confusing.

Narue 5,707 Bad Cop Team Colleague

>Its always priniting output as 16. Why is it like that?
Because that's the actual size of the object. The implementation probably looks something like this:

template <class T, class Traits = char_traits<T>, class Alloc = allocator<T> >
class basic_string {
public:
  // Lots and lots of interface stuff
private:
  size_type size;     // Probably a 32-bit type
  size_type capacity; // Probably a 32-bit type
  _buf_type buffer;
};

Where _buftype is likely defined as something along these lines:

struct _buf_type {
  T *base; // Beginning of buffer
  T *mark; // Helper pointer within buffer
};

The actual size of the string data itself is dynamic, so sizeof won't tell you how much memory has been allocated to the string. You can find that with the capacity member function.

Narue 5,707 Bad Cop Team Colleague

Please don't attach your code like that. Most of us won't go through the effort of downloading an attachment. Your code is short enough to fit easily within the post, as are your homework requirements. There's no need to attach it as a word document.

And no, we won't do your homework for you.

Narue 5,707 Bad Cop Team Colleague

>i have no clue what i'm doing
Obviously.

>It's fairly easy to use a ostringstream object or a derived class to output
>text like a normal stream class like cout.
And it's usually a disaster. The correct way to customize a stream to a different destination is to replace the streambuf with one of your own. However, off the top of my head, that seems like it would be overly complicated in this case, so it's up to you. Oh well, have you considered a manipulator?

#include <iostream>
#include <ostream>
#include <sstream>
#include <string>

class console: public std::ostringstream {
public:
  void Render()
  {
    std::cout<< str();
  }
};

std::ostream& send ( std::ostream& out )
{
  // Evil cast
  ((console&)out).Render();
  return out;
}

int main()
{
  console con;

  con<<"this is a test\n"<<send;
}
Narue 5,707 Bad Cop Team Colleague

A pointer to a member requires three distinct parts. First, you need to create a pointer (qualified by the class whose member it points to):

#include <iostream>

class test {
public:
  int i;
};

int main()
{
  int test::*p;
}

Then you need to assign the member you wish to point to by taking its address (also qualified by the class name):

#include <iostream>

class test {
public:
  int i;
};

int main()
{
  int test::*p;
  p = &test::i;
}

Finally, to actually point to an object, you need an existing object and you need to use a member access operator on it using the dereferenced pointer as the "member":

#include <iostream>

class test {
public:
  int i;
};

int main()
{
  int test::*p;
  p = &test::i;
  test a;
  a.*p = 12345;
}

This covers pointers in pretty good detail, and there's a bit of coverage on pointers to members.

Narue 5,707 Bad Cop Team Colleague

Then post your current code, the file you're using, and tell use what compiler and OS you're using.

Narue 5,707 Bad Cop Team Colleague

My brain hasn't been working to well lately, sorry about that. :p You want "r+b" because the "w" mode always empties the file if it already exists.

Narue 5,707 Bad Cop Team Colleague

The "a" mode of fopen always appends, regardless of any seeking you do afterward. You want to open the file with "w+b" for write/update access in binary. You have to use binary because text doesn't allow arbitrary seeking like you're trying to do.

You may also want to read up on how file streams work in C...

Narue 5,707 Bad Cop Team Colleague

>I need the logic to implement a stack by using two queues.
There's no practical application for this, so we can definitely conclude that it's homework. In that case, what have you tried and what have you learned from it? We're not going to just solve the problem for you so you can get a good grade without earning it.

>Do you mean implement a queue using two stacks?
Maybe, but it could be either. I've seen both as homework assignments.

Narue 5,707 Bad Cop Team Colleague

There's no portable way to do this. What compiler and OS are you using?

Narue 5,707 Bad Cop Team Colleague

I'm betting your header file doesn't have inclusion guards on it:

#ifndef MY_HEADER
#define MY_HEADER

#define LOWFREQ    1
#define HIGHFREQ   30
#define CHANNEL    2
#define MAXSAMPLE  200
 
 
class   WAVELET1  {
private:
        double fTF [CHANNEL][HIGHFREQ-LOWFREQ+1][MAXSAMPLE];
public:
        WAVELET1( void );
        ~WAVELET1( void );
        void testTF( int,double, double *);
        double correlation( int, int, int,int, double, double, double *, double *);
} ;

#endif

Failure to do that will cause problems with most headers if you include them multiple times in your project.

Narue 5,707 Bad Cop Team Colleague

>I merely suggested that it could be a possibility.
It's never a possibility until you've exhausted all other options.

>So using %lf with printf is undefined behaviour?
In C89 it's undefined. In C99 the length modifier is quietly ignored because the committee realized this confusion was a very real problem.

>If %lu is okay why should %lf be not.
Because l is meant for integers, not floating-point.

>It sounds so natural.
Just because you see symmetry doesn't mean there is symmetry. What does %lf mean? Double? No, that's what %f means because varargs arguments use pre-C89 promotion rules. Long double? No, that's what %Lf is for. Why a capital L for long double? Because the committee realized that some moron would use %lf thinking it meant double, pass a double, and cause undefined behavior when printf treated it as a long double.

>The authors who wrote books on C and said %lf is a valid printf format specifier should be shot dead
Yes, but not for misleading you. They should be shot dead for trying to teach a language that they still haven't learned properly.

>Dave Sinkula, Narue, Ancient Dragon, Rashakifol and everyone else
>possesing a much better knowledge on C should be shot dead since they
>did not point this one out to me before
It's not our job to delint your programs. If it bothers you so much that we don't take the time to …

Narue 5,707 Bad Cop Team Colleague

>what's the difference between deep copy and shallow copy or not so deep copy
A shallow copy is copying a pointer. A deep copy is copying the data that the pointer points to. A not so deep copy is a reference count. :D

Narue 5,707 Bad Cop Team Colleague

>You are obviously lacking in receiving since you lack the knowledge of giving.
If I give you code without any effort on your part, I've done nothing but hurt you. I wouldn't expect you to have the foresight to appreciate the difference yet, but if you stick to programming, you'll eventually understand. And at that point I hope you regret being a jerk to all of the people who were trying to help you.

Narue 5,707 Bad Cop Team Colleague

>You do realise that in C++, the main function, when an integer, expects a return value.
As a matter of fact, I'm quite familiar with that rule and all of the details that go along with it. Are you?

>So you shouldn't snip off the "return 0;" part.
In standard C++, if you fall off the end of the main function, 0 is returned implicitly. When I'm writing pre-standard C++, the rules for C89 apply and you're correct. But I'm not, so you're not. I would appreciate not being incorrectly corrected, so you would best do your research before asuming that I've made a mistake.

>If you don't want to return a value in main(), declare main() as void
This proves that you're not yet qualified to correct me on technical details of the C++ standard. void main is:

1) Non-portable on a hosted implementation that explicitly allows it.
2) Undefined on a hosted implementation that does not allow it.

At least one very common implementation doesn't allow void main, so your suggestion is very poor. Even on implementations that allow it, void main buys you *nothing*, especially in standard C++ where it doesn't even save you keystrokes.

>int main(void)
This is also a matter of style. C++ doesn't require empty parameter lists to be designated by void.

>void main() is never ever acceptable
It's perfectly acceptable on a freestanding implementation. The rules we refer to when talking …

bumsfeld commented: That is what I am learning in my courses. +1
Narue 5,707 Bad Cop Team Colleague

>Is there any way , memory leaks can be clogged in c/c++ ...
Knowledge and discipline. If you're aware of how memory leaks can come about in your project and apply a disciplined approach to avoid them, that's all you need.

>Are there any good tutorials on how can we make our code Memory Leak free ??/
Probably, but I don't know of any off hand.

Narue 5,707 Bad Cop Team Colleague

>They both don't work.
Do you go to the doctor and say "I don't feel good", and expect an accurate diagnosis?

Narue 5,707 Bad Cop Team Colleague

>How does compiler impliments intializer lits?
It depends on the compiler.

>How intializer lits improve performence?
There are two steps when you initialize in the body of a constructor. The first step is performed before the body, where the data members are allocated memory and given a default value. The second step is the actual initialization that you ask for. So there's an unnecessary step where the data members are given a default value. An initialization list avoids that step and can make your code faster as a result, especially if copying data is expensive for the type that's being initialized.

>When it is must to use intializer lits?
const and reference data members *must* be initialized in an initialization list because they can't exist without being initialized. Everything else is optional.

>When intializer lits must not be used?
There isn't one.

>What is wrong with this pice of code..
You didn't terminate your class with a semicolon.

Narue 5,707 Bad Cop Team Colleague

Just for fun, I wrote up a quick tutorial for basic assembly. At present, it's only in the form of a PDF document, but I'll eventually get around to adding it to my website.

Evenbit commented: excellent resource +3
William Hemsworth commented: Very nice tutorial, I actually understood it :) +2
NicAx64 commented: nice work Narue , so you are a nasm lover aren't you me too. Keep it up. And also a suggetsion just change that font in the tutorial to courier font , really hard to read the kerning fonts. +1
sergent commented: Really nice! +0
<M/> commented: Random upvote +10
hefaz commented: Thanks but Why you prefer nasm ? Just one reason? +0
Narue 5,707 Bad Cop Team Colleague

>See now wasn't that easy?
Yes, it was much harder to come up with something witty and entertaining without diminishing the potency of my message. It's a shame that so few people are capable of appreciating the effort.

>I don't like satire when I need answers.
Satire is a form of answer for those observant enough to interpret it.

Narue 5,707 Bad Cop Team Colleague

>why are there so many programming languages out there?
[satire]
I was wondering, why are there so many tools out there? I mean, I've learned a bit of hammering, a bit of wrenching and a bit of screwdrivering (which I didn't like) and I was wondering, why others keep making more tools.

I can see screwdrivers having to be around, for getting screws that a knife can't, and hammers for teaching tool use funduhmentals, but if wrenches have shortcomings, why not just add attachments (since its apparently not even standardized).

I know some tools are older, but whats the purpose of making whole new ones? I'm reading a book "Serpentine Belts and You", and it has its own tool called a Serpentine Belt Tension Relief Tool, which apparently is only used to install serpentine belts.

So I do wonder: Why not just standardize the tools and work off those, instead of making so many to learn?
[/satire]

MeSam0804 commented: 100 +0
bguild commented: Excellent +6
Narue 5,707 Bad Cop Team Colleague

>But after printing the last word, it just hangs like as if expecting another string too.
Probably because it's expecting another string. A newline is whitespace too, and unless you're terminating the loop with Ctrl+Z (or a read error occurs), it'll just keep asking you for another string. If you want to simply type the line and have the program "do the right thing", you shouldn't use cin's >> operator. Instead, a combination of getline and stringstream will do what you want:

if ( getline ( std::cin, query ) ) {
  std::stringstream ss ( query );
  std::string token;

  while ( ss>> token )
    std::cout<< token <<'\n';
}
WolfPack commented: ��も�り��������。 +1
Narue 5,707 Bad Cop Team Colleague

>Always use scanf() and printf().
Dangerous overgeneralization.

>They are much, much faster than cin and cout.
Uninformed generalization.

>Also use stl algorithms, like sort, reverse and find, especialy
>if they are member functions of a container class you use.
This is actually good advice, though I would have worded it differently: "Prefer the standard library algorithms over writing your own, and prefer member functions over generic alternatives."

Be careful with how you present advice, because if you aren't careful, you'll cause more bad habits than you're trying to prevent.

Narue 5,707 Bad Cop Team Colleague

Getting the numeric value of a single character is simple:

char lang[2][4] = {"en","fr"};
int i = lang[0][0];

You're confused with the distinction between value and representation. The values of lang[0][0] and i are identical. The only difference is how they're printed, much like octal, decimal, and hexadecimal. The values are the same, but the representation is different. That's why you can do this:

printf ( "%c\n", i ); /* Print i as a character */
printf ( "%d\n", i ); /* Print i as an integer */

>int i = sprintf("%d",ch);
This is not how sprintf works. Until you have a stronger foundation in the idioms of C, you should refrain from guessing and use a good reference.

>int x = atoi(char);
That's broken, and you should know better. atoi takes a null terminated string. If you pass it a single character that doesn't have the value of 0, you invoke undefined behavior. Not to mention that atoi will fail and return 0 if the character is non-numeric, which in my world, 'e' most certainly is.

Here's a nice little guideline for both of you: If you don't know, go find out. In C and C++ there's no margin for error, so a guess is just asking for trouble.

Narue 5,707 Bad Cop Team Colleague

No, you're working with vector objects, so if you want to set the size beforehand, you do it with a constructor:

#include <iostream>
#include <iomanip>
#include <vector>

using namespace std;

int main()
{
  vector<vector<int> > items ( 5, vector<int> ( 5 ) );
  int k = 0;

  for ( int i = 0; i < 5; i++ ) {
    for ( int j = 0; j < 5; j++ )
      items[i][j] = k++;
  }

  for ( int i = 0; i < 5; i++ ) {
    for ( int j = 0; j < 5; j++ )
      cout<< setw ( 3 ) << items[i][j] <<' ';
    cout<<'\n';
  }
}

Alternatively, you can build the vectors dynamically:

#include <iostream>
#include <iomanip>
#include <vector>

using namespace std;

int main()
{
  vector<vector<int> > items;
  int k = 0;

  for ( int i = 0; i < 5; i++ ) {
    items.push_back ( vector<int>() );

    for ( int j = 0; j < 5; j++ )
      items[i].push_back ( k++ );
  }

  for ( int i = 0; i < 5; i++ ) {
    for ( int j = 0; j < 5; j++ )
      cout<< setw ( 3 ) << items[i][j] <<' ';
    cout<<'\n';
  }
}
Narue 5,707 Bad Cop Team Colleague

>People just don't like you here, Narue
I don't blame them. I'm a meanie. ;)

>Maybe Narue would get them too were she a decade younger.
I'm not that much older than Dani, you know.

hollystyles commented: I love you +2
Narue 5,707 Bad Cop Team Colleague

A type cast causes a type conversion, so conversion is the effect and casting is the cause. They're not the same thing because you can have conversions without a cast if an implicit conversion is allowed.

Narue 5,707 Bad Cop Team Colleague

>I guess what I'm looking for is more of a formal feel regarding daniweb.
To the best of my knowledge, Daniweb is professional-informal. We're a close community, but still professional enough to inspire confidence in the help that's given. IMO we've succeeded at that kind of feel.

>Forums should be closed out if the question is answered and the user is satisfied.
Aha, but who is the user? Do you only consider the original poster? What if someone has something important to add? What if the accepted answer was wrong in some subtle way? Sure, after several months of no activity, a thread can be safely closed if someone feels the need, but we don't get enough resurrections to warrant that kind of effort. The way I see it, you're suggesting a strict gestapo closing of threads immediately after the OP is satisfied, even if the accepted answer is incorrect. That flies in the face of the nature of public forums, where if someone makes a mistake, others can jump in and correct that person before any damage is done.

You're also ignoring the potential for interesting and educational tangents. This happens more often than you'd think, and it's usually two or more experts tossing around ideas and debating. By simply following these exchanges, the OP (and anyone else interested) can learn a great deal more than if the thread was closed immediately after a suitable answer was given.

>The unanswered post problem should be …

deonnanicole commented: I don't do the rep thing often, but I thought you deserved it for the responses you had given so far in this thread. :) ~deonnanicole +2
Narue 5,707 Bad Cop Team Colleague

>Hey... i am not here for any arguments....
Neither am I, but if you really want to be a jerk about it I'll be happy to oblige.

>I was just asking for a simple answer
Duh! But the answer isn't simple, nor is it relevant to your current studies, that's the whole point.

>u don't have to reply if u don't want to.....
Fine. The constructor does two things: it allocates memory to an object, and initializes the data members of the object to a predictable state. The latter is best defined by the program, but the former is only possible if the C++ run-time handles it. The reason that constructors do not return a value is because to do so would destroy any chance of using temporary objects without a variable to store them, and since the primary desire for a return value is to indicate success or failure, exceptions are the better solution.

Now, you could trick yourself into thinking that a constructor returns the object that it creates, if you really wanted to, because some of the syntax suggests it:

std::string s = std::string ( "This is a test" );

But that's just syntactic sugar. A more accurate description would be that a constructor evaluates to a temporary object, then that object is assigned to an identifier in the symbol table if an "assignment" is made during the declaration. If a constructor really returned the object that it created, this …

Narue 5,707 Bad Cop Team Colleague

>I want to learn how to write a compiler.
Okay, you need to know the language that the compiler processes inside and out, as well as assembly for all of the machines that the compiler will be run on (assuming you're compiling to machine code, it's easiest to output assembly and then assemble it into machine code). You need to figure out the grammar for the language, then design a parsing scheme. Naturally, any decent compiler will also have extensive error checking abilities so that it can produce useful diagnostic messages. Those tasks should give you a start.

>Any tutorial links?
Not off the top of my head, but you might consider picking up the dragon book as well as something more modern for a fairly thorough introduction.

>And can someone help me with becoming a system programmer.
What system? Systems programming is a very broad category that includes operating systems, networking, and databases. We implement the tools that applications programmers use to build and run applications. So what exactly do you want to do?

ddanbe commented: Thanks for the tip on "something more modern" +8
Narue 5,707 Bad Cop Team Colleague

>I didn't realize NULL wasn't part of the language itself...
It isn't a part of the language, just like cout isn't a part of the language. It's a part of the standard library. NULL is a preprocessor macro, usually defined in C++ as:

#define NULL 0

And you might see a definition in C like this:

#define NULL ( (void*)0 )
Narue 5,707 Bad Cop Team Colleague

>well i didnt realy me to draw it but to simply add a exicting image tot he screen
What do you think drawing is? You want to draw an exciting image to the screen. I recommend you start here. Graphics in C++ are not trivial, and you don't strike me as a seasoned programmer.

>btu i cna do all the basicls stuff
Apparently proofreading is not one of your strong suits. I can't imagine how long it takes you to debug your syntax errors.

>also if c++ dosent have this abbilty
*sigh* C++ can do anything your little mind might dream up. However, it doesn't do everything natively, so you have to find a library that does what you want, or write a library that does what you want.

Narue 5,707 Bad Cop Team Colleague

>cin.ignore( 2, '\n');
Feel free to replace 2 with any arbitrary number. Someone invariably pipes in with something like this, but fails to do it properly. Once again, search the forum for a better solution.

Narue 5,707 Bad Cop Team Colleague

>How can one be assisted by Dave Sinkula
By posting a question. If he's interested, he'll help you. Otherwise, you can just settle for the rest of us who don't "understand C++ very well". :rolleyes:

Narue 5,707 Bad Cop Team Colleague

>How does a file open work (in c, as I'm using C functions)?
fopen returns a pointer to FILE (usually a structure), which is basically the bookkeeper for the low level information required to read and write the file from disk and maintain a buffer to avoid excessive device requests. A very simple example would be:

struct _iobuf {
  int fd;
  int offset;
  char *buff;
  char *curr;
  int bufsize;
  int left;
};

typedef struct _iobuf FILE;

The fd field is the file identifier, the offset field is the number of characters into the file we are, the buff field is the buffer, the curr field is the next character in the buffer, bufsize is the size of the buffer, and left is how many characters are left in the buffer. The left field is needed because the buffer might not be completely filled, such as if a read error occurs or end-of-file is reached.

Say you open a file with the contents "This is a test" and the call fopen ( "somefile", "r" ). If we use a buffer size of 6, the contents of the FILE object would be (leaving out fd and bufsize because they don't change):

offset = 0
buff = "This i"
curr = "This i"
left = 6

If you read a single character from the file using fgetc, the resulting object would be:

offset = 1
buff = "This i"
curr = "his …

Drowzee commented: Excellent answer to my question; very helpful to my understanding of C file I/O +1
Narue 5,707 Bad Cop Team Colleague

>u asking me if i cant code why should i be asking about sockets?
No, I was asking you what you know so we don't waste time going over things that we don't need to. But if you want to be a dick about it, just say the word and I'll ignore you so that I can do more interesting things.

>"yes i can"
This answer is worthless. So, since you seem incapable of giving me enough information to properly help you, I'll require that you post a complete program that shows an honest attempt to solve the problem.

jephthah commented: you are the wind beneath my wings +9
Narue 5,707 Bad Cop Team Colleague

>i don't sit here flicking the capslock button
Shift happens.

Troy commented: Excellent stuff. Right on target. +1
zeroth commented: is that a pun? anyway, a good one... +1
Narue 5,707 Bad Cop Team Colleague

For any user-defined type, you can overload operator<< for output to ostreams:

ostream& operator<<(ostream& out, const Relation& r)
{
  return out<< r.toString();
}

Then all you need to do is define Relation::toString to return a string in the format that you want.

Pretty simple really, and overloading operators can be a powerful and flexible tool.

Narue 5,707 Bad Cop Team Colleague

>Is there a "neater" way to code the following?

System.out.println ( "All the values in the array are: " );
for ( T x: array1 )
  System.out.println ( x );

Change T to whatever the type of the array is. Or, if you aren't up to the latest version of Java:

System.out.println ( "All the values in the array are: " );
for ( int i = 0; i < array1.length; i++ )
  System.out.println ( array1[i] );
rotten69 commented: elegent way! +0
Narue 5,707 Bad Cop Team Colleague

Change this:

int numb, tot, count, sum;

to this:

int numb, tot, count, sum = 0;

sum was uninitialized for the first run, but after the first run you set it to 0, so the second and third runs work properly.

Comatose commented: You Rock ;) +1
Narue 5,707 Bad Cop Team Colleague

You need to define an operator>> that looks for an object of your enum:

#include <iostream>
#include <stdexcept>

using namespace std;

enum X { A, B, C };

istream& operator>> ( istream& in, X& x )
{
  int val;

  if ( in>> val ) {
    switch ( val ) {
    case A: case B: case C:
      x = X(val); break;
    default:
      throw out_of_range ( "Invalid value for type X" );
    }
  }

  return in;
}

int main()
{
  X x;

  try {
    cin>> x;
    cout<< x <<endl;
  } catch ( out_of_range& ex ) {
    cerr<< ex.what() <<endl;
  }
}
Narue 5,707 Bad Cop Team Colleague

It means that everyone hates you too much to give you good rep, but you're too leet for them to give you bad rep. Then again, that could just be me. :cheesy:

Catweazle commented: Comment definitely deserved credit. :) +7
Narue 5,707 Bad Cop Team Colleague

>So, since I didn't crown you, you think I'm insulting you, so you snap on me?
I know you weren't insulting me, but others might not be so nice about it, so I felt the need to warn you that such a reply might get flames in return. I don't need or want you to "crown" me, so get off your high horse.

>Thanks anyway means the exact solution I was looking for was not provided
Now you have another definition, consider yourself enlightened.

>Excellent moderation.
This has nothing to do with moderation. Believe it or not, my primary function here is to help people with their problems. I helped you, you responded with a statement that could be interpreted as insulting, and I informed you of that interpretation so you could avoid it in the future. If I moderate you, rest assured that there will be no doubt about it.

By the way, since this thread has turned into you bitching at me and me bitching at you, I'm going to moderate it by locking the thread. If you feel this lock has been added in error, please send me a private message with valid reasons for unlocking it.

alc6379 commented: nice job on handling that thread --alex +5
Narue 5,707 Bad Cop Team Colleague

This assumes you're sorting by last name:

#include <cstring>
#include <iostream>

using namespace std;

void selectionSort ( char names[][17], int n );
int binarySearch ( char name[], char names[][17], int n );

char names[20][17]= {
  "Collins, Bill", 
  "Smith, Bart", 
  "Allen, Jim",
  "Griffin, Jim", 
  "Stamey, Marty", 
  "Rose, Geri",
  "Taylor, Terri", 
  "Johnson, Jill", 
  "Allison, Jeff",
  "Looney, Joe", 
  "Wolfe, Bill", 
  "James, Jean", 
  "Weaver, Jim",
  "Pore, Bob", 
  "Rutherford, Greg", 
  "Javens, Renee",
  "Harrison, Rose", 
  "Setzer, Cathy", 
  "Pike, Gordon",
  "Holland, Beth"
};

int main()
{
  cout<<"Unsorted:"<<endl;
  for ( int i = 0; i < 20; i++ )
    cout<< names[i] <<'\n';
  cout<<endl;

  selectionSort ( names, 20 );

  cout<<"Sorted:"<<endl;
  for ( int i = 0; i < 20; i++ )
    cout<< names[i] <<'\n';
  cout<<endl;

  cout<<"Find Allen, Jim: ";
  cout<< binarySearch ( "Allen, Jim", names, 20 ) <<endl;

  cout<<"Find Wolfe, Bill: ";
  cout<< binarySearch ( "Wolfe, Bill", names, 20 ) <<endl;

  cout<<"Find James, Jean: ";
  cout<< binarySearch ( "James, Jean", names, 20 ) <<endl;

  cin.get();
}

void selectionSort ( char names[][17], int n )
{
  // Fix for last name sort
  for ( int i = 0; i < n; i++ )
    names[i][strcspn ( names[i], "," )] = '\0';

  // Sort
  for ( int i = 0; i < n - 1; i++ ) {
    char save[17];
    int min = i;

    for (int j = i + 1; j < n; j++) {
      if ( strcmp ( names[j], names[min] ) < 0 )
        min = j;
    }

    memcpy ( save, names[i], 17 );
    memcpy ( names[i], names[min], 17 …
SpS commented: Amazing ~ SpS +3
Narue 5,707 Bad Cop Team Colleague

Unless they sign their message, no.