Help with making run program be able to enter decimal numbers

closed account (NqR2y60M)
\
Last edited on
u define n is int( int n) in 2ndline of main.
but 1.0 is float, so error.
closed account (NqR2y60M)
So what would I do next?
You need to replace your line number 7 with float.
float n;

You defined 'n' as an integer and an int can not have decimal spaces. That is why your program was blowing up.
A float can have a decimal point so if you declare 'n' as a float, the compiler knows that it has to accept decimals.
closed account (NqR2y60M)
I have a problem with line 10 now
says "a" was not declared
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
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
   float sum=0, avg=0, var=0, dev=0;
    float n;
    cout << "How many numbers?  ";
    cin >> n;
    int a[n];
    for (int i=0; i<n; i++) {
   		cout << "Enter a number: ";
   		cin >> a[i];
   		sum += a[i];
    }
    avg = sum / n;
    for (int i=0; i<n; i++) {
    var += pow((a[i]-avg),2);  
    }
    var = var/n;
    dev = sqrt(var);
    cout << "You have entered the following" << endl;
    for (int i=0; i<n; i++) {
    	cout << a[i] << endl;  
    }
   cout << endl << "The Sum is: "             << sum;
   cout << endl << "The average is: "          << avg;
   cout << endl << "Variance is: "            << var;
   cout << endl << "Standard Deviation is: "    << dev << endl;
   char c; cin >> c;
   return 0;
}
You can't have an array of float length. I'm pretty sure you can't have an array of variable length anyway unless you're going to use STL containers.
Topic archived. No new replies allowed.