mrnutty 761 Senior Poster

One has the value zero and the other one has the value one. That's the only difference. Its how you interpret the values that matters. For example you interpret the return value of zero as failure, or something else like true. Its all up to the programmer.

mrnutty 761 Senior Poster

Why don't you move away from console application, and move into more graphics? Use libraries like SDL to make a simple 2D game. That way you will enjoy what your doing and you will be learning.

mrnutty 761 Senior Poster

Hello, i have been coding for quite a while now. Up until now i have not figured out weather or not to do this.

somthing * newsomthing; 

newsomthing->funtion();

or this

somthing newsomthing;
newsomthing.function();

I know what a pointer is , but often i dont know weather or not to use one or not. I feel that this will turn into a problem one day if now resolved, can anyone tell me the difference? thanks.

Keep it simple. If you don't need to use pointers, that is you can use regular non-pointer type, then use them. That's all there is to it. If for some reason you do need pointers, (ex linked list?) then use them.

mrnutty 761 Senior Poster

The first one is 'using' the second one is 'creating'.

mrnutty 761 Senior Poster

Using midpoint circle algorithm:

#include <stack>
#include <string>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

typedef std::vector<std::string> Canvas;

void setPixel(int x, int y, Canvas& canvas){
	canvas[y][x] = '*';
}
//midpoint circle algorithm -- src via wiki
void renderCircle(const int x0, const int y0, const int radius,Canvas& c){
  int f = 1 - radius;
  int ddF_x = 1;
  int ddF_y = -2 * radius;
  int x = 0;
  int y = radius;
 
  setPixel(x0, y0 + radius, c);
  setPixel(x0, y0 - radius, c);
  setPixel(x0 + radius, y0, c);
  setPixel(x0 - radius, y0, c);
 
  while(x < y)
  {
    // ddF_x == 2 * x + 1;
    // ddF_y == -2 * y;
    // f == x*x + y*y - radius*radius + 2*x - y + 1;
    if(f >= 0) 
    {
      y--;
      ddF_y += 2;
      f += ddF_y;
    }
    x++;
    ddF_x += 2;
    f += ddF_x;    
    setPixel(x0 + x, y0 + y, c);
    setPixel(x0 - x, y0 + y, c);
    setPixel(x0 + x, y0 - y, c);
    setPixel(x0 - x, y0 - y, c);
    setPixel(x0 + y, y0 + x, c);
    setPixel(x0 - y, y0 + x, c);
    setPixel(x0 + y, y0 - x, c);
    setPixel(x0 - y, y0 - x, c);
  }
}

void show(const Canvas& c){
	struct _show{
		void operator()(const std::string& str){ cout << str << endl; }
	};
	std::for_each(c.begin(),c.end(),_show());
}
int main(){
	const unsigned RADIUS = 10;
	Canvas c = Canvas(50,string(50,' ') );
	renderCircle(25,25,RADIUS,c);
	show(c);
 return 0;
}
*******
                    **       **
                   *           *
                  *             * …
mrnutty 761 Senior Poster

Piece of cake, kinda crude approximation tho.

#include <iostream>
#include <cmath>
#include <string>
#include <vector>

using namespace std;

const unsigned CANVAS_WIDTH = 20;
const unsigned CANVAS_HEIGHT = 20;
const unsigned char BACKGROUND_FILL = '#';
const char POINT = '*';
const float PI = 3.14159265f;
const float DEGREE_TO_RADIAN_FACTOR = 0.0174532925f;

typedef std::vector<std::string> Canvas;

float toRadians(const float deg){ return deg * DEGREE_TO_RADIAN_FACTOR;}
int roundPositive(const float n){ return int(n + 0.5f); }

Canvas createDefaultCanvas(){
 return Canvas(CANVAS_HEIGHT, std::string(CANVAS_WIDTH,BACKGROUND_FILL) );
}
void fillCircle(Canvas& canvas,const unsigned radius){

 const int canvasMidX = canvas[0].size() / 2; //assume same width for each height
 const int canvasMidY = canvas.size() / 2;
 const int STEP_SIZE = 1;
 for(int deg = 0; deg < 360; deg += STEP_SIZE){
     float radian = toRadians(deg);
     float x = cos(radian) * radius + canvasMidX;
     float y = sin(radian) * radius + canvasMidY;
     x = roundPositive(x);
     y = roundPositive(y);
     canvas[y][x] = POINT;
 }
}

void showCanvas(const Canvas& canvas){
 for(unsigned height = 0; height < canvas.size(); ++height){
  for(unsigned width = 0; width < canvas[height].size(); ++width){
   cout << canvas[height][width];
  }
  cout << endl;
 }
}
int main(){
 using namespace std;

 const unsigned RADIUS = 5;
 Canvas canvas = createDefaultCanvas();

 fillCircle(canvas,RADIUS);

 showCanvas(canvas);

 return 0;
}

example output :

####################
####################
####################
####################
####################
########*****#######
######***###***#####
######*#######*#####
#####**#######**####
#####*#########*####
#####*#########*####
#####*#########*####
#####**#######**####
######*#######*#####
######***###***#####
########*****#######
####################
####################
####################
####################
mrnutty 761 Senior Poster

You are trying to fit 6 bytes into 2, in general it's not possible, unless the data in bcd are very trivial or you can compress it. What kind of data does bcd hold? It might be possible if the data in bcd are small enough that you can use some compression techniques.

mrnutty 761 Senior Poster

>>>>(because C++ doesn't do JIT in the same way as Java)

>>C++ does not do JIT at all.
I was actually referring to the tendency of some compilers to delay allocation of a variable until it's absolutely needed rather than doing it when the declaration is made.

Are you referring to pointers, or in general all data-types?

mrnutty 761 Senior Poster

How are you storing the 8 teams?

mrnutty 761 Senior Poster

hello am new here.. can somebody tell me if there's a restriction for a decimal numbers entered? for instance,

cout<<"Enter number: "
cin>>num;

--- if the user put a decimal number on the variable num..
i want this to be INVALID INPUT in the terminal screen as a result.

any idea please.. it's c++.. thanks!

before you can use narue's solution your 'num' variable has to of correct type. Because your checking if cin failed means that your num variable has to be of a type that does not support reading in floating numbers, for example if 'num' was of type char then entering in decimal number would fail it. But if it was like a std::string or char array or any floating or integral type then it wouldn't fail.

mrnutty 761 Senior Poster

If you have a uniform function or can make one, then you could map it.

std::map< int , void(*)()> myMap;
const int ON_UPDATE = 0;
const int ON_QUIT = -1;

myMap[ON_UPDATE] = updateHandler;
myMap[ON_QUIT] = quitHandler;

while( ! someCondition ){
 int state = getState();
 myMap[ state ] (); //call function
}
mrnutty 761 Senior Poster

You want to use random_shuffle function,

#include <algorithm>
#include <vector>
#include <string>
#include <iostream>

int main(){
using namespace std;
 const int COLOR_SIZE = 4;
 string colors[COLOR_SIZE ] = { "yellow", "green", "blue" , "red" }
 std::vector<string> colorVec( colors, colors + COLOR_SIZE  );
 //shuffle colors
 std::random_shuffle( colorVec.begin(), colorVec.end() );
 
 //do stuff with colorVec like print it
 for(int i = 0; i < colorVec.size(); ++i){
   cout << colorVec[i] << endl;
 }
}
mrnutty 761 Senior Poster

>>I have 8 teams and I need to find the top 4,

You don't really need to sort because all you care for is the top 4 team. In fact if you were using the correct data structure, you wouldn't need to worry about picking the top 4 teams, for example using graphs. But I assume your doing something easier like using a vector of structures? You can either sort,partition,or simply find the max 4 times.

mrnutty 761 Senior Poster

This isn't much of a challenge as it is now, so I decided to do the interesting stuff during compilation.

EDIT: A boost preprocessor array can't have more than 25 elements. That's why I had to split the letters like that.

#include <boost/preprocessor/repetition/repeat.hpp>
#include <boost/preprocessor/comparison/less.hpp>
#include <boost/preprocessor/arithmetic/add.hpp>
#include <boost/preprocessor/arithmetic/sub.hpp>
#include <boost/preprocessor/arithmetic/div.hpp>
#include <boost/preprocessor/arithmetic/mod.hpp>
#include <boost/preprocessor/seq/for_each.hpp>
#include <boost/preprocessor/array/elem.hpp>
#include <boost/preprocessor/control/if.hpp>

#define LETTERS_a_m (13, ("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m"))
#define LETTERS_n_z (13, ("n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"))
#define LETTERS_A_M (13, ("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M"))
#define LETTERS_N_Z (13, ("N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"))

#define LETTER_LOWER(n) \
BOOST_PP_IF(BOOST_PP_LESS(n, 13), \
    BOOST_PP_ARRAY_ELEM(n, LETTERS_a_m), \
    BOOST_PP_ARRAY_ELEM(BOOST_PP_SUB(n, 13), LETTERS_n_z))

#define LETTER_UPPER(n) \
BOOST_PP_IF(BOOST_PP_LESS(n, 13), \
    BOOST_PP_ARRAY_ELEM(n, LETTERS_A_M), \
    BOOST_PP_ARRAY_ELEM(BOOST_PP_SUB(n, 13), LETTERS_N_Z))

#define LETTER(z, n, letter_id) \
BOOST_PP_IF(BOOST_PP_LESS(letter_id, 27), \
    LETTER_LOWER(BOOST_PP_SUB(letter_id, 1)), \
    LETTER_UPPER(BOOST_PP_SUB(letter_id, 27)))

#define PRINT_LETTER(r, unused, n) \
BOOST_PP_REPEAT(BOOST_PP_ADD(BOOST_PP_DIV(n, 52), 1), LETTER, BOOST_PP_MOD(n, 52)) "\n"

#include <cstdio>

#define INPUT (1)(2)(4)(8)(16)(32)(50)(64)(100)(128)(150)

int main()
{
    const char * output = BOOST_PP_SEQ_FOR_EACH(PRINT_LETTER, ~, INPUT);

    printf(output);
    fflush(stdout);

    return 0;
}

Compiler output:

.file	"main.cpp"
	.def	___main;	.scl	2;	.type	32;	.endef
	.section .rdata,"dr"
LC0:
	.ascii [B]"a\12b\12d\12h\12p\12F\12X\12ll\12VV\12xxx\12TTT\0"[/B]
	.text
.globl _main
	.def	_main;	.scl	2;	.type	32;	.endef
_main:
	pushl	%ebp
	movl	%esp, %ebp
	andl	$-16, %esp
	subl	$16, %esp
	call	___main
	movl	$LC0, (%esp)
	call	_puts
	movl	__imp___iob, %eax
	addl	$32, %eax
	movl	%eax, (%esp)
	call	_fflush
	xorl	%eax, …
mrnutty 761 Senior Poster

The problem can be solved with many ways. I solved it in a more straight way. Every one should help other people more actively, I do not want a grad.

HAHHAHAH FacePalm. He totally didn't get it.

mrnutty 761 Senior Poster

>>This is static polymorphism in C++:
I don't get why that's called static polymorphism? All that's doing is generating multiple function for each type. There is nothing polymorphic about it. Polymorpic is more like a 1->many relationships, while that's like 1->1 relationship, that is each type has its own function. What do you think?

mrnutty 761 Senior Poster

Yes, but it will be a little work

1) Use Java's JNI to talk to C++ program/functions
2) Use phonegap, javascript to talk to JAVA program.
3) Now your application works with android/iphone and Blackberry 6 or higher

mrnutty 761 Senior Poster

Just for exercise, if people want to give it a go; see how creative one can be.

Problem Statement: Given a list of data from a file( or user input which ever) as such:

char 1
char 2
char 26 
char 27
char 52
char 53

output the mapping of the number like so:

char 1 => 'a'
char 2 => 'b'
char 26 = 'z'
char 27 => 'A'
char 52 => 'Z'
char 53 => 'aa'

If you don't see the pattern then here it is:

[1...26] gets mapped to ['a'...'z']
[27...52] gets mapped to ['A'...'Z']

any thing after 52, ex 53 gets wrapped back. For example 53 gets wrapped to 'aa' or 54 gets wrapped to 'bb' or 79 gets wrapped to 'AA'
Free to use any language paradigm....blah...blah...blah, just make sure its understandable.

Difficulty: 2.0f/5.0f

m4ster_r0shi commented: Make more threads like this one! +7
mrnutty 761 Senior Poster
// overloads the equivalence operator which allows two Words to be compared using ==
bool Word::operator==(const Word& rhs) const		
{
	if (_word == rhs._word)			  
		return true;
	else
		return false;
}

That compares another Word with the current Word. The current Word has its own '_word' variables, so it checks if the another Word( rhs )'s '_word' value is the same as the current Word's. In other words, it checks if rhs._word is equal to this->_word

mrnutty 761 Senior Poster

I just looked at your code. It is identical to mine. You are counting the number of BS's and not pushing, whereas I count the number of BS's and "don't discard". What does "don't discard" mean? In your code it means "push onto another stack". I left the definition open to include an array or string.

The problem with your code is you require linelength. But it's a stack. You have no idea what linelength is, and don't need to. Just pop the characters and when the stack is empty, you're done.

What are you talking about? This code,

stack<char> processLine(const stack<char> &stk){
  stack<char> src(reverseStack(stk));
  stack<char> resultStack;
  char prev = -1;  
  while(! src.empty()){
	  char top = src.top();
	  //if no sign of backspace just add it
	  if(top != '-' && top != '<' && prev != '<'){		
		  resultStack.push(top);
	  }
	  //else its either '-' or '<', so do proper checking
	  else{
		  //check if the 2 sequenced character created a backspace? 
		  if(prev == '<' && top == '-'){
			  //backspace detected, so pop top if any
			  if(!resultStack.empty()) resultStack.pop();
		  }
		  prev = top; //update previous		 
	  }
	  src.pop();
  }
  return reverseStack(resultStack);
}

requires no length parameter, and requires no counter for backspace. It just checks if the sequence produced a backspace and if so, then pops the latest result added. In fact here is a live code.

mrnutty 761 Senior Poster

Congratulations! You are certainly a geek! I wish you can do all the things that has to be done so that it will come into reality. (Don't mind those people who gave negative reps, I don't understand them either and thanks for the +1 rep ;-) )

haha thanks, I guess. I'll take it as a compliment.

mrnutty 761 Senior Poster

Of course. If you have multiple BS's, you need to know how many.

Yes but if you reverse the stack, then a counter is not needed as shown by the algorithm I posted in the other thread.

Not following you. Original post states "array stack of characters that reads as follows:
Nn<-<-<-<-No <-w iz<-sthe<-<-<- the time.
" Therefore you have a stack that contains the information. You mention adding. Adding what? To where? The only thing you can do with the stack given is pop. There is no adding. Please explain what is lacking in my algo as a way to answer the original post. All popping. As a stack with data is supposed to do.

In you algorithm, you have something that says in psuedocode, "don't discard the characters". Well then he's gonna have to store it somewhere else, presumably in another stack, assuming he's not allowed to store it in vectors or strings.
So with your algorithm, every time you find a valid character to add to that another stack, you have to check to see if the backspace counter is greater than 0, if so then don't add it to the stack and decrements counter, hence the emphasis is on adding. But if you reverse the stack then every time you encounter a backspace, you can simply just remove the last element added in the result stack, or wherever he stores the "don't discard the characters'" part. You get it?

mrnutty 761 Senior Poster

Maybe someone will correct me, but I believe its just convention. But WinMain does have extra parameter options than int main() which might become useful. But a thing to note is that returning in WinMain really doesn't do much since its returned value is passed to ExitThread instead of ExitProcess.

mrnutty 761 Senior Poster

The problem with FirstPerson's solution to reverse the stack is ... it's a stack. Stacks should not be manipulated in weird ways.

All you need to do is pop the values and you get the data in the proper order to perform the task. A pop gets the last value -- the '.'. The next pop gets '-', then '<'. This is simple. No overhead by reversing, no manipulating the stack in ways that stacks are not meant to be handled. No second or third stacks.

What part of my algorithm didn't make sense?

The problem with that way is that one would need a counter since there could be multiple backspaces, thus when adding new elements, one would use counter to check if it should be added or not. If you reverse the stack, then the emphasis is not on adding, but on popping, that is instead of having to worry about whether to push the next value depending on how many backspaces were encountered, one would simply pop value if there was any.

Anyways, if you are allowed to queue, then thats essentially better than reversing the stack, since reversing the stack essentially transforms the stack into a queue.

mrnutty 761 Senior Poster

I got it now! Thanks so much firstPerson, I don't know why I didn't think of how easier it would be in reverse order haha! We actually had a choice between queues and stacks, and I realize now how much more I'd probably prefer the FIFO structure of queues!

Thanks again, I hope you didn't take too much time to write that example out for me (I know coming up with/writing out that code would have taken me more than an hour alone!).

As a reply to your other post, since that thread was closed, NP. Took about 15-20 minutes. Just a tip, when you try to solve a problem, think about the simplest solution first, and take it step by step.

mrnutty 761 Senior Poster

Okay let me help you with the algorithm, it will be a simple one.


This is you stack content: [N,n,<,-,<,-,<,-,<,-,N,o, ,<,-,w, ,i,z,<,-,s,t,h,e,<,-,<,-,<,-, ,t,h,e, ,t,i,m,e,.]

where as you said the top of the stack is '.' and bottom of the stack is 'N';

Now to make the algorithm easier first you should reverse the stack. Think about how to achieve this.

Now assume the stack is reversed, that is, it is now this:
[.,e,m,i,t, ,e,h,t, ,-,<,-,<,-,<,e,h,t,s,-,<,z,i, ,w,-,<, ,o,N,-,<,-,<,-,<,-,<,n,N]

so now 'N' is the top of the stack, and '.' is the bottom. So as you know, because of the property of stack, you can only access the top of it at a given time. So this should hint you that, the problem will be easier if you used more than one stack.

Hence we create a resultStack. This stack will store the result.

Now we have the preliminary out of the way take a look at the idea. The idea is to save the previous character and use the current character to determine if that sequence made a backspace character. That's really it. Here is the implementation,

stack<char> processLine(const stack<char> &stk){
  stack<char> src(reverseStack(stk));
  stack<char> resultStack;
  char prev = -1;  
  while(! src.empty()){
	  char top = src.top();
	  //if no sign of backspace just add it
	  if(top != '-' && top != '<' && prev != '<'){		
		  resultStack.push(top);
	  }
	  //else its either '-' or '<', so do proper checking
	  else{
		  //check if the …
mrnutty 761 Senior Poster

make sure you have braces, {} , around the return statement

mrnutty 761 Senior Poster

I can change the ints to doubles in the first argument.

In the second argument, I can use int for K's instead of unsigned, but it would be better if I could funnel my unsigned into ints in a pow function to make it easier to deal with.

double pow( double a, unsigned b){
int c = (int) b;
return pow( a, b );
}

is that good?

That function will not do what you want, call it something else like so

double power(const double base, const double exponent){
 return std::pow(base,exponent);
}

Then you can pass your regular datatypes to that functionint res = power(2,4);

mrnutty 761 Senior Poster

can i create a....

double operator^ (double a, double b){
return pow(a,b);
}

and do something similar for integers?

No. I think you are using the wrong language,why not matlab?

mrnutty 761 Senior Poster

>> l<2^(K-1);

thats not correct, if you are looking for the power function, then you need to include cmath:

#include <cmath>
#include <iostream>
int main(){
 using namespace std;
 float res = std::pow(2.0f,4.0f); //2^4 = 16
 cout << res << endl;
}
mrnutty 761 Senior Poster

thank you very much. I have checked the spell many times. it is right.

Post it. Also make sure you add a ';' after the end of class declaration as in,

class Simulation{
 //...
};
mrnutty 761 Senior Poster

Can you post "Simulation.h" ? Make sure you spelled if correctly

mrnutty 761 Senior Poster
class String
{
};

Here is a string class for you. All you need to do is fill in the blank

Actually according to his post, thats all he needed. How dare you give him all that hard working code ;)

mrnutty 761 Senior Poster

Tell us what happened in your presentation yesterday.

I don't know why you got negative reputation for this question, but I +1rep you. Anyways, to answer your question, I presented about the software I was creating and presented a demo and presentation slides. All were impressed, and soon it will be shipped as android/iphone application. Just have to do some more things before that can happen. Thanks for the interest.

mrnutty 761 Senior Poster

You can do this on O(n) using the fact that characters has a number associated with them. Here is live code

mrnutty 761 Senior Poster

Not a clue. Can't read the error message, you don't tell us what line it's on, and your indenting is baaaad.

Give people some break would you. Not all are fluent in english or do they have enough skills to communicate in english. And since he is a beginner, one should expect bad question like this.


@OP, I'm not completely sure why you are doing the things you are doing, but here is a simple fix,

#include "stdafx.h"
#include <iostream>
#include <string>
int printing(const std::string& name);
 
int main()
{
	using namespace std;
	cout << "Enter your name:" << endl; 
        string name;
	cin >> name; 
	printing(name);
	return 0;
}	
 
 
 
int printing(const std::string& userName)
{
	using namespace std;
	cout << "Hello,"<< userName << endl;
		return 0;
}

you need to define, main, which is the entry point of the program;where the program starts. You also need to use std::string if you want the user to input characters.

WaltP commented: So if it was a Spanish board he would have given us more info? It's just harder in English? I don't think so. -4
jingda commented: Waltp is a bit harsh. I see no point that you should get down rep +10
mrnutty 761 Senior Poster
/**
* Note: Use at own risk, this is just for fun
*/
#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
#include <cassert>
#include <ctime>
#include <cmath>


#ifdef __unix__
	#include <unistd.h>
	void clearScreen(){ system("clear"); }
#elif defined _WIN32
  #include <windows.h>
  void clearScreen(){ system("cls"); }
#else 
	void clearScreen(){ std::cout << "\n";}
#endif

const unsigned SCREEN_WIDTH = 40; //screen width
const unsigned SCREEN_HEIGHT = SCREEN_WIDTH; //screen height
const char BACKGROUND = '*'; //what the background of the screen will be filled with
const float ROTATION_SPEED = -15; //rotation speed

typedef std::vector<std::string> ScreenType; 
float toRadians(const float degree){ return degree * 0.0174532925f; }
void initScreen(ScreenType& screen){
	screen = ScreenType(SCREEN_HEIGHT,std::string(SCREEN_HEIGHT,BACKGROUND));
}
void setupScreen(ScreenType& screen, const std::string& model){
	initScreen(screen);
	const unsigned diffSize = SCREEN_WIDTH - model.size();
	const std::string screenRow = model + std::string(diffSize,BACKGROUND);
	screen[0] = screenRow;
}
void transformScreen(ScreenType& screen, const std::string& model, const float rotationInDeg = 0.0f){

	//reset it
	initScreen(screen);

	const float theta = toRadians(rotationInDeg);	
	const unsigned MID_X = SCREEN_WIDTH / 2;
	const unsigned MID_Y = SCREEN_HEIGHT /2;
	const unsigned X_OFFSET = (SCREEN_WIDTH - model.size())/2;
	const unsigned Y_OFFSET = X_OFFSET;

	for(unsigned i = 0; i < model.size(); ++i){
		const float cosTheta = cos(theta);
		const float sinTheta = sin(theta);
		const int x = i;
		const int y = 0;
		float xPrime = x * cosTheta - y * sinTheta;
		float yPrime = x * sinTheta + y * cosTheta;
		int newX = int(ceil(xPrime + X_OFFSET));
		int newY = int(ceil(yPrime + Y_OFFSET));
		screen[newY][newX] = model[i];
	}
}
void displayScreen(const ScreenType& screen){
	struct _displayPixels{
		void operator()(const std::string& …
mrnutty 761 Senior Poster

Waiting for IO uses no processor time. The processor is busy running other processes.

Maybe I'm confused. The clock() function returns an approximation of processor time used by the program. When I/O operation occurs, CPU usually sends the job to a IO processor and proceeds with other jobs until it gets interrupted right? So in that wait period, the CPU was busy doing something else, but there is still that wait period. So why shouldn't for example,

long s = clock();
cin.get();
long e = clock() - s;

'e' be some time unit? I'm not saying that CPU just waits for IO to be done, but I'm saying that there is this IO waiting time, in which although CPU does other useful work.

mrnutty 761 Senior Poster

Is gettimeofday() portable to windows as well? I am using Linux right now, but I will want this game to be portable. It looks like SDL can measure time in milliseconds, so I may end up using that. If gettimeofday() is portable it looks like it may work better.

If you want to use windows, check out queryPerformanceCounter, that's what boost uses if available.

EDIT: Oh I see, why not just use boost?

mrnutty 761 Senior Poster

Yea your right use this,

/**
* Note: Use at own risk, this is just for fun
*/
#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
#include <cassert>
#include <ctime>
#include <cmath>


#ifdef __unix__
	#include <unistd.h>
	void clearScreen(){ system("clear"); }
#elif defined _WIN32
  #include <windows.h>
  void clearScreen(){ system("cls"); }
#else 
	void clearScreen(){ std::cout << "\n";}
#endif

const bool USE_NON_STANDARD_CLS = true; //if false use system("cls") else print new line characters
const unsigned SCREEN_WIDTH = 40; //screen width
const unsigned SCREEN_HEIGHT = SCREEN_WIDTH; //screen height
const char BACKGROUND = '*'; //what the background of the screen will be filled with
const float ROTATION_SPEED = -15; //rotation speed

typedef std::vector<std::string> ScreenType; 
float toRadians(const float degree){ return degree * 0.0174532925f; }
void initScreen(ScreenType& screen){
	screen = ScreenType(SCREEN_HEIGHT,std::string(SCREEN_HEIGHT,BACKGROUND));
}
void setupScreen(ScreenType& screen, const std::string& model){
	initScreen(screen);
	const unsigned diffSize = SCREEN_WIDTH - model.size();
	const std::string screenRow = model + std::string(diffSize,BACKGROUND);
	screen[0] = screenRow;
}
void transformScreen(ScreenType& screen, const std::string& model, const float rotationInDeg = 0.0f){

	//reset it
	initScreen(screen);

	const float theta = toRadians(rotationInDeg);	
	const unsigned MID_X = SCREEN_WIDTH / 2;
	const unsigned MID_Y = SCREEN_HEIGHT /2;
	const unsigned X_OFFSET = (SCREEN_WIDTH - model.size())/2;
	const unsigned Y_OFFSET = X_OFFSET;

	for(unsigned i = 0; i < model.size(); ++i){
		const float cosTheta = cos(theta);
		const float sinTheta = sin(theta);
		const int x = i;
		const int y = 0;
		float xPrime = x * cosTheta - y * sinTheta;
		float yPrime = x * sinTheta + y * cosTheta;
		int newX = int(ceil(xPrime + X_OFFSET));
		int …
mrnutty 761 Senior Poster

http://linux.die.net/man/3/clock
clock() determines (to some approximation) the amount of CPU time used by a program.
Waiting around for I/O typically does not count.

If you want to measure the amount of elapsed time, as by your watch for example, then use the time() functions.

The clock() function returns an approximation of processor time used by the program

Waiting for I/O counts as it constitute to time used by processor for waiting

mrnutty 761 Senior Poster

Meant mostly for windows, uses system("cls") although you can simply set USE_NON_STANDARD_CLS to false and it will use newlines to clear screen. Test it out.

/**
* Note: Use at own risk, this is just for fun
*/
#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
#include <windows.h>
#include <cassert>
#include <ctime>
#include <cmath>


const bool USE_NON_STANDARD_CLS = true; //if false use system("cls") else print new line characters
const unsigned SCREEN_WIDTH = 40; //screen width
const unsigned SCREEN_HEIGHT = SCREEN_WIDTH; //screen height
const char BACKGROUND = ' '; //what the background of the screen will be filled with
const float ROTATION_SPEED = 15; //rotation speed

typedef std::vector<std::string> ScreenType; 

void clearScreen(unsigned n = 0){
	if(USE_NON_STANDARD_CLS){
		system("cls");
	}else{
		while(n--) std::cout << "\n";
	}
}
float toRadians(const float degree){ return degree * 0.0174532925f; }
void initScreen(ScreenType& screen){
	screen = ScreenType(SCREEN_HEIGHT,std::string(SCREEN_HEIGHT,BACKGROUND));
}
void setupScreen(ScreenType& screen, const std::string& model){
	initScreen(screen);
	const unsigned diffSize = SCREEN_WIDTH - model.size();
	const std::string screenRow = model + std::string(diffSize,BACKGROUND);
	screen[0] = screenRow;
}
void transformScreen(ScreenType& screen, const std::string& model, const float rotationInDeg = 0.0f){

	//reset it
	initScreen(screen);

	const float theta = toRadians(rotationInDeg);	
	const unsigned MID_X = SCREEN_WIDTH / 2;
	const unsigned MID_Y = SCREEN_HEIGHT /2;
	const unsigned X_OFFSET = (SCREEN_WIDTH - model.size())/2;
	const unsigned Y_OFFSET = X_OFFSET;

	for(unsigned i = 0; i < model.size(); ++i){
		const float cosTheta = cos(theta);
		const float sinTheta = sin(theta);
		const int x = i;
		const int y = 0;
		float xPrime = x * cosTheta - y * sinTheta;
		float yPrime = x …
mrnutty 761 Senior Poster

Bad naming, time is already defined in ctime

#include <iostream>
#include <ctime>

using namespace std;

int main()
{
	clock_t start = clock();
	cout << "press enter...";
	cin.ignore();
	clock_t finish = clock();
	double doneTime = float(finish-start)/CLOCKS_PER_SEC;
	cout << "you took " << doneTime << " seconds\n";
	return 0;
}
mrnutty 761 Senior Poster

Take the BMP image, copy it into paint. Then save it as JPEG

mrnutty 761 Senior Poster

Ok here is an analogy I head and I will try it out:

Imagine you picked a hotel, and then you bought a room, so they gave you the key to that room. You take out your bible, read it and then put it inside your drawer. Next morning you have to leave because your time is up. You pack everything up and leave, but you left your bible in the drawer and you forgot to hand in your key. Next day you remember that you left your bible in the drawer and you remember you still have the key to that room. Now you know your not allowed to go to that room because your time is up, but you figure, what the hell I'll just see if this key works and try to get my book back. Surprisingly, your key does work and what do you know your book is still in the drawer. Why would anyone remove it anyways? They probably didn't need that space yet, so they just left it as it, which is why your book was still there.

Now use the above analogy to the problem you've shown. You picked a hotel, in your case, its called Pig. You rented a room, in your case its room Jack. Now you have a key to that room, the pointer. You do stuff in that room, in your case call Rof(), and your day has expired, you called delete on Jack, now you come …

mrnutty 761 Senior Poster

I'm presenting to day at my internship in front of the whole company and the president, wish me luck!!!! I'm so nervous.

mrnutty 761 Senior Poster

>>char*** b_array_ptr

You can think this as a pointer to a 2D array of chars. Example will suffice:

#include <iostream>
using namespace std;

int main(){
 char ***ptr = 0;
 //dynamically initialize it
 const int WIDTH = 3;
 const int HEIGHT = 3;
 const int DEPTH = 3;
 
 ptr = new char**[DEPTH];

 //allocate memory
 for(int i = 0; i < DEPTH; ++i){
    ptr[i] = new char*[HEIGHT];
 }
 for(int j = 0; j < DEPTH; ++j){
   for(int k = 0; k < HEIGHT; ++k){
      ptr[j][k] = new char[WIDTH];
    }
  }

 //populate it
 for(int i = 0; i < DEPTH; ++i){
   for(int j = 0; j < HEIGHT; ++j){
     for(int k = 0; k < WIDTH; ++k){
          ptr[i][j][k] = 'A' + (i * j * k) % 27;
     }
   }
 }

  //print it
  for(int i = 0; i < DEPTH; ++i){
   for(int j = 0; j < HEIGHT; ++j){
     for(int k = 0; k < WIDTH; ++k){
          cout << ptr[i][j][k] << " ";
     }
     cout << endl;
    }
   cout << endl;
  }
 
 //deallocate it
  for(int i = 0; i < DEPTH; ++i){
   for(int j = 0; j < HEIGHT; ++j){
     delete [] ptr[i][j];
   }
    delete [] ptr[i];
  }
  delete [] ptr;
  return 0; 
}
fruitymo commented: Thank you for this! I understood the fact that char*** is a pointer to 2D array of chars but I struggled to allocate memory for all 3 dimensions. This comment has been of great help. +0
mrnutty 761 Senior Poster

You can use a date as reference time, and calculate the number of seconds since that that time. You can do this for each data, and do comparison and analysis between dates that way.

mrnutty 761 Senior Poster

@OP:

int userInput = 0;
const int QUIT = 8;
do{
  cin >> userInput;
  switch(userInput){ 
     case ADD: doAdd(); break;
     case SUB: doSub(); break;
     case MULT: doMult(); break;
     case DIV: doMult(); break;
     case FACT: doFact(); break;
     case QUIT: break;
     default: doErrorInput(); break;
  }
 if(userInput != QUIT){ showMenu(); }

}while(userInput != QUIT);
WaltP commented: Thank you for making my post a waste. -4
mrnutty 761 Senior Poster

what happens if we delete a pointer twice..??

The FBI, CIA, and the military comes to your house via dragon and breaths fire onto your soul sending you to hell for eternity to burn, simply because you deleted a pointer twice. So next time be aware.