使用2d数组计算总数,高数和低数

时间:2017-03-31 19:28:58

标签: c++ arrays multidimensional-array ifstream

我正试图找出每个月和主题销售的书籍总数。这就是我到目前为止所拥有的。我不确定如何更改函数参数或调用函数。我知道在main中,调用函数的参数是不正确的。结果也应显示在表格的底部和右侧。

#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>

using namespace std;

typedef int Sales[108];

int sales[6][12];
static const int rows = 12;
static const int cols = 6;
int findhighestmath(int[][6], int);
int findhighestcs(int[][6], int);
int findhighestphy(int[][6], int);
int findlowestmath(int[][6], int);
int total(int[][6], int);
void descendmonth(int[][6], int);
void descendsubject(int[][6], int);
int searching(int[][6], int, int);
int totalmath(int[][6], int);


int main()
{   
ifstream inFile;
int sales[12][6];
int numberofbooks = 0, sum = 0, highestmath = 0, math = 0, highestcs= 0,cs = 0;
int totalbooks = 0, found, TotalMath = 0, TotalCS = 0, highestphy;
int overalltotal = 0, highest = 0, lowest = 0;
string months;
string subject;
string Month[12] = { "January", "February", "March", "April", "May", "June", "July",
    "August", "September", "October", "November", "December" };
string category[6] = { "Math", "CS", "Phy", "Chem", "Bio", "Geo" };


//Used for finding total books sold
inFile.open("sale.dat");
while (!inFile.eof())
{
    for (int i = 0; i < 12; i++)
    {
        for (int j = 0; j < 6; j++)
        {
            sales[i][j] = 0;
            inFile >> sales[i][j];
        }
        cout << endl;
    }
}

inFile.close();
//Totals up all books
for (int k = 0; k < 12; k++)
{
    for (int l = 0; l < 6; l++)
    {
        sum = sum + sales[k][l];
    }
}

numberofbooks = sum;


cout << setw(40) << "Book Sale Analysis" << endl;
cout << setw(40) << "The book store sold a total of " << numberofbooks << " books."
    << endl;
cout << endl;

cout << setw(7) << "Month" << setw(7) << "Math" << setw(7) << "CS" << setw(7) << "Phy" << setw(7) << "Chem"
    << setw(7) << "Bio" << setw(7) << "Geo" << setw(9) << "Total" << setw(7)
    << "High" << setw(7) << "Low" << endl;


//Used for displaying a table of books sold
inFile.open("sale.dat");
while (!inFile.eof())
{
    for (int i = 0; i < 12; i++)
    {
        for (int j = 0; j < 6; j++)
        {
            sales[i][j] = 0;
            inFile >> sales[i][j];
            cout << setw(7) << sales[i][j] << " ";
        }
        cout << endl;
    }
}

inFile.close();



for (int i = 0; i < 12; i++)
{
    for (int j = 0; j < 6; j++)
    {
        math = math + sales[i][j];
    }
}

//Total, high, and low outputs for subjects
cout << endl;
TotalMath = totalmath(sales, math);
cout << "Total" << setw(5) << TotalMath <<endl;

highestmath = findhighestmath(sales, math);
cout << "High" << setw(5) << highestmath;
highestcs = findhighestcs(sales, cs);
cout << setw(5) << highestcs << endl;
highestphy = findhighestphy(sales, numberofbooks);
cout << setw(5) << highestphy;

lowest = findlowestmath(sales, math);
cout << "Low" << setw(5) << lowest << endl;

//Sorted analysis
cout << endl;
cout << "Sorting month in ascending order of total sale" << endl;

cout << endl;
cout << "Sorting book category in ascending order of total sale" << endl;

//Search month and book sales using binary search
found = searching(sales, numberofbooks, totalbooks);
cout << months << endl;
cout << subject << endl;
cout << "The total sale is " << found << endl;

return 0;

}


//Function finds highest sale value for math
int findhighestmath(int arr[][6], int b)
{
int highestsale = arr[0][0];
for (int i = 0; i < b; i++)
{
    for (int j = 0; j<b; j++)
    {
        if (arr[i][j]>highestsale)
        {
            arr[i][j];
            highestsale = arr[i][j];
        }
    }
}
return highestsale;
}


int findhighestcs(int arr[][6], int b)
{
int highestsale = 0;
for (int i = 0; i < b; i++)
{
    for (int j = 0; j < b; j++)
    {
        if (arr[i][j]>highestsale)
        {
            arr[i][j];
            highestsale = arr[i][j];
        }
    }
}
    return highestsale;
}


int findhighestphy(int arr[][6], int b)
{
int highestsale = 0;
for (int i = 0; i < b; i++)
{
    for (int j = 0; j < b; j++)
    {
        if (arr[i][j]>highestsale)
        {
            arr[i][j];
            highestsale = arr[i][j];
        }
    }
}
return highestsale;
}

//Function finds lowest sale value
int findlowestmath(int arr[][6], int b)
{
int lowestsale = 0;
for (int i = 0; i < b; i++)
{
    for (int j = 0; j < b; j++)
    {
        if (arr[i][j] < lowestsale)
        {
            arr[i][j];
            lowestsale = arr[i][j];
        }
    }
}
return lowestsale;
}


int totalmath(int arr[][6], int total)
{
for (int row = 0; row < 12; row++)
{
    for (int col = 0; col < 1; col++)
    {
        arr[row][col];
        total = arr[row][col];
    }
}
return total;
}

1 个答案:

答案 0 :(得分:0)

如果要输出'空格',则必须输出空格。这是一个好的开始。

cout << ' ';

我看到你使用了setw操纵器。这与其他操纵器结合使用可以提供相当大的灵活性。看看这个网站。 http://en.cppreference.com/w/cpp/io/manip/left。 std :: right,std :: left做了人们所期望的。 std :: internal很有趣,但很有限。以上所有网站都是C ++基础知识的一个很好的参考