Page 1 of 6 123 ... LastLast
Results 1 to 10 of 52

Thread: Algoritms

  1. Top | #1
    Veteran Member
    Join Date
    Nov 2017
    Location
    seattle
    Posts
    4,016
    Rep Power
    10

    Algoritms

    I have been doodling to get back into it.

    Sorting abd searching are najor areas. Although computer speeds have increased so has the size of data sets. Data ming requires multiple searches and corelations on large data bases.

    starting simple finding a a nax or min value is easy. C is my language but it should be readable.

    int find_max(int *list[]){ // pass a pointer to the start of the list

    // finds max number in an integer array
    // array is passed by pointer

    int number_entries, i, max_value;

    number_entries = szeof(list)/sizeof(list[0]); // Divide total size of list by size of one entry
    max_value = list[0]; // initialize to first element c arrays start at 0 not 1

    For (i = 1; i < number_entries; i++){
    if(list[i] > max_value) max_value = list[i];
    }

    return(max_value);

    }// find_max() end

    integer array 2, 0, 4, 1
    max_value 2, 2, 4, 4

    I'd have to run it to make sure it worked for negative numbers

  2. Top | #2
    Administrator lpetrich's Avatar
    Join Date
    Jul 2000
    Location
    Lebanon, OR
    Posts
    4,635
    Archived
    16,829
    Total Posts
    21,464
    Rep Power
    74
    You won't get the length of the list with sizeof(). Instead, you'll get the size of the list's pointer. You'll need to pass as args the list pointer and the list size.

    But otherwise, the code looks correct.


    sizeof() is most useful when you have to do something byte-by-byte, like with malloc() or memcpy() or memset().

  3. Top | #3
    Veteran Member
    Join Date
    Nov 2017
    Location
    seattle
    Posts
    4,016
    Rep Power
    10
    Quote Originally Posted by lpetrich View Post
    You won't get the length of the list with sizeof(). Instead, you'll get the size of the list's pointer. You'll need to pass as args the list pointer and the list size.

    But otherwise, the code looks correct.


    sizeof() is most useful when you have to do something byte-by-byte, like with malloc() or memcpy() or memset().
    sizeof gives you the total size of the array. Dividing by the size of on element yields the number of entries.f( cint *x[]) tells the complier the pointer is to an array.

    f(int*a) is not the same as f(int *a[]). In the latter the compiler sees a as an array of ints. In the former the compiler sees a single int.

    int *a, sizeof(a) yields size of the pointer.
    int *a[], sizeof(a) yields total count in the array. sizeof(a/sizeof(a[0]) yields number of integers in the array.

    https://stackoverflow.com/questions/...-my-array-in-c

    When passing arrays of variable by pointer to functions you have to determine the max number of elements in the array to prevent indexing a pointer where you do not want to. Indexing past the end of an array in C using a pointer is a problem in C requiring precautions. C is pointers.

    sizeof() has a number of uses. String processing. With the amount of PC memory tyese days I doughty dynamic memory allocation has a lot of use. It used to be when doing a lot of work with large arrays you had to swap them in and out of memory.

  4. Top | #4
    Veteran Member
    Join Date
    Nov 2017
    Location
    seattle
    Posts
    4,016
    Rep Power
    10
    Working my way up. A bubble sort.

    #define TRUE 1
    #define FALSE 0
    #define FOREVER 1

    void bubble_sort(int *list[]){

    // sorts a list of integers in ascending order

    int list_length, j, swap_flag, temp;
    list_length = sizeof(list)/sizeof(list[0]);

    while(FOREVER){

    swap_flag = FALSE;

    for(k = 0; k < list_length -2, k++){

    if(list[k] > list[k+1]{list[k]){temp = list[k]; list[k] = list[k+1]; list[k+1] =temp; swap_flag = TRUE;}

    } //for

    if(swap_flag == FALSE) break;

    } //while
    } bibble_sort() ends

  5. Top | #5
    Veteran Member
    Join Date
    Nov 2017
    Location
    seattle
    Posts
    4,016
    Rep Power
    10
    All variables and function nanmes in C are pointers. In C you can create a pointer and set it to any point in memeory. It allows for less code and faster operation but can be dangerous if used without thought.

    When you compile a high level language the compiler does not know where it will go in memory. The compiler creates assembly language code and hhe assembler creates machine binary executable code. Code for an OS like Windows is reloadable. The binary file has a table with relative positions of variables and functions. The absolute agrees the OS places the code at in memory is used to resolve memory locations.

    All variables in a high level language are aliases for an absolute memory location when loaded.

    https://www.programiz.com/c-programming/c-pointers

  6. Top | #6
    Veteran Member
    Join Date
    Nov 2017
    Location
    seattle
    Posts
    4,016
    Rep Power
    10
    Finding the occurrences of an item in an integer list, creating a list of locations in the list.

    #define ERROR -1

    int find_occurences(int target, int *list[]; int *results[]){

    // list and results are existing arrays. sizes must match for the state where
    //the target exists in the entire list.

    lnt k, count, results_length list_length;
    list_length = sizeof(list)/sizeof(list[0]);
    results_length = sizeof(results)/sizeof(results[0]);
    if(list_length != results_length)return(ERROR);
    count = 0; //counts number of matches, indexes results array
    for(k = 0; k < list_length; k++){if(target == list[k]){results[count++] = k;}//store location of match in results
    return(count);

  7. Top | #7
    Administrator lpetrich's Avatar
    Join Date
    Jul 2000
    Location
    Lebanon, OR
    Posts
    4,635
    Archived
    16,829
    Total Posts
    21,464
    Rep Power
    74
    sizeof() will get the size of a static array, but not a dynamically-allocated one.

    Code:
    /*
    
    Tests sizeof()
    
    */
    
    #include <cstdio>
    using namespace std;
    
    #define PRINTSIZE(x) printf("%lu\n",sizeof(x));
    
    int main(void) {
    
    	int val = 0;
    	int arsta[] = {1,2,3,4,5,6,7,8,9,10};
    	int *ardyn = new int[10];
    	
    	PRINTSIZE(val)
    	PRINTSIZE(arsta)
    	PRINTSIZE(ardyn)
    	
    	return 0;
    }
    Output:
    4
    40
    8

  8. Top | #8
    Administrator lpetrich's Avatar
    Join Date
    Jul 2000
    Location
    Lebanon, OR
    Posts
    4,635
    Archived
    16,829
    Total Posts
    21,464
    Rep Power
    74
    I redid it with better labeling:
    Code:
    /*
    
    Tests sizeof()
    
    */
    
    #include <cstdio>
    using namespace std;
    
    #define PRINTSIZE(lbl,x) printf("%s: %lu\n",lbl,sizeof(x));
    
    int main(void) {
    	
    	char val1 = 1;
    	short val2 = 2;
    	int val3 = 3;
    	long val4 = 4;
    	long long val5 = 5;
    	float val6 = 6;
    	double val7 = 7;
    	long double val8 = 8;
    	int arsta[] = {1,2,3,4,5,6,7,8,9,10};
    	int *ardyn = new int[10];
    	
    	PRINTSIZE("char",val1)
    	PRINTSIZE("short",val2)
    	PRINTSIZE("int",val3)
    	PRINTSIZE("long",val4)
    	PRINTSIZE("long long",val5)
    	PRINTSIZE("float",val6)
    	PRINTSIZE("double",val7)
    	PRINTSIZE("long double",val8)
    	PRINTSIZE("static array: 10 of int",arsta)
    	PRINTSIZE("dynamic array: 10 of int",ardyn)
    	
    	return 0;
    }
    Output:
    char: 1
    short: 2
    int: 4
    long: 8
    long long: 8
    float: 4
    double: 8
    long double: 16
    static array: 10 of int: 40
    dynamic array: 10 of int: 8

    OSX / macOS now runs in 64-bit mode, and its C++ compiler assumes 64-bit mode. It can still run 32-bit apps, though Apple has deprecated them with warnings that they won't run in future versions.

  9. Top | #9
    Veteran Member
    Join Date
    Nov 2017
    Location
    seattle
    Posts
    4,016
    Rep Power
    10
    Moving on to strings. You need to understand the ASCII format. Characters in strings are normally stored in ASCII format.

    https://www.ascii-code.com/

    Strings are terminated by the ASCII NULL code 000. Looking at the table it can be seen sections of characters like lower case letters are in increasing values. This means characters can be sorted by a numerical value without a lot of complicated logic. Determining the size of a string.

    #define NULL 0x00
    #define MAX_STRING_LENGTH 1000
    #define STRING_ERROR 1

    int string_length(char *str[]){

    // NULL terminator counted as a character.

    int k;
    for(k = 0; k < MAX_STRING_LENGTH; k++){if(str[k] == NULL)return(k + 1);}
    return(STRING_ERROR);
    }

  10. Top | #10
    Veteran Member
    Join Date
    Nov 2017
    Location
    seattle
    Posts
    4,016
    Rep Power
    10
    Sorting text strings for the entire chatacter set would require setting up a precedence order. Sorting by just lower case letters is easy. To do a bubble sort on an array of text strings requires a string compare function.

    Looking at the table the difference between upper and lower case letters is one bit. One way to sort is to ignore case and convert all characters to upper or lower case.




    int string_compare(char s1[]; char s2[]){

    // checks to see if s2 is equal to, greater than, or less than s1
    // in terms of an ascending character sort
    // assumes all lower case or upper case letters

    int k, ls1l ls2;

    // check for valid strings and equal string lengths
    if( (ls1 = string_length(s1)) == STRING_ERROR)return(STRING_ERROR).
    if( (ls2 = string_length(s2)) == STRING_ERROR)return(STRING_ERROR).
    if(ls1 != ls2)return(STRING_ERROR);


    // if first chars do mot math sort by first char
    if( s1[k] < s2[k]) return (ABOVE);
    if( s1[k] > s2[k]) return (BELOW);

    // sort by first mismatch following a match
    For(k = 0; k < ls1; k++){
    if( (s1[k] == s2[k]) && ( (s1[k + 1] < s2[k + 1]) ) return (ABOVE);
    if( (s1[k] == s2[k]) && ( (s1[k + 1] > s2[k + 1]) ) return (BELOW);
    }
    return(EQUAL);
    }

    john
    jack
    josh

    sorts to

    jack
    john
    josh
    would sor5 to jake
    Last edited by steve_bank; 01-06-2019 at 10:32 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •