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

All these questions are under c++ condition, each follows a brief main function.

ID: 3598071 • Letter: A

Question

All these questions are under c++ condition, each follows a brief main function. thx.

Part 1 - Read a file line by line 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 array 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 [ [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 first 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 80 93.5 76 75.4 90.2 94 80.2 98.2 87.5 82 92 88.2 90.1

Explanation / Answer

All 4 parts have been answered according to the given requirements. For questions requiring splitting of the input line, stringstream has been used. Let me know if you would want it done differently and I could make the necessary changes. Please keep all the input files in the same folder as the executable or change the fileName variable accordingly. Please upvote my answer if you find this helpful. Have a good day!

-------------------------------PART 1------------------------------

#include <iostream>
#include <fstream>
using namespace std;

float avgCharsPerLine(string filename)
{
ifstream f;
f.open(filename.c_str());// opening file for reading   
string str;
float sum = 0, avg;
int count = 0;
// reading file line by line
while(getline(f,str))
{
sum+=str.length();
count++;
}
if(count == 0) // if file is empty
avg = 0;
else
avg = sum/count;
f.close();
return avg;
}

int main()
{
string fileName = "inp1.txt";
float avgChars = avgCharsPerLine(fileName);
cout<<"Avg chars per line in "<<fileName<<" is "<<avgChars<<endl;
return 0;
}

----------------- inp1.txt -----------------------

Hello this is the solution
for the first part of
the question you posted.
I hope you have a good day.

--------------------- PART 2 ----------------------------

#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

int fillArray(string filename,float array[][5])
{
ifstream f;
f.open(filename.c_str());// opening file for reading   
string str;
int count = 0;
// ignoring first line
getline(f,str);

// reading rest of the file line by line
while(getline(f,str))
{
// you could modify the split function and use it here
std::stringstream ss(str);
  
float num;
for(int i=0;i<5,ss>>num;i++)
{
array[count][i] = num;
if (ss.peek() == ',')
ss.ignore();
}
count++;
}
return count;
}

int main()
{
string fileName = "inp2.txt";
float array[100][5];
int numLines = fillArray(fileName,array);
cout<<"No of lines in "<<fileName<<" is "<<numLines<<endl;
cout<<"The array is: "<<endl;
for(int i=0;i<numLines;i++)
{
for(int j=0;j<5;j++)
cout<<array[i][j]<<' ';
cout<<endl;
}
return 0;
}

----------------- inp2.txt ---------------------

A1, 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

---------------- PART 3 ----------------------

#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

int fillArray(string filename,float array[][5])
{
ifstream f;
f.open(filename.c_str());// opening file for reading   
string str;
int count = 0;
// ignoring first line
getline(f,str);

// reading rest of the file line by line
while(getline(f,str))
{
// you could modify the split function and use it here
std::stringstream ss(str);
  
float num;
for(int i=0;i<5,ss>>num;i++)
{
array[count][i] = num;
if (ss.peek() == ',')
ss.ignore();
}
count++;
}
return count;
}

float arrayStats(string filename, float numbers[][5])
{
int rows = fillArray(filename, numbers);
float sum = 0, avg;
// adding mean of odd rows
for(int i=1;i<rows;i=i+2)
{
avg = 0;
for(int j=0;j<5;j++)
avg+=numbers[i][j];
sum += avg/5;
}
// adding mean of odd cols
for(int j=1;j<5;j=j+2)
{
avg = 0;
for(int i=0;i<rows;i++)
avg+=numbers[i][j];
sum += avg/rows;
}
return sum;
}

int main()
{
string fileName = "inp3.txt";
float array[100][5];
float stats = arrayStats(fileName,array);
cout<<"Array stats for "<<fileName<<" is "<<stats<<endl;
return 0;
}

---------------- inp3.txt ---------------------

v1, v2, v3, v4, v5
4, 1, 2, 3, 5
2, 3, 4, 1, 0
6, 4, 0, 2, -1
5, -3, 2, 1, 0
6, 2, -7, 3, 0

---------------------- PART 4 -------------------------

#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

int findUser(string users[], string name, int count)
{
for(int i=0; i<count; i++)
if(!name.compare(users[i]))
return i;
return -1;
}

void addBookRatings(string filename, string users[], int ratings[][50])
{
ifstream f;
f.open(filename.c_str());// opening file for reading   
string str;
int count = 0;
// ignoring first line
getline(f,str);
string name;
int book_id, rating;
// reading rest of the file line by line
while(getline(f,str))
{   
std::stringstream ss(str);
ss>>name;
name = name.substr(0,name.length()-1);
ss>>book_id;
ss.ignore();
ss>>rating;
int pos = findUser(users,name,count);
if(pos == -1)
{
for(int i=0;i<50;i++)
{
ratings[count][i] = 0;
}
pos = count;
count++;
}
users[pos] = name;
ratings[pos][book_id] = rating;
}
// Comment out to verify that it works
/*
for(int i=0;i<count;i++)
{
cout<<users[i]<<" ";
for(int j=0;j<50;j++)
cout<<ratings[i][j];
cout<<endl;
}*/
}

int main()
{
string fileName = "inp4.txt";
int ratings[100][50];
string users[100];
addBookRatings(fileName,users,ratings);

return 0;
}

------------------- inp4.txt --------------------------

Name, book_id, rating
Camilla, 2, 5
Arcadia, 3, 4
Vipra, 4, 5
Carter, 28, 2
Carter, 29, 4
Camilla, 24, 3