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

int ParseScores(string filename, int array[], int n) { int c=0; string words[3];

ID: 3598887 • Letter: I

Question

int ParseScores(string filename, int array[], int n)
{
int c=0;
string words[3];
ifstream file;
file.open(filename.c_str());
string line;
int j=0;
if(file.is_open())
{
while(!file.eof())
{
getline(file,line);
split(line, ',', words, 3);
for(int i=0; i<3;i++)
{
c=c+stoi(words[i]);
}
array[j]=c;
j++;
  
}
return j;   

}
else
{
return -1;
}
}

Create a function named ParseScores, which takes a string input filename as a parameter, an integer array, and size of the array. The function will read the student scores on each line of the file and store the sum of the values into another integer array. This function returns the number of entries read from the file. If the input file cannot be opened, return -1 and do not store anything in the array. Each line of the file contains , ,

Explanation / Answer

Your answer is right , if the provided function split() by the question ,

here is your code


int ParseScores(string filename, int array[], int n)   // specify the parameters
{
    int c=0;   // declared c for calculating the sum of each row
    string words[3];   // words is a type of string , array of three strings
    ifstream file;   // create an object of the input stream , ie ifstream for reading
    file.open(filename.c_str()); // open the file
    string line;   // line declared as string
    int j=0;   // j for get the number of rows
    if(file.is_open()) // check whether the file is openable or not
    {
            while(!file.eof()) // till the end of the file
            {
          getline(file,line);    // getline() method will take the entire line into line
          split(line, ',', words, 3); // split function that is a user defined function given by your teacher
          for(int i=0; i<3;i++) // create a for loop of 3 times , ie only 3 values per row
          {
           c=c+stoi(words[i]);   //stoi string to integer
          }
          array[j]=c;   // store the value c ie sum in the array
          j++;   // increment the value of j for next row
              
            }
return j;            // j has the number of rows , that is returned

    }
    else
    {
        return -1; // return if the file is not openable
    }
}

you can done it without the help of the split() function ie


#include <iostream>
#include <fstream>


using namespace std;

int ParseScores(string filename, int array[], int n)
{
    int c=0;
    string words[3];
    char a;
    ifstream file;
    file.open(filename.c_str());
    string line;
    int j=0,p;
    if(file.is_open())
    {
            while(!file.eof())
            {
               for(int i=0;i<3;i++)
               {
              
                   file>>p;   // get the integer
                   file>>a;   // just read the character ie ,
                   c+=p;    // update c as the sum ie c=c+p p hold the integer value
               }
              
               /*
          getline(file,line);
          split(line, ',', words, 3);
          for(int i=0; i<3;i++)
          {
           c=c+stoi(words[i]);
          }
          */
          array[j]=c;
          j++;
              
            }
return j;          

    }
    else
    {
        return -1;
    }
}

int main()
{
   string filename;
   int array[20],n;
  
   cout<<"Enter file name ";
   cin>> filename;
  
   n = ParseScores(filename,array,20);
   if(n>0)
   {
       for(int i=0;i<n;i++)
       {
           cout<<array[i]<<endl;
       }
   }
}