Advertisement
dmilicev

array of 10 integers v1.c

Oct 1st, 2019
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.23 KB | None | 0 0
  1. /*
  2.  
  3. array of 10 integers v1.c
  4.  
  5. Write a program that reads in 10 integers, then
  6. (The numbers here are just an example):
  7.  
  8. Prints out the lowest value:
  9. Minimum: 5
  10. Prints out the highest value:
  11. Maximum: 12
  12. Prints out the sum of all the values
  13. Sum: 78
  14. Prints out the average (make sure you use %g with printf to round properly):
  15. Average: 15.6
  16. Prints out the numbers, sorted
  17. Sorted: 1 4 5
  18. Prints out the median
  19. Median: 4.2
  20. Requirements: Minimum, maximum, sum, average and median must be
  21. calculated using separate functions you write, not directly in main.
  22. Here how the sum function could look like (more in the lecture):
  23.  
  24. int sum(int numbers[], int count)
  25. {
  26. ...
  27. }
  28.  
  29. The functions must be called:
  30.  
  31. minimum with return type int
  32. maximum with return type int
  33. sum with return type int
  34. average with return type float
  35. median with return type float
  36.  
  37. Approach: Try to work on one part at a time, same as last week.
  38. I recommend starting with reading in ten numbers and printing them back
  39. to make sure that part is working before you continue.
  40. Then write functions for each calculation (sum etc.) one at a time.
  41. Don’t start on multiple things at once, build often, test often!
  42.  
  43. Tip: For the sorting have a look at qsort,
  44. which already exists in the standard library.
  45. You should be able to find an example that use it easily.
  46. If you want to sort manually then bubble sort might be the easiest.
  47.  
  48. */
  49.  
  50. #include <stdio.h>
  51. #include <limits.h> // For INT_MAX and INT_MIN
  52.  
  53. // Function to print array
  54. void ShowArray(char text[],int arr[],int n)
  55. {
  56.     int i;
  57.  
  58.     printf("\n%s",text);
  59.     for(i=0;i<n;i++)
  60.         printf("%5d", arr[i]);
  61.  
  62.     printf("\n");
  63. }
  64.  
  65. // Function to print three smallest elements
  66. void print3smallest(int arr[], int arr_size)
  67. {
  68.     int i, first, second, third;
  69.  
  70.     if (arr_size < 3)
  71.     {
  72.         printf("\n Invalid input. There should be atleast three elements in array. \n");
  73.         return;
  74.     }
  75.  
  76. // 1) Initialize the smallest three elements as plus infinite.
  77. //    first = second = third = +Infinity
  78.  
  79.     first = second = third = INT_MAX;
  80.  
  81.     printf("\n How algorithm work: \n");
  82.     printf("\n  i  arr[i]       first     second        third \n"); // header
  83.  
  84. // 2) Iterate through all elements of array.
  85.  
  86.     for (i = 0; i < arr_size ; i ++)
  87.     {
  88.         if (arr[i] < first)         // Let current array element be arr[i]
  89.         {                           // If current element is smaller than first
  90.             third = second;         // then update third, second and first
  91.             second = first;         // This order of assignment is important
  92.             first = arr[i];
  93.         }
  94.         else if (arr[i] < second)   // If arr[i] is in between first and second
  95.         {                           // then update second
  96.             third = second;
  97.             second = arr[i];
  98.         }
  99.         else if (arr[i] < third)    // If arr[i] smaller then third
  100.             third = arr[i];         // then update third
  101.  
  102.         // print how algorithm work
  103.         printf("\n %2d  %4d %12d  %12d  %12d \n",i,arr[i],first,second,third);
  104.     }
  105.  
  106. // 3) Print first, second and third.
  107.  
  108.     printf("\n Three smallest elements are: %3d %3d %3d \n", first, second, third);
  109. }
  110.  
  111.  
  112. // function returns the smallest element of the array of arr_size integers
  113. int minimum(int arr[], int arr_size)
  114. {
  115.     // Let's suppose that the smallest element of array is the largest possible integer
  116.     int i, smallest=INT_MAX;
  117.  
  118.     for (i = 0; i < arr_size ; i++) // we go through the whole arrray
  119.         if (arr[i] < smallest)      // if current element arr[i] is smaller than smallest
  120.             smallest=arr[i];        // then arr[i] becomes the smallest
  121.  
  122.     return smallest;
  123. }
  124.  
  125.  
  126. // function returns the largest element of the array of arr_size integers
  127. int maximum(int arr[], int arr_size)
  128. {
  129.     // Let's suppose that the largest element of array is the smallest possible integer
  130.     int i, largest=INT_MIN;
  131.  
  132.     for (i = 0; i < arr_size ; i++) // we go through the whole arrray
  133.         if (arr[i] > largest)       // if current element arr[i] is larger than largest
  134.             largest=arr[i];         // then arr[i] becomes the largest
  135.  
  136.     return largest;
  137. }
  138.  
  139.  
  140. // function returns the sum of all elements of the array of arr_size integers
  141. int sum(int arr[], int arr_size)
  142. {
  143.     int i, sum=0;   // The starting value of the sum is zero
  144.  
  145.     for (i = 0; i < arr_size ; i++) // we go through the whole arrray
  146.         sum=sum+arr[i];             // new sum is old sum plus current element arr[i]
  147.  
  148.     return sum;
  149. }
  150.  
  151.  
  152. // function returns the average of all elements of the array of arr_size integers
  153. float average(int arr[], int arr_size)
  154. {
  155.     int i, sum=0;   // The starting value of the sum is zero
  156.     float average;
  157.  
  158.     for (i = 0; i < arr_size ; i++) // we go through the whole arrray
  159.         sum=sum+arr[i];             // new sum is old sum plus current element arr[i]
  160.  
  161.     average=sum/arr_size;
  162.  
  163.     return average;
  164. }
  165.  
  166.  
  167. // function to sort the array in ascending order
  168. void Array_sort(int *array , int n)
  169. {
  170.     int i=0 , j=0 , temp=0;         // declare some local variables
  171.  
  172.     for(i=0;i<n;i++)
  173.         for(j=0;j<n-1;j++)
  174.             if(array[j]>array[j+1]) // swap array[j] and array[j+1]
  175.             {
  176.                 temp        = array[j];
  177.                 array[j]    = array[j+1];
  178.                 array[j+1]  = temp;
  179.             }
  180. }
  181.  
  182.  
  183. // function to calculate the median of the array
  184. float median(int array[],int n)
  185. {
  186.     float median=0;
  187.  
  188.     // if number of elements are even
  189.     if(n%2 == 0)
  190.         median = (array[(n-1)/2] + array[n/2])/2.0;
  191.     // if number of elements are odd
  192.     else
  193.         median = array[n/2];
  194.  
  195.     return median;
  196. }
  197.  
  198.  
  199. // Main program to test above functions
  200. int main(void)
  201. {   // Loading 10 elements of array is boring, so let's put it this way
  202.     int arr[] = {12,13,1,10,34,2,25,8,17,5};    // try different one
  203.     int n = sizeof(arr)/sizeof(arr[0]);     // a way to get n, number of elements in array
  204.     int i;
  205.  
  206.     printf("\n Array arr[] has n = %d elements. \n", n);    // print number of elements in array
  207.  
  208.     ShowArray(" Array is: ",arr,n);
  209.  
  210.     printf("\n\n The smallest integer in array is %d \n",minimum(arr,n));
  211.  
  212.     printf("\n The largest integer in array is %d \n",maximum(arr,n));
  213.  
  214.     printf("\n The sum of all integers in array is %d \n",sum(arr,n));
  215.  
  216.     printf("\n The average of all integers in array is %f \n",average(arr,n));
  217.  
  218.     Array_sort(arr,n);
  219.  
  220.     ShowArray("\n Array sorted by bubble sort is: \n\n",arr,n);
  221.  
  222.     printf("\n The median of all integers in array is %f \n",median(arr,n));
  223.  
  224.  
  225.     return 0;
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement