Thread: segment and some portions of an array homework help needed

  1. #1
    Registered User
    Join Date
    May 2018
    Posts
    2

    segment and some portions of an array homework help needed

    100% honesty I am stuck on a homework assignment. I need to sum the raindata for each individual year. I am sure my code is overly complicated but so far I have done my best on my own and am starting to get desperate. My code is below. I am sure the multiple if statements and creation of the count variable are unneeded but I dont want the exact easy answer if possible I would like to fix the current code but if I need to scrap the entire function I would rather know now since I am running out of time.
    Code:
    // C code
    // This program will input and store meteorological data into an array.
    // Developer: Faculty CMIS102
    // Date: Jan 31, XXXX
    #define NUMMONTHS 12
    #define NUMYEARS 5
    #include <stdio.h>
    // function prototypes
    void inputdata();
    void printdata();
    void sumdata();
    // Global variables
    // These are available to all functions
    float Raindata[NUMYEARS][NUMMONTHS];
    float Winddata[NUMYEARS][NUMMONTHS];
    float sum;
    int count;
    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();
    
    
    sumdata();
    
    
    }
    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;
    float Wind=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);
    scanf("%f",&Wind);
    Raindata[year][month]=Rain;
    Winddata[year][month]=Wind;
    }
    }
    }
    // Function to printdata
    void printdata()
    {
    // Print data
    printf ("year\t month\t rain\t wind\n");
    for (int year=0;year < NUMYEARS; year++) {
    for (int month=0; month< NUMMONTHS; month++) {
    printf("%s\t %s\t %.2f\t %f\n",
    years[year],months[month],Raindata[year][month],Winddata[year][month]);
    }
    }
    }
    //Function to sumdata
    void sumdata()
    {
    sum = 0.0;
    count = 0;
    for (int year=0;year < NUMYEARS; year++) {
    for (int month=0; month< NUMMONTHS; month++) {
    count = count + 1;
    sum = sum + Raindata[year][month];
    
    
    if (count == 12)
    printf("%f\n",sum);
    
    
    if (count == 24)
    printf("%f\n",sum);
    
    
    if (count == 36)
    printf("%f\n",sum);
    
    
    if (count == 48)
    printf("%f\n",sum);
    
    
    if (count == 60)
    printf("%f\n",sum);
    
    
    }
    }
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,914
    I recommend that you:
    • Format your code properly, particularly in the area of indentation.
    • Only declare local variables, and hence pass around arguments to functions as needed.
    • Declare another array of 12 floats for the annual rain data sums. Likewise for wind data if that is required.
    • Rewrite the function to sum the rain data such that it only takes the rain data and sums it, storing the results into the new array. It should not print anything.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,165
    How much code have you written from scratch, and how much have you been ripping off from homework mills?

    Expert Help on CMIS 102 Week 8 Hand-On Lab | Smart Homework Help
    CMIS 102 Hands-On Lab Week 8 | WeCode - Sellfy.com
    Solved: // This Program Will Input And Store Meteorologica... | Chegg.com
    ad nauseam

    Sure, you've added a couple of lines for wind data - hardly taxing as it's just copy/paste what was done for rain.

    If you really want to learn, stop googling for pre-chewed and regurgitated code samples.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    May 2018
    Posts
    2
    Obviously the wind data was a easy alteration and the majority was provided to us for use the original code is everything in the sum function. I actually have not been using code samples Iím actually using the textbook and books I bought. If I was trying to cheat I would just pay the money to hire someone or use the coursehero because they have a full answer to exact assignment I could pay to use. Instead I chose to use forums and ask for help because I really donít know what I am doing I assume there is some ways to call only the month data from a year and sum that but I cannot figure it out. I have tried just making an ugly basic math equation to crudely force it for the best I could do was get a sum for 3 years individually the nature of that equation resets my sum to 0 for the other 2 sums. Thank you for assuming I have been cheating and trying to find an easy out and having to ask what was original it shows that at a glance it looks like I know why Iím doing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Programming homework help needed
    By bowmanx4587 in forum C Programming
    Replies: 1
    Last Post: 12-06-2015, 09:34 PM
  2. Replies: 1
    Last Post: 08-28-2012, 06:11 AM
  3. Urgent homework / debugging help needed!!!
    By jollykiddo in forum C Programming
    Replies: 2
    Last Post: 12-16-2011, 03:02 AM
  4. Homework help needed. cin.get issues.
    By DHart07 in forum C++ Programming
    Replies: 13
    Last Post: 04-13-2010, 10:30 PM
  5. Declare an array in the BSS segment
    By ^xor in forum C Programming
    Replies: 1
    Last Post: 05-27-2005, 05:12 PM

Tags for this Thread