/* preprocessor directive */
#include
/* preprocessor directive */
#define KMPERMILE 1.6
/* main() function: beginning */
int main() {
/* integer variable declaration */
int miles;
/* float variable declaration */
float km;
/* output */
printf("\n\nMiles2Kilometers'R'US!\n");
printf("\n");
printf("How many miles? ");
/* input */
scanf("%d", &miles);
/* assignment statement */
km = miles * KMPERMILE;
/* formatted output */
printf("%d miles is %f kilometers\n", miles, km);
/* return statement */
return 0;
/* end of main() function */
}
C Miles To Kilometers Converter Program
CSS Constrain An Image To A Box Keeping Proportion No Distortion
Very simple and very useful css trick. Using CSS it constrains an image to a box while keeping it in the right proportion without distorting it. Just set either the width or the height and it will maintain the original ratio. Or to ‘trim’ it you could do this with the overflow hidden:
<div style="width: 100px; height: 100px;
overflow: hidden;">
<img src="pic.jpg" style="width: 100px;" />
</div>
C Floating Point Division Example
/* preprocessor directive */
#include <stdio.h>
/* main() function: beginning */
int main()
{
float x,y,z;
x = 4.0;
y = 5.0;
z = x/y;
printf(”%f\n”, z);
return 0;
}
/* end of main() function */
How To Compile C And C++ Programs With Linux
1. Editing source files
a) Use either the emacs/xemacs editor or vi editor (can be difficult to use) or pico (easy to use). If you use emacs editor, you can compile and run your program from within this fancy editor.
b) Save the source file with a .cpp extension or .h extension if you are creating a header file.
2. Compiling and linking programs (done in one step)
a) For one source file, enter at the prompt
g++ filename
This will produce an executable with filename a.out
b) For multiple source files, enter g++ filename1 filename2 filename_etc.
c) To explicitly link in a library such as the math library, enter g++ filename -lm.
d) Executable file will automatically be called a.out unless you use the option -o output.
g++ -o output_filename input_filename
3. Running the program
a) Enter a.out (or the output filename if you used the -o option)
4. For debugging, you should compile your source code with the –g option. gdb –help will list help for the debugger. You can then use the following command for debugging.
gdb output_filename
How To Compile C And C++ Programs with Microsoft Visual C++ 6.0
1) Start the Microsoft Visual C++ 6.0, which is under Microsoft Visual C++ Developer Studio Meuu.
2) Select File | New and click the Projects tab. Click on Win32 Console Application and in the Location textbox position to the directory in which you want to create the project (Recommended directory is the directory of your intranet account). In the Project Name textbox type the name of the project, e.g. proj1 and set the radio button to “create new workspace.” Accept all other default settings. You do not need to add the extension .dsw to the project name (Visual C++ will do it). Click OK. In the window that follows, set the radio button to “An empty project.” Click the Finish button and then the OK button. You will create a proj1 subdirectory as well as the proj1.dsw project workspace file within the subdirectory.
3) To create source code files and insert them into the project, select File | New, click the Files tab and select the file type. Specify a file name, a directory location (should be the same directory as the one in which you created your project) and the project to which you want to add the file. Click OK to create a new file of the chosen type. Type in your source code and save it, e.g. save it as Hello.cpp in the proj1 directory.
4) If you need to add files to the project at a later date, select Project | Add to Project | New and click on the Files tab. You will see a window similar to the window from the step above.
5) To compile your project, click on Build | Build proj1.exe. Correct any compiler errors and if necessary, build the project again.
6) To run your program, select Build | Execute proj1.exe.
7) To run the debugger, select Build | Start Debug.
a) The submenu option Go executes the program from start to finish.
b) To use the debug options, select the submenu option Step Into. You will now see a main menu option called Debug.
i) F11 steps into functions.
ii) F10 steps over functions.
iii) Shift + F11 steps out.
c) The output screen can be seen by selecting the window from the screen bottom list of open windows.
d) You can also set up breakpoints (via the Edit menu or the hand icon or F9) and view the contents of variables.
e) Use the Debug | QuickWatch option to inspect the contents of variables. You can also see auto and local variables and “this” in the lower panels of your screen.
8) To close the project, click File | Close Workspace. After a project has been built, you can open it by selecting File | Open Workspace and selecting the .dsw file (the project workspace file) in the directory in which the project was originally created.
Java Organizing And Compiling Guide
Because of its unfussiness, this my favorite way of organizing and compiling Java programs.
Organizing Java
Here is the file SampleOranization.java which contains a class SampleClass and a driver to test it:
/////////////////////////////////////////////////////////////////////
//
// Java Organizing And Compiling Guide
//
/////////////////////////////////////////////////////////////////////
class TestClass
{
public static void main (String [] args)
{
SampleClass x;
x = new SampleClass();
x.test();
}
} // end class TestClass
/////////////////////////////////////////////////////////////////////
class SampleClass
{
public SampleClass()
{
}
public void test()
{
System.out.println("Test Works!");
}
} // end class SampleClass
/////////////////////////////////////////////////////////////////////
Notice slashes (/////////…) that separate classes. In more complex cases, I would recommend comments at the top of each class.
Similarly, you can separate methods with a row of the form //—–…, and I would recommend comment methods which are not self-explanatory.
Compiling Java
/user/root/java/ > ls
SampleOranization.java
Note that the directory contains only one file, SampleOranization.java. We now compile it.
/user/root/java/ > javac SampleOranization.java
/user/root/java/ > ls
total 6
SampleOranization.java
SampleClass.class
TestClass.class
Notice that I have created two class files, one for each class defined inside the file I just compiled (SampleOranization.java).
Notice that SampleOranization is not the name of a class inside the file SampleOranization.java, so I don’t a file SampleOranization.class does not get created.
Now let’s try runing the program. We use the java command and give it the name of the class which contains the method named main. In our case, this is the class TestClass.
/user/root/java/ > java TestClass
Test Works
This is the output we expect.
Best Coding And Debugging Guide
* Write small simple programs to test constructs you haven’t used before or about which you feel shaky. It’s easier to locate your misunderstanding in a short program than in a long one.
* When you’re debugging a program, pay attention only to the first error. The others may simply be side-effects of that first error.
* When you’re writing a program, keep the specification in front of you. How else can you be sure that you’re solving the correct problem?
* Don’t attempt to write out all the code at once and then try to debug it.
* Always build on existing code which compiles and runs.
* Grow programs incrementally. If you extend a working program and it doesn’t work anymore, then you can localize the error.
* Keep working checkpoint programs as you move along. If you seriously wound your code, you can retreat to a prior version which worked.
* Implement a large program in well-defined steps. Examine the problem and determine how you can implement the required capabilities in an incremental manner.
* Learn how to document errors:
· they must be repeatable;
· the code should be short;
· collect hardcopy of the program, the compile, and the test run.
* Human memory is fallible, and computers are unforgiving, so no one will believe you if you don’t have documentation. Experts can’t help you if you come in with a folktale — “I tried that, but it gave me an error.” You must be able to replicate the error.
* If you encounter an error that you can’t understand, then keep the code which generated the error and find out what was wrong. You may produce a working program by trying another approach, but if you don’t know why you got the error, then there’s a gap in your knowledge.
PHP Copyright Date Script
This is one of the most basic scripts you can
ever create with PHP but also extreemly
useful for web site copyright notices. It
doesn’t take any effort to replace your
current static year with the following
script.
© <?php echo date("Y"); ?> – eRunways
C++ RYG Guess The Random Number Game + Source Code
Program description: Program to generate a random
number b/n 100 and 999 and accept users guess.
The user keeps guessing untill he guesses the random
number. The program gives the user a hint if he guessed
one of the digits but its one wrong place.
RED: you got a number wrong
YELLOW: you got a digit close
GREEN: you got a digit right
Python Socket Programming Client and Server
Sockets are one of the most popular means of network communication and they follow client-server architecture. I have created two programs, one to run on a client computer and
the other on the server computer. In my program I am showing how using a sockets client program can communicate to a server. The client program is run at the client-side while
the server program runs in the Internet. I am using a socket module which is provided by Python. When the client runs the echo-client.py program it is prompted to enter the
address to the server which could either be a domain name which I used for the testing or an IP address. Keep in mind that the server side program echo-server.py has to be running
in order for the connection to be established. Once the user enters the address to the server it’s prompted to enter a message for the server. Once he enters the message and presses
enter the message is send to the server. The server side program gets the message and displays it and also returns it back to the client. The final step for the server is to close itself
once that one cycle is complete. The client on the other side displays a message that the message has been received by the user and even shows it just to so the user is sure of it and
finally closes itself.







