{"id":6759,"date":"2025-01-11T00:01:10","date_gmt":"2025-01-11T08:01:10","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6759"},"modified":"2025-01-18T16:58:29","modified_gmt":"2025-01-19T00:58:29","slug":"o-value-where-are-you","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6759","title":{"rendered":"O Value! Where are You?"},"content":{"rendered":"<p>Finding things is an unwanted pastime for humans. &#8220;Where are the good scissors?&#8221; &#8220;Who has seen the cat?&#8221; &#8220;What happened to all my money?&#8221; These issues don&#8217;t exist for a program that dutifully locates any data tidbit without complaint. Finding the smallest needle in the largest haystack isn&#8217;t an issue for a computer.<br \/>\n<!--more--><br \/>\nConsider the following code, which searches an array of integers. The code obtains the array&#8217;s size and asks the user to input a key value. The program loops through all elements in the array to scan for the input value. The results are reported.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_01_11-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2025_01_11-Lesson-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int v[] = { 23, 25, 1, 17, 20, 14, 3, 19 };\r\n    int size,key,x;\r\n\r\n    <span class=\"comments\">\/* obtain array size *\/<\/span>\r\n    size = sizeof(v)\/sizeof(int);\r\n\r\n    <span class=\"comments\">\/* locate a value *\/<\/span>\r\n    printf(\"Input key value: \");\r\n    scanf(\"%d\",&amp;key);\r\n    for( x=0; x&lt;size; x++ )\r\n    {\r\n        if( v[x]==key )\r\n        {\r\n            printf(\"Key %d found!\\n\",key);\r\n            return 0;\r\n        }\r\n    }\r\n    printf(\"Key %d not found.\\n\",key);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>Here&#8217;s sample output:<\/p>\n<p><code>Input key value: 14<br \/>\nKey 14 found!<\/code><\/p>\n<p>The code is rather simple, but is it efficient? Does it represent the best way to locate a value &mdash; or any information &mdash; in a collection of data? One way to tell is to increase the scale.<\/p>\n<p>The following code update uses the <em>makedata()<\/em> function to generate up to 1,000 integer values in the range of 1 through 999.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_01_11-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2025_01_11-Lesson-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;time.h&gt;\r\n\r\nint *makedata(int s)\r\n{\r\n    int x,*data;\r\n\r\n    <span class=\"comments\">\/* allocate 'size' storage *\/<\/span>\r\n    data = malloc( sizeof(int) * s );\r\n    if( data==NULL )\r\n    {\r\n        fprintf(stderr,\"Unable to allocate memory\\n\");\r\n        exit(1);\r\n    }\r\n\r\n    <span class=\"comments\">\/* assign random values, 1 through 999 *\/<\/span>\r\n    for( x=0; x&lt;s; x++ )\r\n        *(data+x) = rand() % 999 + 1;\r\n\r\n    return(data);\r\n}\r\n\r\nint main()\r\n{\r\n    int *v,size,key,x;\r\n\r\n    <span class=\"comments\">\/* obtain size 'n' 100 &lt; n &lt; 1000 *\/<\/span>\r\n    srand( (unsigned)time(NULL) );        <span class=\"comments\">\/* seed randomizer *\/<\/span>\r\n    size = rand() % 900 + 100;\r\n    printf(\"Dataset size = %d\\n\",size);\r\n\r\n    <span class=\"comments\">\/* create data *\/<\/span>\r\n    v = makedata(size);\r\n\r\n    <span class=\"comments\">\/* locate a value *\/<\/span>\r\n    printf(\"Input key value: \");\r\n    scanf(\"%d\",&amp;key);\r\n    for( x=0; x&lt;size; x++ )\r\n    {\r\n        if( *(v+x)==key )\r\n        {\r\n            printf(\"Key %d found!\\n\",key);\r\n            return 0;\r\n        }\r\n    }\r\n    printf(\"Key %d not found.\\n\",key);\r\n\r\n    <span class=\"comments\">\/* clean-up *\/<\/span>\r\n    free(v);\r\n    return 0;\r\n}\r\n<\/pre>\n<p>As random values, the program can&#8217;t guarantee that a specific key can be located, but it does take longer to search through the dataset. Here&#8217;s a sample run:<\/p>\n<p><code>Dataset size = 995<br \/>\nInput key value: 123<br \/>\nKey 123 found!<\/code><\/p>\n<p>Call it dumb luck that I could pluck out a legitimate value from 995 random values. In fact, nothing in the code guarantees that a value is there or isn&#8217;t repeated several times. The point is that the larger and more complex the data, the more time is consumed searching. Imagine a dataset with millions of records and trying to locate a specific one? Sure, the program finds what you&#8217;re searching for . . . eventually.<\/p>\n<p>Just as with sorting data, techniques are available to make finding data more efficient. One of them is the binary search. It uses a magical algorithm that quickly locates a key value in a sorted data set. In fact, just like sorting has <em>qsort()<\/em> function, the binary search has the <em>bsearch()<\/em> function in the C library. These two functions go hand-in-hand.<\/p>\n<p>Starting with <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6767\">next week&#8217;s Lesson<\/a>, I cover the <em>bsearch()<\/em> function and how it&#8217;s used to quickly and efficiently locate information.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Along with sorting, searching is another repetitive task that computer programs enjoy. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6759\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-6759","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6759","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6759"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6759\/revisions"}],"predecessor-version":[{"id":6824,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6759\/revisions\/6824"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6759"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6759"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6759"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}