简体   繁体   中英

How do I sort,search, and get get the sum and average in an array

For my code I need to search for the largest number in the array, add up and get the averages of the numbers inside of it, and search for the largest number in it. my code has the basic idea of what i want to do for each.

#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;
        }
    }
}

If your array name is arr and size is n .

Search the largest number as follows:

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

Finally, max is the largest number.

To add up the array:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM