/* 6. Write a program to read a file of ints. Save every other int into an array, starting with the first one. Assume that there are no more than 10000 ints in the file (but you don't know how many). Find the average of the ints that you scanned and return that number. */ #include #include #include using namespace std; float averageOfEveryOfInt(const string& filename, int []); int main() { int array[5000]; cout << averageOfEveryOfInt("int10000.txt",array) << endl; } float averageOfEveryOfInt(const string& filename, int a[]) { ifstream fin(filename); if (!fin) { cerr << "Cannot open " << filename << endl; exit(1); } int sum = 0; int loopcount = 0; int index = 0; int num; while (fin >> num) { loopcount++; // process only odd numbered values if (loopcount % 2 == 1) { a[index] = num; // store the value into the array index++; // increment array index sum += num; // cumulate the value } } // return the average return static_cast(sum)/index; }