Confused after initializing variables. Please Help !

Hello
Everyone
I got an assignment as follows
Write a program to calculate the standard deviation of a list of numbers. Use a constant in your program for the size of the list.
The stanadard deviation of a set of data is a measure of the central tendancy of that data (how closely bunched around the mean it is). It is calculated by :
1) finding the mean (average)
2) finding the sum of the squares of each element's distance from the mean
3) dividing by the number of elements in the data set
4 ) taking the square root
Example:
Data 5, 6, 7, 10, 12
Average: 8

Distances from the Mean: 3, 2, 1, 2, 4
Sum of the Squares of Distances from the Mean: (3*3 + 2*2 + 1*1 + 2*2 + 4*4) = 34
Divide by number of elements, and take the square root : Square root of 34/5= 2.61 (approx)

Can someone please show how to do this ?
I have no sample code to show as I am really confused in even initializing the variables.
instead of cout and cin our programs are made with printf and scanf
Please kindly help me in this assignment so i can learn how to do it.How do make the formulas ?
Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

int main ()
{
const int data[5]={ 5, 6, 7, 10, 12};
int list=5;
int DistancefromtheMean;
int SumoftheSquares;
int divide;
float squareroot;
int sum;
int average;
int i=0;
int j=0;
int k=0;




system("pause");
return(0)
}
Loop. The for. You must have seen it mentioned. I bet you are supposed to use loops here. First to read the values, then to compute sum for average, then again to get the sum of distances.

Note: it does sound like you are using C rather than C++.
Hello
Thanks for your reply . Yes we are supposed to use for loops but how do you use for loops for an example like this question. I have done for loops for other things but for the arrays unit I dont really get it .Can you give me an example code
Thank you
Show an example of your loops instead.

Then think whether "dereferencing an array element" has been mentioned.
As I said i am stuck after initializing the variables but still ill give it a try
1
2
for ( data[0]+data[1]+data[3]+data[4]){
      scanf("%d", &sum)


I chose this because first of all i guess we need a sum of all the numbers so later we can find the average and etc. But not sure :(
I dont understand one thing what is cout and cin. I use Dev c++ and our teachers taught us printf and scanf. Well i am in grade 11 so dont have that much knowledge yet as school started a month ago
thanks
Ok. The article that you gave me did not have any references in how to add data in for loops.

My question remains the same If anyone knows how to do it please help me out.
Thanks
for ( data[0]+data[1]+data[3]+data[4])

That's not the correct syntax for a for loop. Before you do anything else, I suggest you go back to your textbook and really learn how to write a for loop. It's one of the most fundamental, indispensable control statements in the language, and without learning that, you'll be very lost.
Last edited on
The tutorial about 'for' may not show how to add up,but it does explain the syntax of 'for', which seems to elude varun*


http://www.cplusplus.com/reference/numeric/accumulate/
Well I spent some time and finally have come here
[code]
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int main(){

int n, i;
float num[100], sum=0.0, average=0.0;
float data[n];


printf("Enter the numbers of data: ");
scanf("%d", &n);
for (i=0; i<n; i++) {
printf("\nPlease enter number %d: ", (i+1));
scanf("%f", &data[i]);
average = ((average * (i)) + data[i]) / (i+1); // Average
}
for (i=0; i<n; i++) {
sum += ((data[i] - average) * (data[i] - average));
}

printf("Average = %.2f\n", average);
printf("Standard deviation = %.2f\n", sqrt(sum / n));


system("pause");
return(0);
}
[code]



How do i find the
Distances from the Mean: 3, 2, 1, 2, 4
Sum of the Squares of Distances from the Mean: (3*3 + 2*2 + 1*1 + 2*2 + 4*4) = 34
Please help me
Please use code tags when posting code.

1
2
int n;
float data[n]; 


This is illegal. You can't declare an array when you haven't specified the size of it. Doesn't your compiler give an error because of it?
Topic archived. No new replies allowed.