Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

C revision and basic programs

This is just a casual dump topic. I have a C exam coming up, just very basics, so I'll be dumping some very basic programs and code in here. Don't expect anything fantastic or usable, I just need some practice.

[pastebin]39[/pastebin]

The questions we get are going to be on paper, so no testing or correcting or anything. They will be things like make a program to sort three numbers; must use a switch statement. (Or like that)
 
strange thing is that I actually understand some of this; i'd read about 400 pages of a c++ college textbook(which was about 8 years outdated, but still) a while ago, and some of the basics I can still remember.

it's a surprisingly easy code. :| The best I can figure is that, Ignoring the fact that the instructions are there(>.<), the user press 3 numbers, which is stored into 3 diff variables. and then the computer tells which variable is largest.

Question: what if two variables are the same, and the 3rd one is different? Wouldn't the default statement be only partially right?
 
Good call - I guess something like this would be better (haven't tested):

[pastebin]40[/pastebin]

-- missed off num1 and num3 being the same.

D:
 
I'll never understand programmers.

Apparently

Deltaswap(Array[floor(i)],Array[ceil(Arraysize-i)])


is "the basics", and simpler than

for (i=9; i >= 0; i--)


Granted I should have used the size of the array rather than "9", but apart from that, I would have said my way was way simpler and shorter.
 
Actually, for the sake of readability, most real programmers prefer the for loop. However, you should declare the i in the initialization statement of the loop. (IE, (int i = 0; i < [sizeofarray]; i++)). In terms of performance, real compilers (MSVC, GCC, Intel Compiler etc.) will all optimize that automatically. In terms of memory, you are taking up one byte for the duration of the loop, and then it falls out of scope and can be reclaimed. You can also optimize it a little bit more and do (int i = [size of array];i--;). Note that there is no increment statement, and the conditional statement is i--. That way, it checks what i-- is each time, so when i is 1 and then it gets to the i--, (1)-- becomes 0, and 0 evaluates to false. I believe that MSVC automatically does that optimization where it can, but I am not sure about GCC or Intel's Compiler.

EDIT: Oh yeah, the reason why you should declare i in the for loop is because that way, the variable falls out of scope when the for loop ends, and you don't have to manually delete it.
 
Oh, that makes sense.

I'm using GCC, I believe. Well, I use codeblocks, but my uni makes us do it all from the command line in actual classes.



The exam is over now ~ can only wait

Both the above came up :)
 
Best of luck to you. Hope your C class is better than mine was. I have vivid memories of waiting in line for 3 hours to ask a TA for help on a homework assignment and when it was finally my turn, she glanced at my program and said: "Well, I can't figure out. Throw some stars at it until it works." and moved on to the next person.
 
C::B let's you pick what compiler you want to use, but generally you use GCC, so you are probably using GCC when you use C::B. MSVC is wierd, in that it's the only C/C++ compiler (that I know of) that is directly tied to a specific IDE.
 

Gust

Member

DeM0nFiRe":2rjzutxq said:
C::B let's you pick what compiler you want to use, but generally you use GCC, so you are probably using GCC when you use C::B. MSVC is wierd, in that it's the only C/C++ compiler (that I know of) that is directly tied to a specific IDE.

Not really. Actually, you can use C::B with MSVC.
 

Gust

Member

I just meant that MSVC is not directly tied to a specific IDE.
Maybe the IDE is specifically tied to MSVC, but the compiler is independent.
 
Oh, no, I understand that the MSVC Compiler and Linker can be used separately from the MSVC IDE, but they are packaged together. They are all part of the package known as Microsoft Visual C++ (Or I guess technically they make up one part of the package known as Microsoft Visual Studio, but you know what I mean). There is no GCC IDE.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top