Author Topic: I wrote my first bit of source code!  (Read 12906 times)

0 Members and 1 Guest are viewing this topic.

Offline karajorma

  • King Louie - Jungle VIP
  • Administrator
  • 214
    • Karajorma's Freespace FAQ
Re: I wrote my first bit of source code!
Just be warned that for loops do not work as expected in MS compilers.

Code: [Select]
for (int i=0; i < something ; i++)
{
.....
}

i = 0;

That would give a complier error on any compiler other than the MS ones. When you take lessons in scope you'll learn that the variable i is only supposed to be accessible from within the loop itself. On MS compilers that code will work perfectly well with no sign that anything is wrong.
Karajorma's Freespace FAQ. It's almost like asking me yourself.

[ Diaspora ] - [ Seeds Of Rebellion ] - [ Mind Games ]

 

Offline Backslash

  • 29
  • Bring Our Might To Bear
Re: I wrote my first bit of source code!
On the bright side that's an option you can set in the Project settings under Language: "Force Conformance In For Loop Scope".

Also, now (afaict) Visual Studio 2005 defaults to that being set Yes. :yes:

 

Offline WMCoolmon

  • Purveyor of space crack
  • 213
Re: I wrote my first bit of source code!
The toolkit compiler is actually the one included in the professional edition of 2003 .NET; the standard edition lacks a number of the optimizations available in the professional edition compiler. Installing the toolkit compiler enables these optimizations through use of the manual command line settings; the drop-down boxes and other controls in the standard edition will remain disabled, although the optimizations will still take effect.

For more information, see Xona.com - Visual C++ .NET Standard Edition - Free Optimizing Compiler.


I have got to cut down on the wiki reading...
-C

 

Offline Herra Tohtori

  • The Academic
  • 211
  • Bad command or file name
Re: I wrote my first bit of source code!
You know what...

I took a Java course during summer. And the tasks we were given made me scream inside my head of how to NOT show the advantages of object-oriented programming...

Most of the time I just wanted to write a ****ng long main method that would simply do the program. All those method calls and null pointers and what not were driving me crazy. It obviously is more of an advantage when the coding gets more advanced, methods get longer and classes grow exponentially measured in code lines... Then it starts to be a good thing to have the code in separate objects.


But writing a program wwhich has a main method that asks for information, area size calculating method, volume calculating method AND an output method (StringTo) is just frustrating, it adds many things to code that are actually not needed and complicates the coding process quite a bit.

I wrote more complex programs in Texas Instruments TI-86 in high school. One of them prompted to feed in several variables (initial velocity, starting height, direction, mass and radius to name most important) and counted the trajectory of a ball thrown in air. It took air friction in account and used some pretty nice features of the calculator. It drew the trajectory in calculator's own parametre function screen, and then paused, and after pressing a key it printed a screen of information including maximum height, distance of impact from the start point (on x-axis), impact velocity and impact angle. :nervous: Sadly, the program memory was purged before the final exams and I forgot to write it down on more static format. I could write it again, though. It wouldn't take that much time...

I suppose it can't be that much more difficult to do C programming than programs with TI-86... learning the syntax just takes time and effort and getting fast demands a lot of time and excersizes. :nod:
There are three things that last forever: Abort, Retry, Fail - and the greatest of these is Fail.

 

Offline Freespace Freak

  • 28
  • Official forum permanewb
Re: I wrote my first bit of source code!
Yeah, I wrote some programs with my TI-84, but I like the formatting in C.  I like how it makes expandable + and - boxes and has stuff with a $ or # sign in blue and the dev comments in green.  It makes looking for stuff easy(er), at least from the standpoint of a guy who's never done any real hard-core programming before.  I did take a BASIC course back in high school.  Not Visual BASIC, but BASIC 1.0.  That was old school!  We used computers that were built when I was born.  To say the school had a low tech budget would be an understatement.  For our final exam we had to write the code to make the game Pong!

 

Offline Herra Tohtori

  • The Academic
  • 211
  • Bad command or file name
Re: I wrote my first bit of source code!
What program are you using to actually write the code?

I use Eclipse with Java, it also has different colours and it does the indentions automagically when you enter something inside, say, wave brackets... There are instructions on how to use Eclipse on C and C++ programming, too.

http://www.eclipse.org/

How to Use Eclipse for C and C++ Development (link also in the main page of eclipse.org)

There are three things that last forever: Abort, Retry, Fail - and the greatest of these is Fail.

 

Offline ME$$ENGER

  • 26
  • The Yellow Peril
Re: I wrote my first bit of source code!
My IDE of choice is Eclipse, too. Sometimes a bit of DrJava to get a quick check on the code using the interaction pane.

I only code in Java & Python, though. Maybe I should teach myself C someday.
« Last Edit: September 14, 2006, 05:31:25 pm by ME$$ENGER »

 

Offline Ferret

  • 28
  • A very hungry Fiona.
Re: I wrote my first bit of source code!
Yeah Eclipse is a seriously good IDE, I'm using it for PHP right now.

 
Re: I wrote my first bit of source code!
Just wish Eclipse didn't run a like a hog though   :blah:
That's cool and ....disturbing at the same time o_o  - Vasudan Admiral

"Don't play games with me. You just killed someone I like, that is not a safe place to stand. I'm the Doctor. And you're in the biggest library in the universe. Look me up."

"Quick everyone out of the universe now!"

 

Offline Turey

  • Installer dude
  • 211
  • The diminutive form of Turambar.
    • FreeSpace Open Installer Homepage
Re: I wrote my first bit of source code!
Just wish Eclipse didn't run a like a hog though   :blah:

yeah, that really sucks when you're running it on a less than top-of-the-line comp.

i've done a lot of basic code, quite a bit of C code, and a small amount of java. I'll look over the code and see if i can help at all.


EDIT: Has anyone compiled the source with eclipse? I haven't used it for C programming before (All my previous C programming was in MPLAB) If you've gotten it to compile in eclipse, could you explain how?
« Last Edit: September 14, 2006, 09:06:28 pm by Turey »
Creator of the FreeSpace Open Installer.
"Calm. The ****. Down." -Taristin
why would an SCP error be considered as news? :wtf: *smacks Cobra*It's a feature.

 

Offline Freespace Freak

  • 28
  • Official forum permanewb
Re: I wrote my first bit of source code!
What program are you using to actually write the code?

I use Eclipse with Java, it also has different colours and it does the indentions automagically when you enter something inside, say, wave brackets... There are instructions on how to use Eclipse on C and C++ programming, too.

http://www.eclipse.org/

How to Use Eclipse for C and C++ Development (link also in the main page of eclipse.org)



Er, win32 console application, I think.  I just installed Visual C++ introductory edition on my PC, but I can't recreate what I did in class.  I guess I didn't take good enough notes.  :nervous:

 

Offline Freespace Freak

  • 28
  • Official forum permanewb
Re: I wrote my first bit of source code!
I made my first C program, all by myself.   :)  Again, it's a Win32 console app, and I wrote it in both C and C++ so I can understand C just for the SCP!  It's pretty simple, though.  Here it is, if you're interested.

Code: [Select]
#include<stdio.h>
int main ()

{

//Declare Variables

const double PI=3.1415926536;
const double r_earth=6371000;
double X0; //extra length
double c_earth; //circumference of earth (constant)
double c_band, r_band, height; //calculated values

//Input Data
printf("\n HEIGHT OF HYPOTHETICAL ELASTIC BAND");
printf("\n\n Suppose an inelastic band was to be fitted around the circumference of");
printf("\n the Earth at the equator.  If this band was to be cut and had an additional");
printf("\n length added to it, it would be a certain heigth above the surface of the");
printf("\n Earth, if it could float freely.");
printf("\n\n This program calculates the height of that band, given the length inserted.");
printf("\n\n Enter extra length of band in meters:");
scanf("%lf", &X0);

//Calculate
c_earth = 2 * PI * r_earth;
c_band  = c_earth + X0;
r_band  = c_band / (2 * PI);
height  = r_band - r_earth;

//Output Data
printf("\n Height of band: %8.7f meters", height);
printf("\n\n");
return (0);
}

 
Re: I wrote my first bit of source code!
Looks good. Although I'd put the \n at the end rather than the front, but thats just a nitpick. 

BTW is scanf("%lf", &X0) overflow safe?  (Otherwise strlin >> X0 would work)
That's cool and ....disturbing at the same time o_o  - Vasudan Admiral

"Don't play games with me. You just killed someone I like, that is not a safe place to stand. I'm the Doctor. And you're in the biggest library in the universe. Look me up."

"Quick everyone out of the universe now!"

 

Offline Freespace Freak

  • 28
  • Official forum permanewb
Re: I wrote my first bit of source code!
Looks good. Although I'd put the \n at the end rather than the front, but thats just a nitpick. 

BTW is scanf("%lf", &X0) overflow safe?  (Otherwise strlin >> X0 would work)

%lf and &X0 are C-style formats, while >> X0 and such are C++.  What we call C exist nowadays within C++ compilers, it's just referred to as standard C formatting.  scanf, printf and such are examples.  One of the big advantages that C has over C++ is that you can get it to output exactly the way you want it to without using iomanip.h.  It even does it better than iomanip.h can in fact, all without having to include a seperate .h.  C uses stdio.h, while C++ uses iostream.h.  Here's a copy of the exact same program in C++.

Code: [Select]
//Setup

#include<iostream.h>

int main ()
{

//Declare Variables
const double PI=3.1415926536;
const double r_earth=6371000;
double X0; //extra length
double c_earth; //circumference of earth (constant)
double c_band, r_band, height; //calculated values

//Input Data
cout << "\n HEIGHT OF HYPOTHETICAL ELASTIC BAND";
cout << "\n\n Suppose an inelastic band was to be fitted around the circumference of";
cout << "\n the Earth at the equator.  If this band was to be cut and had an additional";
cout << "\n length added to it, it would be a certain heigth above the surface of the";
cout << "\n Earth, if it could float freely.";
cout << "\n\n This program calculates the height of that band, given the length inserted.";
cout << "\n\n Enter extra length of band in meters:";
cin  >> X0;

//Calculate
c_earth = 2 * PI * r_earth;
c_band  = c_earth + X0;
r_band  = c_band / (2 * PI);
height  = r_band - r_earth;

//Output Data
cout << "\n Height of band: "<< height << "meters";
cout << "\n\n";
}

 
Re: I wrote my first bit of source code!
I was mostly worried about the scanf's overflow security.
That's cool and ....disturbing at the same time o_o  - Vasudan Admiral

"Don't play games with me. You just killed someone I like, that is not a safe place to stand. I'm the Doctor. And you're in the biggest library in the universe. Look me up."

"Quick everyone out of the universe now!"

 

Offline Freespace Freak

  • 28
  • Official forum permanewb
Re: I wrote my first bit of source code!
I was mostly worried about the scanf's overflow security.

Not really sure what that is, all I know is that it works without any errors or warnings, except it complains that I don't have anything written in the parenthesis for int main.

Edit:  %lf and &X0 are actually very necessary in standard C format.  "%lf"  tells the computer to assign a long float (long double) number to memory (I actually could have used just "%f" since f, for float, includes double), and the & sign tell the computer to reference that memory location as being whatever follows the & sign, which in this case is X0.  >> X0 wouldn't work because <stdio.h>, which is the standard C format, won't understand it.
« Last Edit: September 30, 2006, 05:53:17 pm by Freespace Freak »

 

Offline Spicious

  • Master Chief John-158
  • 210
Re: I wrote my first bit of source code!
Doesn't scanf have overflow problems just with strings?

%f doesn't work for doubles with scanf.

 

Offline Freespace Freak

  • 28
  • Official forum permanewb
Re: I wrote my first bit of source code!
Doesn't scanf have overflow problems just with strings?

%f doesn't work for doubles with scanf.

For strings in C, you make %s.  I respectfully point out that you're wrong on the doubles thing, at least according to the manual right here.  For float and double float, you use %f.  For long double you use %lf.  If you doubt me, then compile the code and run it under win32 console app.  It works, trust me.

 

Offline WMCoolmon

  • Purveyor of space crack
  • 213
-C

 

Offline Freespace Freak

  • 28
  • Official forum permanewb
Re: I wrote my first bit of source code!
That's exactly the same reference my proffesor pointed me to.

I retract my earlier statement.  %f is for float, %lf is long float, aka double, and %Lf is for long double.  I'm still not seeing what you're saying about overflow control, though.  As far as I know, if you use << or >> in stdio.h the compiler simply won't understand wtf you're talking about, although I think iostream.h will understand scanf and printf and such.

Heh, I'm getting good at this.  I just wrote another program that calculates the square yardage of concrete needed to build a circular walkway around a garden, given the diameter of the garden, the width of the walkway, and the desired thickness of the walkway.  Works like a charm.   :)
« Last Edit: October 02, 2006, 03:49:38 pm by Freespace Freak »