Thread: Overlooking Something in an Array

  1. #1
    Registered User
    Join Date
    Jul 2016
    Posts
    3

    Overlooking Something in an Array

    Hello All!

    I'm new to programming and am in my last few week's of an intro course. So far I've managed to understand most of it but I'm stumped with an example this week. I had to manipulate the code and sum the rainfall for each year. Unfortunately, I only get 1 answer instead of 4, and the answer is always 0.0000.

    Can someone point me in the right direction? I wasn't able to find a close example in a forum search despite there being many rainfall posts.

    Feralix

    Code:
    #define NUMMONTHS 12
    #define NUMYEARS 5
    #include <stdio.h>
     
    // function prototypes
    void inputdata();
    void printdata();
     
    // Global variables
    // These are available to all functions
    float Raindata[NUMYEARS][NUMMONTHS];
    int sum = 0;
    char years[NUMYEARS][5] = {"2011","2012","2013","2014","2015"};
     char months[NUMMONTHS][12] ={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
    int main ()
    {
      char enterData = 'y';
      printf("Do you want to input Precipatation data? (y for yes)\n");
      scanf("%c",&enterData);
      if (enterData == 'y') {
        // Call Function to Input data
        inputdata();  
     
         // Call Function to display data
         printdata();
       }
       else {
         printf("No data was input at this time\n");
       }
       printf("Please try the Precipitation program again. \n");
       return 0;
    }
    // function to inputdata
    void inputdata() {
      /* variable definition: */
      float Rain=1.0;  
       // Input Data
       for (int year=0;year < NUMYEARS; year++) {
          for (int month=0; month< NUMMONTHS; month++) {
              printf("Enter rain for %d, %d:\n", year+1, month+1);
              scanf("%f",&Rain);
              Raindata[year][month]=Rain;         
          }
       }
    }
    // Function to printdata
    void printdata(){
    // Print data
    printf ("year\t month\t rain\n");
    for (int year=0;year < NUMYEARS; year++) {
    for (int month=0; month< NUMMONTHS; month++) {
    sum = sum + Raindata[NUMYEARS][NUMMONTHS];
    printf("%s\t %s\t %5.2f\n", years[year],months[month],Raindata[year][month]);
    }
    }
    printf("Total rain: %f\n", sum);
    }

  2. #2
    Registered User
    Join Date
    Dec 2015
    Posts
    112
    I noticed you using the wrong format specifier for printing the sum and you had a index for Raindata. You might want to change sum to float, but I left it as an int since that's what you had.

    Code:
    #include <stdio.h>#include <stdio.h>
    
    
    #define NUMMONTHS 12
    #define NUMYEARS 5
    
    
    // function prototypes
    void inputdata();
    void printdata();
    
    
    // Global variables
    // These are available to all functions
    float Raindata[NUMYEARS][NUMMONTHS];
    int sum = 0;
    char years[NUMYEARS][5] = {"2011","2012","2013","2014","2015"};
     char months[NUMMONTHS][12] ={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
    int main ()
    {
      char enterData = 'y';
      printf("Do you want to input Precipatation data? (y for yes)\n");
      scanf("%c",&enterData);
      if (enterData == 'y') {
        // Call Function to Input data
        inputdata();
    
    
         // Call Function to display data
         printdata();
       }
       else {
         printf("No data was input at this time\n");
       }
       printf("Please try the Precipitation program again. \n");
    
    
       system("pause");
       return 0;
    }
    // function to inputdata
    void inputdata() {
      /* variable definition: */
      float Rain=1.0;
       // Input Data
       for (int year=0;year < NUMYEARS; year++) {
          for (int month=0; month< NUMMONTHS; month++) {
              printf("Enter rain for %d, %d:\n", year+1, month+1);
              scanf("%f",&Rain);
              Raindata[year][month]=Rain;
          }
       }
    }
    // Function to printdata
    void printdata()
    {
        // Print data
        sum = 0;
    
    
        printf ("year\t month\t rain\n");
        for (int year=0;year < NUMYEARS; year++)
        {
            for (int month=0; month< NUMMONTHS; month++)
            {
                sum += Raindata[year][month];
                printf("%s\t %s\t %5.2f\n", years[year],months[month],Raindata[year][month]);
            }
        }
        printf("Total rain: %d\n", sum);
    }

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Just a small nit-pick.

    If you can, try not to use global variables in your code. Arrays can be passed to a
    function (they are automatically passed by reference anyway) so any changes will
    effect the calling code.
    Double Helix STL

  4. #4
    Registered User
    Join Date
    Dec 2015
    Posts
    112
    Quote Originally Posted by swgh View Post
    Just a small nit-pick.

    If you can, try not to use global variables in your code. Arrays can be passed to a
    function (they are automatically passed by reference anyway) so any changes will
    effect the calling code.
    I agree and avoid using #define if possible. It's better to use const int in this case since you can see the value while you are debugging.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I agree and avoid using #define if possible. It's better to use const int in this case since you can see the value while you are debugging.
    Since this is a C program #defining a constant is correct, declaring a const is the C++ way.

    Jim

  6. #6
    Registered User
    Join Date
    Jul 2016
    Posts
    3
    Thank you for the quick response. I was able to get results now. I will also take your advice and change sum to a float.

  7. #7
    Registered User
    Join Date
    Jul 2016
    Posts
    3
    Quote Originally Posted by swgh View Post
    Just a small nit-pick.

    If you can, try not to use global variables in your code. Arrays can be passed to a
    function (they are automatically passed by reference anyway) so any changes will
    effect the calling code.
    Hi swgh. So one thing I've noticed in this course is that it assumes one knows something about programming. I don't. It's the first course I've taken and oddly it's a prerequisite to intro programming. The text we have was published in 1991, I had to look up the reference for "global variable" to understand what you meant. However, I do appreciate the tip and I will place it in the function below.

  8. #8
    Registered User
    Join Date
    Dec 2015
    Posts
    112
    Quote Originally Posted by jimblumberg View Post
    Since this is a C program #defining a constant is correct, declaring a const is the C++ way.

    Jim
    http://http://stackoverflow.com/ques...define-vs-enum

    This post discusses the benefits and cons of each method. It seems an enum would be the best option here.

    Your post is correct since I have been reading C++ material, but a const is still an "ok" c solution, but not for globals.

  9. #9
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Quote Originally Posted by Feralix View Post
    Hi swgh. So one thing I've noticed in this course is that it assumes one knows something about programming. I don't. It's the first course I've taken and oddly it's a prerequisite to intro programming. The text we have was published in 1991, I had to look up the reference for "global variable" to understand what you meant. However, I do appreciate the tip and I will place it in the function below.
    No worries. I own a C book "Beginning C" published by Ron House from 1994 and they use global variables
    from chapter 2 up to 6 before finally using function defined local ones and passing them via parameter
    arguments. It's just the way things were back then. A lot of early C programs used global variables
    just for the sake of it being "easier" and "quicker" to code.

    Learning how to pass locally and by pointer reference (when needed) is a good practice to get into
    from the off-set. Global variables do have a use however, generally when even a static variable cannot
    be used in a given situation.
    Double Helix STL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-10-2014, 03:00 PM
  2. Overlooking something in a comparison
    By csharp100 in forum C Programming
    Replies: 7
    Last Post: 04-29-2012, 09:21 PM
  3. Replies: 2
    Last Post: 03-20-2012, 08:41 AM
  4. probably something simple I'm overlooking
    By tomasc in forum C++ Programming
    Replies: 3
    Last Post: 05-19-2008, 11:07 PM
  5. Replies: 6
    Last Post: 11-09-2006, 03:28 AM

Tags for this Thread