1

I'm a beginner, this is my current code so far. What I'm confused with is on on how to implement "working out the sum of the squares of the numbers".

#include <stdio.h>

int main()
{
    int i,n,Sum=0,numbers;
    float Average;
    
    printf("\nPlease Enter How many Numbers you want?\n");
    scanf("%d",&n);
    
    printf("\nPlease Enter the Number(s) one by one\n");
    for(i=0;i<n;++i)
    {
        scanf("%d", &numbers);
        Sum = Sum +numbers;
    }
    
    Average = Sum/n;

    printf("\nSum of the %d Numbers = %d",n,Sum);
    printf("\nAverage of the %d Numbers = %.2f",n, Average);
    // printf("\nSum of the Square of %d Numbers = %d",n, );
    
    return 0;
}
Ryuutorii
  • 13
  • 3
  • How would you do this on paper? – Jabberwocky Nov 05 '21 at 08:02
  • 1
    `Sum = Sum + numbers * numbers;` to add up the squares. Not sure that's what you want since the commented out printf talks about square roots which would be different. – Retired Ninja Nov 05 '21 at 08:08
  • Note that the direct implementation can be numerically unstable, which means that the rounding errors can build up. For actual use to calculate average and especially stdev, either you should use an off-the-shelf library, or you'll have to find a suitable numerically stable algorithm and implement it carefully – Jiří Baum Nov 05 '21 at 09:05
  • 1
    @JiříBaum Good point, but they are inputting and using *integers* in the posted code, even when they shouldn't (`Sum/n`). – Bob__ Nov 05 '21 at 09:10

2 Answers2

0

You can create a variable and do exactly as you do with Sum, except you are now adding the squares.

int sumOfSqaure = 0;
// code

for(i = 0; i < n; ++i)
{
    scanf("%d", &numbers);
    // a = a + b is equal to a += b
    sumOfSqaure += numbers;

    sumOfSqaure += numbers * numbers; // n * n == n ^ 2
}
0

This is a solution to your problem : ( Changes are marked with //******** )

#include <stdio.h>

int main()
{
    int i,n,Sum=0,numbers, square = 0;//***********************
    float Average;
    
    printf("\nPlease Enter How many Numbers you want?\n");
    scanf("%d",&n);
    
    printf("\nPlease Enter the Number(s) one by one\n");
    for(i=0;i<n;++i)
    {
        scanf("%d", &numbers);
        Sum = Sum +numbers;
        square = square + (numbers*numbers); //***************
    }
    
    Average = Sum/n;

    printf("\nSum of the %d Numbers = %d",n,Sum);
    printf("\nAverage of the %d Numbers = %.2f",n, Average);
    printf("\nSum of the Square of %d Numbers = %d",n, square);//****************
    
    return 0;
}

Although other methods could be used to solve your question.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • 1
    This was what I was looking for, was vaguely mentioned on a comment, but this gave me more insight on how I implement it into my code, probably wasn't very clear with my question, but thanks so much, appreciate it! – Ryuutorii Nov 05 '21 at 08:55
  • Please note that `Sum/n` is an [integer division](https://stackoverflow.com/questions/3602827/what-is-the-behavior-of-integer-division) and it may not be what the OP (or their lecturer) were expecting. You may want to cast to `float` one of the terms *before* performing such operation. – Bob__ Nov 05 '21 at 09:03