printf ("sizeof (int) = %d\n", (int)sizeof (int));
#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
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 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
Quadratic probing is a collision resolution technique used in hash tables. In C++, you can implement it by defining a hash table class and a hash function, then using a quadratic formula to calculate the next index when a collision occurs. The formula typically used is (hash + i^2) % table_size, where i is the number of attempts. Here's a simple implementation outline: #include <iostream> #include <vector> class QuadraticProbingHashTable { std::vector<int> table; int size; public: QuadraticProbingHashTable(int s) : size(s), table(s, -1) {} void insert(int key) { int index = key % size; int i = 0; while (table[index] != -1) { index = (index + i * i) % size; // Quadratic probing i++; } table[index] = key; } void display() { for (int i = 0; i < size; i++) std::cout << i << ": " << table[i] << std::endl; } }; This code snippet initializes a hash table, inserts keys using quadratic probing, and displays the table's contents.
#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.
sizeof (long int) usually 4 or 8
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); }
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.
To implement a Sudoku 9x9 solver in C, you'll typically create a 2D array to represent the grid. The program uses a backtracking algorithm, where you recursively attempt to fill the grid with numbers 1-9, checking for validity at each step. If a number fits, you continue; if not, you backtrack and try the next number. Here’s a basic structure for the code: #include <stdio.h> #include <stdbool.h> #define SIZE 9 bool isSafe(int grid[SIZE][SIZE], int row, int col, int num); bool solveSudoku(int grid[SIZE][SIZE]); void printGrid(int grid[SIZE][SIZE]); // Complete your isSafe, solveSudoku, and printGrid functions accordingly. int main() { int grid[SIZE][SIZE] = { /* initialize with the Sudoku puzzle */ }; if (solveSudoku(grid)) printGrid(grid); else printf("No solution exists\n"); return 0; } You'll need to implement the logic in the isSafe and solveSudoku functions to handle the rules of Sudoku.
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 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