Array

Pages: 12
closed account (1CohAqkS)
Create an array and store the following Student marks 68, 72, 78, 69, 85, 95, 75, 77, 82, 84, 91, 89, 95, 74.

From the following set of marks.
1. Calculate the mean
2. Calculate the standard deviation.
closed account (1CohAqkS)
i have done the coding but there the user is inputting the marks..................im not able to create the array for the marks mentioned above.

#include <iostream>
#include <cmath>
using namespace std;

double deviation(double[] , int);
double mean (double[] , int);
double mean2;

int main ()
{

int size=10;
double x[14];
mean2=0;

cout << "Enter ten numbers:";


for (int i=0; i<14; i++)
{
cin>>x[i];
}

cout << "The mean is:"<<mean(x,14);
cout << endl;
cout << "The standard deviation is:"<<deviation (x,14);
return 0;

}

double mean(double x[], int size)
{
double sum=0;

for ( int i = 0; i <=size; i++ )
{
sum += x[i];
}
mean2=sum/14;
return mean2;
}

double deviation (double x[], int size)
{
double deviation;
double sum2=0;

for ( int i = 0; i <=size; i++ )
{
sum2 += pow((x[i]-mean2),2);
}
deviation= sqrt(sum2/(size-1));
system ("pause");
return deviation;
}
Last edited on
closed account (1CohAqkS)
can any1 help me in doing this.
closed account (1CohAqkS)
plzzzzzzz help meeee plaese........
closed account (z05DSL3A)
Skills wrote:
im not able to create the array for the marks mentioned above.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

int main()
{
    const int No_of_marks = 14;
    int marks[No_of_marks] = {68, 72, 78, 69, 85, 95, 75, 77, 82, 84, 91, 89, 95, 74};

    for (int i =0; i < No_of_marks; ++i)
    {
        std::cout << "Mark " << i << " = " << marks[i] << std::endl;
    }

    return 0;
}
closed account (1CohAqkS)
now after creating the array how will i b able to code my program?????????????
closed account (z05DSL3A)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>

double mean(int* data_set, int data_count);

int main()
{
    const int No_of_marks = 14;
    int marks[No_of_marks] = {68, 72, 78, 69, 85, 95, 75, 77, 82, 84, 91, 89, 95, 74};

    for (int i =0; i < No_of_marks; ++i)
    {
        std::cout << "Mark " << i << " = " << marks[i] << std::endl;
    }
    std::cout << "The mean is " << mean(marks, No_of_marks) << std::endl;

    return 0;
}

double mean(int* data_set, int data_count)
{
    double value = 0;
    for (int i =0; i < data_count; ++i)
    {
        value += data_set[i];
    }
    value /= data_count;
    return value;
}

You can do the standard deviation
closed account (1CohAqkS)
can any1 help witf standard deviation?????????????????
How about trying it yourself first?
closed account (1CohAqkS)
ok mate........
1
2
3
4
5
6
7
8
9
10
11
12
13
double deviation (double x[], int size)
{
	double deviation;
	double sum2=0;
	
	for ( int i = 0; i <=size; i++ ) //out of bounds
	{
		sum2 += pow((x[i]-mean2),2); //pow is overkill. Also global are evil.
	}
	deviation= sqrt(sum2/(size-1));
	system ("pause"); //why?
	return deviation;
}

What is the problem?
closed account (1CohAqkS)
The problem is that my friend that i dont knw how to put the standard deviation and the mean program given above together to give me answer of mean and standard deviation...............it would be kind enough if u could help me.............................putting it together...........................
closed account (1CohAqkS)
help reh....................
closed account (z05DSL3A)
Your still working on this one? I would strongly suggest that you work through the tutorials
http://www.cplusplus.com/doc/tutorial/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>

double mean(int* data_set, int data_count);
double standard_deviation(int* data_set, int data_count);

int main()
{
    const int No_of_marks = 14;
    int marks[No_of_marks] = {68, 72, 78, 69, 85, 95, 75, 77, 82, 84, 91, 89, 95, 74};

    for (int i = 0; i < No_of_marks; ++i)
    {
        std::cout << "Mark " << i << " = " << marks[i] << std::endl;
    }
    std::cout << "The mean is " << mean(marks, No_of_marks) << std::endl;
    std::cout << "The standard deviation is " << standard_deviation(marks, No_of_marks) << std::endl;

    return 0;
}

double mean(int* data_set, int data_count)
{
    double value = 0;

    for (int i =0; i < data_count; ++i)
    {
        value += data_set[i];
    }
    
    return value / data_count;
}

double standard_deviation(int* data_set, int data_count)
{
    double the_mean = mean(data_set, data_count);
    double num = 0;
    double accumulator  = 0;

    for (int i = 0; i < data_count; ++i)
    {
        num          = (data_set[i] - the_mean);
        accumulator += (num * num);
    }

    return sqrt(accumulator / data_count);
}
closed account (1CohAqkS)
eh blood still cannot compile it yaar.................says sqrt undeclared
closed account (z05DSL3A)
try adding #include <cmath> then.
closed account (1CohAqkS)
but i only get the answer for mean not for standard deviation.......
closed account (1CohAqkS)
okay i finally got it......no worries and thank you..........
closed account (z05DSL3A)
Judging by past form, you have probably put a system ("pause"); in your code...
Pages: 12