【发布时间】:2020-01-13 20:50:09
【问题描述】:
我们正在课堂上编写一个程序来读取大约 100 行代码,将其存储并排序。该代码首先是员工的 ID 号,然后是他们的销售额。只有12名员工。我们必须将它存储在一个 12x4 二维数组中。我们必须有员工编号、他们的销售总数(计数器)、他们的总销售额以及他们所有销售/计数的平均值。
我的问题是弄清楚如何编写库中尚不存在的搜索功能。他提到从搜索功能返回下标以将该 ID 号与下标相关联。我们只能使用用户创建的函数和二维数组。还没学过指针和向量。
到目前为止,我已经完成了基本的 b/c,我不确定如何继续阅读 100 个值,但将它们压缩到 12 个人。有人可以帮助演示如何开始对这个二维数组进行排序吗?请尽可能详细,因为这只是我的第二个学期。如果我没有正确解释,我深表歉意。
// example of data in file. ID number on left and a sale made on right.
-322 10.80
-848 920.00
-828 1267.00
-848 8320.00
-229 66330.00
// the bubble sort we have to use
void sort(float sales[], int size)
{
int i, j;
for(i = 0; i < size - 1; i++) {
for(j = 0; j < size - i - 1; j++) {
//checking if previous value is
//grater than next one or not
if(sales[j] > sales[j+1]) {
float temp = sales[j];
sales[j] = sales[j+1];
sales[j+1] = temp;
}
}
}
}
// completely stuck here
int search_ID(float sales[][4])
{
int r;
for(c = 0; c <= 0; c++) {
for(r = 0; r < 13; r++) {
if(sales[r][c] != sales[r][c]) {
//was thinking of if the ID number
//matches or doesn't match perform a t/F
}
}
}
return sub;
}
// the avg sales for each employee
float avgSale(float sales[][4], int rowNum)
{
int r, c, totalSales;
float avg;
// would just use a for loop in main to call each row num.
for(r = rowNum; r <= rowNum; r++) {
for (c = 3; c < 4; c++) {
avg=sales[r][c]/totalSales;
}
}
return avg;
}
// we have to print a report with ID, Num sales, total sales, and avg for
// each so the 4 columns
void printReport(float sales[][4])
{
int r, c;
cout << fixed << setprecision(2);
for(r = 0; r < 13; r++) {
for(c = 0; c <= 1; c++) {
cout << sales[r][c] << " ";
}
cout << endl;
}
}
int main()
{
infile.open("C://data//input//Sales.txt");
outfile.open("C:\\data\\SalesmenReport.txt");
//check if file opens
if(!infile) {
cout << "File did not open. Please try again."<<endl;
return 0;
}
int size=12;
int sub;
float avg;
float sales[size][4];
int r, c, sub;
for(r = 0; r < 13; r++) {
for(c=0;c<=1;c++) {
infile >> sales[r][c];
// he showed us this as an example to call the search, idk where
// to put it or what to put in it.
sub = search_ID(sales);
}
}
printReport(sales);
avg = avgSale(sales, 0);
编辑并运行
我希望程序完成后能够在表格中从左到右打印 12 个员工 ID 号、他们的销售额、每个销售员的总销售额以及每个人的平均数。
【问题讨论】:
标签: c++ arrays function sorting 2d