【问题标题】:How to search and sort a 2D array如何搜索和排序二维数组
【发布时间】: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


    【解决方案1】:

    您需要将 empId(待搜索)作为参数传递给函数。例如,如果找到具有给定员工 id 的记录,则以下函数将返回 1,否则返回 0。

    int search_ID(float sales[][4], int empIdToBeSearched)  
    {
        int r, found;
        found = 0;
            for(r = 0; r < 13; r++) {
                if(sales[r][0] == empIdToBeSearched) {
                  found = 1;
                  break;
            }
        }
        return found;
    }
    

    在对记录进行排序时,您应该将排序算法应用于您想要的任何列但在交换数据时,您应该交换整行,而不仅仅是特定单元格。因此,在这种情况下,您的 temp(用于排序函数)应该是二维数组,例如 float temp[1][4]

    【讨论】:

    • 在做search_ID函数时,函数调用需要写什么?返回1的目的是什么?每次读入一个数字时,我都必须能够调用此函数,以确保该数字是/不是重复的,如果是,则将第 2 列中的总和添加到先前写入第 2 列的总和中。如果是不是重复的,我需要跳过一行并添加新数字。在值 empIdToBeSearched 的函数调用中,是否需要为 r 表示行,因为我不知道下一个值是否重复?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-01
    • 2021-12-18
    • 1970-01-01
    • 1970-01-01
    • 2017-09-06
    相关资源
    最近更新 更多