arrays and functions

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2006
Posts: 7
Reputation: madonnamama is an unknown quantity at this point 
Solved Threads: 0
madonnamama madonnamama is offline Offline
Newbie Poster

arrays and functions

 
0
  #1
Apr 3rd, 2006
I am having a huge problem with writing this program. Every time I ask the teacher for help she is too busy helping someone else. so she just tell me one thing and runs off to the next person without checking if it worked and what other help I might need. It has become very frustrating. I have been able to ask for a classmates help, but that is only when we have free time during class, if he's not too busy writing his own code. I am turning to you for help. I have written and adjusted this program so many times that I am so confused. Please take a look at my code and steer me in the right direction. I greatly appreciate it. Thank you. I also hope that I use these code tags the right way. Sorry if it doesn't work. Oh, and P.S. the teacher said we had to call a function and pass something by reference(what, I'm not sure, but I specifically remember her pointing it out) I'm thinking it's to do the calculations. I've tried to design this program using only function, but I'm not that great with those, so it's been difficult.

  1. //chapter 7 lab p475 ex8 gradebook
  2. //Intructions:
  3. //A teacher has five students who have taken four tests.
  4. //The teacher uses the following grading scale to assign a letter
  5. //grade to a student, based on the average of his or her test scores.
  6. //___________________________________
  7. // Test Score Letter Grade
  8. //-----------------------------------
  9. // 90-100 A
  10. // 80-89 B
  11. // 70-79 C
  12. // 60-69 D
  13. // 0-59 F
  14. //___________________________________
  15. //Write a program that uses a two-dimensional array of charachters
  16. //that hold the five student names, a single-dimensional array of
  17. //five characters to hold the five students' letter grades, and five
  18. //single-dimensional arrays of four doubles to hold each student's
  19. //set of test scores.
  20. //The program should allow the user to enter each student's name and
  21. //his or her scores. It should then calculate and display each
  22. //student's average test score and a letter grade based on that average.
  23. //Imput Validation: Do not accept test scores less than zero or greater
  24. //than 100.
  25.  
  26. #include <iostream>
  27. using namespace std;
  28.  
  29. //function prototype
  30. void calcdata(double []);
  31. void display data();
  32.  
  33.  
  34. //start of main
  35. int main()
  36. {
  37.  
  38. const int NUM_NAMES = 5; //how many occurances
  39. const int NAMESIZE = 11; //how long names can be, 10 letters
  40. const int NUM_TESTS = 4; //how many tests
  41. char name[NUM_NAMES][NAMESIZE]; //two-dimensional name array
  42. char grade[NUM_NAMES]; //grade for each student array
  43. double average[NUM_NAMES]; //average for each student array
  44. double student1[NUM_TESTS] //student1 array
  45. double student2[NUM_TESTS] //student2 array
  46. double student3[NUM_TESTS] //student3 array
  47. double student4[NUM_TESTS] //student4 array
  48. double student5[NUM_TESTS] //student5 array
  49.  
  50.  
  51. cout << β€œEnter the student’s nameβ€?
  52. for (int count = 0; count < NUM_NAMES; count++)
  53.  
  54. { // beginning of nested for loop
  55. cout << "Student" << (count +1) <<": ";
  56. cin >> name[count];
  57.  
  58. for (i = 0; i < NUM_NAMES; i++)
  59. cout >> name [i]; //stub statement
  60. } //end of for loop
  61.  
  62. //Beginning of validation sequence taken from chapter 6 lab assignment
  63. //********Need to change to fit with this lab assignment********
  64.  
  65. cout << "Please enter test 1, with scores between 0 and 100.\n";
  66. cin >> val1;
  67. while (val1 < 0 || val1 > 100)
  68. {
  69. cout << "You have not entered a number between 0 and 100. Please re-enter test 1.\n";
  70. cin >> val1;
  71. }//end of while loop
  72.  
  73. cout << "Please enter test 2, with scores between 0 and 100.\n";
  74. cin >> val1;
  75. while (val2 < 0 || val2 > 100)
  76. {
  77. cout << "You have not entered a number between 0 and 100. Please re-enter test 2.\n";
  78. cin >> val2;
  79. }//end of while loop
  80.  
  81. cout << "Please enter test 3, with scores between 0 and 100.\n";
  82. cin >> val1;
  83. while (val3 < 0 || val3 > 100)
  84. {
  85. cout << "You have not entered a number between 0 and 100. Please re-enter test 3.\n";
  86. cin >> val3;
  87. }//end of while loop
  88.  
  89. cout << "Please enter test 4, with scores between 0 and 100.\n";
  90. cin >> val4;
  91. while (val4 < 0 || val4> 100)
  92. {
  93. cout << "You have not entered a number between 0 and 100. Please re-enter test 4.\n";
  94. cin >> val4;
  95. }//end of while loop
  96.  
  97. cout << "Please enter test 5, with scores between 0 and 100.\n";
  98. cin >> val5;
  99. while (val5 < 0 || val5 > 100)
  100. {
  101. cout << "You have not entered a number between 0 and 100. Please re-enter test 5.\n";
  102. cin >> val5;
  103. }//end of while loop
  104.  
  105. //display output for validation
  106. cout << "Test 1 is: " << val1 << endl;
  107. cout << "Test 2 is: " << val2 << endl;
  108. cout << "Test 3 is: " << val3 << endl;
  109. cout << "Test 4 is: " << val4 << endl;
  110. cout << "Test 5 is: " << val5 << endl;
  111.  
  112.  
  113. //call to function calcdata
  114. calcdata();
  115.  
  116.  
  117.  
  118. return 0;
  119. } //end of main
  120.  
  121. //************************************************
  122. //function definition of calcdata
  123. // this function is going to get the test scores
  124. // array and calculate the averages
  125. // and calculate grade
  126. //i need to pass the student names to correspond
  127. // with the test scores, avgs, and grade
  128. //************************************************
  129. void calcdata()
  130.  
  131. const int NUM_STUDENTS = 5;
  132. const int NUM_SCORES = 4;
  133. double total, average;
  134. double scores[NUM_STUDENTS][NUM_SCORES];
  135.  
  136. //get each students average score
  137. for (int row = 0; row < NUM_STUDENTS; row++)
  138. {
  139. //set the accumulator.
  140. total = 0;
  141.  
  142. //sum a row
  143. for (int col = 0; col < NUM_SCORES; col++
  144. total += scores[row][column];
  145.  
  146. //get the average
  147. average = total / NUM_SCORES;
  148.  
  149. //
  150.  
  151. //**************************************************
  152. // loop used to determine grade for each students average
  153. //for (int index = 0; index < NUM_NAMES; index++)
  154. //nested loop
  155. //for (int count =0; index < NUM_NAMES; count++)
  156.  
  157.  
  158. if (average[0] < 60)
  159. grade[0] = β€˜F’
  160. else if (average [0] < 70)
  161. grade[0] = β€˜D’
  162. else if (average [0] < 80)
  163. grade[0] = β€˜C’
  164. else if (average [0] < 90)
  165. grade[0] = β€˜B’
  166. else (average [0] <= 100)
  167. grade[0] = β€˜A’
  168.  
  169. if (average[1] < 60)
  170. grade[1] = β€˜F’
  171. else if (average [1] < 70)
  172. grade[1] = β€˜D’
  173. else if (average [1] < 80)
  174. grade[1] = β€˜C’
  175. else if (average [1] < 90)
  176. grade[1] = β€˜B’
  177. else (average [1] <= 100)
  178. grade[1] = β€˜A’
  179.  
  180. if (average[2] < 60)
  181. grade[2] = β€˜F’
  182. else if (average [2] < 70)
  183. grade[2] = β€˜D’
  184. else if (average [2] < 80)
  185. grade[2] = β€˜C’
  186. else if (average [2] < 90)
  187. grade[2] = β€˜B’
  188. else (average [2] <= 100)
  189. grade[2] = β€˜A’
  190.  
  191. if (average[0] < 60)
  192. grade[3] = β€˜F’
  193. else if (average [3] < 70)
  194. grade[3] = β€˜D’
  195. else if (average [3] < 80)
  196. grade[3] = β€˜C’
  197. else if (average [3] < 90)
  198. grade[3] = β€˜B’
  199. else (average [3] <= 100)
  200. grade[3] = β€˜A’
  201.  
  202. if (average[0] < 60)
  203. grade[4] = β€˜F’
  204. else if (average [4] < 70)
  205. grade[4] = β€˜D’
  206. else if (average [4] < 80)
  207. grade[4] = β€˜C’
  208. else if (average [4] < 90)
  209. grade[4] = β€˜B’
  210. else (average [4] <= 100)
  211. grade[4] = β€˜A’
  212.  
  213. //call to function displaydata
  214. displaydata();
  215.  
  216. return 0;
  217. }//end of function calc data
  218.  
  219.  
  220. //***************************************************
  221. //definition of function displaydata
  222. //this function is designed to display the student's
  223. // average and letter grade
  224. //***************************************************
  225. void displaydata()
  226.  
  227. for (int i = 0; i < NUM_SIZE; i++)
  228. cout << "Student: " <<name [i]
  229. <<"average: " << average [i]
  230. <<"grade: " << grade [i]
  231. << endl;
  232.  
  233.  
  234.  
  235. return 0;
  236. } // end of main
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 7
Reputation: madonnamama is an unknown quantity at this point 
Solved Threads: 0
madonnamama madonnamama is offline Offline
Newbie Poster

Re: arrays and functions

 
0
  #2
Apr 3rd, 2006
P.S. I think it might be important to mention that I'm required to used Visual Studio.NET to do my programs. I couldn't get it to work on my home computer, so I have only been able to try to run my programs while I'm at school, which isn't very often. We also only run them without debugging. It builds, links, compiles and runs. I am on chapter 7 of my Starting Out with C++ 4th Edition Update. I've basically done looping, functions and arrays. We are not suposed to use any information from later chapters. Searching and sorting arrays is my next chapter, so that might give you an idea of where I'm at in my book. I hope this helps. Thank you.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 54
Reputation: HackWizz is an unknown quantity at this point 
Solved Threads: 2
HackWizz HackWizz is offline Offline
Junior Poster in Training

Re: arrays and functions

 
0
  #3
Apr 4th, 2006
hello madonnamama,
please try and be specific in your problem. whether it is a logical or whatever error you get point it out..try and shorten your program by using more for loops and switch statements..
return 0;
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 27
Reputation: ahluka is an unknown quantity at this point 
Solved Threads: 2
ahluka's Avatar
ahluka ahluka is offline Offline
Light Poster

Re: arrays and functions

 
0
  #4
Apr 4th, 2006
You would do well to indent it better to make it more readable.
In Mother Russia, car drive you.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 54
Reputation: HackWizz is an unknown quantity at this point 
Solved Threads: 2
HackWizz HackWizz is offline Offline
Junior Poster in Training

Re: arrays and functions

 
0
  #5
Apr 4th, 2006
Originally Posted by ahluka
You would do well to indent it better to make it more readable.
I suppose it is NOT required in such a short post!!!

It is READABLE & COMPREHENSIBLE for the NORMAL!!


EDIT::
HEY AHLUKA
I AM SORRY ...REALLY SORRY :o FOR WHAT I TYPED...IN A HASTE I MISUNDERSTOOD IT FOR MY POST ..M REALLY SORRY!! MY MISTAKE..AND YOU MADONNAMAMA YOU SHOULD SURELY INDENT THE CODE!!!
Last edited by HackWizz; Apr 4th, 2006 at 12:31 pm. Reason: READ THE NEXT POST
return 0;
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 27
Reputation: ahluka is an unknown quantity at this point 
Solved Threads: 2
ahluka's Avatar
ahluka ahluka is offline Offline
Light Poster

Re: arrays and functions

 
0
  #6
Apr 4th, 2006
Oh of course it is.
The "beginning of for loop" and "end of for loop" on the same line as the brackets is really good commenting too. I mean the loop is so short, how would I ever have noticed where it begins and ends.

Badly formatted code is one sure-fire way to shoot yourself in the foot, as is over-commenting.
In Mother Russia, car drive you.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 7
Reputation: madonnamama is an unknown quantity at this point 
Solved Threads: 0
madonnamama madonnamama is offline Offline
Newbie Poster

Re: arrays and functions

 
0
  #7
Apr 4th, 2006
I'm sorry about the indenting problem. I originally had the program written in word with indents and when I copied it to notepad so that I could open it from my email at school all the indents disappeared.

I also added the beginning of and end of loop comments, because my last program had so many brackets, I started getting confused as to what loops they belonged to. So I started adding the comments to prevent my confusion, not to annoy other readers.

I don't know how to change the if, else statement to a for loop.

When I have school tomorrow, I will check to see what errors I get. I haven't tried to run the program yet because I don't think I'm writing the code right to begin with.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 27
Reputation: ahluka is an unknown quantity at this point 
Solved Threads: 2
ahluka's Avatar
ahluka ahluka is offline Offline
Light Poster

Re: arrays and functions

 
0
  #8
Apr 5th, 2006
You should probably be compiling and working on it at home. Dev-C++ is a good free compiler.

Sorry if you already are and I misread it.
In Mother Russia, car drive you.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: arrays and functions

 
0
  #9
Apr 5th, 2006
Originally Posted by madonnamama
I'm sorry about the indenting problem. I originally had the program written in word with indents and when I copied it to notepad so that I could open it from my email at school all the indents disappeared.
Dont use MS=Word to write code. Most of the the text formatting information in MS-word are lost when you copy t hem to notepad or another text editor.
Dont use notepad to write code unless you are writing a hello world program. If you are using Visual Studio there is a very good code editor in it that supports syntax highlighting, auto indenting... If you dont have Visual Studio, Dev-Cpp is a good free compiler with a code editor. Personally I use a seperate editor to write code as it loads quickly compared to onethat comes in a Development Environment. I use the IDE only for compiling, and even that I do in the command line when possible, but that depends on personal taste. Some good free editors that with syntax highlighting and auto indenting are Notepad++, TextPad. These are good even when you are programming in another language as most of the document classes are supported in them.

Originally Posted by madonnamama
I also added the beginning of and end of loop comments, because my last program had so many brackets, I started getting confused as to what loops they belonged to. So I started adding the comments to prevent my confusion, not to annoy other readers.
If you use Auto indenting you will not get confused on what bracket belongs to what loop. Usually the starting brace should be in the same vertical line as the ending bracket. Then no confusion. As for indenting: there should be an option that says "replace tabs with spaces". If you set this option to on, then the indenting information will not be lost even if you open it in another text viewer. I have see that even if you use [code][/code] tags the indenting goes crazy when it is done using tabs.
Originally Posted by madonnamama
I don't know how to change the if, else statement to a for loop.
This is not possible and an unnecessary attempt. An if else statement is for condition checking. A For loop is for repetition.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 27
Reputation: ahluka is an unknown quantity at this point 
Solved Threads: 2
ahluka's Avatar
ahluka ahluka is offline Offline
Light Poster

Re: arrays and functions

 
0
  #10
Apr 5th, 2006
Wolfpack has made an excellent point I think, especially about loads times when it comes to IDE's. Still I use one because I like all the nice features but could do without the 40 second wait while it loads larger projects
In Mother Russia, car drive you.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
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