-4

I have a problem with C++ and I dont know how to start this code since I am new to this.

My problem is I have a BUNCH of arrays and the one below is ONE example. From the bunch of arrays I have to find all the numbers present from a range of 0.0 to 0.5 Once the code has found all the numbers it has found and add them all together and divide it by the amount of numbers there are; in order to find the average.

I was hoping someone would be able to tell me how to search an array for numbers between the specified range and then find the average of those numbers.

float array1[] = { 0.141573001, 0.129732453, 1.689353116, 1.445072308, 
                   1.767702844, 1.967608838, 0.792822868, 1.836018189, 
                   0.521325809, 1.242620743, 0.30556143 , 1.45634    ,
                   1.242620743, 0.30556143 , 1.45634    , 1.340469519,
                   0.02216116 , 0.030461417, 1.420794672, 0.700459128,            
                   0.959538479, 0.716117771, 1.612446026
                 };
Neb
  • 2,270
  • 1
  • 12
  • 22
salvax18
  • 3
  • 3
  • 2
    You'll have to look at all numbers in the array using a loop. Within the loop, check if each value is within the range. – J...S Sep 09 '17 at 07:47
  • is it possible that I could see an example? because I am new to coding and dont really understand what a loop is – salvax18 Sep 09 '17 at 07:51
  • http://www.geeksforgeeks.org/loops-in-c/ It's for C but the basic things are same for C and C++ – J...S Sep 09 '17 at 07:53
  • If you don't know what a loop is, we can [recommend some beginners books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Bo Persson Sep 09 '17 at 09:14
  • Thank you, its gonna be a hella of a read but thanks – salvax18 Sep 09 '17 at 09:16

1 Answers1

0

Iterate over array, on each iteration check if element is in range (via 0.0 <= element && element <= 0.5), if so increment counter and add element to sum:

float array[] = { 0.141573001, 0.129732453, 1.689353116, 1.445072308, 1.767702844, 1.967608838, 0.792822868, 1.836018189, 0.521325809, 1.242620743, 0.30556143, 1.45634, 1.340469519, 0.02216116, 0.030461417, 1.420794672, 0.700459128, 0.959538479, 0.716117771, 1.612446026};

size_t count = 0;
double sum = 0;
for (float element : array) {
    if (0.0 <= element && element <= 0.5) {
        ++count;
        sum += element;
    }
}
double average = sum / count;

Ideone

diralik
  • 6,391
  • 3
  • 28
  • 52
  • Thank you so much, this solved most of my problem but it leaves me with one question, Is it possible to implement the code above to search through 20 arrays and then find the average with the given range ? – salvax18 Sep 09 '17 at 08:30
  • Yes, it is possible. Clarify, please, you have 20 arrays and you need 20 averages (one for each array) or just one average? And how do you define your 20 arrays (`float arrays[][]`)? – diralik Sep 09 '17 at 08:32
  • I have 20 arrays and I need to find one average whose range is from 0.0 to 0.5 and my arrays are defined like so float array1[ ] all the way to float array20 [ ]. Basically Im asking how to do I go through all the arrays and find the average of numbers between 0.0 to 0.5 Also I really appreciate the help – salvax18 Sep 09 '17 at 08:38
  • Why you have so bad structure? I think you should use `vector>` to get things much more simplier. – diralik Sep 09 '17 at 08:42
  • Its for an assignment and for some reason I need all those 20 arrays. Not even sure why. So if i use vector> into the code it'll be able to go through all the arrays and find the average ? Also I do apologise for the questions I ask, I am really new to this kind of stuff – salvax18 Sep 09 '17 at 08:49
  • @salvax18, getting 20 arrays is imho totaly bad idea. If you have `vector>`, then iterate over them is very simple: `for (vector &array : arrays) for (float element : array) { // same code as in my answere }`. – diralik Sep 09 '17 at 10:00
  • Like so ? for (vector &array1 : array20) for (float element : array1) { if (0.0 <= element && element <= 0.5) { ++count; sum += element; } } – salvax18 Sep 09 '17 at 11:25
  • @salvax18, yes. You can define `array20` like so: `vector> array20 = {{/*first array numbers*/}, {/*second array numbers/*}, ..., {/* 20th arrays numbers */}}` – diralik Sep 09 '17 at 11:26
  • I keep on getting errors with the addition of (vector &array1 : array20) – salvax18 Sep 09 '17 at 11:27
  • Error (active) E0434 a reference of type "std::vector> &" (not const-qualified) cannot be initialized with a value of type "float" <---That keeps popping up, Is there something I have to include into the code? – salvax18 Sep 09 '17 at 11:29
  • Thank you for the example, I pasted the code into visual studio and placed the first 3 of my arrays into the vector> arrays = { } and I ended up getting an error stating that Error C2398 Element '1' all the way to Element '20': conversion from 'double' to 'float' requires a narrowing conversion – salvax18 Sep 09 '17 at 11:47
  • Is this because my arrays have numbers like 0.01213231 ? also I do apologise for the inconvenience – salvax18 Sep 09 '17 at 11:48
  • In C++ numbers like `0.5` are `double`'s and numbers like `0.5f` are floats. So you can use `vector>` instead of `vector>` or add `f` suffix to your numbers. – diralik Sep 09 '17 at 11:50
  • 1
    Thank you for teaching me and also showing me how its done. I appreciate it very much. – salvax18 Sep 09 '17 at 12:57