| | |
converting C++ to C
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2005
Posts: 8
Reputation:
Solved Threads: 0
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.
C++ Syntax (Toggle Plain Text)
#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; }
Last edited by Dave Sinkula; Nov 9th, 2005 at 2:02 pm. Reason: Added [code][/code] tags.
•
•
Join Date: Aug 2005
Posts: 80
Reputation:
Solved Threads: 2
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
-Fredric
delete all these lines
and replace it with this
replace c++ references with pointer, for example
C++ Syntax (Toggle Plain Text)
#include <fstream> #include <iostream> #include <iomanip> using namespace std;
C++ Syntax (Toggle Plain Text)
#include <stdio.h>
replace c++ references with pointer, for example
C++ Syntax (Toggle Plain Text)
void Process(int *scoresArrayPtr, int numOfScores, int& highestScore, int* lowestScore, double* averageScore);
•
•
Join Date: Nov 2006
Posts: 2
Reputation:
Solved Threads: 0
C doesn't have definition for "new." The way to dynamicly create arrays in C is to use malloc.
for example instead of:
you would use
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. returns the size of that type which you can then multiply by the dynamic number to get the exact size you need.
for example instead of:
C++ Syntax (Toggle Plain Text)
int* intArrayPtr; intArrayPtr = new int[someSize];
C++ Syntax (Toggle Plain Text)
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.
C++ Syntax (Toggle Plain Text)
sizeof(type)
Last edited by pigeonfoot; Nov 10th, 2006 at 12:14 am.
•
•
•
•
C++ Syntax (Toggle Plain Text)
int* intArrayPtr; intArrayPtr = (int*) malloc(someSize * sizeof(int));
malloc always returns a void* which you can typecast to be anything you want to...
IOW, don't cast malloc()
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
![]() |
Similar Threads
- converting vrml 1.0 to 2.0 (Computer Science)
- Converting lowercase to capitals (Java)
- Converting Excel to Mysql (MySQL)
- Converting Bytes to MB? (Visual Basic 4 / 5 / 6)
- converting base class to dervived class (C)
- Converting from VB code to C (C)
- Converting Access Forms to PHP Forms (PHP)
Other Threads in the C++ Forum
- Previous Thread: Beginner C++ help
- Next Thread: web interface
Views: 4148 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api array arrays beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui iamthwee ifstream input int java lib library lines linkedlist linker loop looping loops map math matrix memory microsoft newbie news number output pointer problem program programming project python random read recursion recursive reference return sort stream string strings struct studio system temperature template templates test text text-file tree unix url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






