dmilicev

lets_learn_c_arrays_of_int_v1.c

Nov 3rd, 2019
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 22.33 KB | None | 0 0
  1. /*
  2.  
  3.  lets_learn_c_arrays_of_int_v1.c
  4.  
  5.  Work with C arrays from meny.
  6.  
  7. The idea is that this program should cover everything about
  8. C arrays with int numbers.
  9.  
  10. The program has many comments because it is intended for beginners.
  11.  
  12. I want to encourage beginners in programming to study this program
  13. regardless of the fact that it consists of many lines.
  14. Half of these lines belong to the comments and explanations.
  15.  
  16. The point of the functions is to divide a big problem
  17. into several smaller problems.
  18. Each function should solve only one problem and nothing more.
  19. Functions should therefore not have more than a couple dozen lines of code.
  20. The features of the functions are arguments (parameters, input values)
  21. and the value returned by the function to the part of the program
  22. from which it was called.
  23. If we pass to the function the pointers to the variables as arguments,
  24. function then has the ability to change the value of the arguments
  25. beyond its scope, and in that case it does not even have to have
  26. its own return value.
  27. Our conversations should soon come down to questions about how to write
  28. a function that receives those arguments and has that return value.
  29.  
  30. Please understand that English is not my native language and
  31. please feel free to correct me about it.
  32.  
  33. Feel free to let me know if you think I left out something,
  34. or if you find some bugs in this program.
  35.  
  36. This program will be updated according to your suggestions.
  37.  
  38. Author Dragan Milicev
  39. https://www.facebook.com/dmilicev
  40. You can find all my C programs at Dragan Milicev's pastebin:
  41. https://pastebin.com/u/dmilicev
  42.  
  43. An example of C arrays
  44. https://www.tutorialspoint.com/cprogramming/c_strings.htm
  45.  
  46. */
  47.  
  48.  
  49. #include <stdio.h>
  50.  
  51.  
  52. // Displays an array of n integers
  53. void display_array_of_integers( char text[], int array[], int n ) {
  54.  
  55.     int i;
  56.  
  57.     printf("\n\n%s ( size = %d )\n\n", text, n );
  58.  
  59.     for( i=0; i<n ; i++ )               // display of array items
  60.         printf("%4d", array[i] );
  61.  
  62.     printf("\n");
  63. }
  64.  
  65.  
  66. // We return all 10 array elements to be the same as their indexes
  67. void reset_array( int array[], int *n ) {
  68.  
  69.     int i;
  70.  
  71.     *n = 10;                    // the function has an address of n, so it can change it
  72.  
  73.     for( i=0; i<*n ; i++ )
  74.         array[i] = i;           // we set the value of the array element
  75. }
  76.  
  77.  
  78. // called from meny_1, choice_1, choice_2 and choice_3
  79. // From the end of the array to the left to the desired index,
  80. // it moves the array members one place to the right.
  81. // To the vacancy thus made insert new_number into index place.
  82. // https://www.geeksforgeeks.org/c-program-to-insert-an-element-in-an-array/
  83. // https://codeforwin.org/2015/07/c-program-to-insert-element-in-array.html
  84. void insert_new_number_into_array_on_index_k( int array[], int *n , int new_number, int k) {
  85.  
  86.     int i;
  87.  
  88.     if( k>(*n) || k<0 )             // If index k of element is not valid
  89.     {
  90.         printf("\n Invalid index of new element! Please enter index between 0 to %d \n", (*n) );
  91.     }
  92.     else
  93.     {
  94.         for( i=(*n); i>=k; i-- )    // Make room for new array element by shifting to right
  95.         {
  96.             array[i] = array[i-1];
  97.         }
  98.  
  99.         array[k] = new_number;      // Insert new element at given position
  100.                                     // the function has an address of n, so it can change it
  101.         (*n)++;                     // increment *n because we add an array element
  102.     }
  103. }
  104.  
  105.  
  106. // meny_1, inserting a new number into the array, call insert_new_number_into_array_on_index_k()
  107. int meny_1( int array[], int *n ) {
  108.  
  109.     int choice, new_number, k;      // new_number will be in the place with index k
  110.  
  111.     while( 1 ) {                    // an endless loop that exits with the choice 0 or Escape
  112.  
  113.         //system("CLS");              // clear command window
  114.  
  115.         display_array_of_integers( " Array is: ", array, *n );  // Displays an array of n integers
  116.  
  117.     printf("\n\n +------------------------------------------------------+ \n"
  118.                " | meny_1: INSERTING A NEW NUMBER INTO THE ARRAY        | \n"
  119.                " +------------------------------------------------------+ \n"
  120.                " |  1. Enter a number at the beginning of the array     | \n"
  121.                " |                                                      | \n"
  122.                " |  2. Enter a number at the end of the array           | \n"
  123.                " |                                                      | \n"
  124.                " |  3. Enter a number in a place with index k           | \n"
  125.                " |                                                      | \n"
  126.                " |  0. ESC or 0 for previous meny                       | \n"
  127.                " +------------------------------------------------------+ \n"
  128.                "\n\n Izaberite (1-3)  ?  ");
  129.  
  130.         switch( choice = getche() ) {   // based on the choices we call the appropriate functions
  131.  
  132.             case '1':                   // Enter a number at the beginning of the array
  133.                     printf("\n\n Enter a number you wish to add at the beginning of the array: ");
  134.                     scanf("%d",&new_number);
  135.                     insert_new_number_into_array_on_index_k( array, n , new_number, 0 );
  136.                     break;
  137.  
  138.             case '2':                   // Enter a number at the end of the array
  139.                     printf("\n\n Enter a number you wish to add at the end of the array: ");
  140.                     scanf("%d",&new_number);
  141.                     insert_new_number_into_array_on_index_k( array, n , new_number, (*n) );
  142.                     break;
  143.  
  144.             case '3':                   // Enter a number in a place with index k
  145.                     printf("\n\n Enter a number you wish to add in the array: ");
  146.                     scanf("%d",&new_number);
  147.                     printf("\n\n Enter the index k of the place where you want \n to insert the new element of the array: k = ");
  148.                     scanf("%d",&k);
  149.                     insert_new_number_into_array_on_index_k( array, n , new_number, k );
  150.                     break;
  151.  
  152.             case '0' :                  // 0 for previous meny
  153.                     return( (int)choice );
  154.                     break;
  155.  
  156.             case 27  :                  // Escape for previous meny
  157.                     return( (int)choice );
  158.                     break;
  159.  
  160.             case  0  :                  // if a function key is pressed
  161.                     choice = getche();  // let's empty that 0
  162.  
  163.             case -32 :                  // if a special function key is pressed
  164.                     choice = getche();  // let's empty that -32
  165.                     break;
  166.  
  167.             default:                    // if entered character is not from the meny
  168.                     if( choice < '0' || choice > '6' ) {
  169.                         printf("\n\n You have to choose (0-9) !!! \n");
  170.                         //system("PAUSE");
  171.                     }
  172.                     break;
  173.         } // switch( choice )
  174.     } // while( 1 )
  175.  
  176.     return( choice );
  177. }
  178.  
  179.  
  180. // meny_2, choice_1
  181. // Modifying one element of array that has index
  182. void modifying_one_element_of_array_that_has_index( int array[], int n, int index ) {
  183.  
  184.     int NewValue;
  185.  
  186.     while( 1 ) {                        // endless loop to control number entry
  187.  
  188.         printf("\n\n Enter the index of the element you want to modify ");
  189.         scanf("%d",&index);
  190.  
  191.         if( index<0 || index>n )
  192.             printf("\n\n Enter the index between 0 i %d !\n\n", n );
  193.         else
  194.             break;                      // a good index is entered and we exit the infinite loop while (1)
  195.     }
  196.  
  197.     printf("\n\n Element to modify is array[%d] = %d . Change it with: ", index, array[index] );
  198.     scanf("%d",&NewValue);
  199.  
  200.     array[index] = NewValue;
  201. }
  202.  
  203.  
  204.  
  205. // meny_2, choice_2
  206. // modifying all occurrences of OldValue in array with NewValue
  207. // Returns 0 if NewValue does not exist in the array, otherwise returns number_of_elements_modified
  208. int modifying_all_occurrences_of_OldValue_in_array_with_NewValue( int array[], int n, int OldValue, int NewValue ) {
  209.  
  210.     int i, number_of_elements_modified=0;
  211.  
  212.     for( i=0; i<n; i++ )                        // we move through the whole array
  213.  
  214.         if( array[i] == OldValue ) {            // if we have found the value we need to change
  215.             array[i] = NewValue;                // we assign it a new value
  216.             number_of_elements_modified++;      // we increase the number of changed elements
  217.         }
  218.     return( number_of_elements_modified );
  219. }
  220.  
  221.  
  222. // meny_2, Modifying of elements of array
  223. int meny_2( int array[], int *n ) {
  224.  
  225.     int choice, index, OldValue, NewValue;
  226.  
  227.     while( 1 ) {                                // an infinite loop that exits with choice 0
  228.  
  229.         //system("CLS");                      // clear console window
  230.  
  231.         display_array_of_integers( " Array is: ", array, *n );
  232.  
  233.     printf("\n\n +----------------------------------------------------------+ \n"
  234.                " | meny_2:     MODIFYING OF ELEMENTS OF ARRAY               | \n"
  235.                " +----------------------------------------------------------+ \n"
  236.                " |  1. Modifying one array element                          | \n"
  237.                " |                                                          | \n"
  238.                " |  2. Modifying all occurrences of an value in array       | \n"
  239.                " |                                                          | \n"
  240.                " |  0. ESC or 0 for previous meny                           | \n"
  241.                " +----------------------------------------------------------+ \n"
  242.                "\n\n Choose (1-3)  ?  ");
  243.  
  244.         switch( choice = getche() ) {       // based on the choices we call the appropriate functions
  245.  
  246.             case '1':                       // Modifying one array element
  247.  
  248.                     modifying_one_element_of_array_that_has_index( array, *n, index );
  249.                     break;
  250.  
  251.             case '2':                       // Modifying all occurrences of an element in an array
  252.                     printf("\n\n Enter the value of the element you want to modify ");
  253.                     scanf("%d",&OldValue);
  254.  
  255.                     printf("\n\n Enter the NewValue value of the element %2d you want to modify ", OldValue );
  256.                     scanf("%d",&NewValue);
  257.  
  258.                     modifying_all_occurrences_of_OldValue_in_array_with_NewValue( array, *n, OldValue, NewValue );
  259.                     break;
  260.  
  261.             case '0':                       // 0 for previous meny
  262.             case 27 :                       // ESC for previous meny
  263.                     return( choice );
  264.                     break;
  265.  
  266.             case  0  :                      // if a function key is pressed
  267.                     choice = getche();      // let's empty that 0
  268.  
  269.             case 224 :                      // if a special function key is pressed
  270.                     choice = getche();      // let's empty that 224
  271.                     break;
  272.  
  273.             default:                        // if entered character is not from the meny
  274.                     if( choice < '0' || choice > '6' ) {
  275.                         printf("\n\n You have to choose (0-9) !!! \n");
  276.                         //system("PAUSE");
  277.                     }
  278.                     break;
  279.  
  280.         } // switch( choice ) {
  281.  
  282.     } // while( 1 )
  283.  
  284.     return( choice );
  285. }
  286.  
  287.  
  288. // meny_3, choice_1
  289. // deleting one array element whose index is index_of_the_element_to_delete
  290. // https://codeforwin.org/2015/07/c-program-to-delete-element-from-array.html
  291. void deleting_one_array_element_whose_index_is( int array[], int *n ) {
  292.  
  293.     int i, index_of_the_element_to_delete;
  294.  
  295.     while( 1 ) {                    // endless loop to control number entry
  296.  
  297.         printf("\n\n Enter the index of the element you want to delete ");
  298.         scanf("%d",&index_of_the_element_to_delete);
  299.  
  300.         if( index_of_the_element_to_delete < 0 || index_of_the_element_to_delete > (*n)-1 )
  301.             printf("\n\n Enter a number between 0 i %d !\n\n", (*n)-1 );
  302.         else
  303.             break;                  // a good number is entered and we exit the infinite loop while (1)
  304.     }
  305.  
  306.     for( i=index_of_the_element_to_delete; i<(*n)-1; i++ )  // Copy next element value to current element
  307.     {
  308.         array[i] = array[i+1];
  309.     }
  310.  
  311.     (*n)--;                         // the function has an address of n, so it can change it
  312. }
  313.  
  314.  
  315. // meny_3, choice_2
  316. // deleting all occurrences of value in array
  317. // Returns 0 if value does not exist in array, otherwise returns number_of_deleted_elements
  318. int deleting_all_occurrences_of_value_in_array( int array[], int *n ) {
  319.  
  320.     int i=0, value, new_n=0, number_of_deleted_elements=0;
  321.  
  322.     printf("\n\n Enter the value of the element you want to delete ");
  323.     scanf("%d",&value);
  324.  
  325.     while( i < *n ) {                           // we move through the whole array
  326.         if( array[i] == value )                 // if we find value we need to delete
  327.             number_of_deleted_elements++;       // increase number of deleted elements
  328.         else
  329.             array[new_n] = array[i];
  330.  
  331.         i++;                                    // we go to the next element by increasing the index
  332.         new_n = i - number_of_deleted_elements; // calculate new number of elements in array
  333.     }
  334.                                                 // the function has an address of n, so it can change it
  335.     *n = new_n;                                 // *n becomes new number of elements in array
  336.  
  337.     return( number_of_deleted_elements );
  338. }
  339.  
  340.  
  341. // meny_3, DELETING THE ARRAY ELEMENTS
  342. int meny_3( int array[], int *n ) {
  343.  
  344.     int choice, value;
  345.  
  346.     while( 1 ) {    // an infinite loop that exits with choice 0
  347.  
  348.         //system("CLS");          // clear console window
  349.  
  350.         display_array_of_integers( " Array is: ", array, *n );
  351.  
  352.     printf("\n\n +----------------------------------------------------+ \n"
  353.                " | meny_3:     DELETING THE ARRAY ELEMENTS            | \n"
  354.                " +----------------------------------------------------+ \n"
  355.                " |  1. Deleting one array element                     | \n"
  356.                " |                                                    | \n"
  357.                " |  2. Deleting all occurrences of value in an array  | \n"
  358.                " |                                                    | \n"
  359.                " |  0. ESC or 0 for previous meny                     | \n"
  360.                " +----------------------------------------------------+ \n"
  361.         "\n\n\t Choose (1-3)  ?  ");
  362.  
  363.         switch( choice = getche() ) {  // based on the choices we call the appropriate functions
  364.  
  365.         case '1':     // deleting one array element whose index is index_of_the_element_to_delete
  366.                 deleting_one_array_element_whose_index_is( array, n );
  367.                 break;
  368.  
  369.         case '2':     // Deleting all occurrences of value in an array
  370.                 deleting_all_occurrences_of_value_in_array( array, n );
  371.                 break;
  372.  
  373.             case '0' :                      // 0 to end of work
  374.                     return(choice);
  375.                     break;
  376.  
  377.             case 27  :                      // ESC to end of work
  378.                     return(choice);
  379.                     break;
  380.  
  381.             case  0  :                      // if a function key is pressed
  382.                     choice = getche();      // let's empty that 0
  383.                     break;
  384.  
  385.             case 224 :                      // if a special function key is pressed
  386.                     choice = getche();      // let's empty that 224
  387.                     break;
  388.  
  389.             default:                        // if entered character is not from the meny
  390.                     if( choice < '0' || choice > '2' ) {
  391.                         printf("\n\n You have to choose (0-2) !!! \n");
  392.                         //system("PAUSE");
  393.                     }
  394.                     break;
  395.  
  396.         } // switch( choice = getche() )
  397.  
  398.     } // while( 1 )
  399.  
  400.     return( choice );
  401. }
  402.  
  403.  
  404. // interchanging places two elements ( i-th and j-th ) of an array
  405. // Returns 0 if the i-th or j-th element does not exist, otherwise it returns 1
  406. // ( called from main meny )
  407. int interchanging_places_two_elements_of_an_array( int array[], int n ) {
  408.  
  409.     int i, j, mem;
  410.  
  411.     printf("\n\n Enter the index of first element you want to interchange ");
  412.     scanf("%d",&i);
  413.  
  414.                 // checking that the element with index i exists in the array
  415.     if( i<0 || i>n-1 ) {
  416.         printf("\n\n Element with index %d does not exist in array ! \n\n", i );
  417.         return(0);
  418.     }
  419.  
  420.     printf("\n\n Enter the index of second element you want to interchange ");
  421.     scanf("%d",&j);
  422.  
  423.                 // checking that the element with index j exists in the array
  424.     if( j<0 || j>n-1 ) {
  425.         printf("\n\n Element with index %d does not exist in array ! \n\n", j );
  426.         return(0);
  427.     }
  428.                                 // we interchange places of the i-th and j-th elements of the array
  429.     mem = array[i];             // the value of the i-th is stored in the memory mem
  430.     array[i] = array[j];        // i-th becomes j-th
  431.     array[j] = mem;             // j-th becomes mem ( it becomes old i-th )
  432.  
  433.     return(1);
  434. }
  435.  
  436.  
  437. // Search for the occurrences of a value in an array
  438. // Returns the number_of_occurrences of the requested value in a string
  439. // ( called from main meny )
  440. int search_for_the_occurrences_of_a_value_in_an_array( int array[], int n ) {
  441.  
  442.     int i=0, value, number_of_occurrences=0;
  443.  
  444.     printf("\n\n Enter the value you are looking for in the array ");
  445.     scanf("%d",&value);
  446.  
  447.     while( i < n ) {                    // we move through the entire array
  448.  
  449.         if( array[i] == value ) {       // if we find the value we are looking for
  450.             number_of_occurrences++;
  451.             printf("\n %2d. found: array[%2d] = %2d \n", number_of_occurrences, i, array[i] ); // the elements found are displayed
  452.         }
  453.  
  454.         i++;                            // we go to the next element by increasing the index
  455.     }
  456.  
  457.     if( number_of_occurrences == 0 ) {
  458.         printf("\n\n There is no value %d in array !\n\n", value);
  459.         return (0);
  460.     }
  461.     else
  462.     {
  463.         return( number_of_occurrences );
  464.     }
  465. }
  466.  
  467.  
  468. // Main meny
  469. int meny( int array[], int *n ) {
  470.  
  471.     int i, j;
  472.     int choice;
  473.  
  474.     while( 1 ) {    // an endless loop that exits with a choice of 0 or Escape
  475.  
  476.         //system("CLS");          // clear command window
  477.  
  478.         display_array_of_integers( " The values of the array elements are the same as their indexes:", array, *n );
  479.  
  480.         printf("\n\n +----------------------------------------------------------------+ \n"
  481.                    " |       MAIN MENY FOR C ARRAYS WITH INTEGERS                     | \n"
  482.                    " +----------------------------------------------------------------+ \n"
  483.                    " | 1. Inserting a new number into the array (own meny_1)          | \n"
  484.                    " | 2. Modification, change of one number by another (own meny_2)  | \n"
  485.                    " | 3. Deleting array element (own meny_3)                         | \n"
  486.                    " | 4. Substitution of place with i-th and j-th number             | \n"
  487.                    " | 5. Search for a number in the array                            | \n"
  488.                    " | 6. Reset the array to initial values                           | \n"
  489.                    " | 0. ESC or 0 to end of work                                     | \n"
  490.                    " +----------------------------------------------------------------+ \n"
  491.                    "\t Your choice is (0-6)  ?  " );
  492.  
  493.         switch( choice = getche() ) {       // based on the choices we call the appropriate functions
  494.  
  495.             case '1' :
  496.                     meny_1(array,n);        // Inserting a new number into the array (own meny)
  497.                     break;
  498.  
  499.             case '2' :
  500.                     meny_2(array,n);        // Modification, change of one number by another
  501.                     break;
  502.  
  503.             case '3' :
  504.                     meny_3(array,n);        // Deleting array element (own meny)
  505.                     break;
  506.  
  507.             case '4' :
  508.                     interchanging_places_two_elements_of_an_array( array, *n );     // interchanging places two elements of an array
  509.                     break;
  510.  
  511.             case '5' :
  512.                     search_for_the_occurrences_of_a_value_in_an_array( array, *n ); // search for the occurrences of a value in an array
  513.                     break;
  514.  
  515.             case '6' :                      // Reset the array to initial values
  516.                    reset_array( array, n ); // The 10 elements of an array become the same as their indexes
  517.                    break;
  518.  
  519. //            case '7' :
  520. //                    meny_7(array,n);
  521. //                    break;
  522.  
  523. //            case '8' :
  524. //                    meny_8(array,n);
  525. //                    break;
  526.  
  527. //            case '9' :
  528. //                    meny_9(array,n);
  529. //                    break;
  530.  
  531.             case '0' :                      // 0 to end of work
  532.                     return(choice);
  533.                     break;
  534.  
  535.             case 27  :                      // ESC to end of work
  536.                     return(choice);
  537.                     break;
  538.  
  539.             case  0  :                      // if a function key is pressed
  540.                     choice = getche();      // let's empty that 0
  541.  
  542.             case 224 :                      // if a special function key is pressed
  543.                     choice = getche();      // let's empty that 224
  544.                     break;
  545.  
  546.             default:                        // if entered character is not from the meny
  547.                     if( choice < '0' || choice > '6' ) {
  548.                         printf("\n\n You have to choose (0-9) !!! \n");
  549.                         //system("PAUSE");
  550.                     }
  551.                     break;
  552.  
  553.         } // switch( choice )
  554.  
  555.     } // while( 1 )
  556.  
  557.     return( choice );
  558. }
  559.  
  560.  
  561.  
  562. int main(void) {
  563.  
  564.     int array[100];             // the array of 100 integers we work with
  565.     int n;                      // n is the number of array elements, will be set in next line
  566.  
  567.     reset_array( array, &n );   // 10 array elements become the same as their indexes
  568.  
  569.     meny( array , &n );         // we invoke the main program menu
  570.  
  571.     return 0;
  572. }
Add Comment
Please, Sign In to add comment