469,641 Members | 1,256 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,641 developers. It's quick & easy.

anyone can help me out to convert it to c++. i am a fresh student .

Initialize total to zero

Initialize counter to zero

Input the first grade

while the user has not as yet entered the sentinel

add this grade into the running total

add one to the grade counter

input the next grade (possibly the sentinel)

endwhile



if the counter is not equal to zero

set the average to the total divided by the counter

print the average

else

print 'no grades were entered'

endif
Nov 6 '20 #1
2 3081
dev7060
518 Expert 512MB
  • initialize/set -> assignment operator
  • input -> input stream object
  • 'while the user' -> loop
  • add -> '+' operator
  • print -> output stream object
  • if -> if conditional
  • use += with 'total'
  • average (should be float/double) -> (sum of all values)/(total number of values)
It's mentioned where the loop or if block ends. Boolean conditions are stated as well.
Nov 6 '20 #2
AjayGohil
84 64KB
Hello,

you can refer following syntax for your program:

Initialize total and counter as follow

Expand|Select|Wrap|Line Numbers
  1. int total=0;
  2. int counter=0; 
  3. int grade=0;
  4. int average=0;
for input grade use cin

Expand|Select|Wrap|Line Numbers
  1. cin>>grade;
for loop use while loop
Expand|Select|Wrap|Line Numbers
  1. while (sentinel) {
  2.  cin>>grade;
  3.  total=total+grade;
  4.  counter=counter+1;
  5. }
  6. if(counter!=0)
  7. {
  8. average=total/counter;
  9. cout<<"Average"<<average;
  10. }
  11. else
  12. {
  13. cout<<"No grades were entered";
  14. }
  15.  
  16.  
Nov 11 '20 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by J. Nielsen | last post: by
5 posts views Thread by irin07 | last post: by
reply views Thread by moh5658 | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.