CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2012
    Posts
    3

    ALMOST DONE.. LITTLE HELP please!

    PHP Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;

    int main()
    {
     
    double totalAverage[6];
     
    int monkeyday;
     
     
     const 
    int NUM_MONKEYS 3;
     const 
    int NUM_DAYS 7;    
     
    double average [NUM_MONKEYS][NUM_DAYS];
     
    double highest=0lowest=30000;
     
     
    cout << "Enter the amount of food in pounds eaten by the referred monkey.\n";
     
     for (
    monkey 0monkey NUM_MONKEYSmonkey++)
     {
         for (
    day 0day NUM_DAYSday++)
         {
         
    cout << "Monkey " << (monkey 1) << ", "
         
    << "Day " << (day 1) << ": ";
         
    cin >> average [monkey][day];
         while ((
    average [monkey][day] < 0))
         {
         
    cout << "Please enter positive values.\n";
         
    cin >> average [monkey][day]; 
          }
            }
    cout << endl;
            }
    [
    B]cout << "Average of the three monkeys per day from day 1 to day 7\n";
    totalAverage[0]=0;
    for (
    day =0dayNUM_DAYSday++)

    {
        for (
    monkey 0monkey NUM_MONKEYSmonkey++)
    {    
    totalAverage[day] += average[monkey][day];}
        
    cout << "Average: " << totalAverage[day]/NUM_MONKEYS << endl;
    }[/
    B]
    //


        
    for (monkey 0monkey NUM_MONKEYSmonkey++)
    {
        for (
    day =0dayNUM_DAYSday++)
        {
            if ((
    average [monkey][day]) > highest)       //ESTE ESTA BIEN SEGUN YO ASI
            
    highest average [monkey][day];
        }
    //
    }
         for (
    monkey 0monkey NUM_MONKEYSmonkey++)
    {
        for (
    day =0dayNUM_DAYSday++)
        {
        if ((
    average [monkey][day]) < lowest )                    // PERO ESTE ES EL DEL LOWEST Y PUES NOSE :S 
        
    lowest average [monkey][day];
    }
    //
    }
      
    cout << fixed << showpoint << setprecision (2);    // PARA QUE SEA DECIMAL POR EL DOUBLE

    cout << "The highest amount is: " << highest << endl;
    cout << "The lowest amount is: " << lowest << endl;    
        
          
             
    system ("PAUSE");
             return 
    0;


    WHEN YOU RUN and enter all the values, the part where the loop that say: cout << "Average of the three monkeys per day from day 1 to day 7\n"; does the average correct but on the third or fifth goes with power e

  2. #2
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Location
    Lahore, Pakistan
    Posts
    4,211

    Re: ALMOST DONE.. LITTLE HELP please!

    Code:
    #include <iostream> 
    #include <iomanip> 
    using namespace std; 
    
    int main() 
    { 
    // double totalAverage[6];   // <---- THIS IS WHERE THE PROBLEM IS
    double totalAverage[6] = { 0 };  // <---- THIS SHOULD FIX THE PROBLEM, LESSSON: ALWAYS INITLIAZE THE VARIABLES WITH PROPER VALUES
    int monkey, day; 
    
    
    const int NUM_MONKEYS = 3; 
    const int NUM_DAYS = 7;     
    double average [NUM_MONKEYS][NUM_DAYS]; 
    double highest=0, lowest=30000; 
    
    cout << "Enter the amount of food in pounds eaten by the referred monkey.\n"; 
    
    for (monkey = 0; monkey < NUM_MONKEYS; monkey++) 
    { 
         for (day = 0; day < NUM_DAYS; day++) 
         { 
         cout << "Monkey " << (monkey + 1) << ", " 
         << "Day " << (day + 1) << ": "; 
         cin >> average [monkey][day]; 
         while ((average [monkey][day] < 0)) 
         { 
         cout << "Please enter positive values.\n"; 
         cin >> average [monkey][day]; 
          } 
            } 
    cout << endl; 
            } 
    cout << "Average of the three monkeys per day from day 1 to day 7\n"; 
    totalAverage[0]=0; // YOU ARE JUST SETTING THE 0th INDEX, NOT THE ENTIRE ARRAY, WHICH LEADS TO THE RESULTS THAT YOU ARE GETTING
    for (day =0; day< NUM_DAYS; day++) 
    
    { 
        for (monkey = 0; monkey < NUM_MONKEYS; monkey++) 
    {    totalAverage[day] += average[monkey][day];} 
        cout << "Average: " << totalAverage[day]/NUM_MONKEYS << endl; 
    } 
    // 
    
    
        for (monkey = 0; monkey < NUM_MONKEYS; monkey++) 
    { 
        for (day =0; day< NUM_DAYS; day++) 
        { 
            if ((average [monkey][day]) > highest)       //ESTE ESTA BIEN SEGUN YO ASI 
            highest = average [monkey][day]; 
        } 
    // 
    } 
         for (monkey = 0; monkey < NUM_MONKEYS; monkey++) 
    { 
        for (day =0; day< NUM_DAYS; day++) 
        { 
        if ((average [monkey][day]) < lowest )                    // PERO ESTE ES EL DEL LOWEST Y PUES NOSE :S 
        lowest = average [monkey][day]; 
    } 
    // 
    } 
      cout << fixed << showpoint << setprecision (2);    // PARA QUE SEA DECIMAL POR EL DOUBLE 
    
    cout << "The highest amount is: " << highest << endl; 
    cout << "The lowest amount is: " << lowest << endl;     
         
           
             system ("PAUSE"); 
             return 0; 
    }

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: ALMOST DONE.. LITTLE HELP please!

    Quote Originally Posted by juliani91 View Post
    PHP Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;

    int main()
    {
     
    double totalAverage[6];
     
    int monkeyday;
    ...
             
    system ("PAUSE");
             return 
    0;

    Please, don't use PHP tags with C++ code. use Code tags (# toolbar button) instead!
    Victor Nijegorodov

  4. #4
    Join Date
    Apr 2012
    Posts
    3

    Re: ALMOST DONE.. LITTLE HELP please!

    @Ejaz Thank you it worked perfectly, and sorry for the php tag, im still learning.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured