Question

This is a revision of a previous exercise, here is the question: Perform exercise 4 at...

This is a revision of a previous exercise, here is the question:

Perform exercise 4 at the end chapter 8 of your textbook using dynamic arrays. (This time the program must use dynamic arrays). I have regular array version posted below.

C++: Write a program that reads a file consisting of students’ test scores in the range 0-200. It should then determine the number of students having scores in each of the following ranges: 0-24, 25-49, 50-74, 75-99, 100-124, 125-149, 150-174, and 175-200. Output the score ranges and the number of students. (Run your program with the following input data: 76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189, 167, 200, 175, 150, 87, 99, 129, 149, 176, 200, 87, 35, 157, 189.)

Here is the data file: grades.txt

76
89
150
135
200
76
12
100
150
28
178
189
167
200
175
150
87
99
129
149
176
200
87
35
157
189

Special Note: The program needs to be run on dynamic arrays. This must be on C++ format and it needs to be a working code with Comments (I would appreciate it even if the comments are in the dynamic array parts only). I have the regular array version posted below, it just needs to be turned into dynamic array. Please check first if the program has compiling errors before posting it. I really dont wish to waste a question again because of compiling errors, so please check it first. I would really appreciate it if this becomes a working program. Please help, thanks.

#include iostream #include fstream #include string

using namespace std;

const int maxCategory = 8; //You have 8 categories (0-24, 25-49, 50-74, 75-99, 100-124, 125-149, 150-174, and 175-200) so why did you put 7?
int getCategory(int);
void initialize(int student[]);
void getStudent(int category, int student[]);
void printArray(int category, int student[]);

int main()
{
   int category;
   int score;
   int student[maxCategory]; //should be array of 8 ints instead of 7 as you defined maxCategory...
   ifstream infile;
   infile.open("grades.txt");

   if(!infile)
   {
      cout << "Cannot open input file." << endl;
      system("pause");
      return 0;
   }
   initialize(student);
   infile>>score;

   while (!infile.eof())
   {
      category = getCategory(score);
      getStudent(category, student);

      infile>>score;
   }
   infile.close();
   printArray(category, student);


   return 0;
}

void initialize(int student[])
{
   int j;
   for(j = 0; j < maxCategory; j++) //for array index range from 0 to (elements - 1), so j should be run from 0 to 7 and does not have to equal maxCategory, just put j < maxCategory
   {
      student[j] = 0;
   }
}

void getStudent(int c, int s[])
{
   if (c < maxCategory) //same here does not need c <=
   {
      s[c]++;
   }
}

void printArray(int category, int student[])
{
   string scoreRange;
   cout << "Score Range" << " ";
   cout << "Number of Students" << endl;

   for (category = 0; category < maxCategory; category++) //here too
   {
      switch(category)
      {
         case 0:
         scoreRange="0-24";
         break;
         case 1:
         scoreRange="25-49";
         break;
         case 2:
         scoreRange="50-74";
         break;
         case 3:
         scoreRange="75-99";
         break;
         case 4:
         scoreRange="100-124";
         break;
         case 5:
         scoreRange="125-149";
         break;
         case 6:
         scoreRange="150-174";
         break;
         case 7:
         scoreRange="175-200";
         break;
      }
      cout << scoreRange << " ";
      cout << student[category] << endl;
   }
}

int getCategory(int score)
{
   int scoreRange;

   if (score>=0 && score<25)
      scoreRange=0;
   else if (score>=25 && score<50)
      scoreRange=1;
   else if (score>=50 && score<75)
      scoreRange=2;
   else if (score>=75 && score<100)
      scoreRange=3;
   else if (score>=100 && score<125)
      scoreRange=4;
   else if (score>=125 && score<150)
      scoreRange=5;
   else if (score>=150 && score<175)
      scoreRange=6;
   else if (score>=175 && score<=200)
      scoreRange=7;
   else
      cout << "Out of range 0-200\n";
   return scoreRange;
}

0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
This is a revision of a previous exercise, here is the question: Perform exercise 4 at...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • I have 2 issues with the C++ code below. The biggest concern is that the wrong...

    I have 2 issues with the C++ code below. The biggest concern is that the wrong file name input does not give out the "file cannot be opened!!" message that it is coded to do. What am I missing for this to happen correctly? The second is that the range outputs are right except the last one is bigger than the rest so it will not output 175-200, it outputs 175-199. What can be done about it? #include <iostream> #include...

  • fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string>...

    fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; struct book { int ISBN; string Author; string Title; string publisher; int Quantity; double price; }; void choice1(book books[], int& size, int MAX_SIZE) { ifstream inFile; inFile.open("inventory.txt"); if (inFile.fail()) cout <<"file could not open"<<endl; string str;    while(inFile && size < MAX_SIZE) { getline(inFile, str); books[size].ISBN = atoi(str.c_str()); getline(inFile, books[size].Title);    getline(inFile, books[size].Author); getline(inFile, books[size].publisher);          getline(inFile,...

  • written in c++ Write a program that reads a file consisting of students’ test scores in...

    written in c++ Write a program that reads a file consisting of students’ test scores in the range 0–200. It should then determine the number of students having scores in each of the following ranges: 0–24, 25–49, 50–74, 75–99, 100–124, 125–149, 150–174, and 175–200. Output the score ranges and the number of students. (Run your program with the following input data:76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189, 167, 200, 175, 150, 87, 99, 129, 149,...

  • codeblock c++ language Write a program that reads students test scores in the range 0-200 from...

    codeblock c++ language Write a program that reads students test scores in the range 0-200 from a file until end of file (use e of() to check if ifstream reaches the End of File Stream) and store those scores in an array. It should then determine the number of students having scores in each of the following ranges: 0-24, 25-49, 50-74, 75-99, 100- 124, 125–149, 150-174, and 175–200. Finally, the program outputs the total number of students, each score range...

  • This needs to be linux compatible in C++ code. Write a program that reads a file...

    This needs to be linux compatible in C++ code. Write a program that reads a file consisting of students’ test scores in the range 0–200. It should then determine the number of students having scores in each of the following ranges: 0–24, 25–49, 50–74, 75–99, 100–124, 125–149, 150–174, and 175–200. Output the score ranges and the number of students. (Run your program with the following input data: 76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189, 167,...

  • Posted this a few hours ago. Can anyone help with this? IN C++ Perform the following:...

    Posted this a few hours ago. Can anyone help with this? IN C++ Perform the following: Read 10 bowlers with 3 scores each into 1 string array and 1 numeric array(2 dimension double array) Sort the data by individual bowlers averages, Don't forget to sort their names also. Calculate the average across and store in your existing array. Calculate the average down and store in your existing array. Print out the contents of both arrays. This will print the averages...

  • 9. At your job, you are creating a library. A co-worker brought this test code to...

    9. At your job, you are creating a library. A co-worker brought this test code to you. They expect that the output would be "12 12 12 12 12". However, they are getting "Empty List" (a) Describe why the error is occurring. (b) Explain how to fix the code. #include <iostream> using namespace std; CON void increaseArray (int* array, int size, int value) { int newSize = size + 5; if (size ==0) { size = 5; O int* newArray...

  • Update your first program to dynamically allocate the item ID and GPA arrays. The number of...

    Update your first program to dynamically allocate the item ID and GPA arrays. The number of items will be the first number in the updated “student2.txt” data file. A sample file is shown below: 3 1827356 3.75 9271837 2.93 3829174 3.14 Your program should read the first number in the file, then dynamically allocate the arrays, then read the data from the file and process it as before. You’ll need to define the array pointers in main and pass them...

  •    moviestruct.cpp #include <iostream> #include <fstream> #include <cstdlib> #include <ostream> #include <fstream> #include <cstdlib> #include <cstring>...

       moviestruct.cpp #include <iostream> #include <fstream> #include <cstdlib> #include <ostream> #include <fstream> #include <cstdlib> #include <cstring> using namespace std; typedef struct{ int id; char title[250]; int year; char rating[6]; int totalCopies; int rentedCopies; }movie; int loadData(ifstream &infile, movie movies[]); void printAll(movie movies[], int count); void printRated(movie movies[], int count); void printTitled(movie movies[], int count); void addMovie(movie movies[],int &count); void returnMovie(movie movies[],int count); void rentMovie(movie movies[],int count); void saveToFile(movie movies[], int count, char *filename); void printMovie(movie &m); int find(movie movies[], int...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT