I will explain in the easiest way the difference between the function and recursive function in C language. Simple Answer is argument of the function is differ but in the recursive function it is same:) Explanation: Function int function(int,int)// function declaration main() { int n; ...... ...... n=function(a,b); } int function(int c,int d) { ...... ...... ...... } recursive Function: int recursive(int,int)// recursive Function declaration main() { int n; ..... ..... ..... ..... n=recursive(a,b); } int recursive(int a,int b) { ..... .... .... .... } Carefully see, In the recursive Function the function arguments are same.
Code example:/* ******************************************************************************** * FUNCTION: bIsAPrime ******************************************************************************** * AUTHORS: * Flaun * * DESCRIPTION: * Finds if a given number is a prime number. * * PARAMETERS: * uiTestNumber: An unsigned integer to test. * * RETURNS: * bTRUE or bFALSE ******************************************************************************** */ #define bTRUE 1 #define bFALSE 0 #define iNO_REMAINDER 0 #define bIS_ODD(uiNumber) ((uiNumber) & 1) typedef int BOOL BOOL bIsAPrime( unsigned int uiTestNumber) { /* 4 is the first non prime number. */ if(uiTestNumber > 3) { /* The only even prime number is 2. */ if(!bIS_ODD(uiTestNumber)) { return bFALSE; } else { /* The only numbers left to divide against are 3, 5 and 7. */ /* All numbers that are divisible by a number > 7 are divisible */ /* by a number < 8. */ unsigned int uiDivisor = 0; for(uiDivisor = 3; uiDivisor
i=0; do{ i++; }while(i<10);
whatever the variables we declare in function signature to receive the arguments at the calling that are known as parameters.. e.g. int sum(int a,int b); here a & b are known as parameters.....
// The "LCMTest" class. import hsa.*; public class LCMTestDanyalM { // Place your lowestCommonMultiple code here public static int lowestCommonMultiple (int firstNumber, int secondNumber) { /// int product = 1; // int limit = firstNumber * secondNumber; for (int count = 1 ; ; count++) { if (count % firstNumber 0) { return count; } } } // Do not modify the code below public static void main (String [] args) { Console c = new Console (); final int NO_OF_TEST_CASES = 1000000; // maximum of 1000000 int [] first = new int [NO_OF_TEST_CASES]; int [] second = new int [NO_OF_TEST_CASES]; int [] LCM = new int [NO_OF_TEST_CASES]; c.println ("Loading the data..."); TextInputFile inFile = new TextInputFile ("lcmdata.txt"); int index = 0; while (!inFile.eof () && index < first.length) { first [index] = inFile.readInt (); second [index] = inFile.readInt (); LCM [index] = inFile.readInt (); index++; } inFile.close (); // Testing the correctness and speed c.println ("Start Timing"); long startTime = System.currentTimeMillis (); for (int nextTest = 0 ; nextTest < first.length ; nextTest++) { int studentLCM = lowestCommonMultiple (first [nextTest], second [nextTest]); if (studentLCM != LCM [nextTest]) { c.println ("Incorrect: For " + first [nextTest] + " and " + second [nextTest] + " you got " + studentLCM + " for the lowest common mulitple," + "\nbut the answer should be: " + LCM [nextTest]); break; } } long endTime = System.currentTimeMillis (); c.print ("Total Time: "); c.println ((endTime - startTime), 20); c.println ("Program Complete"); } // main method } // Perfect class
The value range. Example for 16-bit integers: signed: -32768..32767 unsigned: 0..65535
There are far more than 4 integral types in C++. As of C++11, there were 27 integral types: bool char signed char unsigned char wchar_t char16_t char32_t short signed short unsigned short short int signed short int unsigned short int int signed int unsigned int long signed long unsigned long long int signed long int unsigned long int long long signed long long unsigned long long long long int signed long long int unsigned long long int
A signed int can take a value between -32768 to 32767 and an unsigned int can take a value 0 to 65535.
Unsigned int does not have a sign, meaning that it can be zero or a positive number in the range of data type (int). Signed data has a sign and can be positive, zero or negative.
A type modifier changes the default variable type to the other possible type. An int is signed by default and may be made unsigned int a; unsigned int b;
For example 'int' is a data-type, 'short', 'long', 'signed' and 'unsigned' are modifiers, 'extern', 'auto', 'static', 'register' are storage-classes. The declarations go like this: storage-class modifiers data-type identifier example: static unsigned short int x;
signed char unsigned long int void *
There are four modifiers in C++: long, short, signed and unsigned. They are used to modify primitive types (int, char, float and double) to change their behaviour. If no type is specified, int is assumed. Thus a long long turns a 32-bit integer into a 64-bit integer while unsigned ensures an integer is always in the positive range.
Well, uh, const unsigned int and const signed int..
There are five type modifiers in C++: long short signed unsigned [] - the array subscript The first four are used solely to modify the characteristics of the int type. Signed and unsigned can also be applied to the char and wchar_t types while long can also be applied to a double. If no type is specified, int is assumed. The array subscript modifier can be applied to any type including modified types and pointers to a type. Excluding arrays, pointers and the built-in bool type, C++ has the following fundamental (built-in) types: char signed char unsigned char wchar_t signed wchar_t unsigned wchar_t int signed int unsigned int short int signed short int unsigned short int long int signed long int unsigned long int long long int signed long long int unsigned long long int float double long double A plain (unmodified) char may be signed or unsigned (implementation defined) but must behave exactly as an explicitly signed or unsigned char would. However, all three are deemed separate types in C++. Similarly with wchart_t. However, an int is implicitly signed. The length of each fundamental type is implementation defined, but can be found by using the sizeof operator either upon the type itself or upon an instance of the type. The standard library <type_traits> header can be used to determine other information, particularly useful in static (compile-time) assertions. The C++ standard library also provides additional types of specific size, including int8_t, int16_t, int_32_t, int64_t, uint_8_t, uint_16_t, uint32_t and uint64_t. Note that the _t suffix is often used to denote that a type is really a typedef (an alias) for a modified type, such as size_t (an unsigned int). However wchar_t is typically implemented as a built-in type. All fundamental types can be found in <cstddef> (which is usually included when you include any standard library header such as <iostream>) while the fixed-length integers can all be found in <cstdint>.
The primitive data types in C include:[signed|unsigned] char[signed|unsigned] short[signed|unsigned] int[signed|unsigned] long[signed|unsigned] long longfloatdoublelong doubleEnumerations (enum) and arrays may also be considered to be primitive types.
#include <iostream> #include <math.h> // for std::pow() unsigned int get_length(unsigned int num,const unsigned int base=10) { unsigned int len=1; while(num && (num/=base)) ++len; return( len ); } bool is_armstrong(const unsigned int num,const unsigned int base=10) { unsigned int len=get_length(num,base); unsigned int sum=0; unsigned int tmp=num; while(tmp) { sum+=(unsigned int)std::pow((double)(tmp%base),(double)len); tmp/=base; } return(num==sum); } int main() { std::cout << "Armstrong series (base 10):"; for(unsigned int num=0; num<=0xffffffff; ++num) if(is_armstrong(num)) std::cout << " " << num; std::cout << std::endl; return(0); }
if it is a signed int the the range is -32768 to 32767if its unsigned then 0 to 65535
#include <iostream> #include <math.h> // for std::pow() unsigned int get_length(unsigned int num,const unsigned int base=10) { unsigned int len=1; while(num && (num/=base)) ++len; return( len ); } bool is_armstrong(const unsigned int num,const unsigned int base=10) { unsigned int len=get_length(num,base); unsigned int sum=0; unsigned int tmp=num; while(tmp) { sum+=(unsigned int)std::pow((double)(tmp%base),(double)len); tmp/=base; } return(num==sum); } int main() { std::cout << "Armstrong series (base 10):"; for(unsigned int num=0; num<=0xffffffff; ++num) if(is_armstrong(num)) std::cout << " " << num; std::cout << std::endl; return(0); }
#include <iostream> #include <math.h> // for std::pow() unsigned int get_length(unsigned int num,const unsigned int base=10) { unsigned int len=1; while(num && (num/=base)) ++len; return( len ); } bool is_armstrong(const unsigned int num,const unsigned int base=10) { unsigned int len=get_length(num,base); unsigned int sum=0; unsigned int tmp=num; while(tmp) { sum+=(unsigned int)std::pow((double)(tmp%base),(double)len); tmp/=base; } return(num==sum); } int main() { std::cout << "Armstrong series (base 10):"; for(unsigned int num=0; num<=0xffffffff; ++num) if(is_armstrong(num)) std::cout << " " << num; std::cout << std::endl; return(0); }
int x;float x;char x;double x;long x;long long x;short x;unsigned int x;unsigned float x;unsigned double x;signed char x;unsigned long x;unsigned long long x;int *x;float *x;char *x;double *x;long *x;long long *x;int x[100];typedef struct rect {int left;int top;int right;int bottom;};int main(int argv, char* argc[]) {return 0;}That enough for you?Well, these are definitions, declarations are like these:extern char x;int main (int argc, char **argv);
factorial using recursion style in c++ is unsigned int fact(unsigned int a) { if (a<=1) return 1; else { f*=fact(a-1); return a; } } when using looping structure factorial is unsigned int fact (unsigned int n) { unsigned int i,f=1; for(i=1;i<=n;i++) f*=i ; return f; }
#include <iostream> #include <time.h> unsigned int reverse( unsigned int num ) { const unsigned int base=10; unsigned int rev=0; while( num ) {rev*=base;rev+=num%base;num=num/base;} return( rev ); } int main () { srand(( unsigned ) time( NULL )); // Print 10 random numbers in reverse. for( int i=0; i<10; ++i ) {unsigned int r = ( unsigned int ) rand();std::cout << r << " = " << reverse(r) << std::endl;} return(0); }
In terms of storage there is no difference. A signed int is the same length (in bits) as an unsigned int. The only difference is the range of values that can be represented by those bits. With signed integers, the most-significant bit is used to denote the sign (0 = positive, 1 = negative), so an 8-bit signed char really only has 7-bits to store the value, thus limiting its positive range to [0:127] whereas the unsigned equivalent has the full 8-bits at its disposal so can represent values in the range [0:255]. Another difference is the way in which negative values are physically represented. There are in fact two ways of achieving this depending on the hardware. Older hardware generally uses ones-complement notation, which simply switches every bit. Thus 00101010 (+42) becomes 11010101 (-42). However, since the value zero is neither positive nor negative, we end up with two representations for the value zero (00000000 and 11111111). This also limits the range to [-127:127] for 8-bit signed values. Newer hardware uses twos-complement notation. To switch the sign, we first take the ones-complement as before but we then add 1 and ignore any overflow. Thus 00000000 becomes 11111111 + 1 which is 00000000. This, in turn, increases the negative range by 1, such that an 8-bit signed char now has a range of [-128:127]. Generally speaking, we use unsigned integers whenever we need to represent natural numbers, such as file sizes or array sizes; values that can never be negative. For all other work, we use signed integers. Note that in C++ a plain char may be signed or unsigned (depending on the implementation), but is not considered the same type as either a signed or unsigned char. They are three completely independent types. Thus a plain char is only guaranteed to represent values in the range [0:127], the ASCII character codes; the only values common to both signed and unsigned char. If you specifically need to represent values outwith this range, such as [-128:127] or [0:255], use an explicitly signed or unsigned char respectively. All other integer types (short, int, long and long long) are implicitly signed unless you explicitly declare them with the unsigned modifier. Note also that floats, doubles and long doubles are always signed (they cannot be modified).
#include <math.h> inline unsigned int get_num_digits(const unsigned int n) { return ((unsigned int) log10(n) + 1); }