对于我的代码,我需要搜索数组中最大的数字,加起来并获得其中数字的平均值,并搜索其中的最大数字。我的代码基本了解了我想为每个人做的事情。
#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;
}
}
}
答案 0 :(得分:0)
如果您的数组名称为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];
是最大的数字。
要添加数组:
from pytz import timezone
from datetime import datetime
tz_france = timezone('Europe/Paris')
loc_dt = tz_france.localize(datetime.strptime('Apr 27 2015 9:00AM', '%b %d %Y %I:%M%p'))