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

Rate my solution and please do comment if any doubt

Code:

#include<iostream>
#include<fstream>
#include<string>
#include<cstdlib>
using namespace std;
int main(){
   ifstream file;
   //this is the file pointer variable
   string line;
   string filename;
   int x,y,count=0,odd_count=0,even_count=0;
   int max,min,even_sum=0,odd_sum=0;
   int max_grp,min_grp;
   //above declarations are the variable for all the given requirements
   cout<<"What is name of the input file? ";
   cin>>filename;
   //filename is variable for file name
   file.open(filename.c_str());
   //c_str() will return a null terminated string
   getline(file,line);
   //getline is used for reading each line
   while(file){
       x=line.find(' ');
       //It will find the space between the pair
       //After the space is our required second number
       //Space is found to convert second number(string) to int
       //below if is used to skip new line
       if(x==-1){
           getline(file,line);
           //read each line
           continue;
       }
       //atoi() will convert stringt to int
       //substr() will find the substring (second number)
       //c_str() will return a null terminated string
       y=atoi(line.substr(x+1).c_str());  
       if(count==0){
           //initially max,min are the first group student count
           //If new maximum,minimum is found later it will be updated
           max=y;
           max_grp=count+1;
           min_grp=count+1;
           min=y;
       }
       else{
           if(max < y){
               //new maximum is found and updated
               max=y;
               max_grp=count+1;
           }
           if(min > y){
               //new minimum is found and updated
               min =y ;
               min_grp=count+1;
           }
       }
       if(y%2==0){
           //Even count and even sum is calculated here
           even_count++;
           even_sum+=y;
       }
       else{
           //Odd count and odd sum is calculated here
           odd_count++;
           odd_sum+=y;
       }
       //new line is read
       getline(file,line);  
       //count is incremented to find number of groups
       count++;
   }
  
   int n,sum=0;
   n=max;
   //below loop is used to find The sum of all the digits in the largest student
   while(n>0){
       //loop will end when there are no digits to add
       sum += (n%10);
       //last digit is taken and added to the sum
       n /= 10;
       //last digit is removed
   }
   cout<<"The number of groups in the file is "<<count<<endl;
   cout<<"There are "<<odd_count<<" odd student count and "<<even_count<<" even student count"<<endl;
   cout<<"The sum of the odd student count is "<<odd_sum<<endl;
   cout<<"The sum of the even student count is "<<even_sum<<endl;
   cout<<"The sum of all the student count is "<<odd_sum+even_sum<<endl;
   cout<<"The group number of the largest student count is "<<max_grp<<endl;
   cout<<"The group number of the smallest student count is "<<min_grp<<endl;
   printf("The average of %d and %d is %.2f\n",max,min,(float)(max+min)/2.0);
   cout<<"The sum of all the digits in the largest student count is "<<sum<<endl;
   return 0;
}

Output:

The below is for the input data given in the question . You can try for your own input

Note: The value given for sum of the odd student count is wrong

Below is the correct answer.Please check with your calculator

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
  • 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: Displays the number of groups in the file (5 %) Displays the number of even and odd student...

  • 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...

  • 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...

  • 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 program in ARM assembly using ARMSim# to perform the following tasks: 1. Open a...

    Write a program in ARM assembly using ARMSim# to perform the following tasks: 1. Open a file named "integers.dat" and keep track of the following information: a.The first integer value is (i) b. The ith integer value is (j) c. Count the total number of integers in the file d. Count the number of integers that are less than j e. Count how many even numbers are less than j f. And how many odd numbers are less than j...

  • 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...

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