Results 1 to 7 of 7

Thread: C++ help

  1. #1
    What is time to an immortal?
    AOD_Cadimus's Avatar
    Rank
    Sergeant
    Division
    Battlefront II
    Status
    Active
    Join Date
    Aug 2012
    Location
    Santa ana, CA
    Age
    31
    Posts
    1,081

    Default C++ help

    So I am stuck on my midterm with the two following questions I haven't the slightest clue were to begin; can anyone help me I have never encountered this before

    2. Write one program that uses counter-controlled repetition to process temperatures. The program has the following display:

    (1st Example Display)
    Enter the number of temperatures: 2
    Enter a temperature: 77
    Enter a temperature: 78
    The average temperature is 77.5 degrees for the 2 days.
    End of Program

    (2nd Example Display)
    Enter the number of temperatures: 3
    Enter a temperature: 80
    Enter a temperature: 84
    Enter a temperature: 79
    The average temperature is 81.0 degrees for the 3days.
    End of Program

    (3rd Example Display)
    Enter the number of temperatures: 0

    No temperatures were entered.
    End of Program






    3. Write one program that uses sentinel-controlled repetition to process the hours an employee works. The program has the following display:

    (1st Example Display)
    Enter the number of hours or -1 to quit: 9
    Enter the number of hours or -1 to quit: 4
    Enter the number of hours or -1 to quit: -1

    The employee worked an average of 6.5 hours/day for 2 days.
    End of Program

    (2nd Example Display)
    Enter the number of hours or -1 to quit: 6
    Enter the number of hours or -1 to quit: 3
    Enter the number of hours or -1 to quit: 8
    Enter the number of hours or -1 to quit: -1

    The employee worked an average of 5.6 hours/day for 3 days.
    End of Program

    (3rd Example Display)
    Enter the number of hours or -1 to quit: -1

    No hours were entered
    End of Program
    "In midnight clad" "If you suspect you have a case of the Pilot Tinkiez please report to the nearest medical officer"

  2. #2
    Looks like I picked the wrong week to quit sniffing glue Yetzederixx's Avatar
    Rank
    Forum Member
    Division
    None
    Status
    Active
    Join Date
    Feb 2014
    Location
    Sulphur, LA
    Age
    42
    Posts
    37

    Default

    When you hear "counter controlled loop" you would normally be thinking a for loop, but not in this case. This is one of those cases where a do...while construct could work.

    I'm not going to write your homework for you, but I will give you a basic algorithm in comment style.

    Code:
    // do
      // get number of temps from user
      // Should that number be > 0
        // Get other temperatures, good place for a for loop
        // display output to user
      // otherwise 
        // display exit message
    // while number of temps > 0
    That's how I would approach it at least. If you want more help on this I'm going to have to see code however.

  3. #3
    King of the World and Principle Penetration Engineer of ClanAOD
    AOD_Archangel's Avatar
    Rank
    Command Sergeant
    Division
    Battlefield
    Status
    Active
    Join Date
    Sep 2004
    Location
    Raleigh, NC
    Age
    32
    Posts
    7,467

    Default

    Hey Cadimus, I appreciate the bind you are in. I want you to do well so you can be successful, but answering an exam question for you is really against what AOD stands for. Getting generic advice like Yetz gave is fine, but please don't ask us to do your work.

    I don't usually respond to questions like this in more detail, however, I think Yetz advice is bit off and may lead you to an over complicated/wrong solution. The problem doesn't look for you to ask for the number of temps/hours in a loop until the user decides to quit. It wants you to ask for the number up font, then prompt for values until done. In the first problem, you want a structure more like this.

    Code:
    //get number of temps from user
    //for number of temps entered (a while loop is just as useful, but either way you'll need to track the variable separately from the number of temps)
       //get temp from user and store the value in a way that lets you create an average (I would NOT recommend trying to store the values separately)
    //if num temps > 0
      //calculate and show the average 
    //else
      //show default message

    What once was can never be again,
    What is now will never come anew,
    What will be will only pass once.
    Cherish it all.

  4. #4
    What is time to an immortal?
    AOD_Cadimus's Avatar
    Rank
    Sergeant
    Division
    Battlefront II
    Status
    Active
    Join Date
    Aug 2012
    Location
    Santa ana, CA
    Age
    31
    Posts
    1,081

    Default

    Quote Originally Posted by AOD_Archangel View Post
    Hey Cadimus, I appreciate the bind you are in. I want you to do well so you can be successful, but answering an exam question for you is really against what AOD stands for. Getting generic advice like Yetz gave is fine, but please don't ask us to do your work.

    I don't usually respond to questions like this in more detail, however, I think Yetz advice is bit off and may lead you to an over complicated/wrong solution. The problem doesn't look for you to ask for the number of temps/hours in a loop until the user decides to quit. It wants you to ask for the number up font, then prompt for values until done. In the first problem, you want a structure more like this.

    Code:
    //get number of temps from user
    //for number of temps entered (a while loop is just as useful, but either way you'll need to track the variable separately from the number of temps)
       //get temp from user and store the value in a way that lets you create an average (I would NOT recommend trying to store the values separately)
    //if num temps > 0
      //calculate and show the average 
    //else
      //show default message
    Thank you this will help me greatly I will compile what I have and include this algorithm as soon as I am off of work.
    "In midnight clad" "If you suspect you have a case of the Pilot Tinkiez please report to the nearest medical officer"

  5. #5
    What is time to an immortal?
    AOD_Cadimus's Avatar
    Rank
    Sergeant
    Division
    Battlefront II
    Status
    Active
    Join Date
    Aug 2012
    Location
    Santa ana, CA
    Age
    31
    Posts
    1,081

    Default

    This is what I have so far
    #include <iostream>
    using namespace std;

    double countAverageTemperature(int temperatures[], int number)
    {
    int total = 0;

    for (int index = 0; index < number; index++)
    {
    total += temperatures[index];
    }

    return (double)total / number;
    }

    int main()
    {
    cout << "Enter the number of temperatures: ";
    int number = 0;
    cin >> number;

    if (number == 0)
    {
    cout << endl << "No temperatures were entered." << endl;
    cout << "End of Program" << endl;
    }
    else
    {
    int* temperatures = new int [number];
    for (int index = 0; index < number; index++)
    {
    cout << "Enter a temperature: ";
    cin >> temperatures[index];
    }

    double average_temperature = countAverageTemperature(temperatures, number);
    cout << endl << "The average temperature is " << average_temperature << " degrees for the " << number << " days." << endl;
    cout << "End of Program" << endl;
    }

    return 0;
    }


    #include <iostream>
    #include <iomanip>
    using namespace std;

    int main()
    {
    int hours = 0, total = 0, number = 0;
    while (hours != -1)
    {
    cout << "Enter the number of hours or -1 to quit: ";
    cin >> hours;
    if (hours != -1)
    {
    total += hours;
    number++;
    }
    }

    if (number == 0)
    {
    cout << endl << "No hours were entered" << endl;
    cout << "End of Program" << endl;
    return 0;
    }

    cout << endl << "The employee worked an average of " << setprecision(3) << (double)total / number << " hours/day for " << number << " days. " << endl;
    cout << "End of Program" << endl;

    return 0;
    }
    "In midnight clad" "If you suspect you have a case of the Pilot Tinkiez please report to the nearest medical officer"

  6. #6
    Looks like I picked the wrong week to quit sniffing glue Yetzederixx's Avatar
    Rank
    Forum Member
    Division
    None
    Status
    Active
    Join Date
    Feb 2014
    Location
    Sulphur, LA
    Age
    42
    Posts
    37

    Default

    There isn't much I hate worse in this world than trying to read malformed code (except maybe fixing it), and yes I'm aware it's the forums that does this.

    Here's some better solutions to posting code

  7. #7
    Looks like I picked the wrong week to quit sniffing glue Yetzederixx's Avatar
    Rank
    Forum Member
    Division
    None
    Status
    Active
    Join Date
    Feb 2014
    Location
    Sulphur, LA
    Age
    42
    Posts
    37

    Default

    Quote Originally Posted by AOD_Archangel View Post
    I think Yetz advice is bit off and may lead you to an over complicated/wrong solution.
    My algorithm was from a quick glance and could definitely be off. I just got off finals, and started an internship so I was gunning from the hip.


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
vBulletin Skin By: ForumThemes.com
Top