tag:blogger.com,1999:blog-43894438623381860182024-03-14T04:33:27.028+02:00Dystopian CodeProgramming TutorialsUnknownnoreply@blogger.comBlogger117125tag:blogger.com,1999:blog-4389443862338186018.post-40081828719373417172013-01-13T07:47:00.002+02:002013-01-13T07:57:41.195+02:00Reading from Console in ANSI CThe ANSI C standard I/O library (stdio.h) provides us with 3 powerful functions for reading data from STDIN (the stream used for the console):
-Reading a character: getchar
-Reading formatted input: scanf
-Reading an entire line: gets
Here's a short example on how you can use any of these functions to read a string:
#include<stdio.h>
int main(void)
{
const int LENGTH = 100;
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4389443862338186018.post-60105185774687759802013-01-08T01:32:00.003+02:002013-01-08T02:12:11.419+02:00How to Print to Console in ANSI CThe ANSI C standard I/O library (stdio.h) provides us with 3 powerful functions for outputting characters to STDOUT (the stream used for the console):
-Printing a character: putchar
-Printing a string : puts
-Printing a formatted string : printf
A formatted string is a dynamical string that is built on the go. Here's an example:
#include<stdio.h>
int main(void)
{
int myInt Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4389443862338186018.post-11698939200198246152013-01-04T00:14:00.002+02:002013-01-05T03:25:48.327+02:00Using sizeof to Determine the Size of a Data Type in ANSI CThe size of an ANSI C data type is dependent on the compiler. However it can be easily determined by using the sizeof operator.
Here's a example on how to do it:
#include<stdio.h>
typedef struct
{
int field1;
long field2;
char field3[30];
}ExampleStructure;
typedef union
{
int field1;
double field2;
}ExampleUnion;
typedef enum
{
enum1,
enum2,
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4389443862338186018.post-42267636719688355762012-08-19T18:54:00.002+03:002012-09-12T23:24:24.776+03:00Integer Rounding in ANSI C
1.Unsigned Integers
When two unsigned integers are divided, the result will be rounded down.
result = numerator / denominator;
To round up a result you must apply the following idiom:
result = (numerator + denominator - 1) / denominator;
Example:
#include<stdio.h>
int main(void)
{
unsigned int numerator = 50;
unsigned int denominator = 25;
unsigned int upResult;
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4389443862338186018.post-17107394743028755962012-08-19T16:31:00.002+03:002012-09-13T00:00:15.564+03:00Creating a MessageBox / Message Dialog in Java SwingTo create a dialog in Swing, you will need to use the JOptionPane class.
If you want to trigger a message dialog, you will need to use the static method:
showMessageDialog(Component parentComponent, Object message, String title, int messageType, Icon icon)
1.The Parent Component
Defines the component (usually a JFrame or a JPanel) that will be the parent of the dialog. The Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4389443862338186018.post-622697284180424392012-08-19T12:39:00.001+03:002012-09-12T23:38:33.553+03:00Installing and Configuring JavaDoc for Netbeans in Ubuntu
If you encounter such an error, you probably don't have JavaDoc installed or you don't have it configured correctly.
The JavaDoc error
1.Installing JavaDoc
You can install it using command-line or a package manager.
To install it command-line open a terminal and type:
sudo apt-get install openjdk-6-doc
If you want to install it using Ubuntu Software Center, open it and search for Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4389443862338186018.post-20783920175242761632012-08-18T12:28:00.003+03:002012-08-18T12:30:30.870+03:00Removing Shortcuts From the Gnome Applications Menu ManuallyIf you installed a program without using the Ubuntu Software Center and there is no uninstaller (or you just deleted the folder), the shortcut to your removed application may still remain in the Application Menu.
If you try to remove it using Alacarte, you may be surprised to see that your shortcut doesn't appear there. If you find it in Alacarte, read this.
This happened to me when I Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4389443862338186018.post-19516171411466519262012-08-17T19:59:00.002+03:002012-09-12T23:58:43.285+03:00Euclid's GCD Algorithm in ANSI CIn mathematics, Euclid's algorithm is a simple yet efficient method for computing the greatest common divisor of two positive integers.
There are 3 possible implementation for Euclid's algorithm:
1. Division-Based Euclid
/*
* Description:
* Returns the greatest common denominator of a and b using the
* Euclid's algorithm division-based.
* Parameters:
* a,b - two positive Unknownnoreply@blogger.com1tag:blogger.com,1999:blog-4389443862338186018.post-68394385143918124312012-08-17T13:32:00.001+03:002012-09-12T23:59:48.762+03:00Generic Bitcount Algorithm in ANSI CThe purpose of the Bitcount algorithm is to return the number of bits set to 1 in a memory block.
A simple implementation of the Bitcount algorithm would be:
/*
* Description:
* The function returns the number of bits set the 1 in a integer
* Parameters:
* data - the integer who will be verified
* Returns:
* The number of bits set to 1
*/
int SimpleBitCount(unsigned long data)
{
/Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4389443862338186018.post-85940051445089169432012-08-17T10:09:00.002+03:002013-01-13T08:18:09.123+02:00Using Enumerations in ANSI CIn ANSI C, a enumeration is a collection of integer constants which share something in common. The enumeration is somewhat of an analogy to a set. Also, enumerations are a better alternative than a group of macrodefinitions.
They can be declared as:
#include<stdio.h>
enum Color
{
RED ,
GREEN ,
BLUE ,
};
int main(void)
{
printf("RED = %d\nGREEN = %d\nBLUE = %d",RED,GREENUnknownnoreply@blogger.com0tag:blogger.com,1999:blog-4389443862338186018.post-59536378115830939482012-08-15T10:20:00.002+03:002012-09-13T19:29:41.730+03:00Basic Pointer Operations in ANSI CPointers are variables that hold a memory address. Usually that memory address is the first memory address of another variable or a dynamically allocated block. The type of the pointer indicates to what kind of variable it points to.
Example:
/*A pointer to a int variable*/
int *myPointer;
/*A pointer to a char variable*/
char *myCharPointer;
/*A pointer to a double Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4389443862338186018.post-15366329246167574492012-08-15T09:05:00.001+03:002012-09-13T19:29:11.522+03:00Call by Value and Call by Reference in ANSI C.
In ANSI C, variables can be referred directly or indirectly depending on the context.
A variable who is referred indirectly is a variable who is called-by-value. This means that when you pass the variable to a function, a hard copy of the variable will be created and used in that function. Any modifications that occur to the variable while in the function will not happen toUnknownnoreply@blogger.com0tag:blogger.com,1999:blog-4389443862338186018.post-38503705746341034282012-08-11T19:04:00.002+03:002012-09-13T19:28:53.162+03:00Creating a Dynamic Matrix in ANSI C
The memory of a dynamic matrix can be allocated at run time as opposite to the static matrices (who are allocated at compile-time).
The allocation algorithm is based on the function void* malloc(size_t size). The first thing that must be done is to allocate the space for the entire matrix. After that, each row from the matrix must be allocated separately.
/*
* Description:
* Allocates Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4389443862338186018.post-73864894911550291652012-08-11T12:38:00.001+03:002012-09-13T19:28:29.663+03:00Conditional Inclusion in ANSI CLet us assume that you have a program that will run on multiple platforms. For each platform you have an API which defines the functions that you will call in your program.
Example Scenario:
Your platforms are Windows, Unix, BSD and Mac. The headers for the platform specific operations are:
-Windows: windows_api.h
-Unix: unix_api.h
-BSD: bsd_api.h
-Mac: mac_api.h
Here's an example on how to Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4389443862338186018.post-56879572723972683872012-08-11T11:23:00.001+03:002012-09-13T19:28:14.922+03:00Avoiding Multiple Header Inclusion in ANSI CLet us assume that you have 2 header files a.h and b.h and one source file main.c.
The files are in following relation:
Double-inclusion scenario
Let assume that this is the initial content of the files:
The content of b.h:
/*b.h*/
typedef enum
{
val1 = 1,
val2,
val3
}EValues;
The content of a.h:
/*a.h*/
#include"b.h"
const int resource2 = val1+val2;
The content Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4389443862338186018.post-49421288923002760912012-08-11T09:51:00.001+03:002013-01-13T08:15:48.688+02:00The Literal Suffixes and Prefixes of ANSI CIn order to improve the readability of your code , you can declare constant literal values using suffixes.
Also, if you want to initialize integers in other radix than decimal, you can use prefixes. Characters can be as well declared using the octal, decimal and hexadecimal.
Here are some examples:
/*-----------------------------------------------
Integer constants
------------------------Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4389443862338186018.post-18763283382937587222012-08-06T22:36:00.003+03:002012-09-13T19:27:29.687+03:00Reversing a String in ANSI CIf you're not familiar with how strings are represented in ANSI C, you should read this first:
Concatenating, Copying and Comparing Strings in ANSI C
We shall reverse the string in situ, without using an additional string. The implementation for this function is:
/**
* Description:
* Reverses the received string.
* Parameters:
* string - a pointer to the string who will be reversed
* Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4389443862338186018.post-84252184433003981972012-08-04T20:13:00.004+03:002012-09-13T19:27:10.286+03:00Other Matrix Operations in NMatIn order to understand this article, you should read the following articles first:
The NMat Library
Creating, Destroying and Copying a Matrix
NMat provides several other operations for matrix calculus, such as:
-Finding the minimum/maximum element of a matrix
-Computing the sum/product of all elements of a matrix
-Extracting rows and columns from a matrix into a submatrix
-Computing the Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4389443862338186018.post-79512918422612019892012-08-04T00:28:00.004+03:002012-09-13T19:26:51.400+03:00The NMat LibraryNMat is a ANSI C library that provides an API for matrix operations.
The current operations implemented by NMatlib are:
Operation
Description
Create
Creates a NMatrix object
Destroy
Destroys a NMatrix object
Clone
Creates a hard copy of an existent NMatrix object
Sum
Computes the sum of two matrices
Scalar Multiplication
Multiplies the matrix with aUnknownnoreply@blogger.com0tag:blogger.com,1999:blog-4389443862338186018.post-154091863769135772012-08-02T19:20:00.001+03:002012-09-13T19:26:30.919+03:00Defining an Object for the GDS LibraryIf you don't know about the basics of the GDS library, be sure to read this first:
The Generic Data Structure Library
In order to work with the data structures defined in the GDS library, your structure (which we shall call an object from now on) must be defined in a certain way. In this article I will provide you with a simple example on how to define such an object.
The first Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4389443862338186018.post-2527960487333948962012-07-29T20:45:00.002+03:002012-09-13T19:25:43.399+03:00The Generic Data Structures LibraryThe purpose of the GDS (Generic Data Structures) Library is to provide type-independent data structures such as lists, sets, trees or arrays for the C programming language.
A GDS object contains 2 data structures:
-A interface data structure which contains the object's generic behaviors:
-Copy (Used for creating a hard copy of the object)
-Destroy (UsedUnknownnoreply@blogger.com0tag:blogger.com,1999:blog-4389443862338186018.post-60793545055429883442012-07-27T19:30:00.000+03:002012-09-13T19:24:35.095+03:00Temperature Conversion Library in JavaThis library allows you to perform conversion between the Celsius, Kelvin, Rankine, Delisile, Newton, Reaumur, Romer and Fahrenheit temperature scales.
Here's an example on how to use the library:
package temperatureconversion;
public class TestLauncher
{
public static void main(String[] args)
{
Temperature t1 = Temperature.fromCelsius(-273.15);
Temperature t2 = Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4389443862338186018.post-89836268649206384402012-07-27T15:15:00.003+03:002012-07-27T15:16:10.735+03:00Designing Applets with the Netbeans GUI Designer
If you want to use the Netbeans GUI editor for an Applet you must follow the next steps:
1. In the menu bar go to File -> New Project and then select Java->Java Application
2.Name your application and select a location for the project
3, Select the package where you want to add your applet and then right click -> New -> Other. After that go to Swing GUI Forms -> JApplet Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4389443862338186018.post-58945185030842404022012-07-27T12:20:00.003+03:002012-09-13T19:25:02.469+03:00Pangram Checking Algorithm in ANSI CA pangram is a sentence that contains at least once all the letters of the alphabet. A well known pangram in the English language is the sentence "A quick brown fox jumps over the lazy dog".
To check if a string is a pangram we must implement a flag vector containing entries for each letter of the alphabet. A flag will be set if the corresponding letter is found in the stringUnknownnoreply@blogger.com0tag:blogger.com,1999:blog-4389443862338186018.post-72882503530114537682012-07-27T10:56:00.000+03:002012-09-13T19:23:58.045+03:00Anagram Checking Algorithm in ANSI CAn anagram is a word or phrase formed by reordering the letters of another word or phrase [Free Online Dictionary].
The easiest method to check if a string is an anagram of another string is to build the letter frequency statistics for both strings and compare them. If the statistics vectors are equal, then the strings are anagrams.
An implementation of such function is:
/*
* Unknownnoreply@blogger.com0