• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EST 07:32
CET 13:32
KST 21:32
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
RSL Season 3 - Playoffs Preview0RSL Season 3 - RO16 Groups C & D Preview0RSL Season 3 - RO16 Groups A & B Preview2TL.net Map Contest #21: Winners12Intel X Team Liquid Seoul event: Showmatches and Meet the Pros10
Community News
BGE Stara Zagora 2026 announced12[BSL21] Ro.16 Group Stage (C->B->A->D)4Weekly Cups (Nov 17-23): Solar, MaxPax, Clem win3RSL Season 3: RO16 results & RO8 bracket13Weekly Cups (Nov 10-16): Reynor, Solar lead Zerg surge2
StarCraft 2
General
BGE Stara Zagora 2026 announced SC: Evo Complete - Ranked Ladder OPEN ALPHA When will we find out if there are more tournament Weekly Cups (Nov 17-23): Solar, MaxPax, Clem win Weekly Cups (Nov 10-16): Reynor, Solar lead Zerg surge
Tourneys
Constellation Cup - Main Event - Stellar Fest RSL Revival: Season 3 Tenacious Turtle Tussle [Alpha Pro Series] Nice vs Cure $5,000+ WardiTV 2025 Championship
Strategy
Custom Maps
Map Editor closed ?
External Content
Mutation # 501 Price of Progress Mutation # 500 Fright night Mutation # 499 Chilling Adaptation Mutation # 498 Wheel of Misfortune|Cradle of Death
Brood War
General
BW General Discussion BGH Auto Balance -> http://bghmmr.eu/ A cwal.gg Extension - Easily keep track of anyone Which season is the best in ASL? soO on: FanTaSy's Potential Return to StarCraft
Tourneys
[Megathread] Daily Proleagues [BSL21] RO16 Group B - Sunday 21:00 CET [BSL21] RO16 Group C - Saturday 21:00 CET Small VOD Thread 2.0
Strategy
Game Theory for Starcraft How to stay on top of macro? Current Meta PvZ map balance
Other Games
General Games
Nintendo Switch Thread The Perfect Game Stormgate/Frost Giant Megathread Beyond All Reason Should offensive tower rushing be viable in RTS games?
Dota 2
Official 'what is Dota anymore' discussion
League of Legends
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Deck construction bug Heroes of StarCraft mini-set
TL Mafia
Mafia Game Mode Feedback/Ideas TL Mafia Community Thread
Community
General
Russo-Ukrainian War Thread US Politics Mega-thread The Big Programming Thread Things Aren’t Peaceful in Palestine Artificial Intelligence Thread
Fan Clubs
White-Ra Fan Club
Media & Entertainment
[Manga] One Piece Movie Discussion! Anime Discussion Thread
Sports
2024 - 2026 Football Thread Formula 1 Discussion NBA General Discussion MLB/Baseball 2023 TeamLiquid Health and Fitness Initiative For 2023
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread
TL Community
Where to ask questions and add stream? The Automated Ban List
Blogs
Esports Earnings: Bigger Pri…
TrAiDoS
Thanks for the RSL
Hildegard
Saturation point
Uldridge
DnB/metal remix FFO Mick Go…
ImbaTosS
Customize Sidebar...

Website Feedback

Closed Threads



Active: 2484 users

The Big Programming Thread - Page 26

Forum Index > General Forum
Post a Reply
Prev 1 24 25 26 27 28 1032 Next
Thread Rules
1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution.
2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20)
3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible.
4. Use [code] tags to format code blocks.
inkblot
Profile Joined December 2004
United States1250 Posts
December 01 2010 17:51 GMT
#501
On December 01 2010 23:28 pinkranger15 wrote:
hey guys. i'm having a problem right now about the correct algorithm to use in computing exponent. we all know that the basic way to do it is by iteration ( 2^4 = 2 * 2 * 2* 2), but i want to do it recursively so that i will execute faster.

thanks guys!


Your code doesn't seem to be recursive to me. In pseudocode, it should look like this:



expt(base,power):
if power == 1:
return base
if power % 2 == 0:
return expt(base*base,power/2)
else:
return base * expt(base,power - 1)



No for loops are needed.
pinkranger15
Profile Joined June 2010
Philippines1597 Posts
Last Edited: 2010-12-02 04:12:35
December 02 2010 04:09 GMT
#502
On December 02 2010 02:51 inkblot wrote:
Show nested quote +
On December 01 2010 23:28 pinkranger15 wrote:
hey guys. i'm having a problem right now about the correct algorithm to use in computing exponent. we all know that the basic way to do it is by iteration ( 2^4 = 2 * 2 * 2* 2), but i want to do it recursively so that i will execute faster.

thanks guys!


Your code doesn't seem to be recursive to me. In pseudocode, it should look like this:



No for loops are needed.


i made a function based on what you gave me.

+ Show Spoiler +

int expt(int base,int power){
if (power == 0){
return 1;
}
else if (power == 1){
return base;
}
else if (power % 2 == 0){
return expt(base*base, power/2);
}
else{
return base * expt(base, power - 1);
}
return expt;
}

printf("%d", expt);


but it always outputs the same large number even though i tried different exponents and different bases. :\
yoyo!
inkblot
Profile Joined December 2004
United States1250 Posts
December 02 2010 06:43 GMT
#503
Your file should look like this:


#include <stdio.h>

int expt(int base,int power){
if (power == 0){
return 1;
}
else if (power == 1){
return base;
}
else if (power % 2 == 0){
return expt(base*base, power/2);
}
else{
return base * expt(base, power - 1);
}
//return expt;
}

int main()
{
int base = 2;
int power = 8;
printf("%d", expt(base,power));
return 0;
}



I commented out the "return expt" because it wasn't doing anything. Run a clean build and this should work fine, it does for me.
Anther
Profile Joined March 2010
United States87 Posts
Last Edited: 2010-12-02 09:14:33
December 02 2010 09:12 GMT
#504
Or if you're in the mood for making someone's head explode..
It's far too late so I decided to reverse the process.


public int calculate(int base, int power) {
return exp(base, base,power,1);
}

public int exp(int initial, int base, int power, int count){
if(power == 0)
return 1;

if(count == power -1)
return base * initial;
if(count == power)
return base;
else
return exp(initial, base * base,power,count*2);
}
Wassow
Profile Joined November 2010
United Kingdom32 Posts
Last Edited: 2010-12-02 10:25:14
December 02 2010 10:14 GMT
#505
Hey guys, im having a spot of bother with vectors in C++ I'm currently creating a third person shooter as part of my university work in ogre3d.

I'm trying to do collision detection between enemy and projectiles which both have their own vector containers. I'd like to know what I can do to improve my current method and how to deal with fixing the iterators after removing objects from the vector.


Ogre::Vector3 temp1, temp2;

// Collision Detection
// This for loop goes through the list of projectiles in the
// Projectile Manager
std::vector<Projectile *>::iterator itp;
for (itp = projMan->mProj.begin(); itp != projMan->mProj.end(); itp++)
{
temp1 = (*itp)->getPosition();

// This for loop goes through the list of enemies in the
// Enemy Manager
std::vector<Enemy *>::iterator ite;
for (ite = eneMan->mEn.begin(); ite != eneMan->mEn.end(); ite++)
{
temp2 = (*ite)->getPosition();

// Checking distance between projectile and enemy
// (usefull later to test for splash damage)
if( sDistBtwnPts(&temp1, &temp2) <= 50.0f)
{
// for now, remove the projectile and the enemy
eneMan->removeEnemy((*ite));
projMan->removeProjectile((*itp));
}
}
}


And the code for removing objects from my vector is currently:


for(i = mProj.begin(); i < mProj.end(); i++)
{
(*i)->Update(fTime,iTime);
}


I realise that a loop like this however is quite inneficient since its an O(n2) but as of now im just trying to get stuff that works, and it does to an extent, till a collision occurs and it breaks because the iterator is broken from removing the objects from the vector.

I also wonder if using a vector in the first place is the way to go. Normally I wouldnt be remvoing objects as much as im trying to achieve now since I'd like to remove enemy health and implement splash damage later on instead of just removing the object straight away.

Lastly, to create a conversational point instead of simply crying for help. Does anyone use many software engineering fractices regurally? I wonder just because the main focus of my actuall game is to implement the Open closed principle for my whole code and factory methods when creating the different pickup items in the game.
MasterOfChaos
Profile Blog Joined April 2007
Germany2896 Posts
December 02 2010 10:42 GMT
#506
Just write out all collision pairs to a new vector, and once the collision detection phase is owner do a distinct collision handling phase.
Having more phases in a tick also reduces the number of strange timing problems. Timing problems within a tick are one of the more annoying parts of game development.
The cost of additional phases in a tick is that it tends to trash the cpu-cache. But I wouldn't worry about that yet.
LiquipediaOne eye to kill. Two eyes to live.
holdthephone
Profile Blog Joined August 2010
United States523 Posts
Last Edited: 2010-12-06 06:45:49
December 06 2010 06:43 GMT
#507
anyone know how to do a simple user input read and use it as a field for a mysql select? (in C). i have working code but when i try to implement this aspect, whatever i type in doesn't work.

just trying to do this:


char branch;
printf("Type in a branch name:");
branch = getchar ();

etc.......



/* send SQL query */
if (mysql_query(conn, "SELECT * FROM branch")) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}

holdthephone
Profile Blog Joined August 2010
United States523 Posts
Last Edited: 2010-12-06 18:14:11
December 06 2010 18:13 GMT
#508
arlight i figured that out, simple queries work, but whenever i try to use the WHERE clause, i get this error:

Unknown column 'BranchName' in 'where clause'



here is the code in question:




char input[80];
printf ("Enter a branch name: ");
scanf ("%s",input);

char phrase[125];

strcpy(phrase,"SELECT Title FROM Book Branch WHERE BranchName=");
strcat(phrase,input);


/* send SQL query */
if (mysql_query(conn,phrase))
//mysql_query
{
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}

//etc.....



been driving me crazy for a while, in anyone knows what to do i'd appreciate it.
Craton
Profile Blog Joined December 2009
United States17274 Posts
December 06 2010 20:12 GMT
#509
Probably need to enclose the BranchName in single quotes, such that BranchName='abcd'. I'm not familiar with C, but that's often the case with SQL.
twitch.tv/cratonz
TanGeng
Profile Blog Joined January 2009
Sanya12364 Posts
December 06 2010 23:30 GMT
#510
On December 07 2010 05:12 Craton wrote:
Probably need to enclose the BranchName in single quotes, such that BranchName='abcd'. I'm not familiar with C, but that's often the case with SQL.


Yep. You need the single quotes. You also might find it easier to use sprintf() instead.
Moderator我们是个踏实的赞助商模式俱乐部
domovoi
Profile Joined August 2010
United States1478 Posts
December 07 2010 03:19 GMT
#511
Is this for school or for work? If the latter, be careful; your code is vulnerable to a SQL injection attack. Surely whatever library you're using allows parameterized statements?
KaiserJohan
Profile Joined May 2010
Sweden1808 Posts
December 08 2010 17:59 GMT
#512
Hi,

I have an annoying problem with UDP sockets. (C#)

I have a client-server voice chat application.

Using TCP, the server only needs to open the TCp listening port, then when clients connect using a tcp socket they are assigned a private tcp socket on the server. Niether the client nor the server needs to open ports other than the main listening port server-side to send/receive data.

However, how on earth is this accomplished with UDP? I guess the reason TCP works is because you actually establish a connection, which is then deemed "secure". I really don't want the server to have open 1 port per client, and the client shouldn't have to open any ports either (like they don't in any application you find... skype, ventrilo, msn, call of duty, starcraft, etc, all running UDP and clients dont need to open those ports to use them)
England will fight to the last American
Greggle
Profile Joined June 2010
United States1131 Posts
Last Edited: 2010-12-09 05:01:12
December 09 2010 04:01 GMT
#513
Edit: Solved the problem on my own, feel free to delete this post or ban me.
Life is too short to take it seriously.
Fyodor
Profile Blog Joined September 2010
Canada971 Posts
December 09 2010 09:28 GMT
#514
I have this problem (Java), I stored some objects in an array and I need to check some private variables inside of these objects but I'm not sure how to access them.

I tried something like this (Used an accessor method)


int thePrice = Transaction[0].getPrice();


to get the price off of the first element of the array but it didn't seem to work. It was a shot in the dark but maybe you get an idea of what I'm trying to do.
llllllllllllllllllllllllllllllllllllllllllll
MasterOfChaos
Profile Blog Joined April 2007
Germany2896 Posts
Last Edited: 2010-12-09 09:50:58
December 09 2010 09:49 GMT
#515
@KaiserJohan
The server needs to open one known port.
The client is a bit more complicated. When the client sends a packet to the server, the router typically opens a port which allows the client to receive UDP packets from that server.

But are you sure you really want to work directly on UDP instead of using some high level library built on top of UDP?

@Fyodor
How about supplying a bit more information? For example how your array is declared...
And what does "not work" mean? Runtime or compile-time error?
LiquipediaOne eye to kill. Two eyes to live.
gravity
Profile Joined March 2004
Australia1988 Posts
December 09 2010 09:52 GMT
#516
On December 09 2010 18:28 Fyodor wrote:
I have this problem (Java), I stored some objects in an array and I need to check some private variables inside of these objects but I'm not sure how to access them.

I tried something like this (Used an accessor method)


int thePrice = Transaction[0].getPrice();


to get the price off of the first element of the array but it didn't seem to work. It was a shot in the dark but maybe you get an idea of what I'm trying to do.

Is the array actually called "Transaction"?
Fyodor
Profile Blog Joined September 2010
Canada971 Posts
Last Edited: 2010-12-09 10:16:35
December 09 2010 10:07 GMT
#517
wow, the name is not Transaction, I got that confused, thanks.

edit, wait, that's not even my actual code, nevermind.
Here's an actual problem:

+ Show Spoiler +

public class MyEBServer {

public static Transaction[] registeredSales;
public static int numRegisteredSales = 0;
public static Transaction[] registeredBids;
public static int numRegisteredBids = 0;
public static Transaction[] purchases;
public static int numPurchases = 0;

public static void initialize(int size)
{
registeredSales = new Transaction[size];
registeredBids = new Transaction[size];
purchases = new Transaction[size];
}

public static void addItemToSell(User seller, Item item, double askingPrice)
{

if(numRegisteredSales <= size - 1)
{
registeredSales[numRegisteredSales] = Transaction(seller, item, askingPrice, Transaction.SELL);

numRegisteredSales++;
}
else
{
System.out.println("The catalogue of registered items to sell is full.");
}
}



I get a compiler error "cannot find symbol" when I call Transaction, supposed to be a constructor method. I said I don't know how to check an object array but I don't know how to populate it either I guess.

this is what the Transaction method looks like:

+ Show Spoiler +


public class Transaction {

public static int SELL = 0;
public static int BID = 1;
public static int FINAL_SALE = 2;
private static int nextConfirmation = 100;
private int confirmationNumber;
private int type;
private boolean active = true;
private double price;
private Item item;
private User user;

public Transaction(User user, Item item, double price, int type)
{
this.user = user;
this.item = item;
this.price = price;
this.type = type;
this.confirmationNumber = nextConfirmation;

Transaction.nextConfirmation++;

}
llllllllllllllllllllllllllllllllllllllllllll
gravity
Profile Joined March 2004
Australia1988 Posts
Last Edited: 2010-12-09 12:40:15
December 09 2010 12:39 GMT
#518
On December 09 2010 19:07 Fyodor wrote:
wow, the name is not Transaction, I got that confused, thanks.

edit, wait, that's not even my actual code, nevermind.
Here's an actual problem:

+ Show Spoiler +

public class MyEBServer {

public static Transaction[] registeredSales;
public static int numRegisteredSales = 0;
public static Transaction[] registeredBids;
public static int numRegisteredBids = 0;
public static Transaction[] purchases;
public static int numPurchases = 0;

public static void initialize(int size)
{
registeredSales = new Transaction[size];
registeredBids = new Transaction[size];
purchases = new Transaction[size];
}

public static void addItemToSell(User seller, Item item, double askingPrice)
{

if(numRegisteredSales <= size - 1)
{
registeredSales[numRegisteredSales] = Transaction(seller, item, askingPrice, Transaction.SELL);

numRegisteredSales++;
}
else
{
System.out.println("The catalogue of registered items to sell is full.");
}
}



I get a compiler error "cannot find symbol" when I call Transaction, supposed to be a constructor method. I said I don't know how to check an object array but I don't know how to populate it either I guess.

this is what the Transaction method looks like:

+ Show Spoiler +


public class Transaction {

public static int SELL = 0;
public static int BID = 1;
public static int FINAL_SALE = 2;
private static int nextConfirmation = 100;
private int confirmationNumber;
private int type;
private boolean active = true;
private double price;
private Item item;
private User user;

public Transaction(User user, Item item, double price, int type)
{
this.user = user;
this.item = item;
this.price = price;
this.type = type;
this.confirmationNumber = nextConfirmation;

Transaction.nextConfirmation++;

}


Make sure you are importing the Transaction class in the class where you are using it, and do "new Transaction(...)" instead of just "Transaction(...)".

Also, this code has a few issues at first glance:
1. You should use an enum for flags, not static ints.
2. Don't make everything static unless you have a good reason.
3. You should probably use Lists of Transactions instead of fixed-size arrays.
4. Even if you do use arrays, you don't need to count the size separately. Use eg registeredSales.length instead.
KaiserJohan
Profile Joined May 2010
Sweden1808 Posts
Last Edited: 2010-12-09 16:00:50
December 09 2010 13:02 GMT
#519
On December 09 2010 18:49 MasterOfChaos wrote:
@KaiserJohan
The server needs to open one known port.
The client is a bit more complicated. When the client sends a packet to the server, the router typically opens a port which allows the client to receive UDP packets from that server.

But are you sure you really want to work directly on UDP instead of using some high level library built on top of UDP?

@Fyodor
How about supplying a bit more information? For example how your array is declared...
And what does "not work" mean? Runtime or compile-time error?



I have the voice data sent over a UDP socket, I'm not sure I could get that latency with TCP.

Unless I specifically open that port sockets listen to packets just dont get received (either direction) even though listening on the right port/local ip. Really annoying.


I've heard briefly about UDP implementations like RTP(or did you mean a library class like UdpClient?) but thats abit more complex I guess (and I havn't found a good, free source) and I just want to get something working and then build from thereon, but this port problems makes me angry!
England will fight to the last American
dapierow
Profile Blog Joined April 2010
Serbia1316 Posts
December 09 2010 15:32 GMT
#520
Can anyone give me some help on creating a Connect four game in java?
Eat.Sleep.Starcraft 2
Prev 1 24 25 26 27 28 1032 Next
Please log in or register to reply.
Live Events Refresh
WardiTV Korean Royale
12:00
Playoffs
Zoun vs SHIN
TBD vs Reynor
TBD vs herO
Solar vs TBD
WardiTV612
TKL 224
IndyStarCraft 142
Rex129
Liquipedia
Sparkling Tuna Cup
10:00
2025 November Finals
Shameless vs SKillousLIVE!
YoungYakov vs TBD
CranKy Ducklings250
LiquipediaDiscussion
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
TKL 224
IndyStarCraft 142
Rex 129
Railgan 25
StarCraft: Brood War
Britney 58693
Sea 9446
Rain 2065
Horang2 1867
actioN 719
BeSt 558
Soma 469
Larva 397
firebathero 340
Last 263
[ Show more ]
Hyun 228
Rush 136
Barracks 129
hero 118
Sharp 91
Mong 74
zelot 67
sorry 41
Sea.KH 38
Shinee 35
ajuk12(nOOB) 22
Noble 15
Terrorterran 15
IntoTheRainbow 12
Dota 2
Gorgc4031
singsing1854
XcaliburYe382
Counter-Strike
x6flipin728
Other Games
B2W.Neo1427
ceh9504
Fuzer 282
crisheroes278
Organizations
Other Games
gamesdonequick775
Dota 2
PGL Dota 2 - Main Stream120
StarCraft: Brood War
CasterMuse 39
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 13 non-featured ]
StarCraft 2
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• blackmanpl 52
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
Dota 2
• WagamamaTV561
League of Legends
• Jankos3628
Upcoming Events
3D!Clan Event
1h 28m
BSL 21
7h 28m
Hawk vs Kyrie
spx vs Cross
Replay Cast
11h 28m
Wardi Open
23h 28m
Monday Night Weeklies
1d 4h
StarCraft2.fi
1d 4h
Replay Cast
1d 11h
Wardi Open
1d 23h
StarCraft2.fi
2 days
PiGosaur Monday
2 days
[ Show More ]
Wardi Open
2 days
StarCraft2.fi
3 days
Replay Cast
3 days
The PondCast
3 days
Replay Cast
4 days
Korean StarCraft League
5 days
CranKy Ducklings
5 days
SC Evo League
5 days
BSL 21
6 days
Sziky vs OyAji
Gypsy vs eOnzErG
Sparkling Tuna Cup
6 days
Liquipedia Results

Completed

SOOP Univ League 2025
RSL Revival: Season 3
Eternal Conflict S1

Ongoing

C-Race Season 1
IPSL Winter 2025-26
KCM Race Survival 2025 Season 4
YSL S2
BSL Season 21
CSCL: Masked Kings S3
Slon Tour Season 2
META Madness #9
SL Budapest Major 2025
ESL Impact League Season 8
BLAST Rivals Fall 2025
IEM Chengdu 2025
PGL Masters Bucharest 2025
Thunderpick World Champ.
CS Asia Championships 2025
ESL Pro League S22
StarSeries Fall 2025
FISSURE Playground #2

Upcoming

BSL 21 Non-Korean Championship
Acropolis #4
IPSL Spring 2026
Bellum Gens Elite Stara Zagora 2026
HSC XXVIII
RSL Offline Finals
WardiTV 2025
Kuram Kup
PGL Cluj-Napoca 2026
IEM Kraków 2026
BLAST Bounty Winter 2026
BLAST Bounty Winter 2026: Closed Qualifier
eXTREMESLAND 2025
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2025 TLnet. All Rights Reserved.