Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /************************************/
- /************ PART 1 ************/
- /************************************/
- // Define variables
- string line;
- int counter = 0;
- int pos = 0;
- double average = 0;
- ifstream num_file("numbers.dat");
- bool active = true;
- // Open file and get line of numbers
- if (num_file.is_open()) {
- getline(num_file, line);
- num_file.close();
- }
- // Get the first number and update position
- counter = stoi( line.substr( 0, 1 ) );
- pos = line.find(" ") + 1;
- // Main loop (for each group)
- while (active) {
- // Secondary loop (for marks in each group)
- for (int i = counter; i > 0; i--) {
- average += stoi(line.substr(pos, 2));
- if (line.find(" ", pos) != -1)
- pos = line.find(" ", pos) + 1;
- }
- // Calculate and display average
- average /= counter;
- cout << "For this group of " << counter << " marks, the average is: " << average << endl;
- // Check if at the end
- if (line.find(" ", pos) != -1) {
- counter = stoi(line.substr(pos, 1));
- pos = line.find(" ", pos) + 1;
- }
- // If finished, end loop
- else {
- active = false;
- }
- // Need to clear the average after each group
- average = 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement