# Thread: picking out specific values from an array

1. ## picking out specific values from an array

hi everyone,
i want to pick up the highest value in an array and then pass these values to a another array.
for example:-
A[6]={1,2,3,4,3.1,2.1,1,2.7,3,4.7,3,2,1}

so if we plot the above array on a graph we get something like a sinewave (considering plotting against time)

so my concern is to collect the highest value from the above vector that is 4 and 4.7 and then want to put these two values into a new array.

thanks a lot

2. Sounds like you just need a nested loop, but it's hard to tell what you're trying to do by the example you posted. Some actual attempt at writing the code would be more useful.

3. int main()
{
float Ibatt[2][9]={{1,2,3,4,5,6},{2.5,3.0,6.5,4.0,3.9,5.2,5.7,8.9,3 .5}};
float values[2][9]={0};
float period[9];

int j=0;

for(int i=0;i<9;i++)
{

if(Ibatt[1][i]<Ibatt[1][i+1])
{

}

else if(Ibatt[1][i]>Ibatt[1][i+1])
{
values[1][j]=Ibatt[1][i];
j++;
}
}

but now the problem is it will display the values 4,3.1,2.1 but my interest is only 4 and the next 4.7. I am getting confused about how to add the nested loops
plz help

4. I'm sure there are many, many other ways of doing this. Here is an example.

#include <stdio.h>

Code:
```int main()
{
float Ibatt[14] = { 1, 2, 3, 4, 3.1, 2.1, 1, 2.7, 3, 4.7, 3, 2, 1 };
float values[2] = {};

int i = 0; int j = 0;

for(i = 0; i < 2; i++) {
for(j = 0; j < 14; j++) {
if((Ibatt[j] > values[0]) && (i == 0)) {
values[0] = Ibatt[j];
}

if((Ibatt[j] > values[1]) && (Ibatt[j] < values[0]) && (i == 1)) {
values[1] = Ibatt[j];
}
}
}

for(j = 0; j < 2; j++) {
printf("j: %f\n", values[j]);
}

return 0;
}```

5. dude u r gr8tttttttttttttttttttttt hats off to you man...a true genius thanks a tonnnnnnnnnnnnnnnnnn....now i will analyse hw it is actually working...God Bless

6. well i have one more question that i just placed an example array and say if i have 'n' numbers of values e.g. 100 then how i can reuse the 'if' statement
for e.g.
int main()
{

float Ibatt[20] = { 1, 2, 2, 4, 2, 2, 1, 2, 2, 1,2.1, 2, 2, 1,2,2,3.5,2,2,1 };
float values[6] = {};

int i = 0; int j = 0;

for(i = 0; i < 6; i++) {
for(j = 0; j < 20; j++) {
if((Ibatt[j] > values[0]) && (i == 0)) {
values[0] = Ibatt[j];
}

if((Ibatt[j] > values[1]) && (Ibatt[j] < values[0]) && (i == 1)) {
values[1] = Ibatt[j];
}
}
}

for(j = 0; j < 6; j++) {
printf("j: %f\n", values[j]);
}
system("pause");
return 0;
}

so according to the requirement it should display three values as 4,2.1 & 3.5 but i get only 4 and 3.5 i know the reason but how can it be changed to show the above three values

the reason i want such a program is i want to detect the rise that is in the above array first the array elements are increasing and then at a particular point (value 4 ) it starts to decrease and then again after few elements it rises and reaches a max point of (2.1) and then again decrease so its like a sin waveform whose every highest point i am trying to extract

sorry for such a long description but i am stuck from morning into this
thanks again

7. I might propose something like this instead:
Code:
```#include <stdio.h>
#include <stddef.h>

size_t identifyPeaks(double points[], size_t num_points, double peaks[]);

int main(void)
{
double points[] = {1, 2, 2, 4, 2, 2, 1, 2, 2, 1, 2.1, 2, 2, 1,2, 2, 3.5, 2, 2, 1};
double peaks[sizeof(points) / sizeof(points[0])];
size_t num_peaks = identifyPeaks(points, sizeof(points) / sizeof(points[0]), peaks);
size_t i;
for (i = 0; i < num_peaks; ++i)
{
printf("%f ", peaks[i]);
}
putchar('\n');
return 0;
}

size_t identifyPeaks(double points[], size_t num_points, double peaks[])
{
size_t num_peaks = 0;
size_t i;
for (i = 1; i < num_points - 1; ++i)
{
if (points[i] > points[i - 1] && points[i] > points[i + 1])
{
peaks[num_peaks++] = points[i];
}
}
return num_peaks;
}```
Basically, my idea is to compare the current element with its neighbours. If it is strictly greater than its immediate neighbours, it is classified as a peak and added to the result array.

Of course, this is rather naive, e.g., if you have two values at a plateau, neither will be identified as a peak. This could be problematic if the input represents a square sine wave.

8. thanks a tonn mam i will try it and will analyse it thanks again thanks a millliiiion