2015-09-05 40 views
-4

**指导原则:**为家长,年收入和家庭成员获取3个单独的数据清单(数组),然后获得所有年收入并将它们平均。以整洁的表格显示。如何在没有向量的情况下改进这个C++程序?

这是来自一个学校项目我没有被允许使用任何非常先进的东西,但我想现在回去改进它。我想让它变得更清洁,特别想找到更多的东西,我可以从它中除去而不是增加它。

// <Program Name> Programming Project #3 Average Income (Using Functions and Arrays) 
// <Author> Brendan Jackson 
// <Date of Programs Release> 08/05/15 
// <Program Description> takes 3 arrays and displays them with average income 
#include <iostream> // allows cin and cout statements 
#include <iomanip> //allows setprecision 
#include <string> //allows strings 
using namespace std; // Namespace std allows program to use entities from <iostream> 

int input(string[], int[], double[]); //function 1 
double calculate_average_income(double[], int); //function 2 
void display_survey_data(string[], int[], double[],int , double); //function 3 


int main() // main function 
{ 

    //variables for functions 
    string name[10]; 

    int members[10]; 

    double income[10]; 

    int count_of_households; 

    double average; 

    //get input 
    count_of_households = input(name, members, income); 

    //calculate average 
    average = calculate_average_income(income, count_of_households); 


    //output all data in table 
    display_survey_data(name, members, income, count_of_households, average); 


    return 0; 

} 


int input(string name[], int members[], double income[]) //function 1 
{ 

    // get household info 
    int count_of_households = 0; 

    cout << "How many house holds were there? "; 
    cin >> count_of_households; 
    //TODO: handle bad input (characters and decimals) 
    if (count_of_households >= 11 || count_of_households < 0) 
    { 
     cout << "must enter valid # " ; //TODO: more description 
     count_of_households = 0; //set back to safe value 
    } 
    else 
    { 

      //cycle through arrays     
     for (int count = 0; count < count_of_households; count++) //TODO: take out (count + 1) start from 1 alternatively 
     { 

      // get survey info for names 
      cout << "Enter household #" << (count + 1) << "'s head of household name\t" ; 
      cin.ignore() ; // ignores keyboard buffer characters 
      getline (cin, name[count]) ; 


      // get survey info for income 
      cout << "Enter household #" << (count + 1) << "'s annual income\t" ; 
      cin >> income[count]; 


      // get survey info for members 
      cout << "Enter household #" << (count + 1) << "'s household members\t" ; 
      cin >> members[count]; 
     } 
    } 
    return count_of_households; 
} 

double calculate_average_income(double income[], int count_of_households) //function 2 
{ 


    //add incomes together 
    double total = 0.0; 
    double average = 0.0; 

    //loop over income 
    for (int count = 0 ; count < count_of_households; count++) 
    { 
     //add income to runnning total 
     total += income[count]; 
    } 

    // save calculations 
    average = total/count_of_households; 
    return average; 
} 




void display_survey_data(string name[], int members[], double income[],int count_of_households, double average) //funtion 3 
{ 
    //print out header 
    cout << setw(30) << "" 
     << setw(30) << "" 
     << setw(30) << "NUMBER OF\n" ; 
    cout << setw(30) << "HOUSEHOLD NAME" 
     << setw(30) << "ANNUAL INCOME" 
     << setw(30) << "HOUSEHOLD MEMBERS\n" ; 
    cout << setw(30) << "--------------------" 
     << setw(30) << "---------------" 
     << setw(30) << "------------------------\n" ;  
    ///loop over values 
    for (int count = 0 ; count < count_of_households; count++) 
    { 
     cout << setw(30) << name[count] 
      << setw(30) << setprecision(2) << fixed << showpoint << income[count] 
      << setw(30) << members[count] 
      << endl; 
    } 
    // display average 
    cout << endl 
     << setw(30) << "AVERAGE INCOME" 
     << setw(30) << average 
     << endl; 
} 
+1

而不是3阵列,定义代表一个家庭的一类,并让这些阵列。或者更好的还是那些矢量。 –

+0

另请注意,您不能将原始数组传递给函数而不提供大小信息,或者在声明中包含大小,或者传递包含大小的另一个参数。 –

回答

0

你可以使用std::array

这简直是在栈上的数组,就像你使用,但迭代器,类型安全,势必安全,使用值语义与大多数STL算法工作。

据声明如下:

array<string, 3> myArray; 

而且必须通过引用传递,因为路过的值会复制它的内容:你必须指定的长度

void doSomething(array<int, 6>& aArray) { 
    // do something 
} 

公告数组,因为它是一个模板参数。如果你想有任何大小的数组,使用tempaltes:

template<size_t length> 
void foobar(array<double, length> theArray) { 
    // do something else 
} 
相关问题