Homework Cont'

This is a discussion on Homework Cont' within the C++ Programming forums, part of the General Programming Boards category; I posted a question last night, but I was unable to include my code. The question is again how to ...

  1. #1
    Kurious
    Guest

    Question Homework Cont'

    I posted a question last night, but I was unable to include my code. The question is again how to determine the highest and lowest value in an array.


    //This program will allow the user to input rainfall amounts for twelve months,
    //It will then calculate the total amount, the average amount, and tell the months
    //with the highest and lowest amounts.

    #include <iostream.h>

    void main(void)
    {
    float rain[12];

    cout<<"Enter the rainfall (in inches) for each month "<<endl;

    for (int count=1; count<=12; count++)
    {
    cout<<"Month "<<count<<":";
    cin>>rain[count-1];
    }

    cout.precision(2);
    cout.setf(ios::fixed|ios::showpoint);

    float total = rain[0]+rain[1]+rain[2]+rain[3]+rain[4]+rain[5]+rain[6]+rain[7]+rain[8]+rain[9]+rain[10]+rain[11];
    cout<<"The total rainfall was: "<<total<<endl;

    float average = total/12;
    cout<<"The average rainfall was: "<< average <<" inches. "<<endl;



    }

    Any other pointers concerning my code would be greatly appreciated, because this is due Monday, and I'm stuck!!

    thanks, George

  2. #2
    In The Light
    Join Date
    Oct 2001
    Posts
    598
    howdy,
    did you say
    Code:
    void main(void)
    DUCK.... IN COMING

    M.R.
    I don't like you very much. Please post a lot less.
    Cheez
    *and then*
    No, I know you were joking. My point still stands.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571

    Re: Homework Cont'

    Originally posted by Kurious
    I posted a question last night, but I was unable to include my code. The question is again how to determine the highest and lowest value in an array.


    //This program will allow the user to input rainfall amounts for twelve months,
    //It will then calculate the total amount, the average amount, and tell the months
    //with the highest and lowest amounts.

    #include <iostream.h>

    void main(void)
    {
    float rain[12];

    cout<<"Enter the rainfall (in inches) for each month "<<endl;

    for (int count=1; count<=12; count++)
    {
    cout<<"Month "<<count<<":";
    cin>>rain[count-1];
    }

    cout.precision(2);
    cout.setf(ios::fixed|ios::showpoint);

    float total = rain[0]+rain[1]+rain[2]+rain[3]+rain[4]+rain[5]+rain[6]+rain[7]+rain[8]+rain[9]+rain[10]+rain[11];
    cout<<"The total rainfall was: "<<total<<endl;

    float average = total/12;
    cout<<"The average rainfall was: "<< average <<" inches. "<<endl;



    }

    Any other pointers concerning my code would be greatly appreciated, because this is due Monday, and I'm stuck!!

    thanks, George
    Well there are some things you could do to make it a bit cleaner. When you are looping through getting the rainfall for each month, why not have a variable called total ( initially set to 0 ) that you add each month to. That would save you the line where you add them all up. What if you had to do that for 100 elements? It would take a while to say array[0]+, array[1]+, ... ,+ array[100]. As for finding the highest and lowest value thats pretty simple. You will need another loop. Assign the lowest value as the first index. Then each loop check in the current index is lower then the "low variable" you have, if it is replace the value with the current array. Use the same method to find the highest value. Try to implement it, if you get stuck let me know.

    P.S. - Use int main.
    Code:
    int main( void )
    {
      return 0;
    }
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  4. #4
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    Code:
    float total = rain[0]+rain[1]+rain[2]+rain[3]+rain[4]+rain[5]+ra
    in[6]+rain[7]+rain[8]+rain[9]+rain[10]+rain[11];
    cout<<"The total rainfall was: "<<total<<endl;
    why so much work..just do

    Code:
    for(int i = 0; i < 12; i++)
    {
      total += rain[i];
    }
    if to find the most try this

    Code:
    float most = rain[0]; // sets the most to the first
    
    for(int j = 1; j < 12; j++)
    {
       if(most < rain[i])
       {
          most = rain[i];
       }
    }
    just try the opposite for less..oh yea have fun and void main(void)...yikes!!!!!
    nextus, the samurai warrior

  5. #5
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Smile

    well if you sort the array from largest to smallest:

    x[0] would hold the largest value and x[n -1] would hold the smallest. loop would be helpful as well.
    Mr. C: Author and Instructor

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 11-03-2001, 04:39 PM
  2. Homework
    By kermi3 in forum C Programming
    Replies: 10
    Last Post: 09-27-2001, 05:49 PM
  3. Homework
    By kermi3 in forum C++ Programming
    Replies: 15
    Last Post: 09-26-2001, 04:16 PM
  4. Homework
    By kermi3 in forum C Programming
    Replies: 0
    Last Post: 09-10-2001, 02:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21