Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

- Re ie ine ne In this part, you will read in a file line by line float avgChars

ID: 3598372 • Letter: #

Question

- Re ie ine ne In this part, you will read in a file line by line float avgCharsPerLine (string filename) The avgCharsPerLine function will take one argument, a string for the name of the file to be read in. Read the file line by line and return a floating point value for the average number of characters on each line. Include every single character (including white spaces) in the character count. Part 2 - Read floats into 2 dimensional arrav In this part, you will read a file with floating point values separated by commas and store them in an array int fillArray (string filename, float array [1 [5]) The fillArray function takes in two arguments: the filename and a float two dimensional array that can hold 5 floats in each row. Your function should fill the array with the values from the file, and return the total number of lines of the file, excluding the header line The input file will look like this. The fîrst line is a header line that describes the columns of data in the file. This line must be ignored when reading the data Assignmentl, A2, A3, A4, A5 90.2, 80, 0, 75.4, 98.2 94, 93.5, 92, 88, 87.5 80.2, 76, 88.2, 90.1, 82 For the above input, you should get an array like this 75.4 90.2 94 80.2 80 93.5 76 0 92 88.2 98.2 87.5 82 90.1

Explanation / Answer

Please find my implementation for Part 1 and Part 2.

Please repost others in separate post.

#include <iostream>

#include <fstream>

using namespace std;

float avgCharsPerLine(string filename) {

ifstream file(filename);

string str;

int count = 0;

float total = 0;

while (getline(file, str))

{

total = total + str.size();

count++;

}

file.close();

if(count == 0)

return 0;

return total/count;

}

void fillArray(string filename, float array[][5]) {

ifstream file(filename);

string str;

int i = 0, j= 0;

float num;

while (cin>>num)

{

// if current row has filled up

if(j == 5){

i++;

j = 0;

}

array[i][j] = num; // adding elements in array

}

file.close();

}

int main()

{

cout<<"average characters per line: "<<avgCharsPerLine("avgCharacters.cpp")<<endl;

return 0;

}