Question

Language is C++ NOTE: No arrays should be used to solve problems and No non-constants global...

Language is C++

NOTE:

No arrays should be used to solve problems and No non-constants global variables should be used.

PART B: (Statistics Program) – (50%)

Please read this lab exercise thoroughly, before attempting to write the program.

Write a program that reads the pairs of group number and student count from the text file (user must enter name of file) and:

  1. Displays the number of groups in the file (5 %)
  2. Displays the number of even and odd student count (5%)
  3. Displays the sum of the even student count and the sum of the odd student count as well as the sum of all the student count (5%)
  4. Displays the group numbers of the largest student count and the smallest student count (5 %)
  5. Computes the average of the largest and smallest student count (10 %)
  6. Compute the sum of ALL the digits in the largest student count (i.e. 952 – Sum is 9+5+2 = 16) (15 %)

Assume the contents of the file are as follows but that the pairs of data can change later:

1 25

2 123

3 475

4 61

5 77

6 910

7 234

8 138

9 134

10 95

11 674

12 345

13 31

14 211

15 952

16 873

17 22

18 7

19 876

20 347

21 450

The following is a sample output: User input in red

What is name of the input file? integers.dat

The number of groups in the file is 21

There are 12 odd student count and 9 even student count

The sum of the odd student count is 2645

The sum of the even student count is 4390

The sum of all the student count is 7035

The group number of the largest student count is 15

The group number of the smallest student count is 18

The average of 952 and 7 is 479.5

The sum of all the digits in the largest student count is 16

NOTE:

  1. Use two decimal point precision for the average
  1. To check your code, adjust one of the student count in the file, recompile your code and run again (suggest changing the largest or smallest student count )
  1. Even though there are 21 pairs of numbers in the file, the program should NOT presume there will always be 21 pairs of numbers. In other words, you should not solve this problem by declaring 21 pairs of values. If I choose to use another integers.dat file other than the one provided with the assignment, your program should still work correctly with that file as well. All you can assume is that a series of integers will be read in from the file. Therefore, you need to implement a solution that uses the repetition control structures (for, while, do…while) in the reading and processing of these values to produce your output.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

The code is implemented in C++

First we read the file name then open file

we read each line of file then we know that program reads the pairs of group number and student count from the text file

so used group_number and student_count

then checking student_count parity that means even or odd then incrementing count and sum.

in the code maxi means largest student count

mini means smallest student count

#include<bits/stdc++.h>
using namespace std;
int main(){
   ifstream fin;
   string lin;
   string line,word,s;
   cin>>s;
    fin.open(s);
    long int maxi=-1,mini=LONG_MAX,groups=0,even_student_count=0,sum_of_digits=0,
    odd_student_count=0,even_student_count_sum=0,odd_student_count_sum=0,maxi_group=-1,mini_group=-1;
    while (fin){
        getline(fin, line);
        istringstream ss(line);
       while (ss>>word){
           int group_number=stoi(word);
           ss>>word;
           int student_count=stoi(word);
           //cout<<group_number<<" "<<student_count<<endl;
           if(student_count%2==0){
               even_student_count+=1;
               even_student_count_sum+=student_count;
           }else{
               cout<<odd_student_count_sum<<" "<<student_count<<endl;
               odd_student_count+=1;
               odd_student_count_sum+=student_count;
           }
           if(maxi<student_count){
               maxi_group=group_number;
               maxi=student_count;
           }
           if(mini>student_count){
               mini_group=group_number;
               mini=student_count;
           }
          
       }
       groups++;
    }
    groups--;//decrementing because of end line
    fin.close();
    double avg=(mini+maxi)/2.0;
    long int temp=maxi;
    while(temp){
       sum_of_digits+=temp%10;
       temp/=10;
    }
    cout<<"The number of groups in the file is "<<groups<<endl;
    cout<<"There are "<<odd_student_count<<" odd student count and "<<even_student_count<<" even student count"<<endl;
    cout<<"The sum of the odd student count is "<<odd_student_count_sum<<endl;
    cout<<"The sum of the even student count is "<<even_student_count_sum<<endl;
    cout<<"The sum of all the student count is "<<(even_student_count_sum+odd_student_count_sum)<<endl;
    cout<<"The group number of the largest student count is "<<maxi_group<<endl;
    cout<<"The group number of the smallest student count is "<<mini_group<<endl;
    cout<<"The average of "<<maxi<<" and "<<mini<<" is ";
    cout <<fixed<<setprecision(2)<<avg<<endl;
    cout<<"The sum of all the digits in the largest student count is "<<sum_of_digits<<endl;
}

Add a comment
Know the answer?
Add Answer to:
Language is C++ NOTE: No arrays should be used to solve problems and No non-constants global...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Note: The order that these functions are listed, do not reflect the order that they should...

    Note: The order that these functions are listed, do not reflect the order that they should be called. Your program must be fully functional. Submit all .cpp, input and output files for grading. Write a complete program that uses the functions listed below. Except for the printOdd function, main should print the results after each function call to a file. Be sure to declare all necessary variables to properly call each function. Pay attention to the order of your function...

  • I need to write a C++ program that reads two integers from a data filed called...

    I need to write a C++ program that reads two integers from a data filed called "integers.dat" The first integer should be low and the other one high. The program should loop through all integers between the low and high values and include the low and high integers and then display Display the integers in the file Displays the number of integers divisible by 5 OR 6, but not both. Displays the sum of the integers from condition 2 as...

  • Please Use C++ for coding . . Note: The order that these functions are listed, do...

    Please Use C++ for coding . . Note: The order that these functions are listed, do not reflect the order that they should be called. Your program must be fully functional. Submit all.cpp, input and output files for grading. Write a complete program that uses the functions listed below. Except for the printodd function, main should print the results after each function call to a file. Be sure to declare all necessary variables to properly call each function. Pay attention...

  • PLEASE HELP C++ some of the content in random.txt Program #2 This problem is a modification...

    PLEASE HELP C++ some of the content in random.txt Program #2 This problem is a modification of Problem 24 on page 302 of the Gaddis text with two small additions. (A scan of this problem can be found on the last page of the assignment.) In this assignment, your program will read in a list of random numbers from an input file, random. txt (found on eLearning in the "Homework Input Files" folder), and calculate the following statistics on those...

  • How to write this code in C++???? using fstream Write a program which: Asks the user...

    How to write this code in C++???? using fstream Write a program which: Asks the user to enter a positive integer greater than 0 Validates that the entry is a positive integer Outputs the digits in reverse order with a space separating the digits Outputs the even digits not in reverse order with a space separating the digits (consider zero to be even) If there are no even digits, the an appropriate message should be displayed: There are no even...

  • C++ Manually create a file Numbers.txtand fill it with integers, one below the other. Write a...

    C++ Manually create a file Numbers.txtand fill it with integers, one below the other. Write a program that reads ANY sequence of integers (can be positive, 0 or negative) from this file. Using a looping construct, you will read numbers from the file till end of the file is reached. Calculate the largest and the smallest numbers among the data in the file. Your code must display both of those on the terminal screen along with total count of numbers...

  • Write a C++ program that simulates a lottery game. Your program should use functions and arrays....

    Write a C++ program that simulates a lottery game. Your program should use functions and arrays. Define two global constants: - ARRAY_SIZE that stores the number of drawn numbers (for example 5) -MAX_RANGE that stores the highest value of the numbers ( for example 9 ) The program will use an array of five integers named lottery, and should generate a random number in the range of 0 through 9 for each element of the array. The user should enter...

  • This is a C++ assignment that I'm trying to create and would like some help understanding...

    This is a C++ assignment that I'm trying to create and would like some help understanding while loops, with possible integration of for loops and if statements. This program only uses while, for, and if. I have some code that I have started, but where to go from there is what's giving me some trouble. This is involves a sentinel controlled while loop, and there are a lot of specifications below that I must have in the program. The program...

  • I need #5 done i cant seem to understand it. it must be written in c++...

    I need #5 done i cant seem to understand it. it must be written in c++ 4. Numeric Processing Write a program that opens the file "random.txt", reads all the numbers from the file, and calculates and displays the following: a. The number of numbers in the file. b. The sum of all the numbers in the file (a running total) c. The average of all the numbers in the file numFile.cpp Notes: Display the average of the numbers showing...

  • This assignment should be relatively easy. Write a program in C language, that reads in numbers...

    This assignment should be relatively easy. Write a program in C language, that reads in numbers from any file, one per line, until it detects that a 0 (zero not an oh') has been entered The 0 does not count as part of the number series. After all numbers have been entered print out the number of items entered, the sum, the minimum value entered, the maximum value entered, and the arithmetic mean (one on each line), in the order...

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
ADVERTISEMENT