converting C++ to C

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2005
Posts: 8
Reputation: bucsoldier03 is an unknown quantity at this point 
Solved Threads: 0
bucsoldier03 bucsoldier03 is offline Offline
Newbie Poster

converting C++ to C

 
0
  #1
Nov 9th, 2005
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.

  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 80
Reputation: Daishi is an unknown quantity at this point 
Solved Threads: 2
Daishi Daishi is offline Offline
Junior Poster in Training

Re: converting C++ to C

 
0
  #2
Nov 9th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,401
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: converting C++ to C

 
0
  #3
Nov 9th, 2005
delete all these lines
  1. #include <fstream>
  2. #include <iostream>
  3. #include <iomanip>
  4. using namespace std;
and replace it with this
  1. #include <stdio.h>

replace c++ references with pointer, for example
  1. void Process(int *scoresArrayPtr, int numOfScores,
  2. int& highestScore, int* lowestScore,
  3. double* averageScore);
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 2
Reputation: pigeonfoot is an unknown quantity at this point 
Solved Threads: 0
pigeonfoot pigeonfoot is offline Offline
Newbie Poster

Re: converting C++ to C

 
0
  #4
Nov 10th, 2006
C doesn't have definition for "new." The way to dynamicly create arrays in C is to use malloc.
for example instead of:
  1. int* intArrayPtr;
  2. intArrayPtr = new int[someSize];
you would use

  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.
  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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: converting C++ to C

 
0
  #5
Nov 10th, 2006
Originally Posted by pigeonfoot View Post
  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()
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 2
Reputation: pigeonfoot is an unknown quantity at this point 
Solved Threads: 0
pigeonfoot pigeonfoot is offline Offline
Newbie Poster

Re: converting C++ to C

 
0
  #6
Nov 10th, 2006
Originally Posted by WaltP View Post
See this...

IOW, don't cast malloc()
Good eye I appreciate it. Honestly I am new to using malloc.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC