gerard4143 371 Nearly a Posting Maven

I was curious so I trolled google a little bit, and chanced upon a solution to your problem:

namespace
{
std::string toString( const std::pair< size_t, size_t >& data)
{
    std::ostringstream str;
    str << data.first << ", " << data.second;
    return str.str();
}
} // namespace anonymous

std::transform( 
    some_map.begin(), 
    some_map.end(), 
    std::ostream_iterator< std::string >( std::cout, "\n" ),
    toString );

Just tried your solution and it works...Thanks everyone.

#include <iostream>
#include <string>
#include <map>
#include <iterator>
#include <sstream>
#include <algorithm>

namespace
{
  std::string toString( const std::pair< std::string, unsigned long >& data)
  {
      std::ostringstream str;
      str << data.first << ", " << data.second;
      return str.str();
  }
}

int main(int argc, char**argv)
{
  std::map<std::string, unsigned long> the_words;
 
  for (int i = 1; i < argc; ++i)
    ++the_words[argv[i]];

  std::transform( the_words.begin(), the_words.end(), std::ostream_iterator< std::string >( std::cout, "\n" ),toString );
 
  return 0;
}

usage/output

./testit this is the test
is, 1
test, 1
the, 1
this, 1

gerard4143 371 Nearly a Posting Maven

You could write a program and test them yourself...Or you could check here for some examples:

http://cplus.about.com/od/learningc/ss/clessontwo_6.htm

gerard4143 371 Nearly a Posting Maven

Before I close this, can anyone tell me if this will compile on different compiler. I know it fails on GCC's g++ but I'm curious to see if it'll succeed on a different one.

gerard4143 371 Nearly a Posting Maven

First thing....Your function call "listPrimeExperiment()" where does it save/pass the address of the linkedlist? What I see is two local variables that don't pass back the addresses of the the linked list....So how can you access these unknown values?

gerard4143 371 Nearly a Posting Maven

Because the server is accepting the character(from the client) and then incrementing it and then sending it back to the client...

Check lines 11 - 13 in the server program(the second part).

gerard4143 371 Nearly a Posting Maven

Try adding this line to your program

while ((ch = cin.get()) != '\n'){}

and read this link to find out what's going on

http://www.daniweb.com/forums/thread90228.html

#include <iostream>
#include <string>
using namespace std;

int main()
{
  string *name;
  // int *votes;
  int i;
  int numVotes;
  char ch;

  cout << "how many voters: ";
  cin >> numVotes;

  name = new string[numVotes];
  
  while ((ch = cin.get()) != '\n'){}
  
  for(i = 0; i < numVotes; i++)
  {
    cout <<"enter canidates last names: ";
    getline(cin,name[i]);
  }

  for(i = 0;i < numVotes; i++)
  {
    cout << name[i] << endl;
  }
  
  return 0;
}
gerard4143 371 Nearly a Posting Maven

& std::ostream_iterator<_Tp, _CharT, _Traits>::operator=(const _Tp&) [with _Tp = std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, long unsigned int>

and that messy looking std::pair is actually how <string, long> is represented in the template hierarchy.

std::ostream_iterator = std::pair<std::string, unsigned long>; <--- is your issue imo

So is this an issue with gnu g++ compiler?

gerard4143 371 Nearly a Posting Maven

Thanks for reply. But it still doesn't work. If I enter the temp, the value for Max is always the last one I enter.

Can we see the most recent version of your code?

gerard4143 371 Nearly a Posting Maven

Here's the compiler errors...The whole lot

g++ testit.cpp -Wall -ansi -pedantic -o testit
In file included from /usr/include/c++/4.4/iterator:67,
from testit.cpp:5:
/usr/include/c++/4.4/bits/stream_iterator.h: In member function ‘std::ostream_iterator<_Tp, _CharT, _Traits>& std::ostream_iterator<_Tp, _CharT, _Traits>::operator=(const _Tp&) [with _Tp = std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, long unsigned int>, _CharT = char, _Traits = std::char_traits<char>]’:
/usr/include/c++/4.4/bits/stl_algobase.h:313: instantiated from ‘static _OI std::__copy_move<<anonymous>, <anonymous>, <template-parameter-1-3> >::__copy_m(_II, _II, _OI) [with _II = std::_Rb_tree_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, long unsigned int> >, _OI = std::ostream_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, long unsigned int>, char, std::char_traits<char> >, bool <anonymous> = false, bool <anonymous> = false, <template-parameter-1-3> = std::bidirectional_iterator_tag]’
/usr/include/c++/4.4/bits/stl_algobase.h:397: instantiated from ‘_OI std::__copy_move_a(_II, _II, _OI) [with bool _IsMove = false, _II = std::_Rb_tree_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, long unsigned int> >, _OI = std::ostream_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, long unsigned int>, char, std::char_traits<char> >]’
/usr/include/c++/4.4/bits/stl_algobase.h:436: instantiated from ‘_OI std::__copy_move_a2(_II, _II, _OI) [with bool _IsMove = false, _II = std::_Rb_tree_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, long unsigned int> >, _OI = std::ostream_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, long unsigned int>, char, std::char_traits<char> >]’
/usr/include/c++/4.4/bits/stl_algobase.h:468: instantiated from ‘_OI std::copy(_II, _II, _OI) [with _II = std::_Rb_tree_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, long unsigned int> >, _OI = std::ostream_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, long unsigned int>, char, std::char_traits<char> >]’
testit.cpp:23: instantiated from here
/usr/include/c++/4.4/bits/stream_iterator.h:191: error: no match for ‘operator<<’ in ‘*((std::ostream_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, long unsigned int>, char, std::char_traits<char> >*)this)->std::ostream_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, long unsigned int>, char, std::char_traits<char> >::_M_stream << __value’
/usr/include/c++/4.4/ostream:108: note: candidates are: std::basic_ostream<_CharT, _Traits>& …

gerard4143 371 Nearly a Posting Maven

I tried your suggestions and the code fails to compile..Here's the update code

#include <iostream>
#include <string>
#include <map>
#include <algorithm>
#include <iterator>

std::ostream& operator<<(std::ostream & out, const std::pair<const std::string, unsigned long> & p)
{
  return out << p.first << " " << p.second;
}

int main(int argc, char**argv)
{
  std::map<std::string, unsigned long> the_words;
  
  for (int i = 1; i < argc; ++i)
    ++the_words[argv[i]];
  
  std::map<std::string, unsigned long>::const_iterator begin = the_words.begin();
  
  std::cout << *begin << std::endl;
  
  copy(the_words.begin(), the_words.end(), std::ostream_iterator< std::pair<const std::string, unsigned long> >(std::cout, "\n"));
  return 0;
}
gerard4143 371 Nearly a Posting Maven

Is it possible to pass a map container to the copy algorithm and display it to the std::cout? If so how?

What I tried below doesn't work but it looks like it should. Any comments or pointers will be appreciated.

#include <iostream>
#include <string>
#include <map>
#include <algorithm>
#include <iterator>

std::ostream& operator<<(std::ostream & out, const std::pair<std::string, unsigned long> & p)
{
  return out << p.first << " " << p.second;
}

int main(int argc, char**argv)
{
  std::map<std::string, unsigned long> the_words;
  
  for (int i = 1; i < argc; ++i)
    ++the_words[argv[i]];
  
  std::map<std::string, unsigned long>::const_iterator begin = the_words.begin();
  
  std::cout << *begin << std::endl;
  
  //copy(the_words.begin(), the_words.end(), std::ostream_iterator< std::pair<std::string, unsigned long> >(std::cout, "\n"));
  return 0;
}
gerard4143 371 Nearly a Posting Maven

Use either

int* p1 = &p0[0];

or

int* p1 = p0;
gerard4143 371 Nearly a Posting Maven

Your function needs a return type.

double sum_funk(n1,n2,n3,n4)
{}

endl is a stream manipulator not a variable.

gerard4143 371 Nearly a Posting Maven

Look at this line

cin>> n1>> n2 >> n3 >> n4;// >> endl;

Notice what I remarked out.

gerard4143 371 Nearly a Posting Maven

O.K...What weird errors. Could you post them?

gerard4143 371 Nearly a Posting Maven

Are you trying to display the character's value as a hexadecimal?

#include <iostream>

int main()
{
  unsigned char myc = 'o';
  
  std::cout << (int)myc << std::endl;
  std::cout << std::hex << (int)myc << std::endl;
  std::cout << std::oct << (int)myc << std::endl;
  
  return 0;
}
gerard4143 371 Nearly a Posting Maven

You need to create a thread to run your sound file.

gerard4143 371 Nearly a Posting Maven

how can i read the object files generated by the c compiler ?

You can either open the files in a hex editor or you can download something to reverse engineer the code and display it as source code.

gerard4143 371 Nearly a Posting Maven

Are you sure you didn't invalidate your iterators when you called erase...Try reinitializing you iterators after your call to erase and see if it displays properly.

gerard4143 371 Nearly a Posting Maven

Here's a pipe program that forks 5 times, with each child adding a row of the array longarray and then communicating the sum back to the parent...

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>

#define ARR_ROW 5
#define ELEMENTS_PER_ROW 3

unsigned long longarray[ARR_ROW][ELEMENTS_PER_ROW] = 	{
								{1, 2, 3},
								{4, 5, 6},
								{7, 8, 9},
								{10, 11, 12},
								{13, 14, 15}
							};

enum PIPES {READ, WRITE};

int main(int argc, char**argv)
{
	unsigned long i = 0, tot = 0; 
	int hpipe[2];
	char ch[10];

	signal(SIGCHLD, SIG_IGN);
	pipe(hpipe);

	for (i = 0; i < ARR_ROW; ++i)
	{
		if (!fork())
		{
			int j = 0; 
			unsigned long ans = 0;
			
			close(hpipe[READ]);
			dup2(hpipe[WRITE], 1);
			close(hpipe[WRITE]);
			
			for (j = 0; j < ELEMENTS_PER_ROW; ++j)
				ans += longarray[i][j];
				
			fprintf(stdout, "%lu\n", ans);

			exit(EXIT_SUCCESS);
		}
	}

	close(hpipe[WRITE]);
	dup2(hpipe[READ], 0);
	close(hpipe[READ]);

	for (i = 0; i < ARR_ROW; ++i)
	{
		fgets(ch, 10, stdin);
		tot += strtol(ch, NULL, 10);
	}

	fprintf(stdout, "parent received a total of->%lu\n", tot);
	exit(EXIT_SUCCESS);
}
gerard4143 371 Nearly a Posting Maven

Try running this code

#include <stdio.h>
#include <stdlib.h>

#define WORDSIZE 35

typedef struct Panvowels
    {
        int num;
        char* firstFive[5];
        int firstFiveSize;
        char* shortest;
	char* longest;
    } PANVOWELS;

int main()
{
    
  PANVOWELS panvowel = {
			    0, 
			    {NULL, NULL, NULL, NULL, NULL}, 
			    0, 
			    NULL, 
			    NULL
			};

  panvowel.shortest = (char*)malloc(WORDSIZE);
  
  if (!panvowel.shortest)
  {
    fputs("allocation failed!\n", stderr);
    exit(EXIT_FAILURE);
  }
  
  panvowel.shortest[0] = 'a';
  panvowel.shortest[1] = '\0';
  
  fprintf(stdout, "ans->%c\n", panvowel.shortest[0]);
  
  return 0;
}

A side note....When you have

typedef char* Word;

and then you have

Word something, something_else;

something is a character pointer while something_else is a character..


char *something, *something_else;

will create two character pointers....

gerard4143 371 Nearly a Posting Maven

Also you should never do batch memory allocations without checking to see if malloc failed..

Try initializing your structure like this.

PANVOWELS panvowel = {
			    0, 
			    {NULL, NULL, NULL, NULL, NULL}, 
			    0, 
			    NULL, 
			    NULL
			};

And then allocate your resources checking to make sure malloc succeeded.

gerard4143 371 Nearly a Posting Maven

For starters I would initialize these like so

panvowel.shortest[0] = '\0';
panvowel.longest[0] = '\0';
gerard4143 371 Nearly a Posting Maven

You are kidding, where is your code tags?

gerard4143 371 Nearly a Posting Maven

There are many ways to accomplish this...Here's a very simple way

#include <iostream>
#include <string>
#include <cctype>

int main()
{
  const char S_CHAR = '\"';
  bool start_char = false;
  
  std::string the_str("I HaVe A CaT caLLed \"SaLly\" AnD DoG called \"BeLLY\"");
  
  std::string::iterator begin = the_str.begin();
  std::string::iterator end = the_str.end();
  
  while (begin != end)
  {
    if (*begin == S_CHAR)
      start_char =! start_char;
    
    if (!start_char)
      *begin = tolower(*begin);
    
    ++begin;
  }
  
  std::cout << the_str << std::endl;

  return 0;
}
gerard4143 371 Nearly a Posting Maven

Posting your assignment will produce nothing...Please post the code that you tried and please indicate where your having problems..

gerard4143 371 Nearly a Posting Maven

When you have

int x[5] = {1,2,3,4,5}; 
int *q = &x[0];

Initially q points to the first element x[0]. When you increment q++ it then points to x[1].

The same can be said for the second for loop.

Initially q points to the first element x[0]. When you add i(i = 1) to it (q + i) it then points to x[1].

gerard4143 371 Nearly a Posting Maven

What's (*q++)++ do?

Well lets look inside the parenthesis at *q++.

This is basically dereferencing the pointer *q and then incrementing the value *q++.
(*q++)++ is incrementing the pointer q.

The second for loop? It does the same thing but uses pointer arithmetic differently to achieve its goals.

Please note - When you add 1 to a pointer you increase its value by one unit of its type. In your case you would add sizeof(int) to your pointer. The compiler is nice enough to do the converting for you, all you have to do is q++ or q + i.

gerard4143 371 Nearly a Posting Maven

I would loop through your string like below

#include <iostream>
#include <string>

int main()
{
  std::string the_string;
  
  std::cout << "enter your number->";
  std::cin >> the_string;
  std::cout << std::endl;
  
  
  for (unsigned int i = 0; i < the_string.size(); ++i)
  {
    //do your comparisons here
    // (the_string[i] == 'I')
    std::cout << the_string[i] << std::endl;
  }
  return 0;
}

I find it cleaner but maybe that's the C in me talking. Also note that my way uses characters in the comparison not strings.

gerard4143 371 Nearly a Posting Maven

Your if statements are wrong.

It should be

if (condition) {statement;}

While you have

if (condition)
if (condition)
if (condition)

Plus a = 0 is an assignment(a now equals 0)....You want

a == 0 which is a comparison and yields true or false.

gerard4143 371 Nearly a Posting Maven

So what do I do if i want to create multiple class files that can be executed to perform different functions?

Here's a very simple example.

testit.cpp(the main program)

#include <iostream>
#include "test2.h"

int main()
{
  myc me(123);
  
  std::cout << me.getitsvalue() << std::endl;
  
  me.setitsvalue(456);
  
  std::cout << me.getitsvalue() << std::endl;
  return 0;
}

test2.cpp(additional file with a class)

class myc
{
public:
  myc();
  myc(unsigned long val);
  ~myc();
  
  unsigned long getitsvalue() const;
  void setitsvalue(unsigned long val);
private:
  unsigned long itsvalue;
};

myc::myc()
:itsvalue(0)
{}

myc::myc(unsigned long val)
:itsvalue(val)
{}

myc::~myc()
{}

unsigned long myc::getitsvalue() const
{
  return itsvalue;
}

void myc::setitsvalue(unsigned long val)
{
  itsvalue = val;
}

test2.h(header file)

class myc
{
public:
  myc();
  myc(unsigned long val);
  ~myc();
  
  unsigned long getitsvalue() const;
  void setitsvalue(unsigned long val);
private:
  unsigned long itsvalue;
};

Now the compiling...I use the GCC compiler.

First create the object files.

g++ -c testit.cpp
g++ -c test2.cpp

Now we create the main executable testit

g++ testit.o test2.o -o testit

gerard4143 371 Nearly a Posting Maven

I'm only guessing here but should you be using some sort of trig functions to get your points...

gerard4143 371 Nearly a Posting Maven

Yes only have one main function.

The main function is a placeholder for the start of execution, you can change the name if you inform the linker of the new name but generally you don't.

gerard4143 371 Nearly a Posting Maven

You don't have a variable to save the day that the max temp occurred on. Plus never and I mean never use a variable in a comparison without initializing it..

int Max;

should be

int Max = 0;/*Or whatever makes sense*/

gerard4143 371 Nearly a Posting Maven

I assume your talking about a system monitor that displays your processes and its resources. To know how and what its displaying you'll have to get a look at its source code.

gerard4143 371 Nearly a Posting Maven

Well I can't declare and initialize in the class constructor or else the vector will only exist for the scope of the constructor(instead of the class). I would still have to declare the object in the class and then either push_back objects after calling reserve (or something similar) in the class constructor.

////////////////////////////////////////////////
This is actually part of a larger problem. I was trying to boil it down to its essence.

I am trying to create a vector of objects. Speed is the issue. It is very fast when I can simply do:

vector[myclass] v1(10);

This creates and initializes a vector of ten myclass objects. The first myclass object is created using the myclass constructor and then the myclass copy constructor is used to fill the remaining 9 elements. It is lightning fast, but it doesn't work when I encapsulate the line within another class.

This does not work either.

vector<int> myVofInt;
myVofInt(10);

Are you referring to a static class member?

gerard4143 371 Nearly a Posting Maven

I would use the class constructor to initialize your vector to the appropriate number of elements.

Jsplinter commented: thank you +1
gerard4143 371 Nearly a Posting Maven
gerard4143 371 Nearly a Posting Maven

yes, what would specific actions would help using the debugger?

You can set break points in your executing process, allowing you to examine where(and why) your process runs away with all the processing power.

gerard4143 371 Nearly a Posting Maven

Have you tried running the program via a debugger?

gerard4143 371 Nearly a Posting Maven

Yes there is an easy way.

gerard4143 371 Nearly a Posting Maven

Your posting your homework questions?

gerard4143 371 Nearly a Posting Maven

Are you talking about binaries? If you are, what do mean view? Do you mean open a binary and view the hex or the original source code.

gerard4143 371 Nearly a Posting Maven

Yeah ive seen this one, but its in c++. I need it in c with pthreads.
I see that they are wraping the code in c++, but its a bit hard to transfer the code to c with the short amount of time that I have.

My apologies, I didn't realize it was C++.

gerard4143 371 Nearly a Posting Maven

Try here

http://www.hlnum.org/english/projects/tools/threadpool/doc.html

It was hidden in the google query "posix thread pool"

gerard4143 371 Nearly a Posting Maven

I'm not really sure what you mean by 'all of a'....

a[0] is equal to 0.
a is the address of the first element or &a[0].

gerard4143 371 Nearly a Posting Maven

Does stuff.txt exist in the same directory as the program? If not use a full path.

gerard4143 371 Nearly a Posting Maven

The C Programming Language...Why is this posted in two sections?

gerard4143 371 Nearly a Posting Maven

If roll is an int then you can set it to 'q' by simply doing this

roll = 'q';

but having one variable for two independent uses can lead to confusing code.

gerard4143 371 Nearly a Posting Maven

A "default" is just a value or action that happens automatically if some other action or value is not specified in its place.

The switch "default case" is just one example. It is probably the only one example that actually uses the keyword "default". It's the case that executes if no other case matches the value of variable used as a selector. But there are other types of "defaults" in different contexts.

Default function parameters:

int someFunc(int aNumber, int defaultParam = 10) {
  /* ... */
}

In this example, you can call the function as either someFunc(int) or as someFunc(int, int). This is permissible because the second argument/parameter has a default value of 10 if no second argument is provided to the function call.

int multiply(int aNumber, int multiplier = 10);

int main() {
  int product1(0), product2(0);

  product1 = multiply(1);      //calls multiply() with default second argument of 10
  product2 = multiply(2, 20);  //calls multiply(), does not use default value of second argument

  return 0;
}

int multiply(int aNumber, int multiplier) {
  return (aNumber * multiplier);
}

In that sample, product 1 will be 10, and product2 will be 40.

Default class constructors:

class sampleClass {
 public:
   sampleClass();   //"default" constructor
   sampleClass(int);//overloaded or "specified" constructor
};

The default constructor allows declarations such as this:

someClass defaultSomeClassObj;        //declares a "default" "someClass" object
someClass specifiedSomeClassObj(2);   //declares a "specified" "someClass" object

Nice list here's another one to add

What are the default values of the fundamental types?