int main(){ int num = 0, i = 0, j = 0, size = 20; int randomNums[20] = {0}, count[101] = {0}; int nums[20] = {0}; srand((unsigned int) time(NULL)); // TASK 1 //task1(); //TASK 2 //task2(randomNums, 20, count); //TASK 3 populateArray(nums, size); printArray(nums, size); sortArray(nums, size, 1); printf("\n"); printArray(nums, size); printf("\n"); sortArray(nums, size, 0); printArray(nums, size); return 0; }
void doSortAndMerge(){ const int SIZE_GLOBAL = 5; int array1[SIZE_GLOBAL]; int array2[SIZE_GLOBAL]; int array3[SIZE_GLOBAL+SIZE_GLOBAL]; for (int i = 0; i < SIZE_GLOBAL; i++) { cout << "Enter the num for array1["<<i<<"]"; cin >> array1[i]; } for (int i = 0; i < SIZE_GLOBAL; i++) { cout << "Enter the num for array2["<<i<<"]"; cin >> array2[i]; } printNumber(array1,SIZE_GLOBAL); printNumber(array2,SIZE_GLOBAL); sortArray(array1,SIZE_GLOBAL); sortArray(array2,SIZE_GLOBAL); printNumber(array1,SIZE_GLOBAL); printNumber(array2,SIZE_GLOBAL); margeArray(array1,array2,array3,SIZE_GLOBAL); printNumber(array3, SIZE_GLOBAL+SIZE_GLOBAL); }
//Recursive function to split the array into sub arrays for merging void sortArray(int start, int end, int masterArray[], int tempArray[]) { int split = (start + end) / 2; if (start < end) { sortArray(start, split, masterArray, tempArray); sortArray(split + 1, end, masterArray, tempArray); mergeArray(start, split, end, masterArray, tempArray); } }
sortStatus_t SORTAPI sortDevice(sortEngine_t engine, CUdeviceptr keys, CUdeviceptr values, int numElements, int numBits) { MgpuSortData data; data.AttachKey(keys); if(values) data.AttachVal(0, values); sortStatus_t status = data.Alloc(engine, numElements, values ? 1 : 0); if(SORT_STATUS_SUCCESS != status) return status; data.endBit = numBits; status = sortArray(engine, &data); if(SORT_STATUS_SUCCESS != status) return status; if(data.parity) { CUresult result = cuMemcpyDtoD(keys, data.keys[1], sizeof(uint) * numElements); if(CUDA_SUCCESS != result) return SORT_STATUS_DEVICE_ERROR; if(values) { cuMemcpyDtoD(values, data.values1[1], sizeof(uint) * numElements); if(CUDA_SUCCESS != result) return SORT_STATUS_DEVICE_ERROR; } } return SORT_STATUS_SUCCESS; }
int main(void) // Takes a series of scores from judges and displays all scores, // the highest score, the lowest score, and the average score. { #define scores_size 6 int scores[scores_size] = {0}; double score; int usable_total; double avg; for (int i = 0; i < scores_size; i++) { printf("\nPlease input the score from judge #%d: ", i + 1); scanf("%lf", &score); score = (int)rint(score*adjust); scores[i] = score; } // Return all scores printf("The gymnast's scores are "); printArray(scores, scores_size); // Sort scores and output high and low values sortArray(scores, scores_size); printf("The high score is %.1f\n", (double)largest(scores, scores_size) / adjust); printf("The low score is %.1f\n", (double)smallest(scores, scores_size) / adjust); // Calculate and output average score without high/low values usable_total = sum(scores, scores_size); usable_total -= largest(scores, scores_size); usable_total -= smallest(scores, scores_size); avg = average(usable_total, (scores_size - 2)); printf("The score for this event is %.2f\n", avg / adjust); }
int main(int argc, const char * argv[]){ for (int i = 1; i < argc ; i++){ if (argv[i][0] == '-' && argv[i][1] == 'w') { i++; width = atoi(argv[i]); printf("width changed to %d \n", width); } else if (argv[i][0] == '-' && (argv[i][1] == 'r'|| argv[i][1] == 'j')) { align = argv[i][1]; printf("alignment is now %c \n",align ); } else if (argv[i][0] == '-' && argv[i][1] == 's') { spacing = 1; printf("Spacing is on \n"); } else if (argv[i][0] == '-') { printf("Please use only the options from the man page.\n"); exit(1); } } widthFormatting(); sortArray(); printf("\n"); for (int i = 0; i < 5000; i++) { if (str[i] == '\0') { break; } printf("%c",str[i]); } printf("\n"); return 0; }
void main(int argc, char const *argv[]) { //二级指针的第一种内存模型 //首先是一个数组,指针数组 ---》 只不过每一个元素都是指针而已 char* myArr[] = {"bbbb","aaaa","cccc","11111"}; { int a[10]; printf("sizeof(a[10]):%d\n",sizeof(a)); printf("&a=%d\n",sizeof(&a)); //代表整个数组元素 } { printf("1:%d\n", sizeof(myArr)); printf("2:%d\n", sizeof(*myArr)); } printf("排序之前结果为:\n"); printArray(myArr,4); sortArray(myArr,4); printf("排序之后的结果为:\n"); printArray(myArr,4); system("pause"); }
bool Matrix::eig(vector<double> &eigval,Matrix *eigvec){ bool flag = false; if(this->rows!=this->cols){ cout<<"Inappropriate Input Matrix! Only SQUARE Matrix Permitted In EIGEN Calculating"<<endl; return false; } int size = rows; valarray<double> main_diag(size), minor_diag(size); Matrix q(size,size); Matrix digenvalue(size,size); digenvalue.setdata(data); double accuracy = 1.0e-8; int k=Householder(digenvalue,q,main_diag,minor_diag); if(k==0){ cout<<"The Matrix is not SYMMETRICAL"<<endl; return false; } k = QR(main_diag,minor_diag,q,accuracy,160); if(k<0){ cout<<"Calculating failed in Required Iteration Times"<<endl; return false; } vector<int> id; sortArray(main_diag,id); for(int j=0;j<size;j++) eigval.push_back(main_diag[j]); q = q.sort(id,2); *eigvec = q; flag = true; return flag; }
bool CCardOperator::getCard(int seq, vector<int>& vecCards){ int begin, end; if (seq == 0) { begin = 0; end = 18; }else { begin = seq * 17 + 1; end = begin + 17; } sortArray(cardArray+begin, end-begin); vecCards.clear(); bool redThree = false; for (int i=begin; i<end; i++) { vecCards.push_back(cardArray[i]); if (IS_RPTHREE(cardArray[i])) { redThree = true; } } if (redThree) { vecCards.push_back(36); vecCards.push_back(38); return true; } return false; }
int main () { int array[SAMPLE_INT_ARRAY_SIZE];// define an array to be filler fillArray(array, SAMPLE_INT_ARRAY_SIZE);// fill the array with random numbers print_int_array(array, SAMPLE_INT_ARRAY_SIZE);// print the filled array sortArray(array, SAMPLE_INT_ARRAY_SIZE); return 0; }
main() { int numbers[size]; getArray(numbers); sortArray(numbers); displayArray(numbers); system("pause"); }
int findSingleOccurenceNumber(int *A, int len) { if (A == NULL) return -1; sortArray(A, len); for (int index = 0; index < len; index += 3) if (index == len - 1 || A[index] != A[index + 1]) return A[index]; return -1; }
int main(int argc, char const *argv[]) { int n[10]={0,1,2,3,4,8,34,21,6,10}; sortArray(n,10); putArray(n,10); return 0; }
void TrackData_toggleBookmark(TrackData* trackData, int row) { int i, count = trackData->bookmarkCount; int* bookmarks = trackData->bookmarks; if (!bookmarks) { bookmarks = trackData->bookmarks = malloc(sizeof(int)); *bookmarks = row; trackData->bookmarkCount++; return; } for (i = 0; i < count; ++i) { if (bookmarks[i] == row) { bookmarks[i] = 0; sortArray(bookmarks, count); return; } } // look for empty slot for (i = 0; i < count; ++i) { if (bookmarks[i] == 0) { bookmarks[i] = row; sortArray(bookmarks, count); return; } } // no slot found so we will resize the array and add the bookmark at the end bookmarks = trackData->bookmarks = realloc(bookmarks, sizeof(int) * (count + 1)); bookmarks[count] = row; sortArray(bookmarks, count + 1); trackData->bookmarkCount = count + 1; }
void * removeArrayDuplicates(int *Arr, int len) { if (Arr == NULL || len < 0) return NULL; else { sortArray(Arr, len); deletedup(Arr, &len); } }
int main() { int array[5]; int array_length; array_length = sizeof(array) / sizeof(int); scanArray(array, array_length); invertArray(array, array_length); sortArray(array, array_length); printf("\n"); return(0); }
void main() { struct student a[5]; init(a, 5); printf("ÅÅÐòÇ°£º"); printArr(a,5); sortArray(a, 5); printf("ÅÅÐòºó£º"); printArr(a, 5); system("pause"); }
sortStatus_t MgpuBenchmark(MgpuTerms& terms, sortEngine_t engine, double* elapsed) { MgpuSortData data; // Put the output arrays as the input arrays into the terms.. This saves // some device memory. data.AttachKey(terms.sortedKeys->Handle(), 1); for(int i(0); i < abs(terms.valueCount); ++i) data.AttachVal(i, terms.sortedVals[i]->Handle(), 1); sortStatus_t status = data.Alloc(engine, terms.count, terms.valueCount); if(SORT_STATUS_SUCCESS != status) return status; data.earlyExit = terms.earlyExit; data.endBit = terms.numBits; CuEventTimer timer; for(int i(0); i < terms.iterations; ++i) { if(!i || terms.reset) { timer.Stop(); uint bytes = sizeof(uint) * terms.count; terms.randomKeys->ToDevice(0, data.keys[0], bytes); for(int i(0); i < terms.valueCount; ++i) terms.randomVals[i]->ToDevice(0, data.values[i][0], bytes); } timer.Start(false); if(!terms.bitPass) status = sortArray(engine, &data); else status = sortArrayEx(engine, &data, terms.numThreads, 8, terms.bitPass, terms.useTransList); if(SORT_STATUS_SUCCESS != status) return status; } *elapsed = timer.Stop(); // Copy the results back to terms.sortedKeys and terms.sortedVals. if(!data.parity) { uint bytes = sizeof(uint) * terms.count; CUresult result = terms.sortedKeys->FromDevice(0, data.keys[0], bytes); if(CUDA_SUCCESS != result) return SORT_STATUS_DEVICE_ERROR; for(int i(0); i < abs(terms.valueCount); ++i) { result = terms.sortedVals[i]->FromDevice(0, data.values[i][0], bytes); if(CUDA_SUCCESS != result) return SORT_STATUS_DEVICE_ERROR; } } return SORT_STATUS_SUCCESS; }
int main (int argc, const char* argv[]) { int n; // Integer read from the command line goes here int count; count = argc -1; int arr[count]; if (argc <2) { printf("Must enter a number on the command line!\n"); } getIntegersFromCommand(argv, count, arr); printf("The sorted array is\n"); sortArray(arr,count); return 0; // Indicate success! }
int main(void) { const int nelem = 10; int *array; array = (int *)malloc(nelem*sizeof(int)); /* Initialize the arrays */ for (indx = 0; indx < nelem; indx++) { array[indx] = 0; } /* Replace initial values of array elements */ populateArray(nelem, array); /* Print the elements of the unsorted array */ printf("The sequence of elements in the unsorted array are: [ "); for (indx = 0; indx < nelem; indx++) { printf("%d ", array[indx]); } printf("]\n\n"); /* Sort the array of integers */ sortArray(nelem, array, ascending); /* Print the elements of the sorted array */ printf("The sequence of elements in the sorted array are: [ "); for (indx = 0; indx < nelem; indx++) { printf("%d ", array[indx]); } printf("]\n\n"); /* Compute average value of elements in array */ getAverage(nelem, array); /* Print sum of elements in array */ printf("The sum of the %d elements in the array is: %d\n\n ", nelem, sum); /* Print average */ /* printf("The average value of the array elements is: %f\n\n ", getAverage(nelem, array)); */ free(array); return 0; }
void main() { int masterArray[size]; int tempArray[size]; buildArray(masterArray); bruteCount(masterArray); cout << "Inversion from Brute: " << INVERSIONS_BRUTE << endl; cin.get(); sortArray(0, size - 1, masterArray, tempArray); cout << "Inversions from Sort: " << INVERSIONS_SORT << endl; cin.get(); }
// ----------------------------------------------------------- // FUNCTION Heap Sort : (heap) // Sorts m random integers using the heap sort // PARAMETER USAGE : // m : The number of integers to sort using heap sort // FUNCTION CALLED : // generateArray: It creates a random array of size m, of ints // printArray: Prints the array given to it. For both sorted and unsorted array // sortArray: Sorts the array given to it //----------------------------------------------------------- int heap( long int *m ) { char buffer[256]; int heapArray[*m], i; sprintf( buffer, " Heap Sort Process Started\n" ); write( 1, buffer, strlen( buffer ) ); generateArray( heapArray, *m ); printArray( heapArray, *m, 0 ); sortArray( heapArray, *m ); printArray( heapArray, *m, 1 ); sprintf( buffer, " Heap Sort Process Exits\n" ); write( 1, buffer, strlen( buffer ) ); }
int search(uint* arrayName, uint arraySize, uint* opearionsAmount, uint valueToSearch) { sortArray(arrayName, arraySize); int firstIndex = 0; int lastIndex = arraySize - 1; int index = -1; while (firstIndex < lastIndex) { (*opearionsAmount)++; index = firstIndex + (lastIndex - firstIndex) / 2 ; valueToSearch <= arrayName[index] ? (lastIndex = index) : (firstIndex = index); } if (valueToSearch == arrayName[lastIndex]) return lastIndex; return index; }
int main(int argc,char** argv) { //Error checking for the number of argument// if (argc !=3) { printf("Insufficent arguments"); return -1; } //Error checking for existance of input// int size=loadArray(argv[1]); if(size==0) { printf("Unable to open the input file"); return -1; } //Declaration of variables// int id, ngrade,search; //Display output// printf("Student Record\n"); printArray(size); //Search ID and rewritng new grade// printf("\nEnter the ID of the student to search:"); scanf("%d",&id); printf("Enter a grade of the student:"); scanf("%d",&ngrade); // Checking for the existance of the id// if((search=searchArray(size,id,ngrade))==0) { printf("Student with id %d is not present in the class\n"); } //Write a new file with the new revision// else { int x=writeContent(argv[2],size); //Print new output// printf("\nUpdated student record\n"); printArray(size); } sortArray(size); //Bonus Part Sorting array// printf("\nBonus part\nPrinting sorted student record\n"); printArray(size); return 0; }
/************************************************** * This function opens the file and reads each * * line into the buildArray function. It then * * calls the rest of the functions to sort the * * array and build the balanced binary tree. * * EXIT 2 for file input error * * EXIT 1 for memory error * **************************************************/ void initialize(char *filename) { FILE *fp; char buffer[21], **names; int count = 0, *count_ptr = &count, size = 2, *size_ptr = &size; BNODE* root; if ((fp = fopen(filename, "r")) == NULL) { printf("Error: Filename does not exist\n"); exit(2); } if ((names = malloc(2 * sizeof(char*))) == NULL) { printf("Error: Unable to allocate memory\n"); exit(1); } while (fgets(buffer, sizeof(buffer), fp) != NULL) { /* Double the size of the array after it is full */ if (count == size) names = resizeArray(names, size_ptr); names = buildArray(names, buffer, count_ptr); } fclose(fp); names = sortArray(names, count_ptr); root = buildTree(names, 0, count-1); printf("\n preorder: "); preorder(root); printf("\n\n"); printf(" inorder: "); inorder(root); printf("\n\n"); printf("postorder: "); postorder(root); printf("\n"); }
void main() { char *tmp = NULL; int i = 0, j = 0; //二级指针第一种内存模型 //首先考虑它是一个数组,指针数组,====》只不过每一个元素是指针而已。 //【】优先级高 //打印数组 排序排序这个数组、、、、指针做函数参数 char * myArray[] = {"bbbbb", "aaaa", "ccccc", "1111111"}; printf("排序之前\n"); printAarray(myArray, 4); sortArray(myArray, 4); printf("排序之后\n"); printAarray(myArray, 4); system("pause"); }
int main(int argc, char *argv[]){ if (argc != 2){ printf("Wrong number of arguments. Needed exactly one argument.\n"); return 1; } int n = atoi(argv[1]); int * mas = NULL; initArray(&mas, n); printArray(mas, n); sortArray(mas, n); printArray(mas, n); printf("%d\n", findNumber(mas, mas[n / 2 + 1], 0, n - 1)); free(mas); return 0; }
void calculateLCmergedSite(pdbFile *macromolecule, bindingSite *mergedSites, int numMergedSites, Graph *network, FILE *output) { int i, j; int order[numMergedSites]; float LC[numMergedSites]; int nRes; for (i=0; i<numMergedSites; i++) { order[i] = i; } for (i=0; i<numMergedSites; i++) { mergedSites[i].LC = calculateLCsite(mergedSites, i, network); LC[i] = mergedSites[i].LC; } sortArray(LC, numMergedSites, order); for (i=0; i<numMergedSites; i++) { fprintf(output, "%6d %4f ", order[i], mergedSites[order[i]].LC); if (mergedSites[order[i]].nRes > 10) { nRes = 10; } else { nRes = mergedSites[order[i]].nRes; } for (j=0; j<nRes; j++) { fprintf(output, "%4d ", mergedSites[order[i]].resid[j]); // was: fprintf(output, "%4d ", mergedSites[order[i]].residue[j]); } fprintf(output, "\n"); } return; }
void read10Files() { int i; int totalNames = 0; //keeps track of the total number of names char names[NUM_NAME_BUFFER][NAME_LENGTH_BUFFER] = {"",""}; //array for names int yearNums[NUM_NAME_BUFFER][10] = {0}; //array for numbers of names in years char fileNames[12] = "yob1920.txt"; //file names int indexChange = 5; //controls the year we are accessing for (i = 0; i < 10; i++) { if(fileNames[indexChange] == ':') //ascii table incrementation { fileNames[indexChange] = '0'; fileNames[indexChange - 1] = '0'; // change 9 to a 0 fileNames[indexChange - 2]++; //change the 1 to a 2 } totalNames += readSingleFile(names, yearNums, i, fileNames, totalNames); //returns 1 if a new name is added. 0 if only a new year is added. fileNames[indexChange]++; //ascii table incrementation } sortArray(names, yearNums, totalNames); createOutputFile(names, yearNums, totalNames); }
void KeyListOpsMethods::toArray(bool useNum, SORT_TYPE sortVal) { //TBD: optimize performance with better memory management. if (useNum) { _numArray.resize(_keyList->size()); int i=0; for (begin(); !end(); next()) { _numArray[i] = getColValNum(); i++; } } else { _qsArray.resize(_keyList->size()); int i=0; for (begin(); !end(); next()) { _qsArray[i] = getColVal(); i++; } } if (sortVal != UNSORTED) { sortArray(useNum, sortVal == ASC); } }