/* 
 * File: main.cpp
 * Author: TBuchli
 * 
 * Write a program that helps a real estate agent calculate an average price of
 *up to 20 homes. An array of data type double should be used to contain the
 *prices. The program should prompt the user to enter the number of prices to
 *average — a maximum of 20 prices.
 * 
 * Then, the program should prompt the user to enter each price. Once all of the
 *prices have been entered into the array, sort them in ascending order. The
 *output should include a listing of the prices entered, and the average price
 *should be calculated and displayed. The program must use arrays to store the
 *prices and should use a loop to process the array...
 *
 * Created on November 20, 2013, 12:52 AM
 */

#include <cstdlib>
#include <iostream>
#include <algorithm>

using namespace std;

/*
 * In the beginning there was a real-estate agent that had to add the prices of
 *up to 20 house values, print the data in the order that which it was received,
 *then sort and print the data in ascending order, and finally print the average
 *of all house prices...
 * 
 * Along came TBuchli, and the real estate agent had time to shop for the
 *missus...
 * 
 */
int main(int argc, char** argv) {

    // Initialize Variables...
    int PRICES = 20;
    int housePrice[PRICES];
    int houseNum;
    int a;
    double total = 0;
    double average;
    houseNum = 0;

    // Prompt user for house price...
    cout << "Enter first house price, or type 999 to quit: ";

    // Store entered data as an array...
    cin >> housePrice[houseNum];

    while(houseNum < PRICES && housePrice[houseNum] != 999) {

      total += housePrice[houseNum];

      ++houseNum;

      if(houseNum < PRICES) {

          // Prompt user for next price...
          cout << "Enter next house price, or 999 to quit: ";

          // Store entered data as an array...
          cin >> housePrice[houseNum];

      }

    }

          // Print data...
          cout << "The entered house prices are: ";

          for(a = 0; a < houseNum; a++)

          // Print data in order it was entered...
          cout << housePrice[a] << "  ";

    // Sort function...
    sort(housePrice, housePrice + houseNum);

    // Print data (\n = on the next line)...
    cout << "\nThe entered house prices in ascending order are: ";

    for(a = 0; a != houseNum; ++a)

    // Print data in ascending order...
    cout << housePrice[a] << "  ";

        // Initiate average and store for later use...
        average = total / houseNum;

        // Print the average...
        cout << endl << "The average of all house prices is: " << average << endl;

        return 0;

} // And everyone was Happy, Happy, Happy!!!

Recommended Answers

All 4 Replies

Ran with netbeans IDE... I am getting errors after trying to change int housePrice[PRICES]; from an int to a double as instructions state... Any ideas???

Your code runs without error on my ubuntu Qt5.1.
Can you post your error?

It will not let you use decimal numbers... I need it to...

change line 39 to double housePrice[PRICES];and on line 52 in while loop change 999.0 change all int needed to double for checkups. You should be fine

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.