• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 06:42
CEST 12:42
KST 19:42
  • 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
BGE Stara Zagora 2025: Info & Preview11Code S RO12 Preview: GuMiho, Bunny, SHIN, ByuN3The Memories We Share - Facing the Final(?) GSL44Code S RO12 Preview: Cure, Zoun, Solar, Creator4[ASL19] Finals Preview: Daunting Task30
Community News
GSL Ro4 and Finals moved to Sunday June 15th10Weekly Cups (May 27-June 1): ByuN goes back-to-back0EWC 2025 Regional Qualifier Results26Code S RO12 Results + RO8 Groups (2025 Season 2)3Weekly Cups (May 19-25): Hindsight is 20/20?0
StarCraft 2
General
GSL Ro4 and Finals moved to Sunday June 15th Jim claims he and Firefly were involved in match-fixing Serious Question: Mech BGE Stara Zagora 2025: Info & Preview I made a 5.0.12/5.0.13 replay fix
Tourneys
Bellum Gens Elite: Stara Zagora 2025 Sparkling Tuna Cup - Weekly Open Tournament SOOP Starcraft Global #21 $5,100+ SEL Season 2 Championship (SC: Evo) WardiTV Mondays
Strategy
[G] Darkgrid Layout Simple Questions Simple Answers [G] PvT Cheese: 13 Gate Proxy Robo
Custom Maps
[UMS] Zillion Zerglings
External Content
Mutation # 476 Charnel House Mutation # 475 Hard Target Mutation # 474 Futile Resistance Mutation # 473 Cold is the Void
Brood War
General
Will foreigners ever be able to challenge Koreans? BW General Discussion FlaSh Witnesses SCV Pull Off the Impossible vs Shu BGH auto balance -> http://bghmmr.eu/ Battle.net is not working
Tourneys
[ASL19] Grand Finals Small VOD Thread 2.0 [BSL20] GosuLeague RO16 - Tue & Wed 20:00+CET [Megathread] Daily Proleagues
Strategy
I am doing this better than progamers do. [G] How to get started on ladder as a new Z player
Other Games
General Games
Monster Hunter Wilds Stormgate/Frost Giant Megathread Nintendo Switch Thread Path of Exile Mechabellum
Dota 2
Official 'what is Dota anymore' discussion
League of Legends
LiquidLegends to reintegrate into TL.net
Heroes of the Storm
Simple Questions, Simple Answers
Hearthstone
Heroes of StarCraft mini-set
TL Mafia
Vanilla Mini Mafia TL Mafia Community Thread TL Mafia Plays: Diplomacy TL Mafia: Generative Agents Showdown Survivor II: The Amazon
Community
General
Things Aren’t Peaceful in Palestine Russo-Ukrainian War Thread US Politics Mega-thread European Politico-economics QA Mega-thread Canadian Politics Mega-thread
Fan Clubs
Maru Fan Club Serral Fan Club
Media & Entertainment
Korean Music Discussion [Manga] One Piece
Sports
2024 - 2025 Football Thread Formula 1 Discussion NHL Playoffs 2024
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread Cleaning My Mechanical Keyboard
TL Community
The Automated Ban List
Blogs
Heero Yuy & the Tax…
KrillinFromwales
Research study on team perfo…
TrAiDoS
I was completely wrong ab…
jameswatts
Need Your Help/Advice
Glider
Trip to the Zoo
micronesia
Poker
Nebuchad
Info SLEgma_12
SLEgma_12
Customize Sidebar...

Website Feedback

Closed Threads



Active: 17060 users

C Programming Practice - Page 4

Blogs > CecilSunkure
Post a Reply
Prev 1 2 3 4 All
obesechicken13
Profile Blog Joined July 2008
United States10467 Posts
November 05 2012 04:35 GMT
#61
On November 05 2012 13:31 frogmelter wrote:
Show nested quote +
On November 05 2012 13:25 obesechicken13 wrote:
1) Code a case to check if it is a prime number to begin with. If it isn't, then enter the loop

If I could do this I'd be a millionaire.


It can find the prime factors of 9,223,372,036,854,775,808 pretty fast [sub 5 seconds].

Lol. I don't know if you're trolling or if you misunderstand the problem. But That number above has only one prime factor: 2.


I'm not trolling.

Since the algorithm divides, it will come up with the answer pretty fast. The factors are 2^63, and every pass will divide it by 2. Meaning the loop only needs to run 63 times.

The problem occurs when you have a huge jump, like the first prime factor is like 6,000,000,000+ or so.

Ok. well you seem to know more than I gave you credit for then.

I'm not sure if it is possible to prove if a(any) number that large is prime in 5 seconds. I just heard from wikipedia that:
There is no known useful formula that yields all of the prime numbers and no composites.


http://en.wikipedia.org/wiki/Prime_number
I think in our modern age technology has evolved to become more addictive. The things that don't give us pleasure aren't used as much. Work was never meant to be fun, but doing it makes us happier in the long run.
frogmelter
Profile Blog Joined April 2009
United States971 Posts
Last Edited: 2012-11-05 04:38:21
November 05 2012 04:36 GMT
#62
On November 05 2012 13:35 obesechicken13 wrote:
Show nested quote +
On November 05 2012 13:31 frogmelter wrote:
On November 05 2012 13:25 obesechicken13 wrote:
1) Code a case to check if it is a prime number to begin with. If it isn't, then enter the loop

If I could do this I'd be a millionaire.


It can find the prime factors of 9,223,372,036,854,775,808 pretty fast [sub 5 seconds].

Lol. I don't know if you're trolling or if you misunderstand the problem. But That number above has only one prime factor: 2.


I'm not trolling.

Since the algorithm divides, it will come up with the answer pretty fast. The factors are 2^63, and every pass will divide it by 2. Meaning the loop only needs to run 63 times.

The problem occurs when you have a huge jump, like the first prime factor is like 6,000,000,000+ or so.

Ok. well you seem to know more than I gave you credit for then.

I'm not sure if it is possible to prove if a(any) number that large is prime in 5 seconds. I just heard from wikipedia that:
Show nested quote +
There is no known useful formula that yields all of the prime numbers and no composites.


http://en.wikipedia.org/wiki/Prime_number


http://en.wikipedia.org/wiki/Miller–Rabin_primality_test

Try that test

And yes, wikipedia is correct. What we are doing is a smarter brute force approach. At best we can try to mitigate the worst case run times, but there are some cases where the run time for the particular algorithm I posted will be O(sqrt(n)/2), which in some cases is still too high
TL+ Member
obesechicken13
Profile Blog Joined July 2008
United States10467 Posts
November 05 2012 04:37 GMT
#63
On November 05 2012 13:36 frogmelter wrote:
Show nested quote +
On November 05 2012 13:35 obesechicken13 wrote:
On November 05 2012 13:31 frogmelter wrote:
On November 05 2012 13:25 obesechicken13 wrote:
1) Code a case to check if it is a prime number to begin with. If it isn't, then enter the loop

If I could do this I'd be a millionaire.


It can find the prime factors of 9,223,372,036,854,775,808 pretty fast [sub 5 seconds].

Lol. I don't know if you're trolling or if you misunderstand the problem. But That number above has only one prime factor: 2.


I'm not trolling.

Since the algorithm divides, it will come up with the answer pretty fast. The factors are 2^63, and every pass will divide it by 2. Meaning the loop only needs to run 63 times.

The problem occurs when you have a huge jump, like the first prime factor is like 6,000,000,000+ or so.

Ok. well you seem to know more than I gave you credit for then.

I'm not sure if it is possible to prove if a(any) number that large is prime in 5 seconds. I just heard from wikipedia that:
There is no known useful formula that yields all of the prime numbers and no composites.


http://en.wikipedia.org/wiki/Prime_number


http://en.wikipedia.org/wiki/Miller–Rabin_primality_test

Try that test

No. Good night. I'll do sieve when I feel like it in the future.
I think in our modern age technology has evolved to become more addictive. The things that don't give us pleasure aren't used as much. Work was never meant to be fun, but doing it makes us happier in the long run.
frogmelter
Profile Blog Joined April 2009
United States971 Posts
November 05 2012 04:55 GMT
#64
On November 05 2012 13:37 obesechicken13 wrote:
Show nested quote +
On November 05 2012 13:36 frogmelter wrote:
On November 05 2012 13:35 obesechicken13 wrote:
On November 05 2012 13:31 frogmelter wrote:
On November 05 2012 13:25 obesechicken13 wrote:
1) Code a case to check if it is a prime number to begin with. If it isn't, then enter the loop

If I could do this I'd be a millionaire.


It can find the prime factors of 9,223,372,036,854,775,808 pretty fast [sub 5 seconds].

Lol. I don't know if you're trolling or if you misunderstand the problem. But That number above has only one prime factor: 2.


I'm not trolling.

Since the algorithm divides, it will come up with the answer pretty fast. The factors are 2^63, and every pass will divide it by 2. Meaning the loop only needs to run 63 times.

The problem occurs when you have a huge jump, like the first prime factor is like 6,000,000,000+ or so.

Ok. well you seem to know more than I gave you credit for then.

I'm not sure if it is possible to prove if a(any) number that large is prime in 5 seconds. I just heard from wikipedia that:
There is no known useful formula that yields all of the prime numbers and no composites.


http://en.wikipedia.org/wiki/Prime_number


http://en.wikipedia.org/wiki/Miller–Rabin_primality_test

Try that test

No. Good night. I'll do sieve when I feel like it in the future.


Good night, and good luck
TL+ Member
teamamerica
Profile Blog Joined July 2010
United States958 Posts
November 05 2012 07:45 GMT
#65
Hey can someone tell me if my thought process is right? I'm thinking of how to find if a number is prime given fermats little thm. which states a^p is equivalent to a in mod p (I'm taking a class which covers basic public key exchange).

Knowing that to figure if some p is prime it's just a matter of figuring a^p, and for large p you can use the fast powering algorithm. That splits finding a^p as finding the product to a raised to the binary expansion of p. I.e. a^5 becomes
a^(2^2) *a(2^0). Since this entire set of operations is done mod p, we never exceed p (for overflow - this is how pythons math.pow(x,y,z) works). So we end up with log2 multiplications to perform.

But on thinking about this, why how come you can't just use a=1 and then it would seem that every p is prime o.O...Anyone tell me what obvious part I'm doing wrong/missing?
RIP GOMTV. RIP PROLEAGUE.
mmp
Profile Blog Joined April 2009
United States2130 Posts
Last Edited: 2012-11-05 08:29:41
November 05 2012 08:25 GMT
#66
On November 05 2012 16:45 teamamerica wrote:
Hey can someone tell me if my thought process is right? I'm thinking of how to find if a number is prime given fermats little thm. which states a^p is equivalent to a in mod p (I'm taking a class which covers basic public key exchange).

Knowing that to figure if some p is prime it's just a matter of figuring a^p, and for large p you can use the fast powering algorithm. That splits finding a^p as finding the product to a raised to the binary expansion of p. I.e. a^5 becomes
a^(2^2) *a(2^0). Since this entire set of operations is done mod p, we never exceed p (for overflow - this is how pythons math.pow(x,y,z) works). So we end up with log2 multiplications to perform.

But on thinking about this, why how come you can't just use a=1 and then it would seem that every p is prime o.O...Anyone tell me what obvious part I'm doing wrong/missing?

Fermat's probable prime theorem is probable. You have to repeat the test for various moduli.

Even then, it's ridiculously fast to repeat ~100 times to get good confidence.

Note that Carmichael Numbers break Fermat's Little Theorem, but I believe the density of Carmichael numbers goes down for big numbers, so this can be ignored for applications like RSA.
I (λ (foo) (and (<3 foo) ( T_T foo) (RAGE foo) )) Starcraft
teamamerica
Profile Blog Joined July 2010
United States958 Posts
November 05 2012 12:41 GMT
#67
On November 05 2012 17:25 mmp wrote:
Show nested quote +
On November 05 2012 16:45 teamamerica wrote:
Hey can someone tell me if my thought process is right? I'm thinking of how to find if a number is prime given fermats little thm. which states a^p is equivalent to a in mod p (I'm taking a class which covers basic public key exchange).

Knowing that to figure if some p is prime it's just a matter of figuring a^p, and for large p you can use the fast powering algorithm. That splits finding a^p as finding the product to a raised to the binary expansion of p. I.e. a^5 becomes
a^(2^2) *a(2^0). Since this entire set of operations is done mod p, we never exceed p (for overflow - this is how pythons math.pow(x,y,z) works). So we end up with log2 multiplications to perform.

But on thinking about this, why how come you can't just use a=1 and then it would seem that every p is prime o.O...Anyone tell me what obvious part I'm doing wrong/missing?

Fermat's probable prime theorem is probable. You have to repeat the test for various moduli.

Even then, it's ridiculously fast to repeat ~100 times to get good confidence.

Note that Carmichael Numbers break Fermat's Little Theorem, but I believe the density of Carmichael numbers goes down for big numbers, so this can be ignored for applications like RSA.


Oh whoa, I never knew it was probable - also never knew about Carmicheal numbers (or that Fermats thm. failed at all TT). Well at least tomorrow when I ask my teacher some questions I'll look like I've actually been doing the reading!

Anyway, I saw some posts about learning C++ - I'm always an advocate for good books, esp. in languages where it's easy to do stuff the wrong way. So there's
http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list
RIP GOMTV. RIP PROLEAGUE.
dakalro
Profile Joined September 2010
Romania525 Posts
November 05 2012 15:24 GMT
#68
On November 04 2012 17:29 spinesheath wrote:
Show nested quote +
On November 04 2012 07:25 Lysenko wrote:
On November 04 2012 04:15 CecilSunkure wrote:
On November 04 2012 02:09 Sawamura wrote:
now if could write some C++ program practice ^_^ hehehe thanks for the practice anyway 5/5

I could do that. I'm thinking of some object oriented C first though. You know, virtualizing some C++ features in C.


I was on my way to a much longer post, but I don't want to get in the way of what you're doing here with a lot of negativity, so I'll just ask. Who learns C today, absent a specific need to write embedded code, modify a UNIX or Linux kernel, or write a driver?

I'm speaking as someone who paid several years of bills as a C programmer, working on both embedded applications and desktop applications. This was, however, 17 years ago, before (as far as I have personally seen, which is why I'm asking) the entire world moved on to C++.



** I mean, I can see doing it out of nostalgia, or because I got a cheap deal on some single-chip-computer development board, but that's about it. Is this a topic of interest for typical, highly-motivated CS students these days? I'm not saying it's bad, I just don't get it.

You learn C to work with the crappy old codebase many companies still have. If you start a new project from scratch, you better have a damn good reason to use C. But if you work on something that uses old code, chances are it uses C.

That's no reason to start with C as your first language though.


I can tell you from 1st hand experience that you would miss your mobile phone functionality if C were to vanish now :D. C is here to stay and it will keep running the backend of a lot of stuff.
nath
Profile Blog Joined May 2010
United States1788 Posts
November 05 2012 16:56 GMT
#69
On November 04 2012 17:29 spinesheath wrote:
Show nested quote +
On November 04 2012 07:25 Lysenko wrote:
On November 04 2012 04:15 CecilSunkure wrote:
On November 04 2012 02:09 Sawamura wrote:
now if could write some C++ program practice ^_^ hehehe thanks for the practice anyway 5/5

I could do that. I'm thinking of some object oriented C first though. You know, virtualizing some C++ features in C.


I was on my way to a much longer post, but I don't want to get in the way of what you're doing here with a lot of negativity, so I'll just ask. Who learns C today, absent a specific need to write embedded code, modify a UNIX or Linux kernel, or write a driver?

I'm speaking as someone who paid several years of bills as a C programmer, working on both embedded applications and desktop applications. This was, however, 17 years ago, before (as far as I have personally seen, which is why I'm asking) the entire world moved on to C++.



** I mean, I can see doing it out of nostalgia, or because I got a cheap deal on some single-chip-computer development board, but that's about it. Is this a topic of interest for typical, highly-motivated CS students these days? I'm not saying it's bad, I just don't get it.

You learn C to work with the crappy old codebase many companies still have. If you start a new project from scratch, you better have a damn good reason to use C. But if you work on something that uses old code, chances are it uses C.

That's no reason to start with C as your first language though.

maybe in germany, in the US old codebases hardly ever use C
Founder of Flow Enterprises, LLC http://flow-enterprises.com/
nath
Profile Blog Joined May 2010
United States1788 Posts
November 05 2012 16:57 GMT
#70
On November 06 2012 00:24 dakalro wrote:
Show nested quote +
On November 04 2012 17:29 spinesheath wrote:
On November 04 2012 07:25 Lysenko wrote:
On November 04 2012 04:15 CecilSunkure wrote:
On November 04 2012 02:09 Sawamura wrote:
now if could write some C++ program practice ^_^ hehehe thanks for the practice anyway 5/5

I could do that. I'm thinking of some object oriented C first though. You know, virtualizing some C++ features in C.


I was on my way to a much longer post, but I don't want to get in the way of what you're doing here with a lot of negativity, so I'll just ask. Who learns C today, absent a specific need to write embedded code, modify a UNIX or Linux kernel, or write a driver?

I'm speaking as someone who paid several years of bills as a C programmer, working on both embedded applications and desktop applications. This was, however, 17 years ago, before (as far as I have personally seen, which is why I'm asking) the entire world moved on to C++.



** I mean, I can see doing it out of nostalgia, or because I got a cheap deal on some single-chip-computer development board, but that's about it. Is this a topic of interest for typical, highly-motivated CS students these days? I'm not saying it's bad, I just don't get it.

You learn C to work with the crappy old codebase many companies still have. If you start a new project from scratch, you better have a damn good reason to use C. But if you work on something that uses old code, chances are it uses C.

That's no reason to start with C as your first language though.


I can tell you from 1st hand experience that you would miss your mobile phone functionality if C were to vanish now :D. C is here to stay and it will keep running the backend of a lot of stuff.

very true
Founder of Flow Enterprises, LLC http://flow-enterprises.com/
RoyGBiv_13
Profile Blog Joined August 2010
United States1275 Posts
November 05 2012 18:31 GMT
#71
On November 06 2012 01:56 nath wrote:
Show nested quote +
On November 04 2012 17:29 spinesheath wrote:
On November 04 2012 07:25 Lysenko wrote:
On November 04 2012 04:15 CecilSunkure wrote:
On November 04 2012 02:09 Sawamura wrote:
now if could write some C++ program practice ^_^ hehehe thanks for the practice anyway 5/5

I could do that. I'm thinking of some object oriented C first though. You know, virtualizing some C++ features in C.


I was on my way to a much longer post, but I don't want to get in the way of what you're doing here with a lot of negativity, so I'll just ask. Who learns C today, absent a specific need to write embedded code, modify a UNIX or Linux kernel, or write a driver?

I'm speaking as someone who paid several years of bills as a C programmer, working on both embedded applications and desktop applications. This was, however, 17 years ago, before (as far as I have personally seen, which is why I'm asking) the entire world moved on to C++.



** I mean, I can see doing it out of nostalgia, or because I got a cheap deal on some single-chip-computer development board, but that's about it. Is this a topic of interest for typical, highly-motivated CS students these days? I'm not saying it's bad, I just don't get it.

You learn C to work with the crappy old codebase many companies still have. If you start a new project from scratch, you better have a damn good reason to use C. But if you work on something that uses old code, chances are it uses C.

That's no reason to start with C as your first language though.

maybe in germany, in the US old codebases hardly ever use C


As an american engineer who codes entirely in C, I disagree.

Also, I help build a corporate C compiler, ask me anything about those silly optimizations you want, though, under the hood its really just black magic.
Any sufficiently advanced technology is indistinguishable from magic
Lysenko
Profile Blog Joined April 2010
Iceland2128 Posts
November 05 2012 19:50 GMT
#72
On November 06 2012 00:24 dakalro wrote:
Show nested quote +
On November 04 2012 17:29 spinesheath wrote:
On November 04 2012 07:25 Lysenko wrote:
On November 04 2012 04:15 CecilSunkure wrote:
On November 04 2012 02:09 Sawamura wrote:
now if could write some C++ program practice ^_^ hehehe thanks for the practice anyway 5/5

I could do that. I'm thinking of some object oriented C first though. You know, virtualizing some C++ features in C.


I was on my way to a much longer post, but I don't want to get in the way of what you're doing here with a lot of negativity, so I'll just ask. Who learns C today, absent a specific need to write embedded code, modify a UNIX or Linux kernel, or write a driver?

I'm speaking as someone who paid several years of bills as a C programmer, working on both embedded applications and desktop applications. This was, however, 17 years ago, before (as far as I have personally seen, which is why I'm asking) the entire world moved on to C++.



** I mean, I can see doing it out of nostalgia, or because I got a cheap deal on some single-chip-computer development board, but that's about it. Is this a topic of interest for typical, highly-motivated CS students these days? I'm not saying it's bad, I just don't get it.

You learn C to work with the crappy old codebase many companies still have. If you start a new project from scratch, you better have a damn good reason to use C. But if you work on something that uses old code, chances are it uses C.

That's no reason to start with C as your first language though.


I can tell you from 1st hand experience that you would miss your mobile phone functionality if C were to vanish now :D. C is here to stay and it will keep running the backend of a lot of stuff.


To all the folks who keep bringing up embedded applications in response to my comments about C largely not being used today (and the comments on my comments) please note that embedded applications are one of the specific examples (among others) I mentioned that would benefit from learning C.
http://en.wikipedia.org/wiki/Lysenkoism
]343[
Profile Blog Joined May 2008
United States10328 Posts
Last Edited: 2012-11-05 22:04:44
November 05 2012 22:02 GMT
#73
bros bros there exist polynomial-time primality tests like http://en.wikipedia.org/wiki/AKS_primality_test

factoring, on the other hand, is not known to be in P (which is why RSA hasn't been broken, yet)

(in fact a lot of cryptography relies on stuff which is not proven, such as: "factoring is hard"; "discrete log is hard"; and "there exists a pseudorandom number generator" [which is true if "discrete log is hard"])

On November 05 2012 17:25 mmp wrote:
Fermat's probable prime theorem is probable. You have to repeat the test for various moduli.


hmm, I guess it's probabilistic if you choose random guys a and take a^n-1... but yeah the converse of Fermat's Little Theorem is not necessarily true.
Writer
obesechicken13
Profile Blog Joined July 2008
United States10467 Posts
November 06 2012 03:48 GMT
#74
On November 06 2012 07:02 ]343[ wrote:
bros bros there exist polynomial-time primality tests like http://en.wikipedia.org/wiki/AKS_primality_test

Wiki says the original test had a worst case of Õ(log^12(n))
So that's like log(log(log(log...log(n)))))...)))
12 iterated logarithms. I had to go on wolfram to brush up on iterated logarithms. In that case checking for primes is pretty damn fast.

The Euler problem in question though was considered the third easiest question on the site. I highly doubt everyone who solved the problem knew AKS primality test considering the next few problems look really easy in comparison.
I think in our modern age technology has evolved to become more addictive. The things that don't give us pleasure aren't used as much. Work was never meant to be fun, but doing it makes us happier in the long run.
icystorage
Profile Blog Joined November 2008
Jollibee19343 Posts
November 08 2012 13:13 GMT
#75
my solution to #5 used a simple bubble sort :D
+ Show Spoiler +

#include <stdio.h> /* printf */

/* Swap the contents of two pointers. Do not modify the prototype.
Use your function from PointerPractice_3 here. */
void SwapInts(int *a,int *b )
{
/* Place your code here. Modify these lines. */
int temp = *a;
*a = *b;
*b = temp;
}

/* Print contents of an array. Do not modify this function. */
void PrintArray( const int* array, const int size )
{
int i;
for(i = 0; i < size; ++i, ++array)
{
if(i != size - 1)
printf( "%d, ", *array);
else
printf( "%d\n", *array);
}
}

int main( void )
{
/* Create array on the stack. */
int stackArray[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

/* Get size of array. stackArray can be treated as pointer to first element. */
int arraySize = sizeof( stackArray ) / sizeof( *stackArray );

/* Can treat name of an array as pointer to first element. */
PrintArray( stackArray, arraySize );

/* Reverse the array here */
/**/
/**/
/**/
int i, j;
for (i = (arraySize - 1); i > 0; i--)
{
for (j = 1; j <= i; j++)
{
if (stackArray[j-1] < stackArray[j])
{
SwapInts(&stackArray[j-1], &stackArray[j]);
}
}
}

PrintArray( stackArray, arraySize );

/* Correct output:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 */

return 0;
}
LiquidDota StaffAre you ready for a Miracle-? We are! The International 2017 Champions!
SeeDLiNg
Profile Joined January 2010
United States690 Posts
December 19 2012 18:04 GMT
#76
So, I know what it's finding... But I don't really know how it's finding it...

int arraySize = sizeof( stackArray ) / sizeof( *stackArray );

Can someone break down how exactly this is finding the arraySize for me please?
CecilSunkure
Profile Blog Joined May 2010
United States2829 Posts
December 19 2012 21:05 GMT
#77
On December 20 2012 03:04 SeeDLiNg wrote:
So, I know what it's finding... But I don't really know how it's finding it...

int arraySize = sizeof( stackArray ) / sizeof( *stackArray );

Can someone break down how exactly this is finding the arraySize for me please?

Sure!

So with an array on the stack C actually knows the number of elements in the array. So when you do sizeof( stackArray ) the stackArray has not yet implicitly decayed into a pointer, and is actually being treated as a proper array. C multiplies the size of a single element in the array by the number of arrays, and this returns the number of bytes the entire array takes up within memory.

So then sizeof( *stackArray ) is implicitly decaying the stackArray identifier into a pointer and then dereferencing it. The value of *stackArray is that of the first element in the array, since an array's identifier can always be implicitly decayed into a pointer to the first element. So then the size of the first element is 4 bytes: the size of the integer first element.

Then you divide the size of bytes in total by the size in bytes of the first element to get the number of elements total.
Prev 1 2 3 4 All
Please log in or register to reply.
Live Events Refresh
Next event in 18m
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
EnDerr 112
StarCraft: Brood War
Calm 7456
Sea 5382
Jaedong 1803
Bisu 1562
Horang2 1139
Hyuk 840
Pusan 470
EffOrt 247
Zeus 182
Light 137
[ Show more ]
Dewaltoss 121
Last 115
Mini 105
hero 96
Rush 86
ZerO 72
Leta 60
ggaemo 54
Hyun 45
ToSsGirL 40
Killer 31
sSak 30
Sharp 25
Mong 17
Shine 16
GoRush 15
Backho 14
Free 14
Yoon 13
HiyA 12
ajuk12(nOOB) 12
Stork 11
JulyZerg 8
SilentControl 5
Dota 2
XcaliburYe949
BananaSlamJamma426
Fuzer 196
PGG 151
Counter-Strike
shoxiejesuss1040
x6flipin245
Super Smash Bros
Mew2King108
Other Games
singsing1457
ceh9595
Pyrionflax158
crisheroes154
XaKoH 105
ZerO(Twitch)16
Has14
ArmadaUGS6
Organizations
Dota 2
PGL Dota 2 - Main Stream2048
Other Games
gamesdonequick642
StarCraft: Brood War
UltimateBattle 25
lovetv 5
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 12 non-featured ]
StarCraft 2
• StrangeGG 50
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• iopq 4
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
Upcoming Events
WardiTV Qualifier
18m
Bellum Gens Elite
1h 18m
OSC
5h 18m
The PondCast
23h 18m
Bellum Gens Elite
1d
WardiTV Invitational
1d
Replay Cast
1d 13h
OSC
1d 13h
Bellum Gens Elite
2 days
WardiTV Invitational
2 days
[ Show More ]
Replay Cast
2 days
CranKy Ducklings
2 days
SC Evo League
3 days
Bellum Gens Elite
3 days
Fire Grow Cup
3 days
CSO Contender
3 days
Replay Cast
3 days
SOOP
3 days
SHIN vs GuMiho
Sparkling Tuna Cup
3 days
AllThingsProtoss
4 days
Fire Grow Cup
4 days
Replay Cast
4 days
Replay Cast
5 days
Replay Cast
5 days
WardiTV Invitational
6 days
GSL Code S
6 days
Rogue vs GuMiho
Maru vs Solar
Liquipedia Results

Completed

CSL Season 17: Qualifier 1
DreamHack Dallas 2025
Heroes 10 EU

Ongoing

JPL Season 2
BSL 2v2 Season 3
BSL Season 20
KCM Race Survival 2025 Season 2
NPSL S3
Rose Open S1
CSL Season 17: Qualifier 2
2025 GSL S2
Bellum Gens Elite Stara Zagora 2025
BLAST.tv Austin Major 2025
ESL Impact League Season 7
IEM Dallas 2025
PGL Astana 2025
Asian Champions League '25
ECL Season 49: Europe
BLAST Rivals Spring 2025
MESA Nomadic Masters
CCT Season 2 Global Finals
IEM Melbourne 2025
YaLLa Compass Qatar 2025
PGL Bucharest 2025
BLAST Open Spring 2025

Upcoming

CSL 17: 2025 SUMMER
Copa Latinoamericana 4
CSLPRO Last Chance 2025
CSLAN 2025
K-Championship
SEL Season 2 Championship
Esports World Cup 2025
HSC XXVII
Championship of Russia 2025
Murky Cup #2
BLAST Bounty Fall 2025
BLAST Bounty Fall Qual
IEM Cologne 2025
FISSURE Playground #1
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.