Skeen 0 Junior Poster in Training

>> the flamethrower works differently then the gun

Does it?

The flamethrower shoots and so does the gun.
The flamethrower has some mass and so does the gun
The flamethrower can run empty and so does the gun.

The point here is that, they do differ internally, but not so much
externally. Thus a solution to your problem is to create a hierarchy
with virtual and pure virtual functions, as pointed out already, while
making the have the same functions like shoot() or destroy(), or whatever.

You are absolutely right, alle weapons got;

  • Shoot
  • Reload
  • ReloadTime
  • IsRealoading
  • Fireing Speed
  • ect.

However, the flamethrower works in a totally differentmanner, ie. calling my particle engine, while my MachineGunClass, simply calls a glPoint.

Just to be sure before, I'm marking this as solved, then I would just make a class like this, right?

class WeaponMasterClass
{
virtual Shoot();
virtual Reload();
virtual BulletsLeftInClip();
virtual IsReloading();
// virtual vars I need, ect.
};

And then calling this, with it set equal to some other weaponobject, lets say the flamethrower, would make it use the flamethrowers shoot, instead, since it's virtual?

P.S.: Virtual, vs. Pure Virtual?

Skeen 0 Junior Poster in Training

yeah your setting a pointer equal to another. classes don't need an operator=() function for that its handled by the compiler.

Fantastic, just wondered, could be, that I had to overload the = operator for each class I wanted to input to my "WeaponMasterClass", however I guess I doesn't then, and thats just super! :)

Skeen 0 Junior Poster in Training

Will setting these equal to each worker workout without operator overloading?

Skeen 0 Junior Poster in Training

Smarty, a simple workaround using inherence, well guess it could work, but kinda hoped, for the ability to do something like:

(reinterpret_cast<FlameThrowerClass*>(CurrentWeapon));
// or (reinterpret_cast<MachineGunClass*>(CurrentWeapon));

//Setting the CurrentWeapon to the address of the specific class and then:

CurrentWeapon->Shoot();

Will use your solution as a backup, if noone helps me out, in the manner i wants, (+rep 4 u)

Skeen 0 Junior Poster in Training

Okay, I'm making a shooter game, and all my weapons are differnt userdefined objects; ie. the flamethrower works differently then the gun, however, I'm calling some functions in my "main", an example of this would be;

/* <Init> */
MachineGunClass *CurrentWeapon;
MachineGunClass *MachineGun;
MachineGun = new MachineGunClass();

CurrentWeapon = MachineGun;
/* </Init> */

/* <Usage> */
/* Lots of code */
CurrentWeapon->Shoot();
/*More code */
/* </Usage> */

So this works as it should, however let's say I wanted to use my flamethrower, to change to that weapon I would call the following;

FlameThrowerClass *FlameThrower;
FlameThrower = new FlameThrowerClass();
CurrentWeapon = FlameThrower;

However this ofc, wont work, but wouldn't it work if I changed CurrentWeapon to a void pointer, and then typecasted that, on each weapon change? - Wouldn't that be possible, and really is there any good guides on typecasting?

Thanks in advance!

Skeen 0 Junior Poster in Training

Yeah, I've been through that myself. I've never found a way around it.

Google Code is able to do it, however Google itself, ignores punctation, as a rule of thumb!
http://www.google.com/codesearch?hl=en&sa=N&q=%22-%3E%22++lang:c&ct=rr&cs_r=lang:c

Skeen 0 Junior Poster in Training

... what im trying to do is to make a 2nd box or sprite that will follow the frist box ive made ...

What do you mean by follow, like, should the second box, be following the first one, on movement, because you haven't implementated that yet? - Or do you want it to follow it, in terms of aligning? - I'm sorry, I just need to understand the problem, before I'm able to help :)

Skeen 0 Junior Poster in Training

Thread management takes cpu cycles...

True, but equal to 3CPU cores out of 4?

Skeen 0 Junior Poster in Training

So I've got this program I made, and I wanted to speed it up, using multithreading, to make use of my quad core. The program itself fully supports multithreading. The problem is, that when I'm running the program, using just one; CreateThread() it finishes in 6seconds, if I run 2 it finishes in about 12, and with the same result, why would single threading be faster? - The timer is started after the thread creation, so it isn't the extra overhead!

Please help me out, my brain is hurting >.<

Could it be the Windows API threads?

Skeen 0 Junior Poster in Training

So I've been working on two small programs to calculate prime numbers, fast and memory efficiently. Both my algorithms are based upon the 'Sieve of Eratosthenes', and I know that the 'Sieve of Atkin' would be the way to go since its faster, however I'm just trying to optimize the one I got, so currently, I've got 2 different codes;

  • The first one, is using quite some memory (about 30mb when calculating 3-10.000.000);
  • The another one, which is using a lot less memory (about 3mb with the same domain), however the last one is a lot slower (about x10), so it's 10x smaller in memory

First question, which one would you prefer?

I'm somewhat interested in fixing the slower one (due to the smaller memory usage), and really, it's quite buggy;

  • First bug: It only allows me define up to 10.000.000, if I go higher it just crashes? - why? (limitation in bitset?)
  • Second bug: Why is it slower? (and how to make it faster)
  • Third bug: This algorithm (bitset one), got all numbers from 0-max, however the original one, only builds a buffer with the odd ones from 3-max, does anyone see how I'm able to fix the bitset to only add the odd ones? (without really screwing over the logic (or by doing so), as that would reduce the memory needs by 50% :))

Anyone able to help or something? - Code posted below.

Original (fast, somewhat high memory usage)

Skeen 0 Junior Poster in Training

So I'm working on this class, which is basically to wrap a char into 8bools, and it works so far, however I'm running into a problem, in the dedication of values, since I'm to use the overloaded array operator, as well as the overloaded assignment operator, at once, however I can't really find the way to do this; this is my code:

Main.cpp

#include "Boolean.h"
#include <iostream>
using namespace std;


int main(int argc, char *argv[])
{
    Boolean CharBool(0);
    
    bool alfa;
    for (int x=7; x>=0; x--)
        {
        alfa = CharBool[x];
        cout << alfa;
        }
    cout << endl;
    
    alfa = true;
    CharBool = alfa; // Missing the possiblility to do CharBool[Number] = bool
                     // and the ability to do; CharBool[Number] = true (or false for that matter).

    for (int x=7; x>=0; x--)
        {
        alfa = CharBool[x];
        cout << alfa;
        }
    
    cin.get();
    return 0;
}

Boolean.h

#ifndef BOOLEAN_H
#define BOOLEAN_H

class Boolean
{
public:
Boolean();
Boolean(char PresetValue);
~Boolean();

bool operator[] (char nIndex);
void operator= (bool &b);

private:
char *Bool;

protected:      
};

#endif

Boolean.cpp

#include "Boolean.h"

Boolean::Boolean()
{
Bool = new char;  
*Bool = 0;                
}

Boolean::Boolean(char PresetValue)
{
Bool = new char;  
*Bool = PresetValue;                
}

Boolean::~Boolean()
{
delete Bool;                  
}

void Boolean::operator=(bool &b) // This needs to understand the [] overloading, how is that possible?
{
*Bool = b;
}

bool Boolean::operator[] (char nIndex)
{
if (nIndex>7){/*SENT ERROR MESSEGE*/return 0;}
return ((*Bool >> nIndex) & 1);
}
Skeen 0 Junior Poster in Training

So it works, if I fix the resolution into a byte like value, like 128, or 256, ect.

However it still seems like it memory leaks

Skeen 0 Junior Poster in Training

So I'm working with OpenGL, and DevIL to load some JPEG into my program, and I've had some trouble, I've isolated the program in a simple sample program (uploaded), the program is, that when I load the standard file, that came with the guide ("Geeks3D.jpg") it loades instantly and runs with a CPU usage of 0%, however if I load my own file, I'm getting like a heavy lag, before it loads, and then 100% CPU usage.. My question is, WHY!?

I've uploaded the source code, to change between using: "Geeks3D.jpg" and "Test.jpg", simply comment out "#define _PICTUREONE", in "cdemo.cpp".

The project is made in DevC++, and if you're wanting to use help me out, and only got VC laying around, simply compile with;

freeglut (glut will do)
glu32 
opengl32 
winmm 
gdi32

devil
ilut
ilu

Any help, or idea is welcome, kinda hoping to get this to work soon! :)

Skeen 0 Junior Poster in Training

So I've been testing these algorithms, and really, they're SO slow >.<, I myself made a prime number calculator, when I needed to regenerate prime numbers for some RSA encryption, however, I used this program; It's able to find all the prime numbers from 3-50.000.000 in about 2½second.

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

#define TYPE register unsigned int

int main()
{
    TYPE n, p=1, i=0, j, count=1;
    register bool done;
    register clock_t Time;

    printf("250.000.000 = ~500mb ram..\n\n   Calculate primes up to: ");
    scanf("%d", &n);
    printf("\nCalculating...\n");
    n /= 2;       
    
    TYPE *buffer = (unsigned int*)malloc(n * sizeof(unsigned int));  
    if (buffer == 0)
    {
        printf("Could not allocate RAM!\n\n");
        return 0;
    }
    Time = clock();
    
    for (i=0; i != n; i++)
        {
        buffer[i] = i*2+3;
        }
        
    i = 0;
    
    while (1)
    {
        done = true;
        for (j=i; j < n; j++)
        {
            if (buffer[j] > p)
            {
                p = buffer[j];
                done = false;
                break;
            }
        }
    i = j+1;
    if (!done)
       {
       while (j+p < n) buffer[j+=p] = 0;
       } 
    else 
       {
       break;
       }         
    }
    
    for (unsigned int i=0; i != n; i++)
    {
        if (buffer[i])
        {
            count++;
        }
    }
    
    printf("\n...Calculation done :)\n %d Primenumbers found in %1.3f seconds!\n", count, (clock()-Time)/(float)CLOCKS_PER_SEC);
    free(buffer);
    
    return 0;
}

And it's just a based upon the simple (and slow) Sieve of Eratosthenes algorithm! If you're to write anything useful, you should use the Sieve of Atkin algorithm!

Skeen 0 Junior Poster in Training

Project Uploaded:

(if anyone wants to play with it)

Skeen 0 Junior Poster in Training

hey how to clear the screen in c++ i m using dev c++. clrscr()doesnot work

You could try;

#include <windows.h>

System("CLS"); // Not a good way to do it however, but gets the job done

Skeen 0 Junior Poster in Training

So I'm working on this game, and I've been using "glaux.h" to load my textures, however I'm interrested in using JPEG's, so I found an example to do this on: www.morrowland.com, however, when I compile it (after fixing the errors), then I get this, on the linking state, any help to get?

(Don't know if I'm supposed to go to the DEVc++ forums, og the GCG?)

variable '_iob' can't be auto-imported. Please read the documentation for ld's --enable-auto-import for details. 
  .drectve `-defaultlib:uuid.lib ' unrecognized 
  .drectve `-defaultlib:uuid.lib ' unrecognized 
  .drectve `-defaultlib:LIBC ' unrecognized 
  .drectve `-defaultlib:OLDNAMES ' unrecognized 
  .drectve `-defaultlib:uuid.lib ' unrecognized 
  .drectve `-defaultlib:uuid.lib ' unrecognized 
  .drectve `-defaultlib:LIBC ' unrecognized 
  .drectve `-defaultlib:OLDNAMES ' unrecognized 
  .drectve `-defaultlib:uuid.lib ' unrecognized 
  .drectve `-defaultlib:uuid.lib ' unrecognized 
... For all eternaty
  resolving __iob by linking to __imp___iob (auto-import) 
... More .drectve
then:
  [Linker error] undefined reference to `_nm___iob' 
  ld returned 1 exit status

Easy Fixable, or should I simply change and use OpenIL (DevIL)?

Skeen 0 Junior Poster in Training

in
Windows Task Manager --> Processes
there is a list of running exe files.
I am going to write a program that lists these running exe files.
How can I obtain the list?
I use Windows XP :$
& Windows API!:)

Something like this? - Just an old program I had laying around. Needs the "psapi.h", "psapi.lib" and "psapi.dll".

int FindPIDs()
{
DWORD ProcessesIDs[1000], cbNeeded, cProcesses; 
unsigned int i;
//The default of <unknown> is given so that if GetModuleBaseName does not return 
//the base name of the module then <unknown> will be printed instead of the base name.
 
TCHAR szProcessName[50] = TEXT("<unknown>");
 

EnumProcesses( ProcessesIDs, sizeof(ProcessesIDs), &cbNeeded ) ;
 
// Calculate how many process identifiers were returned.
cProcesses = cbNeeded / sizeof(DWORD);
 
// This for loop will be enumerating each process.
for ( i = 0; i < cProcesses; i++ )
{
// Get a handle to the process. The process to which the handle will be returned //will depend on the variable i.
    HANDLE hProcess =OpenProcess( PROCESS_QUERY_INFORMATION |PROCESS_VM_READ, FALSE, ProcessesIDs[i]);
 
    // Get the process name.
    if (NULL != hProcess )
        {
        GetModuleBaseName( hProcess, NULL, szProcessName, sizeof(szProcessName)/sizeof(TCHAR) );
        }
 
    // Print the process name and identifier.
    cout << "Processname= " << szProcessName << endl;
    cout << "PID = " << ProcessesIDs[i];
    cout << endl << endl;
    
    //Every handel is to be closed after its use is over.
    CloseHandle( hProcess);
    //End of for loop.
    }
    
    return 0;
}

Usefull in anyway?

Skeen 0 Junior Poster in Training

So I'm quite new to c++, and I'm really into pointers, does anyone got like, an idea on how to really get to know pointers, assignments, tasks, tutorials, ect.

Basically, my knowledge in C++, is classes, operator overloading, and like simple stuff, so if you're able to come up with something simple, and then something harder. (I primarily worked with OpenGL, and physics).

Let loose

Skeen 0 Junior Poster in Training

>I've tried stuff like srand(time) in main, however, that doesn't apply to the class
And if you do it in, say, the constructor, what do you expect will happen when you create two objects of that class? The seed is global. Anytime you call srand, the seed will change for every call to rand, regardless of what it applies to in your program's logic.

Depending on your needs, you might want to do one seed at the beginning of main, or you might want to reseed every time an object is created, or you might want to give users of the class the option to reseed at will. But saying that srand should apply to the class is likely to surprise you down the road when it doesn't really apply just to your class.

Uhm.. Thanks for making me realize that srand is global, somehow it just seems like the pattern were about the same. The output is graphical with OpenGL, so must have like, misschecked it. Thanks. Will mark thread as solved.

Skeen 0 Junior Poster in Training

Okay, I'm making a game, and I need to use random inside a class, however I can't figure out how to seed the randomness into the class, so atm I'm getting the same random each time. I've tried stuff like srand(time) in main, however, that doesn't apply to the class, so how am I going to seed it in the class, call srand inside the constructor?

Skeen 0 Junior Poster in Training

What you are already doing should be ok. A semiphore is just another way of doing it.

Okay, like, wouldn't a semiphore be more efficient, and how does a semiphore like work?

Skeen 0 Junior Poster in Training

If you intend to modify Andy's contents from different threads then you need to synchronize the threads so that access to Andy is limited to one thread at a time. The most common way is via semiphores.

For platform independece -- you might check out the Boost libraries to see if it contains something to create and synchronize threads.

Uhm, about the access to Andy, then I've got that covered using handles, and like the WaitforMultipleObjects(), and SingleObjects, in like a mutex queue, can you tell me why a semiphore would be better/worse? - Just interested in optimizing a bit.

About boost, how's boost vs. pthreads?

Skeen 0 Junior Poster in Training

I am trying to write a program to find nearest prime to a number to its left...using for loop
i just don't seem to get it right

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n1,flag=0;
cout<<"Enter The Number"<<endl;
cin>>n1;
for(int i=n1;i>0;i--)
{
for(int j=2;j<(i/2);j++)
{
if(i%j==0)
{
flag=0;
}
else if(flag=1)
{
cout<<i<<endl;
}
}
}
getch();
}

^^first of all, stick to "INT MAIN()" instead of void main, besides, try using logically named variables and incprporation what Sky said, and you'll have something like this:

#include<iostream>
#include<conio.h>
using namespace std;

int main()
   {
   int SeachNumber=0, TestNumber=1, ModuloNumber=0;
   bool IsPrime=1;

   cout << "Enter The Number" << endl;
   cin >> SeachNumber;
   
   for (int a=1; a<SeachNumber; a++)
      {
      TestNumber++;
      IsPrime=true;
      for (ModuloNumber=2; ModuloNumber<TestNumber; ModuloNumber++)
          {if ((TestNumber%ModuloNumber)==0){IsPrime=false;}} 
      
      if(IsPrime==true)
          {
          cout << a << ". " << "Prime:\t"<< TestNumber << endl; 
          }
        }
   system("PAUSE");
   }

However, if you're interrested in finding like, prime numbers above 100mil, try incorporating "The Sieve of Eratosthenes":
http://upload.wikimedia.org/wikipedia/commons/c/c8/Animation_Sieve_of_Eratosth-2.gif

Skeen 0 Junior Poster in Training

I'm making this snake game, and I've created a class to handle the snakes (players and AI snakes), this works out quite well, however, at the moment I'm declaring my main snake (player 1), as global, in terms of:

class Snake
   {
    //... Lots and lots of code.
   }
Snake Andy;

^^ this works out quite fine, however I'm interested in being able to call Andy in the running of the program, but still as a global variable, however I still need Andy to be global, since I've need to access him from a lot of different threads and functions. I've got about 10 threads running in real-time, and about half of them, are using Andy, so somehow pointing to Andy, and sending the pointer as argument, doesn't really satisfy me.

The reason I'm interested in calling Andy in the game code, is to be able to like send him some values doing the constructor, and to be able to declare my snakes on the fly, when they're introduced in the game, rather then having them created and being like, on 'standby'.

Beside of this, then I've got the following lines of code, to switch between full screen and windowed mode:

if (key == GLUT_KEY_F1)     // Toggle FullScreen
	{
	fullscreen = !fullscreen; // Toggle FullScreenBool
	if (fullscreen)
	    {
            glutGameModeString( "1280x800:32@60" );
            glutEnterGameMode();
            }
        else
	    {
	    glutLeaveGameMode();
            }
	}

^^however this seems to somehow 'crash', well the game keeps running, however I'm not entering or leaving fullscreen, …

Skeen 0 Junior Poster in Training

About creating a thread inside a function then this works out:

/*----------------------------------------------------------------------------*/
/*--------------------------------HEADER--------------------------------------*/
/*----------------------------------------------------------------------------*/
#include <iostream>
#include <windows.h>

using namespace std;

/*----------------------------------------------------------------------------*/
/*--------------------------------THREAD1-------------------------------------*/
/*----------------------------------------------------------------------------*/
DWORD WINAPI Thread1(LPVOID lpParam)
{
while(1){cout << "1";}
}

/*----------------------------------------------------------------------------*/
/*--------------------------------THREAD2-------------------------------------*/
/*----------------------------------------------------------------------------*/
DWORD WINAPI Thread2(LPVOID lpParam)
{
while(1){cout << "2";}
}

/*----------------------------------------------------------------------------*/
/*-----------------------THREAD CREATION FUNCTION-----------------------------*/
/*----------------------------------------------------------------------------*/
void Thread()
    {
    CreateThread(NULL, 0, Thread1, NULL, 0, NULL);
    CreateThread(NULL, 0, Thread2, NULL, 0, NULL);
    }

/*----------------------------------------------------------------------------*/
/*-------------------------------MAIN PROGRAM---------------------------------*/
/*----------------------------------------------------------------------------*/
int main ()
    {
    Thread();
    system("PAUSE");
    return 0;
    }
Skeen 0 Junior Poster in Training

Try using a thread like this, and events to halt the thread:

#include <windows.h>
#include <stdio.h>

DWORD WINAPI myfunk(LPVOID input)         // The actual Thread
{
    HANDLE VENTE = (HANDLE) input;    
    Sleep(1000);                          // Sleep for 1 sec
    SetEvent(VENTE);                      // Send a signal to main, asking it to go beyond the WaitForSingleObject
}

int main()
    {
    HANDLE event = CreateEvent(NULL, FALSE, FALSE, NULL);  // Create a event as HANDLE 
    CreateThread(NULL, 0, myfunk, (LPVOID) event, 0, NULL);// Creat a thread and call it myfunk, pass the event to it
    
    printf("Waiting for the thread...\n");     // Screen output
    WaitForSingleObject( event, INFINITE );// Wait for "SetEvent(hEvent);"   
    
    system("PAUSE");                       // Pause
    return 0;                              // Exit
}

For your code, simply edit the event, and add a WaitForSingleObject to the thread, and let main pass a continue when needed.

Skeen 0 Junior Poster in Training

Depens on when you're interrested in loading and closing it; but something like this:

for (int a=0; a<=10;a++)
  {
  if (a==0){LOAD}
  // Some stuff
  if (a==10){CLOSE}
  }

Something like this?

Skeen 0 Junior Poster in Training

Something useful, a simple RSA encrypting application, with all that belongs, wrapped in some yellow paper is a safe bet :)

Skeen 0 Junior Poster in Training
#include <iostream>
#include <string>
using namespace std;

void initialize(int& flight, int& seatAreaOne, int& seatAreaTwo);
void plane(int flight);
void planeOne(int seatAreaOne);
void planeTwo(int seatAreaTwo);
void ClearSeatsTaken();
void SetSmokerSeats();
void MISSING();
bool YouASmoker();
void TESTUNIT();


#define Row 1
#define Column 10
bool SeatTaken [Row][Column];   // Is a specific seat taken?
bool SmokerSeat[Row][Column];   // Is a specific seat for smokers?
 

int main()
{
	string name;
	string passport;
	int flight;
	int seatAreaOne;
	int seatAreaTwo;
	int n;
	
	cout<<"WELCOME TO C++ AIRLINES."<<endl;
	cout<<"Please enter your name:"<<endl;
	getline(cin, name);
	cout<<"Please enter your passport number:"<<endl;
	cin>>passport;
	cout<<"Name:"<<name<<endl;
	cout<<"Passport number:"<<passport<<endl;

	initialize(flight, seatAreaOne, seatAreaTwo);
	plane(flight);
        ClearSeatsTaken();
        SetSmokerSeats();
        while (1){
        if(YouASmoker()==1){break;}
        TESTUNIT();}

    cout<<"Boardingpass slip"<<endl;
    cout<<"Name:"<<name<<endl;
    cout<<"Pasport number:"<<passport<<endl;
    cout<<"Your seat number:"<<n + 1<<endl;
	

	return 0;

}
void initialize(int& flight, int& seatAreaOne, int& seatAreaTwo)
{
	flight = 0;
	seatAreaOne = 0;
	seatAreaTwo = 0;

}

void plane(int flight)
{
	int seatAreaOne;
	int seatAreaTwo;
	seatAreaOne = 0;
	seatAreaTwo = 0;

	cout<<"For your information, our company provides only two flights per day."<<endl;
	cout<<"Enter (1) for flight A101 OR (2) for flight A102."<<endl;
	cin>>flight;

	if (flight==1)
	{
		cout<<"A101"<<endl;
		planeOne(seatAreaOne);
	}
	else
	{
		cout<<"A102"<<endl;
		planeTwo(seatAreaTwo);
	}

}
void planeOne(int seatAreaOne)
{
	
	cout<<"There are two types of seating area provided in this flight:"<<endl;
	bool YouASmoker();
}
void planeTwo(int seatAreaTwo)
{
	
	cout<<"There are two types of seating area provided in this flight"<<endl;
	cout<<"Please choose a type of seating area."<<endl;
	bool YouASmoker();
	
}

void ClearSeatsTaken()
    {
    for (int n=0; n<Column; n++)
    for (int a=0; a<Row; a++)
        SeatTaken [a][n]=0;
    }
 
void SetSmokerSeats()
    {
    for (int n=0; n<Column; n++)
    {for (int …
Skeen 0 Junior Poster in Training

... I want to know how to use randomise() function in C++. ? ...

Try looking at this: http://www.cplusplus.com/reference/clibrary/cstdlib/rand/

Skeen 0 Junior Poster in Training

i ony able do until tis step , since i really dont know how to apply the array on tat
i understand wat u say
the question just request smoking o non smoking but the lecturer say need to include those things , i force to do that ..i dont know how to put the array inside my code

Here's a basic code; that's using two bool arrays to determinate if the seat is taken and if the seat is for smokers;

#include <iostream>

using namespace std;

#define Row 2
#define Column 10
bool SeatTaken [Row][Column];   // Is a specific seat taken?
bool SmokerSeat[Row][Column];   // Is a specific seat for smokers?

void ClearSeatsTaken()
    {
    for (int n=0; n<Column; n++)
    for (int a=0; a<Row; a++)
        SeatTaken [a][n]=0;
    }
    
void SetSmokerSeats()
    {
    for (int n=0; n<Column; n++)
    {for (int a=0; a<Row; a++)
     {SmokerSeat [a][n]=0;}}
    for (int n=0; n<Column/2; n++)
    {for (int a=0; a<Row; a++)
     {SmokerSeat [a][n]=1;}}
    }

void TESTUNIT()
     {
     for (int n=0; n<Column; n++)
     {for (int a=0; a<Row; a++)
        {cout << SeatTaken [a][n] << " ";}   
     cout << endl;
     }           
     }

void MISSING()
     {
     cout << "THIS PART OF THE PROGRAM IS MISSING; IT*S NOT MADE YET. IT SHOULD FIND ANOTHER RUTE IN THIS FUNCTION!" << endl;         
     }

void DedicateSeat(bool Smoker)
    {
    bool GlobalBreak=false;
    for (int n=0; n<Column; n++)
     {
     if (GlobalBreak==true){break;}
     for (int a=0; a<Row; a++)
       {
       if (GlobalBreak==true){break;}
       if ((SmokerSeat [a][n]==Smoker) && (SeatTaken [a][n]==false))
          {
          SeatTaken [a][n]=true;
          cout << "Seat" << a << " at " …
Skeen 0 Junior Poster in Training

just increment by 2 instead of one and print every number..

for(i=0; i<x; i=i+2)

Like Campbel said, the following will do just that:

#include <iostream>                 // General Header
#define POINT 10                    // Max point to go to for the 'for loop'
int main(){                         // Main
    for (int i=0; i<=POINT; i+=2)   /*
                                     * The 'for loop', intialized a var called "i",
                                     * sets it to zero, check if its lower or equal to POINT,
                                     * if this is true, then runs the loop (the single line below,
                                     * then adds two to "i", and checks the statement "i<=POINT" once again,
                                     * and repeats this.
                                     */
        std::cout << i << " ";      // Writes out the value of "i" and a space  
    std::cin.get();                 // Pauses the program, while waiting for input
    return 0;                       // Terminates the program
    }

Pure code:

#include <iostream>                 
#define POINT 10                    
int main(){                         
    for (int i=0; i<=POINT; i+=2)   
        std::cout << i << " ";       
    std::cin.get();                 
    return 0;                       
    }
Skeen 0 Junior Poster in Training

Hey Mafia, I got about the same task some time ago, and I wrote my code using a bitwise buffer to store all uneven numbers from 3 till the entered number, and then by using 'The Sieve of Eratosthenes' to sort out the numbers, simply picking the first one, and removing all that divides with this from the buffer. Then picking the next number like and dividing that thoughout the buffer.

The buffer will be like this;
3 5 7 9 11 13 15 ...

The program now finds 3, writes that to the screen, then does the following, by dividing out the buffer;
5 7 11 13 15 ...

Then it findes 5, and does the same:
7 11 13 ...

And so on, atm I'm trying mulithread the application using OpenMP (Started progamming this summer, so I'm not really good at multitheading applications yet), but as single threaded it's able to find all the primenumbers in the domain of 2-100.000.000 (5761454Primes) in just under 12seconds :), so it's quite effective. ^^that's using about 6mb of ram, and 2GHz, on my Laptop :), so I'm quite satisfied about the code. There are quicker ways, but my professor was impressed, as he was expecting a simple primetest algorithm, like take a number, use it in a modulo loop, to check if any number above to divided with it returns 0. This will work ofc. however it's VERY slow, and ineffecient, so use 'The …

Skeen 0 Junior Poster in Training

- Try this:
3rd number = highest; 5th number = lowest

Thanks for the reply...=)

There's an error in one of your or a lack of a "else if".

Btw, this will do, however I forgot that you weren't supposed to use arrays, but like, just something alike I had laying around.

#include <iostream>
using namespace std;

void getData();
int CheckIsMaxOrMin();
void calcscore();

double input[5];
bool IsMaxOrMin[5];

int main()
    {
    getData();
    calcscore();
    
    system("PAUSE");
    return 0; // Added a return
    }

int CheckIsMaxOrMin()
     {
     int count=0;
     for (int a=0; a < 5; a++)
         if ((IsMaxOrMin[a]==true)){count++;}
     return count;
     }

void getData()
    {
    cout << "Input 5 numbers : \n";
    cin >> input[0] >> input[1] >> input[2] >> input[3] >> input[4];
    double minimum=input[0]; 
    for (int a=0; a < 5; a++)
        if (input[a] < minimum){minimum=input[a];}
    for (int a=0; a < 5; a++)
         if (input[a] == minimum && (CheckIsMaxOrMin()!=1)){IsMaxOrMin[a]=true;}
        
    double maximum=input[0];
    for (int a=0; a < 5; a++)
        if (input[a] > maximum){maximum=input[a];}
    for (int a=0; a < 5; a++)
         if (input[a] == maximum && (CheckIsMaxOrMin()!=2)){IsMaxOrMin[a]=true;}
         
    for (int a=0; a < 5; a++)
         cout << input[a] << " " << IsMaxOrMin[a] << endl;    
    }

void calcscore()
    {
    double num[3]={0,0,0};
    for (int a=0, b=0; a < 5; a++)
        {
        if (!(IsMaxOrMin[a])){num[b]=input[a];b++;}
        }
    cout << "Average: " << (num[0]+num[1]+num[2])/3; 
    cout<<"\n\n\n";    
    }
Skeen 0 Junior Poster in Training

How about something like this?

#include <cstdlib>  // Header for randomness
#include <ctime>    // Header for time (used to seed random)
#include <iostream> // Header for general IO put

using namespace std;

int main()
    {
    bool done=false;
    while (!done)
       {
       srand(time(NULL)); // Seed random
       int CorrectAnswers=0,AnswersDone=0; 
       while(1)
           {
           if (AnswersDone==10){break;}
           int Answer=0;
           int Num  = rand()%10;  // Get a random number between 0 and 10
           int Num2 = rand()%10;  // Get a random number between 0 and 10
           cout << "Solve the following equalation:\n" << Num << " + " << Num2 << " = ";
           cin >> Answer;
           if (Answer==(Num+Num2))
              {
              cout << endl << "CORRECT! \"" << Num << " + " << Num2 << "\" is infact " << Answer << endl;
              CorrectAnswers++;
              }
           else
              {
              cout << endl << "WRONG ANSWER! The correct answer is \"" << Num << " + " << Num2 << " = " << Num+Num2 << endl;
              }
           AnswersDone++;
           }
       if (CorrectAnswers==10){cout << endl << CorrectAnswers << " out of " << AnswersDone << " that's a perfect score!!" << endl;}
       if (CorrectAnswers>=6 && CorrectAnswers<10){cout << endl << CorrectAnswers << " out of " << AnswersDone << " that's pretty good!" << endl;}
       if (CorrectAnswers>=3 && CorrectAnswers<6){cout << endl << CorrectAnswers << " out of " << AnswersDone << " that's kinda bad!" << endl;}   
       if (CorrectAnswers>=0 && CorrectAnswers<3){cout << endl << CorrectAnswers << " out of " << AnswersDone << " that's sucks!" << endl;}   
       cout << endl << …
Skeen 0 Junior Poster in Training

Hello,
I am currently doing a research at my university about the ways in which we can use multicore CPU to enhance graphics performance instead of putting all the load on the GPU, I am trying to work with both OpenGL and OpenMP and I am planning on using gDebugger to measure performance but I am stuck due the lack of information and papers about this subject so if anyone has any knowledge or any contribution or suggestion please do let me know.

Thanks.

Why would you run graphics on the CPU, when the GPU is made for it, and builded upon multi thread architecture? - and how, will you be building like your own kernel or?

About using OpenMP among with OpenGL, then it's pretty much just writing everything into parallel loops, a good way to do this, could be to make a multi layered buffer, and instead of just swapping the buffer, after showing an "image", then you'll swap to the next image, in a buffer of like 10frames (you'll be able to achive higher frame rates by doing this, simply by swapping the buffer, when like the 2next images are ready in the buffer.), All you will need to then is really to multi thread the buffer filling display function, using OpenMP, with something like; "#pragma omp parallel for", another thing you could do is, OpenMP into the main display function, however you'll need to keep threads alive then, to avoid a lot of overhead. …

Skeen 0 Junior Poster in Training

Try taking a look on this thread:
http://www.daniweb.com/forums/thread235673.html

Any luck there?

Skeen 0 Junior Poster in Training

Help!!!
I'm trying to eliminate the highest and lowest number out of 5 inputs then get the average of the remaining 3 inputs...
In some instances, it gives me incorrect average..
(We are not suppose to use arrays yet)

It would be SO much easier using arrays and loops. - Can you give some examples of the incorrect averages you get, and is it every time, or just sometimes?

Skeen 0 Junior Poster in Training

Use code tags please:

#include <iostream>
using namespace std;

void getData(double& input1, double& input2, double& input3, double& input4, double& input5);
//Ask the user to input numbers

void calcscore(double& num1, double& num2, double& num3, double& num4, double& num5);
//should calculate and display the average of the 3 remaining numbers

int main()
    {
    double score1, score2, score3, score4, score5;

    getData(score1, score2, score3, score4, score5);
    calcscore(score1, score2, score3, score4, score5);
    
    return 0; // Added a return
    }

void getData(double& input1, double& input2, double& input3, double& input4, double& input5)
    {
    cout<<"Input 5 numbers : \n";
    cin>> input1 >> input2 >> input3 >> input4 >> input5;
    }

void calcscore(double& num1, double& num2, double& num3, double& num4, double& num5)
    {
    if (0 > num2&&num3&&num1&&num5&&num4)
        {
        cout<<"Error! Please enter numbers between 0 to 10 only" ;
        cout<<"\n\n\n";
        }
    else if (num1 > num2&&num3&&num4 > num5)
        {
        cout<<"Average: "<<(num2 + num3 + num4)/3 ;
        cout<<"\n\n\n";
        }
    else if (num2 > num1&&num3&&num4 > num5)
        {
        cout<<"Average: "<<(num1 + num3 + num4)/3 ;
        cout<<"\n\n\n";
        }
    else if (num3 > num1&&num2&&num4 > num5)
        {
        cout<<"Average: "<<(num1 + num2 + num4)/3 ;
        cout<<"\n\n\n";
        }
    else if (num4 > num1&&num2&&num3 > num5)
        {
        cout<<"Average: "<<(num1 + num2 + num3)/3 ;
        cout<<"\n\n\n";
        }
    else if (num5 > num4&&num2&&num3 > num1)
        {
        cout<<"Average: "<<(num4 + num2 + num3)/3 ;
        cout<<"\n\n\n";
        }
    else if (num1 > num5&&num2&&num3 > num4)
        {
        cout<<"Average: "<<(num5 + num2 + num3)/3 ;
        cout<<"\n\n\n";
        }
    else if (num2 > num5&&num1&&num3 > num4)
        {
        cout<<"Average: "<<(num5 + num1 + num3)/3 ;
        cout<<"\n\n\n"; …
Skeen 0 Junior Poster in Training

I thought that I should add, like the full testing unit for the decryption, and the results it givin me; so here goes:

#include <iostream>
#include <math.h>

using namespace std;

int main()
{
    int M; //input number (unencrypted).
    int C; //output number (encrypted).
    unsigned long long calcul; //used to store calculations.
    int a;
    int n=143; //half of the key.
    int e=53; //half of the public key.
    int d=77; //half of the private key.    

//    cout << "You choose to encrypt, please enter the number, you want to encrypt"; //ask for input.
//    cin >> M; //get input.
M=0;
a=0;
while (a<=143){
    calcul = pow(M,2); calcul = calcul %n;    
    calcul = pow(calcul,2); calcul = calcul %n;
    calcul = pow(calcul,2); calcul = calcul %n;
    calcul = calcul*M; calcul = calcul %n;
    calcul = pow(calcul,2); calcul = calcul %n;  
    calcul = calcul*M; calcul = calcul %n;
    calcul = pow(calcul,2); calcul = calcul %n;  
    calcul = pow(calcul,2); calcul = calcul %n; 
    calcul = calcul*M; calcul = calcul %n;   
    C = calcul %n; //set "C" to the remainder when "calcul" is divided by "n".   
    cout << "Your decrypted number to:\t" << M << "\tis:\t" << C << endl; //write output number "C".
    M++;
    a++;
}

    system("PAUSE"); //Pause program, I know I shouldn't use the "system("Pause")" command, but it's just for testing
    return 0;
}

The decryption will output the following, based on input;

Your decrypted number to:	0	is:	0
Your decrypted number to:	1	is:	1
Your decrypted number to:	2	is: …
Skeen 0 Junior Poster in Training

btw, you'll need the following code to decrypt, again this is build around the "n and public key", where the encrypt algoritm was build around the "n and the private key", so this is when you want to decrypt the encrypted file:

calcul = pow(M,2); calcul = calcul %n;    
        calcul = pow(calcul,2); calcul = calcul %n;
        calcul = pow(calcul,2); calcul = calcul %n;
        calcul = calcul*M; calcul = calcul %n;
        calcul = pow(calcul,2); calcul = calcul %n;  
        calcul = calcul*M; calcul = calcul %n;
        calcul = pow(calcul,2); calcul = calcul %n;  
        calcul = pow(calcul,2); calcul = calcul %n; 
        calcul = calcul*M; calcul = calcul %n;  
        C = calcul %n; //set "C" to the remainder when "calcul" is divided by "n".
Skeen 0 Junior Poster in Training

wow!! Thanks. I'll have to try and convert that to a function.

Brent

You could try to use; the following code to convert ASCII to decimal, so you're able to encrypt your input, you could change the vector into an array if you want to, but in that way you would lose the dynamic.

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

int main()
    {
    char string[1024];
    int a;
    vector<int> BufferVector;
      
    cout << "Indtast dit input [ASCII]\n";
    gets(string);
    cout << endl << "Decimal output [Decimal]\n";
    for(a = 0; a < strlen(string); a++)
     {
     BufferVector.push_back (("%d ", string[a]));
     }

   cout << endl << endl << "Variable skrivning er fuldført!" << endl;

   for (a = 0; a < BufferVector.size(); a++)
       cout << "random værdi; " << BufferVector[a] << endl;

   cin.get();
   return 0;
   }

If you're using this to mod your input, simply send the vector to the function and like run with the vector instead of "a", by removing the while loop around it. you could make a for loop, calling the vector, and like, running it with the same argument as I'm using in my:

for (a = 0; a < BufferVector.size(); a++)
       cout << "random værdi; " << BufferVector[a] << endl;

Then just looping the encryption instead, and btw, this encryption would send the same encrypted value each time, since it's ALWAYS based upon the same key, like:

int n=143; //half of the key.
int e=53; //half of the public key.
int d=77; //half of the private key.
Skeen 0 Junior Poster in Training

wow!! Thanks. I'll have to try and convert that to a function.

Brent

You could try to use; the following code to convert ASCII to decimal, so you're able to encrypt your input, you could change the vector into an array if you want to, but in that way you would lose the dynamic.

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

int main()
    {
    char string[1024];
    int a;
    vector<int> BufferVector;
      
    cout << "Indtast dit input [ASCII]\n";
    gets(string);
    cout << endl << "Decimal output [Decimal]\n";
    for(a = 0; a < strlen(string); a++)
     {
     BufferVector.push_back (("%d ", string[a]));
     }

   cout << endl << endl << "Variable skrivning er fuldført!" << endl;

   for (a = 0; a < BufferVector.size(); a++)
       cout << "random værdi; " << BufferVector[a] << endl;

   cin.get();
   return 0;
   }

If you're using this to mod your input, simply send the vector to the function and like run with the vector instead of "a", by removing the while loop around it. you could make a for loop, calling the vector, and like, running it with the same argument as I'm using in my:

for (a = 0; a < BufferVector.size(); a++)
       cout << "random værdi; " << BufferVector[a] << endl;

Then just looping the encryption instead, and btw, this encryption would send the same encrypted value each time, since it's ALWAYS based upon the same key, like:

int n=143; //half of the key.
int e=53; //half of the public key.
int d=77; //half of the private key.
Skeen 0 Junior Poster in Training

Sorry for doing this, but anyone with GLUT experience willing to look though a little code, since it's not working at all; I posted the thread here:
http://www.daniweb.com/forums/thread235691.html

Skeen 0 Junior Poster in Training

Any addresses you obtain are not real addresses but rather virtual addresses. In terms of real hardware addresses, i.e., physical addresses, only Windows itself deals with them. It isn't like back in the old DOS world where you could actually deal with hardware.

There are ways to share data between processes, but writing a virtual address to a file and reading it with another program isn't one of them.

So you're saying that I'm unable to use my pointer in another program, even tho it isn't released by the main program yet (still running). - Like, what would that pointer, point to then? - if I call it's value in the second program.

And I know that there are ways, it was just 'Some thoughts'.

Skeen 0 Junior Poster in Training

If you're just outputting, there is no difference. Problem is, if you're sending to a file, it's likely to be read back into a program at some point and you have to take that into consideration.

Sure you have to, like if theres just one root, simply make a char instead of an integer as the secondary root, and detect that when you read it into the file at that later point or something, wouldn't that be a simply solution?

Skeen 0 Junior Poster in Training

I had to do an object-oriented version of a quadratic solver for my C++ class last month, but I just had to output to console, not store in a file.

Really what is the big difference between posting it to the console and saving it to the file, shouldn't be a lot of code you'll need to edit?

Skeen 0 Junior Poster in Training

Correct, in that case, both roots are identical. Problem is, if you're going to read that information back into the same program or into another program, you need to satisfy both input variables or the result pairings later in the input stream will get fubarred.

True what are you doing if you don't got any results, ect, there are no roots?

Skeen 0 Junior Poster in Training

I've been having some thought, like, if I run a program, and I'm exporting a pointer to a variable "a" to a file, and I reads that information with another program, wouldn't the second program be able to modify "a" in the first program using that pointer? - Like the memory address is the same right?

And then I need some help, is it possible, for me, to write some code lines in a text file, and like make the program load and execute these lines from the file? - if I got a program, and I need to do a lot of testing in a specific sector or something, is it possible? - I know it's possible with shaders.