943,562 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 5269
  • C++ RSS
Nov 9th, 2005
0

converting C++ to C

Expand Post »
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)
  1. #include <fstream>
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <cstdlib>
  5.  
  6. using namespace std;
  7.  
  8. const int MAXLEN_FN = 30;
  9.  
  10. void OptionPrompt();
  11. void Process(int *scoresArrayPtr, int numOfScores,
  12. int& highestScore, int& lowestScore,
  13. double& averageScore);
  14. void CoutResults(int *scoresArrayPtr, int numOfScores,
  15. int highestScore, int lowestScore,
  16. double averageScore);
  17.  
  18. int main()
  19. {
  20. int *scoresArrayPtrHold = 0, *scoresArrayPtr = 0;
  21. int arraySize = 0, numOfScores = 0, score,
  22. lowestScore, highestScore;
  23. double averageScore;
  24. char inputFilename[MAXLEN_FN + 1],
  25. reply;
  26. ifstream fin;
  27. ofstream fout;
  28.  
  29. cout << "I can process almost any # of scores for you" << endl;
  30. OptionPrompt();
  31.  
  32. do
  33. {
  34. cin >> reply;
  35. cin.ignore(999, '\n');
  36. if (reply != '1' && reply != '2')
  37. {
  38. cout << "You have entered an invalid option" << endl;
  39. cout << "Please re-enter your desired option" << endl;
  40. OptionPrompt();
  41. }
  42. } while (reply != '1' && reply != '2');
  43.  
  44. if (reply == '1')
  45. {
  46. cout << "Enter the 1st score (any invalid score to quit): ";
  47. cin >> score;
  48. while (score >= 0 && score <= 100)
  49. {
  50. ++numOfScores;
  51. if (numOfScores > arraySize)
  52. {
  53. arraySize = int(1.5*arraySize + 1);
  54. scoresArrayPtrHold = scoresArrayPtr;
  55. scoresArrayPtr = new int [arraySize];
  56. for (int i = 0; i < numOfScores - 1; ++i)
  57. *(scoresArrayPtr + i) = *(scoresArrayPtrHold + i);
  58. delete [] scoresArrayPtrHold;
  59. scoresArrayPtrHold = 0;
  60. }
  61. *(scoresArrayPtr + numOfScores - 1) = score;
  62.  
  63. cout << "Enter the next score (any invalid score to end): ";
  64. cin >> score;
  65. }
  66.  
  67. if (numOfScores > 0)
  68. {
  69. Process(scoresArrayPtr, numOfScores, highestScore,
  70. lowestScore, averageScore);
  71. CoutResults(scoresArrayPtr, numOfScores, highestScore,
  72. lowestScore, averageScore);
  73. }
  74. else
  75. cout << "No scores to process..." << endl;
  76. }
  77. else
  78. {
  79. cout << "Enter name of input file (up to "
  80. << MAXLEN_FN << " characters): ";
  81.  
  82. int count = 0;
  83. char oneChar;
  84.  
  85. oneChar = cin.get();
  86. while (oneChar != '\n')
  87. {
  88. if (count < MAXLEN_FN)
  89. {
  90. inputFilename[count] = oneChar;
  91. ++count;
  92. }
  93. oneChar = cin.get();
  94. }
  95. inputFilename[count] = '\0';
  96.  
  97. fin.open(inputFilename, ios::in);
  98. if (fin.fail())
  99. {
  100. cout << "Error opening file " << inputFilename << endl;
  101. exit(EXIT_FAILURE);
  102. }
  103.  
  104. fout.open("ASSIGN08.OUT", ios::out);
  105. if (fout.fail())
  106. {
  107. cout << "Error opening file ASSIGN08.OUT" << endl;
  108. fin.close();
  109. exit(EXIT_FAILURE);
  110. }
  111.  
  112. fin >> score;
  113. while ( ! fin.eof() )
  114. {
  115. ++numOfScores;
  116. fin >> score;
  117. }
  118.  
  119. if (numOfScores > 0)
  120. {
  121. fin.clear();
  122. fin.seekg(0);
  123. scoresArrayPtr = new int [numOfScores];
  124. for (int i = 0; i < numOfScores; ++i)
  125. fin >> *(scoresArrayPtr + i);
  126. Process(scoresArrayPtr, numOfScores, highestScore,
  127. lowestScore, averageScore);
  128. CoutResults(scoresArrayPtr, numOfScores, highestScore,
  129. lowestScore, averageScore);
  130.  
  131. fout << "Scores read: ";
  132. for (int j = 0; j < numOfScores; ++j)
  133. fout << *(scoresArrayPtr + j) << ' ';
  134. fout << endl;
  135. fout << "# of valid scores: " << numOfScores << endl;
  136. fout << "Highest score: " << highestScore << endl;
  137. fout << "Lowest score : " << lowestScore << endl;
  138. fout << "Average score: "
  139. << setiosflags(ios::fixed | ios::showpoint)
  140. << setprecision(2) << averageScore << endl;
  141. }
  142. else
  143. {
  144. cout << "No scores to process..." << endl;
  145. fout << "No scores to process..." << endl;
  146. }
  147. fin.close();
  148. fout.close();
  149. }
  150.  
  151. delete [] scoresArrayPtr;
  152. scoresArrayPtr = 0;
  153.  
  154. return EXIT_SUCCESS;
  155. }
  156.  
  157. void OptionPrompt()
  158. {
  159. cout << "You have the following options:" << endl;
  160. cout << "1 Enter the scores interactively" << endl
  161. << " (results are output to screen only)" << endl;
  162. cout << "2 Specify a file containing the scores" << endl
  163. << " (results are output to screen and file)" << endl
  164. << " (name of output file will be ASSIGN08.OUT)"
  165. << endl;
  166. cout << "Your choice? ";
  167. }
  168.  
  169. void Process(int *scoresArrayPtr, int numOfScores,
  170. int& highestScore, int& lowestScore,
  171. double& averageScore)
  172. {
  173. int sum = lowestScore = highestScore = *scoresArrayPtr;
  174. for (int k = 1; k < numOfScores; ++k)
  175. {
  176. sum += *(scoresArrayPtr + k);
  177. if (*(scoresArrayPtr + k) < lowestScore)
  178. lowestScore = *(scoresArrayPtr + k);
  179. else if (*(scoresArrayPtr + k) > highestScore)
  180. highestScore = *(scoresArrayPtr + k);
  181. }
  182. averageScore = double(sum) / numOfScores;
  183. }
  184.  
  185. void CoutResults(int *scoresArrayPtr, int numOfScores,
  186. int highestScore, int lowestScore,
  187. double averageScore)
  188. {
  189. cout << "Scores read: ";
  190. for (int j = 0; j < numOfScores; ++j)
  191. cout << *(scoresArrayPtr + j) << ' ';
  192. cout << endl;
  193. cout << "# of valid scores: " << numOfScores << endl;
  194. cout << "Highest score: " << highestScore << endl;
  195. cout << "Lowest score : " << lowestScore << endl;
  196. cout << "Average score: "
  197. << setiosflags(ios::fixed | ios::showpoint)
  198. << setprecision(2) << averageScore << endl;
  199. }
Last edited by Dave Sinkula; Nov 9th, 2005 at 2:02 pm. Reason: Added [code][/code] tags.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bucsoldier03 is offline Offline
8 posts
since Oct 2005
Nov 9th, 2005
0

Re: converting C++ to C

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
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
Daishi is offline Offline
80 posts
since Aug 2005
Nov 9th, 2005
0

Re: converting C++ to C

delete all these lines
C++ Syntax (Toggle Plain Text)
  1. #include <fstream>
  2. #include <iostream>
  3. #include <iomanip>
  4. using namespace std;
and replace it with this
C++ Syntax (Toggle Plain Text)
  1. #include <stdio.h>

replace c++ references with pointer, for example
C++ Syntax (Toggle Plain Text)
  1. void Process(int *scoresArrayPtr, int numOfScores,
  2. int& highestScore, int* lowestScore,
  3. double* averageScore);
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Nov 10th, 2006
0

Re: converting C++ to C

C doesn't have definition for "new." The way to dynamicly create arrays in C is to use malloc.
for example instead of:
C++ Syntax (Toggle Plain Text)
  1. int* intArrayPtr;
  2. intArrayPtr = new int[someSize];
you would use

C++ Syntax (Toggle Plain Text)
  1. int* intArrayPtr;
  2. 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)
  1. sizeof(type)
returns the size of that type which you can then multiply by the dynamic number to get the exact size you need.
Last edited by pigeonfoot; Nov 10th, 2006 at 12:14 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pigeonfoot is offline Offline
2 posts
since Nov 2006
Nov 10th, 2006
0

Re: converting C++ to C

Click to Expand / Collapse  Quote originally posted by pigeonfoot ...
C++ Syntax (Toggle Plain Text)
  1. int* intArrayPtr;
  2. 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()
Moderator
Reputation Points: 3275
Solved Threads: 890
Posting Sage
WaltP is offline Offline
7,716 posts
since May 2006
Nov 10th, 2006
0

Re: converting C++ to C

Click to Expand / Collapse  Quote originally posted by WaltP ...
See this...

IOW, don't cast malloc()
Good eye I appreciate it. Honestly I am new to using malloc.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pigeonfoot is offline Offline
2 posts
since Nov 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Beginner C++ help
Next Thread in C++ Forum Timeline: web interface





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC