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

9. Translate the following code from C++ to C# // Week 5 Programing Assignment s

ID: 665288 • Letter: 9

Question

9. Translate the following code from C++ to C#

// Week 5 Programing Assignment solution - I hope.
// If this program works as it is supposed to then I wrote
// it, if not then I don't know who wrote it.

#include
#include
#include
#include
#include
using namespace std;
////////////////////////////////////////////////////////////
const int MAX_ANSWERS = 20;
const float MAX_POINTS = 40.0;
////////////////////////////////////////////////////////////
//
////////////////////////////////////////////////////////////
int main()
{
ifstream inFile;
ofstream outFile;
string fileName;
char testAnswers[MAX_ANSWERS];
char studentAnswers[MAX_ANSWERS + 1]; // Notice the increase in size by one. For NULL terminator.


cout << "Please enter the name of the file: ";
cin >> fileName;
//fileName = "grades.txt"; // For debugging purposes.

// Open File
inFile.open(fileName);
if (!inFile.good())
{
  cout << "Sorry, the file " << fileName << " has not been found. " << endl;
  _getch();
  return 1;
}

// Prepare the output to go to a file.
outFile.open("FinalGrades.txt");
outFile << " Student ID Answers TestScore TestGrade" << endl;
outFile << "************************************************************************************" << endl;


// Fill in the test answers.
for (int i = 0; i < MAX_ANSWERS; i++)
  inFile.get(testAnswers[i]);

// There is a couple of new line characters we need to skip over
char cNewLine;
inFile.get(cNewLine);
inFile.get(cNewLine);

// Loop through each line (student record) in the file.
while (!inFile.eof())
{
  char studentAnswers[MAX_ANSWERS + 1]; // Notice the increase in size by one. For NULL terminator.
  memset(studentAnswers, 0, MAX_ANSWERS + 1); // For debugging purposes.

  // Fill in the test answers.
  for (int i = 0; i < MAX_ANSWERS; i++)
   inFile.get(studentAnswers[i]);
  
  // Must NULL terminate the array so it can be printed like a C-style string below.
  studentAnswers[MAX_ANSWERS] = NULL;

  // At this point "studentGrade" contains one student's grades.

  // Skip the dot before the student ID.
  char cDot;
  inFile.get(cDot);

  // Next get the rest of the line as a string which is the student ID.
  string studentID;
  inFile >> studentID;

  // Next compare this student's grades to the answer key and calculate total points..
  float fGradeTotal = 0;
  for (int i = 0; i < MAX_ANSWERS; i++)
  {
   // Correct answer...
   if (studentAnswers[i] == testAnswers[i])
    fGradeTotal += 2.0;
   else if (studentAnswers[i] == ' ')
     continue; // skip it if blank.
   else
    fGradeTotal -= 1.0; // Wrong answer.
  }

  float fScore = fGradeTotal / MAX_POINTS * 10;

  // Now that we have the total points let's figure out the grade...
  char cLetterGrade = 'E';
  switch (static_cast(fScore))
  {
   case 10:
   case 9:
   cLetterGrade = 'A';
   break;
   
   case 8:
   cLetterGrade = 'B';
   break;

   case 7:
   cLetterGrade = 'C';
   break;

   case 6:
   cLetterGrade = 'D';
   break;
  }

  // Debugging purposes...
  cout << studentID << " " << studentAnswers << " " << setprecision(2) << fixed << fScore * 10.0 << " " << cLetterGrade << endl;

  // Write results to file.
  outFile << studentID << " " << studentAnswers << " " << setprecision(2) << fixed << fScore * 10.0 << " " << cLetterGrade << endl;

  // There is a new line we need to get and throw away.
  inFile.get(cNewLine);

    // Now loop back to the top and process next student...
}

inFile.close();
outFile.close();

cout << "The students have been graded" << endl;
_getch();

return 0;
}

Explanation / Answer

//Converted program from CPP to C#

using System;

namespace ConsoleExample1
{
   public class CPPtoCSharp
   {
       public CPPtoCSharp ()
       {
       }
       const int MAX_ANSWERS=20; //declaring constants
       const float MAX_POINTS=40.0;
       static void Main(string[] args)
       {
           string fileName; //filename as string
           char[] testAnswers=new char[MAX_ANSWERS];
           char[] studentAnswers=new char[MAX_ANSWERS+1];
           StreamReader inFile;
           TextWriter outFile;
           Console.WriteLine("Please enter the name of the file : ");
           fileName=Console.ReadLine();
           try
           {
       //Opening and reading from text file
           inFile=new StreamReader(fileName);
           outFile=new StreamWriter("FinalGrades.txt");
               for(int i=0;i<MAX_ANSWERS;i++) //repeat maximum no.of answers
                   testAnswers[i]=(char)inFile.read(); //reading each character from file
               //couple of new line characters
               char cNewLine;
               cNewLine=(char)inFile.read();
               cNewLine=(char)inFile.read();
               // Loop through each line (student record) in the file.
               while(!inFile.EndOfStream)
               {
                   //char[] studentAnswers=new char[MAX_ANSWERS+1]; //THIS WRONG IN YOUR C++ CODE BECAUSE OF REDECLARATION
                   for(int i=0;i<MAX_ANSWERS;i++)
                       studentAnswers[i]=(char)inFile.read();
                   studentAnswers[MAX_ANSWERS]=''; //copying null character
               // At this point "studentGrade" contains one student's grades.
               // Skip the dot before the student ID.  
               char cDot;
                   cDot=(char)inFile.read();
                   // Next get the rest of the line as a string which is the student ID.
                   string studentID;
                   studentID=inFile.readLine();
                   // Next compare this student's grades to the answer key and calculate total points..
                    float fGradeTotal = 0;
                   for(int i=0;i<MAX_ANSWERS;i++)
                   {
                       if(studentAnswers[i]==testAnswers[i])
                           fGradeTotal+=2.0;
                       else if(studentAnswers[i]==' ')
                           continue;
                       else
                           fGradeTotal-=1.0;
                   }
                   float fScore=fGradeTotal/MAX_POINTS*10;
                   // Now that we have the total points let's figure out the grade...
               char cLetterGrade = 'E';
                      switch (static_cast(fScore))
                      {
                       case 10:
                       case 9:
                       cLetterGrade = 'A';
                       break;
                     
                       case 8:
                       cLetterGrade = 'B';
                       break;
                  
                       case 7:
                       cLetterGrade = 'C';
                       break;
                  
                       case 6:
                       cLetterGrade = 'D';
                       break;
                      }
                   //Debugging process
           Console.WriteLine(studentID+" "+studentAnswers+" "+fScore*10+" "+cLetterGrade);
           //write results to file
                   outFile.writeLine(studentID+" "+studentAnswers+" "+fScore*10+" "+cLetterGrade+" ");
                      
               }  
               inFile.close();
               outFile.close();
               Console.write( "The students have been graded" );
              
           }
           catch(Exception e) //handling file opening error
           {Console.WriteLine("File opening failed :"+e);}
          
       }
   }
}