我怎么能改进这个c++程序没有向量
How could I improve this c++ program without vectors?
**指南:**获得户主、年收入和家庭成员的3个独立数据列表(数组),然后获得所有年收入并将其平均。在一个整洁的表格中显示。
这是来自学校的一个项目,我不允许使用任何非常先进的东西,但我现在想回去改进它。我想让它更干净,特别想找到更多我可以从它身上拿走的东西,而不是增加的东西。
// <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 namet" ;
cin.ignore() ; // ignores keyboard buffer characters
getline (cin, name[count]) ;
// get survey info for income
cout << "Enter household #" << (count + 1) << "'s annual incomet" ;
cin >> income[count];
// get survey info for members
cout << "Enter household #" << (count + 1) << "'s household memberst" ;
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 OFn" ;
cout << setw(30) << "HOUSEHOLD NAME"
<< setw(30) << "ANNUAL INCOME"
<< setw(30) << "HOUSEHOLD MEMBERSn" ;
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;
}
您可以使用std::array
这只是一个堆栈上的数组,就像你使用的那样,但是有迭代器,类型安全,绑定安全,使用值语义,并且可以使用大多数stl算法。
它是这样声明的:
array<string, 3> myArray;
必须通过引用传递,因为通过值传递会复制它的内容:
void doSomething(array<int, 6>& aArray) {
// do something
}
注意你必须指定数组的长度,因为它是一个模板参数。如果你想要一个任意大小的数组,使用模板:
template<size_t length>
void foobar(array<double, length> theArray) {
// do something else
}
相关文章:
- 在遍历处理程序的向量时注册和注销处理程序
- 为什么程序在 c++ 中迭代 emtpy 向量时会抛出运行时错误
- 全局向量导致 C++ 程序结束时出现段错误
- 读/写 OpenMP 中的共享向量会减慢程序速度
- 是否有可能让 c++ dll 在后台运行 python 程序并让它填充向量图?如果是这样,如何?
- 加入向量的所有线程后,程序不稳定,而不是退出
- DLL – 在 DLL 初始化时填充的静态向量,向客户端程序返回零大小
- C++程序中的向量 std::bad_alloc 错误
- 当我尝试将一个向量元素的值分配给另一个向量元素时,为什么我的应用程序会崩溃
- 如何在以下程序中分配向量
- 如何制作一个为程序的每个运行中存储不同数据类型的向量
- C++联系人处理程序与自己的动态模板向量类
- C++ 尝试向类内的数组(或向量)添加值时程序崩溃
- 为什么当我将真或假分配给布尔类型的向量时,下面的程序给我错误?
- 试图删除最后一个std ::向量元素时,程序会崩溃
- 当程序输入主要功能时,为什么要清除此std ::向量
- 程序崩溃,说向量不超出范围异常
- 程序必须将奇数索引元素放在新的向量 C 中,将偶数索引元素放在另一个向量 T 中
- 使用向量的 C++ 程序中的分割错误
- 我的程序没有运行,多维向量函数