• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 04:27
CEST 10:27
KST 17:27
  • 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
[ASL20] Ro24 Preview Pt2: Take-Off7[ASL20] Ro24 Preview Pt1: Runway132v2 & SC: Evo Complete: Weekend Double Feature4Team Liquid Map Contest #21 - Presented by Monster Energy9uThermal's 2v2 Tour: $15,000 Main Event18
Community News
Weekly Cups (Aug 18-24): herO dethrones MaxPax6Maestros of The Game—$20k event w/ live finals in Paris30Weekly Cups (Aug 11-17): MaxPax triples again!13Weekly Cups (Aug 4-10): MaxPax wins a triple6SC2's Safe House 2 - October 18 & 195
StarCraft 2
General
Weekly Cups (Aug 18-24): herO dethrones MaxPax A Eulogy for the Six Pool Geoff 'iNcontroL' Robinson has passed away 2v2 & SC: Evo Complete: Weekend Double Feature The GOAT ranking of GOAT rankings
Tourneys
WardiTV Mondays Maestros of The Game—$20k event w/ live finals in Paris RSL: Revival, a new crowdfunded tournament series Sparkling Tuna Cup - Weekly Open Tournament Monday Nights Weeklies
Strategy
Custom Maps
External Content
Mutation # 488 What Goes Around Mutation # 487 Think Fast Mutation # 486 Watch the Skies Mutation # 485 Death from Below
Brood War
General
Joined effort [ASL20] Ro24 Preview Pt2: Take-Off No Rain in ASL20? BW General Discussion Flash On His 2010 "God" Form, Mind Games, vs JD
Tourneys
[ASL20] Ro24 Group F [ASL20] Ro24 Group E [Megathread] Daily Proleagues [IPSL] CSLAN Review and CSLPRO Reimagined!
Strategy
Simple Questions, Simple Answers Fighting Spirit mining rates [G] Mineral Boosting Muta micro map competition
Other Games
General Games
General RTS Discussion Thread Stormgate/Frost Giant Megathread Nintendo Switch Thread Dawn of War IV Path of Exile
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
Heroes of StarCraft mini-set
TL Mafia
TL Mafia Community Thread Vanilla Mini Mafia
Community
General
Russo-Ukrainian War Thread Things Aren’t Peaceful in Palestine US Politics Mega-thread The year 2050 European Politico-economics QA Mega-thread
Fan Clubs
INnoVation Fan Club SKT1 Classic Fan Club!
Media & Entertainment
Anime Discussion Thread Movie Discussion! [Manga] One Piece [\m/] Heavy Metal Thread
Sports
2024 - 2026 Football Thread TeamLiquid Health and Fitness Initiative For 2023 Formula 1 Discussion
World Cup 2022
Tech Support
High temperatures on bridge(s) Gtx660 graphics card replacement Installation of Windows 10 suck at "just a moment"
TL Community
The Automated Ban List TeamLiquid Team Shirt On Sale
Blogs
Evil Gacha Games and the…
ffswowsucks
Breaking the Meta: Non-Stand…
TrAiDoS
INDEPENDIENTE LA CTM
XenOsky
[Girl blog} My fema…
artosisisthebest
Sharpening the Filtration…
frozenclaw
ASL S20 English Commentary…
namkraft
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1919 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
Jollibee19350 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 1h 33m
[ Submit Event ]
Live Streams
Refresh
StarCraft: Brood War
Sea 2607
Horang2 1924
Stork 471
Larva 247
Tasteless 228
Pusan 219
Backho 195
ggaemo 174
Nal_rA 150
PianO 127
[ Show more ]
Soulkey 73
soO 54
TY 51
ToSsGirL 46
Sacsri 19
Sharp 12
ivOry 5
Dota 2
Fuzer 134
XcaliburYe77
NeuroSwarm75
BananaSlamJamma53
XaKoH 40
Counter-Strike
Stewie2K424
Other Games
summit1g8201
singsing1040
ceh9536
WinterStarcraft442
SortOf154
Happy148
trigger7
Organizations
Other Games
gamesdonequick594
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 14 non-featured ]
StarCraft 2
• Berry_CruncH345
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
League of Legends
• Lourlo686
• Stunt674
• Jankos541
Upcoming Events
Afreeca Starleague
1h 33m
hero vs Alone
Royal vs Barracks
Replay Cast
15h 33m
The PondCast
1d 1h
WardiTV Summer Champion…
1d 2h
Clem vs Classic
herO vs MaxPax
Replay Cast
1d 15h
LiuLi Cup
2 days
MaxPax vs TriGGeR
ByuN vs herO
Cure vs Rogue
Classic vs HeRoMaRinE
Cosmonarchy
2 days
OyAji vs Sziky
Sziky vs WolFix
WolFix vs OyAji
BSL Team Wars
2 days
Team Hawk vs Team Dewalt
BSL Team Wars
2 days
Team Hawk vs Team Bonyth
SC Evo League
3 days
TaeJa vs Cure
Rogue vs threepoint
ByuN vs Creator
MaNa vs Classic
[ Show More ]
Maestros of the Game
3 days
ShoWTimE vs Cham
GuMiho vs Ryung
Zoun vs Spirit
Rogue vs MaNa
[BSL 2025] Weekly
3 days
SC Evo League
4 days
Maestros of the Game
4 days
SHIN vs Creator
Astrea vs Lambo
Bunny vs SKillous
HeRoMaRinE vs TriGGeR
BSL Team Wars
4 days
Team Bonyth vs Team Sziky
BSL Team Wars
4 days
Team Dewalt vs Team Sziky
Monday Night Weeklies
5 days
Replay Cast
5 days
Sparkling Tuna Cup
6 days
Liquipedia Results

Completed

CSLAN 3
uThermal 2v2 Main Event
HCC Europe

Ongoing

Copa Latinoamericana 4
BSL 20 Team Wars
KCM Race Survival 2025 Season 3
BSL 21 Qualifiers
ASL Season 20
CSL Season 18: Qualifier 1
Acropolis #4 - TS1
CSL Season 18: Qualifier 2
SEL Season 2 Championship
WardiTV Summer 2025
BLAST Open Fall Qual
Esports World Cup 2025
BLAST Bounty Fall 2025
BLAST Bounty Fall Qual
IEM Cologne 2025
FISSURE Playground #1
BLAST.tv Austin Major 2025

Upcoming

CSL 2025 AUTUMN (S18)
LASL Season 20
BSL Season 21
BSL 21 Team A
Chzzk MurlocKing SC1 vs SC2 Cup #2
RSL Revival: Season 2
Maestros of the Game
EC S1
Sisters' Call Cup
IEM Chengdu 2025
PGL Masters Bucharest 2025
MESA Nomadic Masters Fall
Thunderpick World Champ.
CS Asia Championships 2025
Roobet Cup 2025
ESL Pro League S22
StarSeries Fall 2025
FISSURE Playground #2
BLAST Open Fall 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.