i had to make a program that avereage the scores of test grades and displayed the highest grade made and also the lowest grade made and the test average. it also showed the number of vaild scores to. after making the program i have to convert it to C language. all im really asking whats the best way about doing that cause i have no programing knowledge of doing c language here my program. I just want to know the best steps to this process so i can do it on my own.

#include <fstream>
#include <iostream>
#include <iomanip>
#include <cstdlib>

using namespace std;

const int MAXLEN_FN = 30;

void OptionPrompt();
void Process(int *scoresArrayPtr, int numOfScores,
             int& highestScore, int& lowestScore,
             double& averageScore);
void CoutResults(int *scoresArrayPtr, int numOfScores,
                 int highestScore, int lowestScore,
                 double averageScore);

int main()
{
   int *scoresArrayPtrHold = 0, *scoresArrayPtr = 0;
   int arraySize = 0, numOfScores = 0, score,
       lowestScore, highestScore;
   double averageScore;
   char inputFilename[MAXLEN_FN + 1],
        reply;
   ifstream fin;
   ofstream fout;

   cout << "I can process almost any # of scores for you" << endl;
   OptionPrompt();

   do
   {
      cin >> reply;
      cin.ignore(999, '\n');
      if (reply != '1' && reply != '2')
      {
         cout << "You have entered an invalid option" << endl;
         cout << "Please re-enter your desired option" << endl;
         OptionPrompt();
      }
   } while (reply != '1' && reply != '2');

   if (reply == '1')
   {
      cout << "Enter the 1st score (any invalid score to quit): ";
      cin >> score;
      while (score >= 0 && score <= 100)
      {
         ++numOfScores;
         if (numOfScores > arraySize)
         {
            arraySize = int(1.5*arraySize + 1);
            scoresArrayPtrHold = scoresArrayPtr;
            scoresArrayPtr = new int [arraySize];
            for (int i = 0; i < numOfScores - 1; ++i)
               *(scoresArrayPtr + i) = *(scoresArrayPtrHold + i);
            delete [] scoresArrayPtrHold;
            scoresArrayPtrHold = 0;
         }
         *(scoresArrayPtr + numOfScores - 1) = score;

         cout << "Enter the next score (any invalid score to end): ";
         cin >> score;
      }

      if (numOfScores > 0)
      {
         Process(scoresArrayPtr, numOfScores, highestScore,
                 lowestScore, averageScore);
         CoutResults(scoresArrayPtr, numOfScores, highestScore,
                     lowestScore, averageScore);
      }
      else
         cout << "No scores to process..." << endl;
   }
   else
   {
      cout << "Enter name of input file (up to "
           << MAXLEN_FN << " characters): ";

      int count = 0;
      char oneChar;

      oneChar = cin.get();
      while (oneChar != '\n')
      {
         if (count < MAXLEN_FN)
         {
            inputFilename[count] = oneChar;
            ++count;
         }
         oneChar = cin.get();
      }
      inputFilename[count] = '\0';

      fin.open(inputFilename, ios::in);
      if (fin.fail())
      {
         cout << "Error opening file " << inputFilename << endl;
         exit(EXIT_FAILURE);
      }

      fout.open("ASSIGN08.OUT", ios::out);
      if (fout.fail())
      {
         cout << "Error opening file ASSIGN08.OUT" << endl;
         fin.close();
         exit(EXIT_FAILURE);
      }

      fin >> score;
      while ( ! fin.eof() )
      {
         ++numOfScores;
         fin >> score;
      }

      if (numOfScores > 0)
      {
         fin.clear();
         fin.seekg(0);
         scoresArrayPtr = new int [numOfScores];
         for (int i = 0; i < numOfScores; ++i)
            fin >> *(scoresArrayPtr + i);
         Process(scoresArrayPtr, numOfScores, highestScore,
                 lowestScore, averageScore);
         CoutResults(scoresArrayPtr, numOfScores, highestScore,
                     lowestScore, averageScore);

         fout << "Scores read: ";
         for (int j = 0; j < numOfScores; ++j)
            fout << *(scoresArrayPtr + j) << ' ';
         fout << endl;
         fout << "# of valid scores: " << numOfScores << endl;
         fout << "Highest score: " << highestScore << endl;
         fout << "Lowest score : " << lowestScore << endl;
         fout << "Average score: "
              << setiosflags(ios::fixed | ios::showpoint)
              << setprecision(2) << averageScore << endl;
      }
      else
      {
         cout << "No scores to process..." << endl;
         fout << "No scores to process..." << endl;
      }
      fin.close();
      fout.close();
        }

   delete [] scoresArrayPtr;
   scoresArrayPtr = 0;

   return EXIT_SUCCESS;
}

void OptionPrompt()
{
   cout << "You have the following options:" << endl;
   cout << "1  Enter the scores interactively" << endl
        << "   (results are output to screen only)" << endl;
   cout << "2  Specify a file containing the scores" << endl
        << "   (results are output to screen and file)" << endl
        << "   (name of output file will be ASSIGN08.OUT)"
        << endl;
   cout << "Your choice? ";
}

void Process(int *scoresArrayPtr, int numOfScores,
             int& highestScore, int& lowestScore,
             double& averageScore)
{
   int sum = lowestScore = highestScore = *scoresArrayPtr;
   for (int k = 1; k < numOfScores; ++k)
   {
      sum += *(scoresArrayPtr + k);
      if (*(scoresArrayPtr + k) < lowestScore)
         lowestScore = *(scoresArrayPtr + k);
      else if (*(scoresArrayPtr + k) > highestScore)
         highestScore = *(scoresArrayPtr + k);
   }
   averageScore = double(sum) / numOfScores;
}

void CoutResults(int *scoresArrayPtr, int numOfScores,
                 int highestScore, int lowestScore,
                 double averageScore)
{
   cout << "Scores read: ";
   for (int j = 0; j < numOfScores; ++j)
      cout << *(scoresArrayPtr + j) << ' ';
   cout << endl;
   cout << "# of valid scores: " << numOfScores << endl;
   cout << "Highest score: " << highestScore << endl;
   cout << "Lowest score : " << lowestScore << endl;
   cout << "Average score: "
        << setiosflags(ios::fixed | ios::showpoint)
        << setprecision(2) << averageScore << endl;
}

Well, one thing that you could do is to take away the couts and use printfs... Use fopen, fprintf, and fscanf instead of the std fstream stuff. Make sure all of your variables for functions are defined at the start of the function (if you have a horrible C teacher then those for loop's i's and j's need to be defined at the start of the function). Other than that, I don't see anything that is too non-C'ish. I'm sure other people can think of more things though.

-Fredric

delete all these lines

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

and replace it with this

#include <stdio.h>

replace c++ references with pointer, for example

void Process(int *scoresArrayPtr, int numOfScores,
             int& highestScore, int* lowestScore,
             double* averageScore);

C doesn't have definition for "new." The way to dynamicly create arrays in C is to use malloc.
for example instead of:

int* intArrayPtr;
intArrayPtr = new int[someSize];

you would use

int* intArrayPtr;
intArrayPtr = (int*) malloc(someSize * sizeof(int));

malloc always returns a void* which you can typecast to be anything you want to. However, because it can point to anything you need to define the literal size.

sizeof(type)

returns the size of that type which you can then multiply by the dynamic number to get the exact size you need.

int* intArrayPtr;
intArrayPtr = (int*) malloc(someSize * sizeof(int));

malloc always returns a void* which you can typecast to be anything you want to...

See this...

IOW, don't cast malloc()

This article has been dead for over six months. Start a new discussion instead.