【问题标题】:How do I sort,search, and get get the sum and average in an array如何排序、搜索和获取数组中的总和和平均值
【发布时间】:2015-05-18 04:31:38
【问题描述】:

对于我的代码,我需要在数组中搜索最大的数字,将其中的数字相加并求平均值,然后在其中搜索最大的数字。我的代码有我想为每个人做什么的基本想法。

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

using namespace std;

void menu();
string createFile();
void displayNumTotalAverage(string);
void displaySortedNums();
void BubbleSort();
void SearchNum();
void displayLargestNum();
void appendRandomNum(string);
void exit();
void CreateFile();
void printFunc(int[]);
void fillFunc(int[]);

int main()
{
    menu();
    string FileName;
    createFile();
    CreateFile();

    system("pause");
    return 0;
}
void menu()
{
    int choice;
    string FileName;
    do
    {

        //program output 
        cout << "** MENU **" << endl << endl;

        cout << "Curret Data File: " << endl << endl;

        cout << "(1) Select / create data file (.txt file extention will be added automaticly)" << endl;
        cout << "(2) Display all numbers, total and average" << endl;
        cout << "(3) Display all numbers sorted" << endl;
        cout << "(4) search for a number and display how many times it occurs" << endl;
        cout << "(5) display the largest number" << endl;
        cout << "(6) Append a random number(s)" << endl;
        cout << "(7) Exit the program" << endl << endl;

        //user input
        cout << "Menu Choice: ";
        cin >> choice;

        while (choice > 7 || choice < 1)
        {
            cout << "Menu Choice: ";
            cin >> choice;
        }

        switch (choice)
        {
        case 1:
            cout << "\nChoice 1" << endl << endl;
            createFile();
            break;

        case 2:
            cout << "\nChoice 2" << endl << endl;
            displayNumTotalAverage(FileName.c_str());
            break;

        case 3:
            cout << "\nChoice 3" << endl << endl;
            break;

        case 4:
            cout << "\nChoice 4" << endl << endl;
            break;

        case 5:
            cout << "\nChoice 5" << endl << endl;
            break;

        case 6:
            cout << "\nChoice 6" << endl << endl;
            appendRandomNum(FileName.c_str());
            break;

        case 7:
            exit();
            break;


        }


    } while (choice != 7);


}

string createFile()
{
    string FileName;
    ifstream inFile;
    cout << "Name of data file: ";
    cin >> FileName;
    FileName = "C:\\Users\Wizard\Libraries\Documents\Final Project.txt" + FileName;
    inFile.open(FileName + ".txt");
    if (inFile)
    {
        cout << FileName;
    }
    else
        cout << "File not found, creating file."<<endl;

    system("PAUSE");
    return FileName;
}

void displayNumTotalAverage(string FileName)
{
    ifstream inFile;
    cout << "Display Number Total Average - Option 2" << endl << endl << endl;
    inFile.open("C:\\Users\Wizard\Libraries\Documents\Final Project" + FileName + ".txt");
    int num;
    int total=0;
    cout << "Display Number Total Average function" << FileName << endl;
    double average;
    bool containsNum = false;
    inFile.open(FileName + ".txt");
    if (inFile)
    {
        while (inFile >> num)
        {
            cout << num << endl;
        }
        inFile.close();
    }
    else
    {
        cout << "Error opening file" << FileName << "." << endl;
    }

    system("PAUSE");
    return;
}

void displaySortedNums(int arr[], int size)
{
    bool swap;
    int temp;

    do
    {
        swap = false;
        for (int count = 0; count < (size - 1); count++)
        {
            if (arr[count]>arr[count + 1])
            {
                temp = arr[count];
                arr[count] = arr[count + 1];
                swap = true;
            }
        }
    } while (swap);




    system("PAUSE");
    return;
}

void searchNum()
{
    cout << " I am the searchNum function - option 4" << endl;

    system("PAUSE");
    return;
}

void displayLargestNum(int arr[], int numElems, int value)
{

    {
        int index = 0;
        int position = -1;
        bool found = false;

        while (index < numElems && !found)
        {
            if (arr[index] == value)
                found = true;
            position = index;
        }
        index++;

        return;
    }


    system("PAUSE");
    return;

}

void appendRandomNum(string FileName)
{
    int num = 0;
    int count = 0;
    ofstream outFile;
    outFile.open(FileName + ".txt", ios::app);
    cout << "How many random numbers: ";
    cin >> count;
    for (int i = 0; i < count; i++)
        outFile << rand() % 10 << endl;
    outFile.close();
    cout << endl << "Number(s) Added" << endl << endl;

    system("PAUSE");
    return;
}

void exit()
{
    std::exit(0);

    system("PAUSE");
    return;
}

void CreateFile()
{
    int random[50]; //Random Numbers

        srand((unsigned)time(NULL));
        fillFunc(random);
        printFunc(random);

        return;

}

void fillFunc(int arr[])
{
        for (int i = 0; i < 50; i++)
        {
                arr[i] = 1 + rand() % 10;

        }

}

void printFunc(int arr[])
{
    ofstream fout("C:\\Users\Wizard\Libraries\Documents\Final Project");
    if (fout.is_open()){
        for (int i = 0; i < 50; i++)
        {
            fout << arr[i] << std::endl;
        }
    }
}

【问题讨论】:

  • 你的问题是什么?
  • 首先我做得对吗。
  • 您可能(或可能不)有兴趣知道标准库有accumulate,它可以将数组中的值相加,max_element,它可以找到数组中的最大值数组,sort 可以对数组进行排序,findlower_boundupper_bound 可以在数组中进行搜索。这些也可以与更明智的集合一起使用,例如 std::vector,而不是实际的数组。
  • 这个问题似乎更适合代码审查网站:codereview.stackexchange.com

标签: c++ arrays sorting search average


【解决方案1】:

如果您的数组名称是 arr 并且大小是 n

搜索最大数如下:

int max = arr[0];
for ( int i = 1; i < n; i++ )
    max = (arr[i] > max ) ? arr[i] : max;

最后,max 是最大的数字。

将数组相加:

int sum = 0;
for ( int i = 0; i < n; i++ )
    sum += arr[i];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-25
    • 2015-04-22
    • 2013-10-31
    • 1970-01-01
    • 1970-01-01
    • 2012-05-08
    相关资源
    最近更新 更多