473,394 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

I need to modify my code to sum the rainfall per year?

Expand|Select|Wrap|Line Numbers
  1. // function prototypes
  2. void inputdata();
  3. void printdata();
  4.  
  5. // Global variables
  6. // These are available to all functions
  7. float Raindata[NUMYEARS][NUMMONTHS];
  8. char years[NUMYEARS][5] = {"2011","2012","2013","2014","2015"};
  9.  char months[NUMMONTHS][12] ={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
  10. int main ()
  11. {
  12.   char enterData = 'y';
  13.   printf("Do you want to input Precipatation data? (y for yes)\n");
  14.   scanf("%c",&enterData);
  15.   if (enterData == 'y') {
  16.     // Call Function to Input data
  17.     inputdata();  
  18.  
  19.      // Call Function to display data
  20.      printdata();
  21.    }
  22.    else {
  23.      printf("No data was input at this time\n");
  24.    }
  25.    printf("Please try the Precipitation program again. \n");
  26.    return 0;
  27. }
  28. // function to inputdata
  29. void inputdata() {
  30.   /* variable definition: */
  31.   float Rain=1.0;  
  32.    // Input Data
  33.    for (int year=0;year < NUMYEARS; year++) {
  34.       for (int month=0; month< NUMMONTHS; month++) {
  35.           printf("Enter rain for %d, %d:\n", year+1, month+1);
  36.           scanf("%f",&Rain);
  37.           Raindata[year][month]=Rain;         
  38.       }
  39.    }
  40. }
  41. // Function to printdata
  42. void printdata(){
  43. // Print data
  44.    printf ("year\t month\t rain\n");
  45.    for (int year=0;year < NUMYEARS; year++) {
  46.       for (int month=0; month< NUMMONTHS; month++) {
  47.           printf("%s\t %s\t %5.2f\n", years[year],months[month],Raindata[year][month]);         
  48.       }      
  49.    }
  50. }
  51.  
  52.  
This is what I've started need help to finish it
Expand|Select|Wrap|Line Numbers
  1. // Function to sum toatal rainfall per year
  2. void printdata(){
  3. // Print data
  4.     printf ("rain+month\n");
  5.     for (int month=0; month< NUMMMONTHS; month++) {
  6.  
  7.     }
  8. }
  9.  
Oct 12 '18 #1
8 1796
weaknessforcats
9,208 Expert Mod 8TB
Have you considered using a struct for Raindata?

Expand|Select|Wrap|Line Numbers
  1. struct Raindata
  2. {
  3.    int year;
  4.    int RainByMonth[12];
  5. }
This would simplify your code. To get the yearly rain amount you would just add the elements of the RainByMonth array.

What do you think?
Oct 13 '18 #2
where would I place this?
Oct 14 '18 #3
weaknessforcats
9,208 Expert Mod 8TB
You were going to use global variables to avoid passing arguments to functions. While this is not a good idea, it does work. So you could:

Expand|Select|Wrap|Line Numbers
  1. struct Raindata
  2. {
  3.  
  4.  
  5. int year;
  6.  
  7. int RainByMonth{12];
  8.  
  9. };
  10.  
  11.  
  12.  
  13.  
  14. Raindata RainByYears[10];
  15.  
  16.  
  17. int main()
  18. {
  19.  
  20. }
Every function can now access the RainByYears array. I just made up the 10.
Oct 14 '18 #4
would you mind showing me where to place it in my code please and thank you
Oct 14 '18 #5
weaknessforcats
9,208 Expert Mod 8TB
The compiler has got know your types and your functions before you use them in the code.

All those #include are to provide this information.

You would place the struct definition above main() but after the #includes.

You would place the Raindata array after the struct definition but before main().

Look at my last post.
Oct 14 '18 #6
Expand|Select|Wrap|Line Numbers
  1. #define NUMMONTHS 12
  2. #define NUMYEARS 5
  3. #include <stdio.h>
  4.  
  5. // function prototypes
  6. void inputdata();
  7. void printdata();
  8.  
  9. // Global variables
  10. // These are available to all functions
  11. float Raindata[NUMYEARS][NUMMONTHS];
  12. char years[NUMYEARS][5] = {"2011","2012","2013","2014","2015"};
  13.  char months[NUMMONTHS][12] ={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
  14. int main ()
  15. {
  16.   char enterData = 'y';
  17.   printf("Do you want to input Precipatation data? (y for yes)\n");
  18.   scanf("%c",&enterData);
  19.   if (enterData == 'y') {
  20.     // Call Function to Input data
  21.     inputdata();  
  22.  
  23.      // Call Function to display data
  24.      printdata();
  25.    }
  26.    else {
  27.      printf("No data was input at this time\n");
  28.    }
  29.    printf("Please try the Precipitation program again. \n");
  30.    return 0;
  31. }
  32. // function to inputdata
  33. void inputdata() {
  34.   /* variable definition: */
  35.   float Rain=1.0;  
  36.    // Input Data
  37.    for (int year=0;year < NUMYEARS; year++) {
  38.       for (int month=0; month< NUMMONTHS; month++) {
  39.           printf("Enter rain for %d, %d:\n", year+1, month+1);
  40.           scanf("%f",&Rain);
  41.           Raindata[year][month]=Rain;         
  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.    }
  54. }
  55.  
  56.  
So where would I place the sum of rainfall per year
Oct 15 '18 #7
Expand|Select|Wrap|Line Numbers
  1. #define NUMMONTHS 12
  2. #define NUMYEARS 5
  3. #include <stdio.h>
  4.  
  5. // function prototypes
  6. void inputdata();
  7. void printdata();
  8.  
  9. // Global variables
  10. // These are available to all functions
  11. float Raindata[NUMYEARS][NUMMONTHS];
  12. char years[NUMYEARS][5] = {"2011","2012","2013","2014","2015"};
  13.  char months[NUMMONTHS][12] ={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
  14.  int Raindata; 
  15.  int Years;
  16.  int sum = Raindata + Years;
  17. int main ()
  18. {
  19.   char enterData = 'y';
  20.   printf("Do you want to input Precipatation data? (y for yes)\n");
  21.   scanf("%c",&enterData);
  22.   if (enterData == 'y') {
  23.     // Call Function to Input data
  24.     inputdata();  
  25.  
  26.      // Call Function to display data
  27.      printdata();
  28.    }
  29.    else {
  30.      printf("No data was input at this time\n");
  31.    }
  32.    printf("Please try the Precipitation program again. \n");
  33.    return 0;
  34. }
  35. // function to inputdata
  36. void inputdata() {
  37.   /* variable definition: */
  38.   float Rain=1.0;  
  39.    // Input Data
  40.    for (int year=0;year < NUMYEARS; year++) {
  41.       for (int month=0; month< NUMMONTHS; month++) {
  42.           printf("Enter rain for %d, %d:\n", year+1, month+1);
  43.           scanf("%f",&Rain);
  44.           Raindata[year][month]=Rain;         
  45.       }
  46.    }
  47. }
  48. // Function to printdata
  49. void printdata(){
  50. // Print data
  51.    printf ("year\t month\t rain\n");
  52.    for (int year=0;year < NUMYEARS; year++) {
  53.       for (int month=0; month< NUMMONTHS; month++) {
  54.           printf("%s\t %s\t %5.2f\n", years[year],months[month],Raindata[year][month]);         
  55.       }      
  56.    }
  57.  //Function to sum rainfall
  58.     sum = rain + year;
  59.     printf("rainfall per year");
  60. }
I'm getting a bunch of errors

prog.c:14:6: error: conflicting types for ‘Raindata’
int Raindata;
^~~~~~~~
prog.c:11:7: note: previous declaration of ‘Raindata’ was here
float Raindata[NUMYEARS][NUMMONTHS];
^~~~~~~~
prog.c:16:12: error: initializer element is not constant
int sum = Raindata + Years;
^~~~~~~~
prog.c: In function ‘inputdata’:
prog.c:44:19: error: subscripted value is neither array nor pointer nor vector
Raindata[year][month]=Rain;
^
prog.c: In function ‘printdata’:
prog.c:54:73: error: subscripted value is neither array nor pointer nor vector
printf("%s\t %s\t %5.2f\n", years[year],months[month],Raindata[year][month]);
^
prog.c:58:8: error: ‘rain’ undeclared (first use in this function)
sum = rain + year;
^~~~
prog.c:58:8: note: each undeclared identifier is reported only once for each function it appears in
prog.c:58:15: error: ‘year’ undeclared (first use in this function)
sum = rain + year;
Oct 15 '18 #8
weaknessforcats
9,208 Expert Mod 8TB
The rule is that you fix only the first error. Often,the first error also causes other errors that will go away when the first error is fixed.

In this case, the first error is caused by having two Raindata variables. One is an array of float and the other is an int.

Fix that, recompile, and repeat fixing first errors until your errors go away.
Oct 15 '18 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Cibulya Dmitriy | last post by:
Hi All, I want to catch the next MSSQL error in my SQL code with following continue calculations Server: Msg 17, Level 16, State 1, Line 1 SQL Server does not exist or access denied.
2
by: Baldy | last post by:
Hi All is it possible to modify code at run time? I have a set of constants that change at deployment (from development to deployment server and a few other consts) I have a menu item that...
34
by: Mark Kamoski | last post by:
Hi-- Please help. I need a code sample for bubble sort. Thank you. --Mark
2
by: Mark Kamoski | last post by:
Hi Everyone-- Please help. I need a code sample for merge sort. Thank you. --Mark
1
by: Riyadh Hossain | last post by:
I am developing USPS shipping API for my site. All the work is going on testing phase. My site uses PHP/MySQL. Now I am working on USPS tracking/confirm. I can send request and get responses from...
0
by: ravibrl | last post by:
Hey I need c++ code to access a webpage and download some software. If I execute that it should download that software and it should install it in my system. Give suggestions how to write that code...
1
by: Krishna | last post by:
In vb2005 I can't modify code in debug mode why? Is it possible to set? Thanks
3
by: dee vee bee | last post by:
this is my project i need a code that will show like this.. "first number __" "second number ___" "choose operation: addition..subtraction..multiplication..division" "the answer is ___" the...
1
by: saravanatmm | last post by:
I need javascript code for validate the email address. Email address field cannot allowed the capital letters, special characters except '@' symbol. But can allowed the small letters, numeric...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.