Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.17 KB | None | 0 0
  1.     /************************************/
  2.     /************   PART 1   ************/
  3.     /************************************/
  4.  
  5.     // Define variables
  6.     string line;
  7.     int counter = 0;
  8.     int pos = 0;
  9.     double average = 0;
  10.     ifstream num_file("numbers.dat");
  11.     bool active = true;
  12.  
  13.     // Open file and get line of numbers
  14.     if (num_file.is_open()) {
  15.         getline(num_file, line);
  16.         num_file.close();
  17.     }
  18.  
  19.     // Get the first number and update position
  20.     counter = stoi( line.substr( 0, 1 ) );
  21.     pos = line.find(" ") + 1;
  22.  
  23.     // Main loop (for each group)
  24.     while (active) {
  25.         // Secondary loop (for marks in each group)
  26.         for (int i = counter; i > 0; i--) {
  27.             average += stoi(line.substr(pos, 2));
  28.             if (line.find(" ", pos) != -1)
  29.                 pos = line.find(" ", pos) + 1;
  30.         }
  31.  
  32.         // Calculate and display average
  33.         average /= counter;
  34.         cout << "For this group of " << counter << " marks, the average is: " << average << endl;
  35.        
  36.         // Check if at the end
  37.         if (line.find(" ", pos) != -1) {
  38.             counter = stoi(line.substr(pos, 1));
  39.             pos = line.find(" ", pos) + 1;
  40.         }
  41.         // If finished, end loop
  42.         else {
  43.             active = false;
  44.         }
  45.         // Need to clear the average after each group
  46.         average = 0;
  47.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement