decimal number

Write your question here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  float c;
            float total;
            float average;
            float number[1000];
            cout<<"Number of units?";
            cin>>c;
            for(int i=0;i<c;i++)
            {
                cout<<"Enter number:";
                cin>>number[i];
            }
            total=0;
            for(int i=0;i<c;i++)
            {
                total=total+number[i];
            }
            average=total/c;
            cout<<"Average="<<average;

guys any ideas why doesnt it write decimal numbers?
What do you mean by "does not write decimal numbers"?
Why input a float for a loop condition? Also the second loop is useless just initialize total to 0 and each time you input a value add it to total. Btw maybe try std::setprecision
The first part ot the code is for summary . I thought it is enough to divide by the number of units cause the summary has worked.BTW MiiNiPaa I meant result is not in decimal number.
i have tried setprecision but it didnt help.
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
#include <iostream>
#include <vector>
#include <cmath>
#include <iomanip>

using namespace std;

int main() {
  cout << "Number of elements:";
  unsigned elements = 0;
  cin >> elements;

  double values[elements];
  double total = 0.0;
  for (unsigned i = 0; i < elements; ++i) {
    cout << "Enter element " << i << ":";
    cin >> values[i];
    total += values[i];
  }

  cout << "Total: " << total << endl;
  cout << "Average: " << total/elements << endl;

  return 0;
}



Number of elements:5
Enter element 0:1.1
Enter element 1:2.2
Enter element 2:3.3
Enter element 3:4.4
Enter element 4:5.9
Total: 16.9
Average: 3.38
I meant result is not in decimal number.
What is it the? Binary? Hex? Show example of output.
Oh man. mine worked to lol but thx . ive just recognized that (1+2+3+4+5)/5=3 :D . It is a shame :D if there is a moderator please delete this topic.
Last edited on
Topic archived. No new replies allowed.