That' what sizeof is good for.
It means to explicitly convert to a certain type.double a = 5.0;int b;b = (int) a;The (int) is the explicit typecast in this case, but any type can be used, including a user-defined type (i.e., a class name). If you omit it, the compiler will complain, because when you convert a double to an int, precision may be lost. If you include it, you override this compiler check - you basically tell the compiler that you know what you are doing.It means to explicitly convert to a certain type.double a = 5.0;int b;b = (int) a;The (int) is the explicit typecast in this case, but any type can be used, including a user-defined type (i.e., a class name). If you omit it, the compiler will complain, because when you convert a double to an int, precision may be lost. If you include it, you override this compiler check - you basically tell the compiler that you know what you are doing.It means to explicitly convert to a certain type.double a = 5.0;int b;b = (int) a;The (int) is the explicit typecast in this case, but any type can be used, including a user-defined type (i.e., a class name). If you omit it, the compiler will complain, because when you convert a double to an int, precision may be lost. If you include it, you override this compiler check - you basically tell the compiler that you know what you are doing.It means to explicitly convert to a certain type.double a = 5.0;int b;b = (int) a;The (int) is the explicit typecast in this case, but any type can be used, including a user-defined type (i.e., a class name). If you omit it, the compiler will complain, because when you convert a double to an int, precision may be lost. If you include it, you override this compiler check - you basically tell the compiler that you know what you are doing.
Variable-declaration is: extern int x; extern double y; extern char a; Variable-definition is: int x; static double y; auto char a; Variable-definition with initialization is: int x = 1; static double y= 2.3; auto char a = 'w';
#include<iostream> #include<time.h> #include<iomanip> #include<string> void swap(int& x, int& y) { x^=y^=x^=y; } void bubble_sort(int* A, int size) { while(size) { int n=0; for(int i=1; i<size; ++i) { if(A[i-1]>A[i]) { swap(A[i-1], A[i]); n=i; } } size=n; } } void insertion_sort(int* A, int size) { for(int i=1; i<size; ++i) { int value=A[i]; int hole=i; while( hole && value<A[hole-1] ) { A[hole]=A[hole-1]; --hole; } A[hole]=value; } } void selection_sort(int* A, int size) { for(int i=0; i<size-1; ++i) { int j=i; for(int k=i+1; k<size; ++k) if(A[k]<A[j]) j=k; if( i!=j ) swap(A[i],A[j]); } } void sort(int* A, int size, int sort_type) { switch(sort_type) { case(0): bubble_sort( A, size ); case(1): insertion_sort( A, size ); case(2): selection_sort( A, size ); } } int* copy_array(int* A, int size) { int* copy=new int[size]; memcpy(copy, A, size*sizeof(int)); return(copy); } void print_array(int* A, int size, char* prompt) { std::cout<<prompt<<"\t"; for(int i=0; i<size; ++i) std::cout<<std::setw(2)<<A[i]<<" "; std::cout<<std::endl; } int get_rand(int range_min=0, int range_max=RAND_MAX) { return((int) ((double)rand() / (RAND_MAX + 1) * ((range_max + 1) - range_min) + range_min)); } int input_char(std::string prompt, std::string input) { char ch; do { std::cout<<prompt<<": "; std::cin>>ch; } while(input.find(ch)==std::string::npos); return(input.find(ch)%(input.size()/2)); } int main() { srand((unsigned) time(NULL)); int size = get_rand( 10, 80); if( int* A = new int[size] ) { for( int i=0; i<size; ++i ) A[i]=get_rand( 1, size ); int choice=input_char("Please select a sorting method:\n[B]ubble, [I]nsert, [S]election", "bisBIS"); std::cout<<"You chose "; switch(choice) { case(0): std::cout<<"bubble"; break; case(1): std::cout<<"insertion"; break; case(2): std::cout<<"selection"; break; } std::cout<<" sort...\n"<<std::endl; print_array( A, size, "Before sorting" ); sort(A, size, choice); print_array( A, size, "After sorting" ); delete [] A; } return(0); }
printf ("sizeof (int) = %d\n", (int)sizeof (int));
sizeof (long int) usually 4 or 8
It means to explicitly convert to a certain type.double a = 5.0;int b;b = (int) a;The (int) is the explicit typecast in this case, but any type can be used, including a user-defined type (i.e., a class name). If you omit it, the compiler will complain, because when you convert a double to an int, precision may be lost. If you include it, you override this compiler check - you basically tell the compiler that you know what you are doing.It means to explicitly convert to a certain type.double a = 5.0;int b;b = (int) a;The (int) is the explicit typecast in this case, but any type can be used, including a user-defined type (i.e., a class name). If you omit it, the compiler will complain, because when you convert a double to an int, precision may be lost. If you include it, you override this compiler check - you basically tell the compiler that you know what you are doing.It means to explicitly convert to a certain type.double a = 5.0;int b;b = (int) a;The (int) is the explicit typecast in this case, but any type can be used, including a user-defined type (i.e., a class name). If you omit it, the compiler will complain, because when you convert a double to an int, precision may be lost. If you include it, you override this compiler check - you basically tell the compiler that you know what you are doing.It means to explicitly convert to a certain type.double a = 5.0;int b;b = (int) a;The (int) is the explicit typecast in this case, but any type can be used, including a user-defined type (i.e., a class name). If you omit it, the compiler will complain, because when you convert a double to an int, precision may be lost. If you include it, you override this compiler check - you basically tell the compiler that you know what you are doing.
like this: sizeof(int); replace int with the kind of data you want to find the size of. of course, to be able to do anything with this size data, you'll have to store it into a variable or display it or something.
Variable-declaration is: extern int x; extern double y; extern char a; Variable-definition is: int x; static double y; auto char a; Variable-definition with initialization is: int x = 1; static double y= 2.3; auto char a = 'w';
#include<iostream> #include<time.h> #include<iomanip> #include<string> void swap(int& x, int& y) { x^=y^=x^=y; } void bubble_sort(int* A, int size) { while(size) { int n=0; for(int i=1; i<size; ++i) { if(A[i-1]>A[i]) { swap(A[i-1], A[i]); n=i; } } size=n; } } void insertion_sort(int* A, int size) { for(int i=1; i<size; ++i) { int value=A[i]; int hole=i; while( hole && value<A[hole-1] ) { A[hole]=A[hole-1]; --hole; } A[hole]=value; } } void selection_sort(int* A, int size) { for(int i=0; i<size-1; ++i) { int j=i; for(int k=i+1; k<size; ++k) if(A[k]<A[j]) j=k; if( i!=j ) swap(A[i],A[j]); } } void sort(int* A, int size, int sort_type) { switch(sort_type) { case(0): bubble_sort( A, size ); case(1): insertion_sort( A, size ); case(2): selection_sort( A, size ); } } int* copy_array(int* A, int size) { int* copy=new int[size]; memcpy(copy, A, size*sizeof(int)); return(copy); } void print_array(int* A, int size, char* prompt) { std::cout<<prompt<<"\t"; for(int i=0; i<size; ++i) std::cout<<std::setw(2)<<A[i]<<" "; std::cout<<std::endl; } int get_rand(int range_min=0, int range_max=RAND_MAX) { return((int) ((double)rand() / (RAND_MAX + 1) * ((range_max + 1) - range_min) + range_min)); } int input_char(std::string prompt, std::string input) { char ch; do { std::cout<<prompt<<": "; std::cin>>ch; } while(input.find(ch)==std::string::npos); return(input.find(ch)%(input.size()/2)); } int main() { srand((unsigned) time(NULL)); int size = get_rand( 10, 80); if( int* A = new int[size] ) { for( int i=0; i<size; ++i ) A[i]=get_rand( 1, size ); int choice=input_char("Please select a sorting method:\n[B]ubble, [I]nsert, [S]election", "bisBIS"); std::cout<<"You chose "; switch(choice) { case(0): std::cout<<"bubble"; break; case(1): std::cout<<"insertion"; break; case(2): std::cout<<"selection"; break; } std::cout<<" sort...\n"<<std::endl; print_array( A, size, "Before sorting" ); sort(A, size, choice); print_array( A, size, "After sorting" ); delete [] A; } return(0); }
printf ("sizeof (int) = %d\n", (int)sizeof (int));
void data type is used in function declaration. It can be explained by examle void add(int,int); this will tell the compiler that no value is going to be returned by the function. int add(int,int); this indicates that an integer type value will be returned by the function
The maximum size of INT is 1. If you go over then it will be an error.
Use the following functions: int sum (const int* const a, const unsigned size) { int s = 0; for (unsigned i=0; i<size; ++i) s += a[i]; return s; } double average (const int* const a, const unsigned size) { return (double) sum (a, size) / size; } Example usage: int main () { int[3] a = {74, 42, 64}; printf ("Sum = %d\n", sum (a, 3)); // Sum = 180 printf ("Average = %d\n", average (a, 3)); // Average = 60.0 return 0; }
With an explicit cast, for example (in Java): int i = 0; char c; c = (char) i; Please note that data may be lost in such a conversion; the explicit cast basically tells the compiler "go ahead; I know what I am doing". Without an explicit cast, the compiler won't accept the conversion.
#include<iostream> void insertion_sort(int* a,int len) { for(int i=1; i<len; ++i) { int* hole=a+i; int* prev=hole-1; int cur=*hole; while(hole!=a && cur<*(prev)) { *(hole)=*(prev); --hole, --prev; } *hole=cur; } } void print_array(int* a,int len) { for(int i=0; i<len; ++i) std::cout<<a[i]<<" "; std::cout<<std::endl; } int main() { int a[]={9,1,8,3,7,2,5,4,6}; int size=sizeof(a)/sizeof(a[0]); std::cout<<"Before:\t"; print_array(a,size); insertion_sort(a,size); std::cout<<"After:\t"; print_array(a,size); return(0); }
sizeof (long int) usually 4 or 8
It is an invalid declaration. If you declare like this then the compiler will terminate with a syntax error stating that there is no type 'Int' defined within this scope (all built-in types must be lowercase). Moreover, a declaration is a statement and all statements must be terminated with a semi-colon. The compiler will warn you of this as well.