15 global variables 16 these are available to all

Info icon This preview shows pages 37–42. Sign up to view the full content.

View Full Document Right Arrow Icon
15. // Global variables 16. // These are available to all functions 17. float Raindata [ NUMYEARS ][ NUMMONTHS ] ; 18. char years [ NUMYEARS ][ 5 ] = { "2011" , "2012" , "2013" , "2014" , "2015" } ; 19. char months [ NUMMONTHS ][ 12 ] = 20. { "Jan" , "Feb" , "Mar" , "Apr" , "May" , "Jun" , "Jul" , "Aug" , "Sep" , "Oct" , "Nov" , "Dec" } ; 21. 22. int main () {
Image of page 37

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
23. float sumAll = 0 ; 24. 25. char enterData = 'y' ; 26. printf ( "Do you want to input Precipitation data? (y for yes) \n " ) ; 27. scanf ( "%c" , & enterData ) ; 28. printf ( " \n " ) ; 29. if ( enterData == 'y' || enterData == 'Y' ) { 30. // Call Function to Input data 31. inputdata () ; 32. 33. // Call Function to Display data 34. printdata () ; 35. 36. // Call Function to Sum yearly data 37. sumdata () ; 38. 39. // Call Function to Sum all Rainfall data 40. sumAll = sumAllData () ; 41. printf ( " \n\n\n >>> Sum of all Rainfall data is: %5.2f" , sumAll ) ; 42. } 43. else { 44. printf ( " \n No data was inputted at this time." ) ; 45. } 46. printf ( " \n\n\n Please try the Precipitation program again. \n\n\n\n " ) ;
Image of page 38
47. return 0 ; 48. } 49. 50. // Function to inputdata 51. void inputdata () { 52. /* Variable definition: */ 53. float Rain = 1.0 ; 54. int year , month ; 55. 56. // Input data 57. for ( year = 0 ; year < NUMYEARS ; year ++ ) { 58. for ( month = 0 ; month < NUMMONTHS ; month ++ ) { 59. printf ( "Enter rain for %d, %d: \n " , year + 1 , month + 1 ) ; 60. scanf ( "%f" , & Rain ) ; 61. Raindata [ year ][ month ] = Rain ; 62. } 63. } 64. } 65. 66. // Function to sumdata 67. void sumdata () { 68. /* Variable definition: */ 69. int year , month ; 70. float sumYear ; 71.
Image of page 39

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
72. //Compute Total of Rainfall data for each Year 73. for ( year = 0 ; year < NUMYEARS ; year ++ ) { 74. sumYear = 0 ; 75. for ( month = 0 ; month < NUMMONTHS ; month ++ ) { 76. sumYear += Raindata [ year ][ month ] ; 77. } 78. printf ( " \n\n Sum of year %s Rainfall data is: %5.2f" , years [ year ] , sumYear ) ; 79. } 80. } 81. 82. // Function to sumAllData 83. float sumAllData () { 84. int year , month ; 85. float total = 0 ; 86. 87. // Loop through Year (arr[0][] to arr[4][]) first 88. for ( year = 0 ; year < NUMYEARS ; year ++ ) { 89. // Then Months to add up each element (arr[0][0] to arr[4][4]) 90. for ( month = 0 ; month < NUMMONTHS ; month ++ ) { 91. total += Raindata [ year ][ month ] ; 92. } 93. } 94. return total ; 95. }
Image of page 40
96. 97. // Function to printdata 98. void printdata () { 99. // Print data 100. printf ( " \n year \t month \t rain \n " ) ; 101. 102. int year , month ; 103. for ( year = 0 ; year < NUMYEARS ; year ++ ) { 104. for ( month = 0 ; month < NUMMONTHS ; month ++ ) { 105. printf ( "%s \t %s \t %5.2f \n " , years [ year ] , months [ month ] , Raindata [ year ][ month ]) ; 106. } 107. } 108. }
Image of page 41

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 42
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern