Help on c programming needed
Need some help on c programming. I''m developing a game but i need a function that can find the smallest value in an array or a function that can sort the values in an array in ascending order.
Kyo Programmer
This is really quite simple... just sift through the array and find the smallest value...
This is an example of the sorting algorithm bubblesort. It walks through the numbers in the array, and compares them to the remaining items in the array... If the other number is lower, they are swapped.
There are others... Quicksort should be more efficient and fast.
//Ksero
Edited by - Ksero on October 26, 2000 4:50:10 AM
int find_min(int *my_array, int num_ints){ int min_index=0; for(int i=0;i<num_ints;i++) { if(my_array[ i ] < my_array[min_index]) min_index=i; }//Now min_index contains the index of the smallest valuereturn min_index;}void bubblesortarray(int *array, int num_ints){ for(int i=0;i<num_ints;i++) //for each remaining int (index above i) for(int j=i+1; j<num_ints; j++) if(array[ i ]>array[ j ]) { //Swap the values... int c; c=array[ i ]; array[ i ]=array[ j ]; array[ j ]=c; }}
This is an example of the sorting algorithm bubblesort. It walks through the numbers in the array, and compares them to the remaining items in the array... If the other number is lower, they are swapped.
There are others... Quicksort should be more efficient and fast.
//Ksero
Edited by - Ksero on October 26, 2000 4:50:10 AM
Not really, cause if you have not noticed i''m from singapore and i hav no idea what you are talking about.
Kyo Programmer
Try looking up qsort(), it''s in the Standard Library somewhere, I think in string.h
------------
- outRider -
------------
- outRider -
#include <stdlib.h>void qsort(void *base, size_t nelem, size_t width, int (_USERENTRY *fcmp)(const void *, const void *));// Example:#include <stdio.h>#include <stdlib.h>#include <string.h>int sort_function( const void *a, const void *b);char list[5][4] = { "cat", "car", "cab", "cap", "can" };int main(void){ int x; qsort((void *)list, 5, sizeof(list[0]), sort_function); for (x = 0; x < 5; x++) printf("%s\n", list[x]); return 0;}int sort_function( const void *a, const void *b){ return( strcmp((char *)a,(char *)b) );}// From the Borland C++ 5.0 Programmer''s Guide
Null and Void
At least I don't know COBOL...
http://www.crosswinds.net/~druidgames/
quote: ... I''m developing a game but i need a function that can find the smallest value in an array or a function that can sort the values in an array in ascending order.
I don''t want to be rude, but if you can''t figure this one out, you shouldn''t be developing a game.
i really am not made for c, basically i develp in visual basic, u may say i suck if u like, just don''t mind me. Anyway i know the bubble sort algorithm, just asking for some opinions.
Kyo Programmer
It doesn''t matter whether you are talking about C or VB, though. The algorithms you''d use here are pretty much the same in either...Only the semantics of the languages change.
I still think it sounds like a homework problem, which is why I offer no answer. Sorry. (Feel free to flame me if you like).
I still think it sounds like a homework problem, which is why I offer no answer. Sorry. (Feel free to flame me if you like).
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement