this code is running but not giving me the correct value of standard deviation and mode where might i have gone wrong?

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#pragma once
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
using namespace std;

int main()
{
	//declare an array
	float numbers [10]; 
	double sum;
	double mean;
	double standardDeviation;
	double sdresult;
	double mode;
	float AA;
	//storing 30 numbers entered by user in an array
	cout<<"enter thirty floating point numbers";

	//compute sum, mean
	for (int i = 0; i < 10; i++) 
	{
        cin >> numbers[10];
        sum += numbers[10];
		mean = sum /10;
		AA += pow(numbers[10] - mean,2);
		//standardDeviation += pow(numbers[i] - mean, 2);
		

    }
	sdresult = sqrt(AA / 10);
	cout<<"sum="<<sum <<endl;
	cout<<"mean="<<mean <<endl;
    cout<<"standard Deviation = " <<sdresult;

	//compute mode
	double count = 1;
	double countMode = 1;
	for (int i=1; i<30; ++i)
	{
		if(numbers[30]== i)
		{//count occurrence of the current number
			++count;
		}//end if
		else
		{//now this is a different number
			if(count>countMode);
			{
				countMode = count; //mode is the highest occurrence
				mode = i;
			}//end if
			count = 1; //reset count for the new number
			i = numbers[30];
		}//end else
	}
	cout<<"mode="<<mode <<endl;
	
	return 0;
}
Last edited on
1
2
 cin >> numbers[10];
        sum += numbers[10];

This is wrong. There's no such array element as numbers[10]

if(numbers[30]== i)
Similarly wrong.

so what do i use? although it is computing the sum using the above formula, the issue is with standard deviation and mode
So line 29 looks better than line 28.

Since you do not use any array functionality just remove everything with squared brackets.
1
2
3
4
5
6
	float numbers [10];
... 
 cin >> numbers[10];
        sum += numbers[10];

...
Looks like your'e reaching an uninitialized arg on numbers[10].
Watch out as this can lead into a lot of errors if you don't recognize it as fast as possible.
If detecting those kind of bugs becomes a hard job there are program that can help you do that. I tend to use checkmarx sometimes but there are more. Anyway, it's recommended to learn how to detect those also by yourself.
Good luck!
thanx alot that helps
but how can i find the mode, this formula doesn't seem to give me my mode although the numbers entered are repeated
as for the standard deviation i have tried [i] as well as [10] none of them give me the answer that i get while doing the manual std dev

You will probably need a vector or an array if you want to compute the mode. But first you probably need to study up on how to use arrays.

http://www.cplusplus.com/doc/tutorial/arrays/

Edit: You'll may also want to sort the array to make finding the mode/s easier.


Last edited on
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
#pragma once
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
using namespace std;

int main()
{
	//declare an array
	float numbers [10]; 
	double sum;
	double mean;
	double standardDeviation;
	double AA;
	double sdresult;
	double mode;
	double max;
	double modeposition;

	//storing 30 numbers entered by user in an array
	cout<<"enter thirty floating point numbers"<<endl;

	//compute sum, mean and standard deviation
	for (int i = 0; i < 10; ++i) 
	{
        cin >> numbers[i];
        sum += numbers[i];
		mean = sum /10;
		AA += pow(numbers[i] - mean,2);
		max= numbers[i];
		modeposition = i;
    }
	sdresult = sqrt(AA/10);
	cout<<"sum="<<sum <<endl;
	cout<<"mean="<<mean <<endl;
    cout<<"standard Deviation = " <<sdresult<<endl;
	cout<<"mode="<<modeposition<<endl;

	return 0;
}
Last edited on
i have now managed to get here but now it gives me one error on line 36 saying subscript is not if integral type
i am so sorry i am very new to this and got to submit this today but i am honestly stuck
your assistance will be highly appreciated... thank you
now it is running but once i enter the values in the console for output, the console just disappears once i have clicked entered after the last value
Topic archived. No new replies allowed.