#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); }
sizeof (long int) usually 4 or 8
#include using std::cin;using std::cout;using std::endl;int main(){int sizeOfArray = 5;int myArray[] = {0};cout myArray[i];}int sum = 0;for (int j = 0; j < sizeOfArray; j++){sum += myArray[j];}cout
int comp(const int a1[], const int a2[], const int size) { int i; for(i = 0; i < size; ++i) { if(a1[i] != a2[i]) { return 0; } } return 1; }
#include<iostream> #include<iomanip> #include<time.h> void print(int a[], size_t size) { using std::cout; using std::endl; using std::setw; for(size_t index=0; index<size; ++index) cout<<setw(5)<<a[index]; cout<<endl; } int main() { srand((unsigned)time(NULL)); const size_t size=10; int a[size], b[size], c[size]; // Initialise a and b with random integers (range 1-99) for(size_t index=0; index<size; ++index) { a[index]=rand()%99+1; b[index]=rand()%99+1; } // Initialise c with products of a and b. for(size_t index=0; index<size; ++index) c[index]=a[index]*b[index]; // Calculate sum of c. int sum=0; for(size_t index=0; index<size; ++index) sum+=c[index]; // Print results. std::cout<<"Array a:\t"; print(a,size); std::cout<<"Array b:\t"; print(b,size); std::cout<<"Products:\t"; print(c,size); std::cout<<"Sum product:\t"<<sum<<std::endl; }
#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); }
#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); }
#include<iostream> int max(int* a, size_t size) { size_t max=a[0]; for(size_t index=0; index<size; ++index) if( max<a[index] ) max=a[index]; return( max ) } int main() { int a[5]={1,5,8,7,4}; int m=max(a,5); // invariant: m==8 }
The maximum size of INT is 1. If you go over then it will be an error.
C++ code example using a vector object (dynamic array): #include<iostream> #include<vector> using namespace std; typedef std::vector<int> vec; typedef std::vector<int>::iterator it; void init_array(vec& v) { v.clear(); for(int i=1; i<=10; ++i) v.push_back(i); } void print_array(vec& v) { for(it i=v.begin(); i!=v.end(); ++i) cout<<*i<<" "; cout<<endl; } it get_element(vec& v, unsigned int index) { it i=v.begin(); while(index--) ++i; return(i); } int main() { vec v; init_array(v); cout<<"Initial array:\t"; print_array(v); // Remove element at index 3 (the 4th element). v.erase( get_element(v, 3)); cout<<"Modified array:\t"; print_array(v); return(0); } Since Java is object-oriented, you can use a similar approach to the above. However, with C, there's a bit more work involved in managing the memory allocation, however the overall approach is the same: #include<iostream> using namespace std; int* init_array(unsigned int len) { int* p=(int*)malloc(len*sizeof(int)); for(unsigned int i=0; i<len; ++i) p[i]=i+1; return(p); } void print_array(int* p, unsigned int len) { for(unsigned int i=0; i<len; ++i) cout<<p[i]<<" "; cout<<endl; } int main() { unsigned int size; int* p; size=10; p=init_array(size); cout<<"Initial array:\t"; print_array(p,size); // Remove element at index 3 (the 4th element) --size; for( unsigned int y=3; y<size; ++y) p[y]=p[y+1]; p=(int*)realloc(p,size*sizeof(int)); cout<<"Modified array:\t"; print_array(p,size); delete [] p; return(0); }
sizeof (long int) usually 4 or 8
int myvar; printf ("size of myvar is %d\n", (int)sizeof (myvar));
printf ("sizeof (int) is %d bytes", (int)sizeof (int)); Most likely it will be 2 or 4.
#include using std::cin;using std::cout;using std::endl;int main(){int sizeOfArray = 5;int myArray[] = {0};cout myArray[i];}int sum = 0;for (int j = 0; j < sizeOfArray; j++){sum += myArray[j];}cout
int comp(const int a1[], const int a2[], const int size) { int i; for(i = 0; i < size; ++i) { if(a1[i] != a2[i]) { return 0; } } return 1; }
void bubblesort (int* array, int size) { if (!array size<2) return; int last_swap = size; while (last_swap>0) { int n=last_swap; for (int i=1; i<last_swap; ++i) { if (array[i]<array[i-1]) { array[i]^=array[i-1]^=array[i]^=array[i-1]; n=i; } last_swap = n; } }
#include using namespace std; void swap(int &a, int &b); int main() { int x=5,y=3; cout