fork download
  1. #define NUMMONTHS 12
  2. #define NUMYEARS 5
  3. #include <stdio.h>
  4. // function prototypes
  5. void inputdata();
  6. void printdata();
  7. // Global variables
  8. // These are available to all functions
  9. float Raindata[NUMYEARS][NUMMONTHS];
  10. char years[NUMYEARS][10] = {"2011","2012","2013","2014","2015"};
  11. char months[NUMMONTHS][7] ={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
  12. float sumofRainfall[NUMYEARS];
  13. int main ()
  14. {
  15. char enterData = 'y';
  16. printf("Do you want to input Precipatation data? (y for yes)\n");
  17. scanf("%c",&enterData);
  18. if (enterData == 'y') {
  19. // Call Function to Input data
  20. inputdata();
  21. // Call Function to display data
  22. printdata();
  23. }
  24. else {
  25. printf("No data was input at this time\n");
  26. }
  27. printf("Please try the Precipitation program again. \n");
  28. return 0;
  29. }
  30. // function to inputdata
  31. void inputdata() {
  32. /* variable definition: */
  33. float Rain=1.0;
  34. // Input Data
  35. for (int year=0;year < NUMYEARS; year++) {
  36. sumofRainfall[year] = 0.0;
  37. for (int month=0; month< NUMMONTHS; month++) {
  38. printf("Enter rain for %d, %d:\n", year+1, month+1);
  39. scanf("%f",&Rain);
  40. Raindata[year][month]=Rain;
  41. sumofRainfall[year] += Rain; // add rain to sum
  42. }
  43. }
  44. }
  45. // Function to printdata
  46. void printdata(){
  47. // Print data
  48. printf ("year\t month\t rain\n");
  49. for (int year=0;year < NUMYEARS; year++) {
  50. for (int month=0; month< NUMMONTHS; month++) {
  51. printf("%s\t %s\t %5.2f\n", years[year],months[month],Raindata[year][month]);
  52. }
  53. // print sum of rainfall for each year
  54. printf("The total Rainfall for the year was: %5.2f\n", sumofRainfall[year]);
  55. }
  56. }
Success #stdin #stdout 0s 9416KB
stdin
1.7
2.1
9.7
2.6
4.7
4.5
5.5
1.7
2.1
9.7
2.6
4.7
4.5
5.5
1.7
2.1
9.7
2.6
4.7
4.5
5.5
1.7
2.1
9.7
2.6
4.7
4.5
5.5
1.7
2.1
9.7
2.6
4.7
4.5
5.5
1.7
2.1
9.7
2.6
4.7
4.5
5.5
1.7
2.1
9.7
2.6
4.7
4.5
5.51.7
2.1
9.7
2.6
4.7
4.5
5.5
1.7
2.1
9.7
2.6
4.7
4.5
5.5
1.7
2.1
9.7
2.6
4.7
4.5
5.5
1.7
2.1
9.7
2.6
4.7
4.5
5.5
stdout
Do you want to input Precipatation data? (y for yes)
No data was input at this time
Please try the Precipitation program again.