picking out specific values from an array

This is a discussion on picking out specific values from an array within the C Programming forums, part of the General Programming Boards category; hi everyone, i want to pick up the highest value in an array and then pass these values to a ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    5

    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.

    please help me in getting this.
    thanks a lot

  2. #2
    Registered User
    Join Date
    Oct 2010
    Location
    USA
    Posts
    5
    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. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    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. #4
    Registered User
    Join Date
    Oct 2010
    Location
    USA
    Posts
    5
    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. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    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. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    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. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,820
    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.
    Last edited by laserlight; 10-13-2010 at 11:14 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    thanks a tonn mam i will try it and will analyse it thanks again thanks a millliiiion

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 09-23-2010, 11:49 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. accessing array of structs values
    By WaterNut in forum C++ Programming
    Replies: 12
    Last Post: 07-08-2004, 08:47 PM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 12:01 AM
  5. getline to store values in an array
    By berenice in forum C++ Programming
    Replies: 6
    Last Post: 07-10-2002, 10:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21