C++ Array average not working right -
Then im using C ++ and I can not understand what I'm doing. Number of numbers in the array and the number of numbers in the array were submerged.
I think my problem is in the array but I am not sure because I am getting it as an average result.
then I input this
2 3 4 5 6 output
average number The correct average of numbers is 4, so it is not to ensure that where my mistake is, any help or hint will be very helpful. Here I have my average line Which I have seen here on the stack overflow but there is no luck yet. #include & lt; Iostream & gt; # Include & lt; iomanip & gt; using namespace std; Zero main () {// variable double average; Double zodiac = 0; Int numabove = 0; Int i, array [5]; Int n = 5; // Sir seat & lt; & Lt; "Input 5 value .." & lt; & Lt; Endl; (I = 0; i & lt; 5; ++ i) cin & gt; & Gt; Array [5]; For (int i = 0; i <5; i ++) average average = ((flat (array [i]) / n)); Cout & lt; & Lt; "Average number is" & lt; & Lt; Avg & lt; & Lt; "\ N" & lt; & Lt; Endl; // loop // I am doing the average work after i will loop because // i need average to work, so i can use the loop which is cout below & lt; & Lt; "There are more than average numbers:" & lt; & Lt; Recumbent & lt; & Lt; Endl; System ("pause"); }
You read all the numbers
Cin & gt; & Gt; Array [5]; Which does not exist and produces undefined behavior , correct it: for (i = 0; I & lt; 5; ++ i) cin & gt; & Gt; Array [i]; And then the average correct count:
double average = 0; For (int i = 0; i std :: vector which is the C ++ dynamic array and it is flexible and easy to use. In this hand you can write: std :: vector & lt; Int & gt; V (5); For (i = 0; i <5; ++ i) std :: cin & gt; & Gt; V [i]; Double zodiac = std :: stored (v.begin (), v.end (), 0.0); Double meaning = sum / v.size ();
Comments
Post a Comment