Question

Write a C++ program that will calculate average of squares up to n. For example, if...

Write a C++ program that will calculate average of squares up to n. For example, if user enters 3 the function should display 1 + 4 + 9/ 3 = 4.6667.

Requirements

Created function to receive 1 parameter and no return parameter.

Called the new function from main function inside a while loop.

Calculated the sum of squares and average correctly in the new function using loop with no library functions.

Sample Output

~~Enter a number ONLY between 1 and 20 : 12
~~1 + 4 + 9 + 16 + 25 + 36 + 49 + 64 + 81 + 100 + 121 + 144 / 12
~~Average of squares of numbers = 54.1667
~~Enter n between 1 and 20 : -1
~~Do you really want to exit? Y/N: Y

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/*
Please find the required solution
*/

#include <iostream>

using namespace std;

/*
Method that takes input integer and calculates averageOfSquares and prints the result
*/

void averageOfSquares(int n){
int sum = 0, i;
cout << "~~";
for(i=1; i<n; i++) {
sum = sum + i*i;
cout << i*i << "+";
}
cout << i*i << "/" << n << endl;
sum = sum+ i*i;
cout << "~~Average of squares of numbers =" << (float)sum/(float)n << endl;
}

/*
Main method to simulate the required solution
*/

int main()
{
int input;
char ch;
while(1)
{
cout << "~~Enter a number ONLY between 1 and 20 : ";
cin >> input;
if(input == -1) {
cout << "~~Do you really want to exit? Y/N: ";
cin >> ch;
if(ch == 'Y')
break;
}
if(input<-1 || input >> 20)
cout << "~~Invalid Input";
averageOfSquares(input);
}
return 0;
}

SAMPLE Output:

Add a comment
Know the answer?
Add Answer to:
Write a C++ program that will calculate average of squares up to n. For example, if...
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
  • Python programming: Write a while loop that prints a. All squares less than n. For example,...

    Python programming: Write a while loop that prints a. All squares less than n. For example, if n is 100, print 0 1 4 9 16 25 36 49 64 81. b. All positive numbers that are divisible by 10 and less than n. For example, if n is 100, print 10 20 30 40 50 60 70 80 90 c. All powers of two less than n. For example, if n is 100, print 1 2 4 8 16...

  • 1.Given a positive integer number says n, write a java program to print the first n...

    1.Given a positive integer number says n, write a java program to print the first n squared numbers recursively. Sample run 1: Enter the value of n: 5 ------------------------------------- First 5 square numbers are: 1 4 9 16 25 Sample run 2: Enter the value of n: 10 ------------------------------------- First 10 square numbers are: 1 4 9 16 25 36 49 64 81 100 Sample run 3: Enter the value of n: 12 ------------------------------------- First 12 square numbers are: 1...

  • Write a java program that asks the user to enter an integer. The program should then...

    Write a java program that asks the user to enter an integer. The program should then print all squares less than the number entered by the user. For example, if the user enters 120, the program should display 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, and 100.

  • QUESTION 12 Write a program that would prompt the user to enter an integer. The program...

    QUESTION 12 Write a program that would prompt the user to enter an integer. The program then would displays a table of squares and cubes from 1 to the value entered by the user. The program should prompt the user to continue if they wish. Name your class NumberPowers.java, add header and sample output as block comments and uploaded it to this link. Use these formulas for calculating squares and cubes are: square = x * x cube = x...

  • Activity 7: Right or Not? Develop or reinforce the Pythagorean theorem and its converse and explo...

    Activity 7: Right or Not? Develop or reinforce the Pythagorean theorem and its converse and explore the relationship between the lengths of the sides of a triangle and its classification as acute, right, or obtuse Online: Centimeter Graph Paper Other: Scissors PURPOSE MATERIALS Work individually or in pairs. GROUPING From a sheet of graph paper, cut out squares with areas 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, and 169 square centimeters. Use three squares to construct a...

  • Write a C program to print the figure as follows. 5, Write a C program to...

    Write a C program to print the figure as follows. 5, Write a C program to print the figure as follows. 1 2 3 4 5 6 7 8 9 2 3 4 5 6 7 8 9 4 6 8 10 12 14 16 18 9 12 15 18 21 24 27 16 20 24 28 32 36 25 30 35 40 45 36 42 48 54 49 56 63 64 72 81 1 Tip: use loop.

  • Please write in java Write a main program that runs the following two recursive methods demonstrating...

    Please write in java Write a main program that runs the following two recursive methods demonstrating that they work. #1) Write a recursive method writeSquares that accepts an integer n and prints the first n squares with the odd squares in decending order, followed by the even squares in acending order. For example • writeSquares(8): prints . 49 25 9 1 4 16 36 64 • writeSquares(11); prints 121 81 49 25 9 1 2 16 36 64 100 •...

  • C++ PROGRAM Write a program that converts from 24-hour notation to 12-hour notation. For example, it...

    C++ PROGRAM Write a program that converts from 24-hour notation to 12-hour notation. For example, it should convert 14:25 to 2:25 PM. The input is given as two integers. There should be at least three functions, one for input, one to do the conversion, and two for output (one for 12-hour time and another for 24-hour time). Record the AM/PM information as a value of type char, ‘A’ for AM and ‘P’ for PM. Thus, the function for doing the...

  • Write a C or C++ program A6pc(pp) that accepts one command line argument which is an integer n between 2 and 6 inclusiv...

    Write a C or C++ program A6pc(pp) that accepts one command line argument which is an integer n between 2 and 6 inclusive. Generate a string of 60 random upper case English characters and store them somewhere (e.g. in a char array). Use pthread to create n threads to convert the string into a complementary string (‘A’<->’Z’, ‘B’<->’Y’, ‘C’<->’X’, etc). You should divide this conversion task among the n threads as evenly as possible. Print out the string both before...

  • need help!! c++ HW_6b - Calculate the average Use a do-while loop Write a program that...

    need help!! c++ HW_6b - Calculate the average Use a do-while loop Write a program that first prompts the user for an upper limit: Enter the number of entries: 5 € (user enters 5) After the user enters a number, the program should prompt the user to enter that many numbers. For example, if the user enters 5, then the program will ask the user to enter 5 values. Use a do-while loop to add the numbers. o With each...

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