Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

need help on computer science. to write c++ program(visual studio) please!!! Ada

ID: 3830837 • Letter: N

Question

need help on computer science. to write c++ program(visual studio) please!!!

Adapt the concepts shown in the demos to provide a program which faithfully reads a list of non-negative integer scores, ultimately terminating the list reading when the sentinel value (lets use -9999) is entered. The program should then correctly report the number of non-negative scores entered and the arithmetic mean (average) of those scores (an accurate floating point or decimal-valued average, please. No, it does NOT need 15 places). Demonstrate your programs behavior in response to errors on input (that is, show that it faithfully rejects improper input such as alphabetic characters, decimal points, and commas). Here are some specific examples demonstrating the requirements of your program. (a) I'll compute the average of your inputs: 10, 20 35 50.5 -9999 Results: 5 non-negative integers entered, average 24//KB Comment: the non-negative integers read were (10, 20, 35, 50, 5) (b) I'll compute the average of your inputs: 5-10 25.40, -9999 Results: 3 non-negative integers entered, average 23.3333//KB Comment: the non-negative integers read were (5, 25, 40) (c) I'll compute the average of your inputs: 30 45 -9 one junk 100.0 more -1000.8 -9999 Results: 5 non-negative integers entered, average 36.6//KB Comment: the non-negative integers read were (30, 45, 100, 0, 8) Here is a pseudo-code version to consider for implementing this task: Initialize a Sum and Counter to zero Initialize a loop Done variable to 'false', or zero Provide a brief console output to describe what the program will do While not Done Attempt to read the next Input, checking for an integer read failure If this attempt to read an integer failed then Clear the input stream status, then ignore the current character on the stream Repeat the attempt to read an integer until successful If the Input integer is the sentinel value Set the loop Done variable to 'true', or nonzero Else if the Input integer is non-negative then Add one to the Counter Add the Input to the Sum Repeat until Done Compute and display the number of inputs and the average (report 0 average for 0 inputs)

Explanation / Answer


import java.util.*;
public class average_non_negitive {
   public static void main(String[] args) {
       Scanner sc=new Scanner(System.in);
       boolean flag=true;
       int sum=0;
       float avg=0.0f;
       int count=0;
       while(flag==true){
           System.out.println("enter a non negitive value to sumup:");
           int input=sc.nextInt();
           if(input>=0 ){
               count++;
               sum+=input;
               flag=true;
              
           }else if(input==-9999){
               System.out.println("you have entered negitive value:");
               flag=false;
               break;
           }else{
               System.out.println("please enter positive value:");
               flag=true;
           }
          
          
       }
       avg=sum/count;
       System.out.println(count+"non negitive numbers entered and sum is "+sum+" average is"+avg);
   }
}


output:


enter a non negitive value to sumup:
1
enter a non negitive value to sumup:
2
enter a non negitive value to sumup:
3
enter a non negitive value to sumup:
-9999
you have entered negitive value:
3non negitive numbers entered and sum is 6 average is2.0