Desperately demand array help
Hey anybody I'm struggling with arrays and demand some help with this programme. I really don't understand why this is giving me -nan for a output. Seems like whatever I attempt the input values are non being passed or stored so it tin can compute the average. And as well, to discover the highest and lowest would a chimera sort be meliorate than a selection assortment equally the test inputs are merely 4 numbers? Thanks so much!
1 2 3 4 5 6 7 eight ix 10 xi 12 xiii 14 xv 16 17 18 19 20 21 22 23 24 25 26 27 28 29 xxx 31 32 33 34 35 36 37 38 39 twoscore 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 xc 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | // This program volition read in a grouping of test scores( positive integers from one to 100) // from the keyboard and then calculates and outputs the average score // too as the highest and lowest score. At that place will exist a maximum of 100 scores. #include <iostream> using namespace std; typedef int GradeType[100]; // declares a new data type: // an integer array of 100 elements float findAverage (const GradeType, int); // finds average of all grades int findHighest (const GradeType, int); // finds highest of all grades int findLowest (const GradeType, int); // finds lowest of all grades int main() { GradeType grades; // the array property the grades. int numberOfGrades; // the number of grades read. int pos; // index to the assortment. float avgOfGrades; // contains the average of the grades. int highestGrade; // contains the highest grade. int lowestGrade; // contains the lowest form. // Read in the values into the array pos = 0; cout << "Please input a grade from 1 to 100, (or -99 to stop)" << endl; cin >> grades[pos]; while (grades[pos] != -99) { // Fill up in the code to read the grades cout << "Please input a grade from one to 100, (or -99 to stop)" << endl; cin >> grades[pos]; } numberOfGrades = pos; // Fill up blank with appropriate identifier // telephone call to the function to discover average avgOfGrades = findAverage(grades, numberOfGrades); cout << endl << "The average of all the grades is " << avgOfGrades << endl; // Make full in the call to the part that calculates highest class highestGrade = findHighest(grades, numberOfGrades); cout << endl << "The highest grade is " << highestGrade << endl; // Fill in the phone call to the part that calculates everyman grade lowestGrade = findLowest(grades,numberOfGrades); // Fill up in lawmaking to write the lowest to the screen cout << "The lowest grade is " << lowestGrade << endl; return 0; } //**************************************************************************** // findAverage // // task: This function receives an array of integers and its size. // It finds and returns the average of the numbers in the array // data in: array of floating point numbers // data returned: avarage of the numbers in the array // //**************************************************************************** float findAverage (const GradeType array, int size) { bladder sum = 0; // holds the sum of all the numbers for (int pos = 0; pos < size; pos++) sum = sum + assortment[pos]; return (sum / size); //returns the average } //**************************************************************************** // findHighest // // job: This office receives an array of integers and its size. // It finds and returns the highest value of the numbers in // the assortment // data in: array of floating point numbers // data returned: highest value of the numbers in the array // //**************************************************************************** int findHighest (const GradeType array, int size) { // Fill in the lawmaking for this office } //**************************************************************************** // findLowest // // job: This function receives an array of integers and its size. // It finds and returns the lowest value of the numbers in // the assortment // information in: assortment of floating bespeak numbers // data returned: everyman value of the numbers in the array // //**************************************************************************** int findLowest (const GradeType array, int size) { // Make full in the code for this function } | |
Last edited on
Y'all're not increment pos in the while loop, so yous're constantly reading input into grades[0].
Last edited on
Thank you and so much simply I cant seem to find out how to increment information technology... ):
I tried to increment it to this only the loop just keeps running even when -99 is the input.
1 2 iii 4 5 6 vii eight | while (grades[pos] != -99) { // Make full in the lawmaking to read the grades cout << "Delight input a course from 1 to 100, (or -99 to finish)" << endl; cin >> grades[pos++]; } | |
The reason being is considering you increment it and y'all're checking the next value if its non equal to 99.
What you're going to desire to do is put an if statement earlier you increment.
Something like this
if( grades[pos] != -99 ) ++pos;
Then when you lot input remove the increment.
Besides technically speaking inputting similar that works and is correct but it can lead to many errors. So it's not suggested.
solved thanks and so much for your help you're a life saver.
lawmaking turned out to be
1 2 3 iv five 6 vii 8 | while (grades[pos] != -99) { pos ++; // Fill in the code to read the grades cout << "Please input a form from 1 to 100, (or -99 to stop)" << endl; cin >> grades[pos]; } | |
Last edited on
You may also consider putting in a second while loop to catch any grades below zilch, excluding -99, and anything above 100 should be excluded as well I assume.
Last edited on
Topic archived. No new replies allowed.
jonesreteneve.blogspot.com
Source: http://cplusplus.com/forum/beginner/113312
0 Response to "This Program Will Read in a Group of Test Scores (Positive Integers From 1 to 100)"
Post a Comment