Help!

This is a discussion on Help! within the C++ Programming forums, part of the General Programming Boards category; Code: void ANOMALY(string STARS[], string STARSIN[], float HIGH[], float LOW[], float SIGNAL[], int ANOMALIES[]) { int temp; for(int i = ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    6

    Help!

    Code:
    void ANOMALY(string STARS[], string STARSIN[], float HIGH[], float LOW[], float SIGNAL[], int ANOMALIES[])
    {
    int temp;
    for(int i = 0; i < MAX2; i++)
    {
    temp = SEARCH(STARS, STARSIN, i);
    if(SIGNAL[i] > LOW[temp] || SIGNAL[i] > HIGH[temp])
    {
    ANOMALIES[temp]++;
    }
    i++;
    }
    }

    is supposed to call this function

    Code:
    int SEARCH(string STARS[], string STARSIN[], int position)
    {
    int ct;
    bool flag = true;
    
    while(flag && ct < MAX)
    {
    if(STARS[ct] == STARSIN[position])
    {
    flag = false;
    }
    else
    {
    cout << STARS[ct] << endl;
    cout << STARSIN[position] << endl;
    ct++;
    }
    }
    
    if(flag)
    {
    cout << "Error star " << STARSIN[position];
    cout << " does not have any normals." << endl;
    }
    
    return ct;
    }
    I tried to run a stove or whatever and I got

    Alpha Centauri
    Betelgeuse
    Polaris
    Betelgeuse
    Deneb
    Betelgeuse
    Altair
    Betelgeuse
    Betelgeuse
    Altair
    Error star Altair does not have any normals.
    Error star Alpha Centauri does not have any normals.
    Error star Deneb does not have any normals.
    Error star Polaris does not have any normals.
    Error star Deneb does not have any normals.

    now all these stars do have normals and all my arrays work and have what they need in them, also the first function isn't recalling the second function it stops for some reason.

    someone help please I need it desparately
    AIM: azazelasends

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    variable ct in SEARCH() is not initialized, so initialize it to zero when you declare it (assuming that where you want to compare STARS[])

    Code:
    int ct = 0;
    also try foratting your code so that it is easier to read and therfore debugg. Also I would recomment not making your functions or variables all CAPS. By convention only globals and constants should be written in CAPS.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    6
    ok thanks for your help but still something is a miss

    I'm getting

    1
    1
    1
    1
    0

    in my anomaly array but with the values my array has now I should get

    0
    4
    2
    2
    1

  4. #4
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    well SAME THING...temp in the first function is not initiallized...so you are probably going out of bounds in that array. I didn't catch it thte first time but you should have after I pointed ct to you.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  5. #5
    Registered User
    Join Date
    Dec 2003
    Posts
    6
    No it's getting assigned a value by the function before it's being used but, I went ahead and intialized it and it didn't help but I changed my if statement around and now it's

    0
    1
    2
    1
    1

    =/ so close

  6. #6
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    how can temp be initialized beofre if it is declared in ANOMALY()? secondly, I can't really see what you are trying to do without the rest of the code. Thirdly, only one element of the anomaly array will be increase the way your function is set up, as temp doesn't change value, it is always zero (if you initialized it in the new code to zero).

    why are you incrementing i by one at the end of the for loop? is there a reason for this?

    Post the revise code, describe what you want to achieve and you should get more responses....this is about all that I can do from that snippet.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  7. #7
    Registered User
    Join Date
    Dec 2003
    Posts
    6
    Code:
    #include <iostream>
    #include <iomanip>
    #include <cstdlib>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    void normals(float low[], float high[], string stars[], ifstream& input1);
    void reading(float signal[], string stars[], string starsin[], ifstream& input2);
    int search(string stars[], string starsin[], int position);
    void anomaly(string stars[], string starsin[], float high[], float low[], float signal[], int anomalies[]);
    
    const int MAX = 5;
    const int MAX2 = 11;
    
    int main()
    {
        float low[MAX], high[MAX];
        string stars[MAX], starsin[MAX2];
        float signal[MAX2];
        ifstream input1, input2;
        int anomalies[MAX] = {0};
            
        normals(low, high, stars, input1);
        reading(signal, stars, starsin, input2);
        anomaly(stars, starsin, high, low, signal, anomalies);
        
        
        cout << "Star Name:" << setw(20);
        cout << "Low Bound:" << setw(20);
        cout << " High Bound:" << endl; 
        
        for(int i = 0; i < MAX; i++)
        {
            cout << stars[i] << setw(13 + stars[0].length() - stars[i].length());
            cout << low[i] << setw(13 + stars[0].length() - stars[1].length());
            cout << high[i] << endl;
        }
        
        for(int i = 0; i < MAX; i++)
        {
            cout << anomalies[i] << endl;
        }
        
        system("PAUSE");
        return 0;
    }
    
    void anomaly(string stars[], string starsin[], float high[], float low[], float signal[], int anomalies[])
    {
        int temp = 0;
        for(int i = 0; i < MAX2; i++)
        {
            temp = search(stars, starsin, i);
            if(signal[i] < low[temp] || signal[i] > high[temp])
            {
                anomalies[temp]++;
            }
            i++;
        }
        
    }
    
    int search(string stars[], string starsin[], int position)
    {
        int ct = 0;
        bool flag = true;
        
        while(flag && ct < MAX)
        {
            if(stars[ct] == starsin[position])
            {
                flag = false;
            }
            else
            {
                cout << stars[ct] << endl;
                cout << starsin[position] << endl;
                ct++;
            }    
        } 
        
        if(flag)
        {        
            cout << "Error star " << starsin[position];
            cout << " does not have any normals." << endl;
        }
        
        return ct;
    }
    
    void reading(float signal[], string stars[], string starsin[], ifstream& input2)
    {
        bool flag;
        
        input2.open("file2");
        
        for(int i = 0; i < MAX2; i++)
        {
            getline(input2, starsin[i]);
            input2 >> signal[i];
            getline(input2, starsin[i+1]);
        }
    }
    
    void normals(float low[], float high[], string stars[], ifstream& input1)
    {
        input1.open("file1");
        
        for(int i = 0; i < MAX; i++)
        {
            getline(input1, stars[i]);
            input1 >> low[i] >> high[i];
            getline(input1, stars[i+1]);
        }
        
        input1.close();
    }
    Ok well It reads in

    Alpha Centauri
    15.0
    140
    Polaris
    17.23
    19.909
    Deneb
    18.0
    25.00
    Altair
    0
    15
    Betelgeuse
    15.5
    100

    as normals and

    Betelgeuse
    15.0
    Altair
    19.999
    Altair
    25.00
    Altair
    0
    Alpha Centauri
    15.5
    Polaris
    259.0
    Deneb
    15.5
    Polaris
    250.3
    Polaris
    250.6
    Polaris
    250.5
    Deneb
    55.97

    as the signals I'm trying to count the number of anomalies and which is any value outside of the normals

    please don't post any large changes if it's possible but if need be I will consider it

  8. #8
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Originally posted by axon
    why are you incrementing i by one at the end of the for loop? is there a reason for this?
    It looks like axon already pointed out the one small change necessary to get the results you expect (its in anomaly()).

    In addition to that, I'd recommend another small change. You are potentially modifying data past the bounds of your stars and starsin arrays inside of normal() and reading(). Since you are ignoring whatever is read in during the second getline call, why not use a temp variable. By using the i+1, on the last time through the loop you are modifying the position past the end of the array. On my compiler that causes a crash.

  9. #9
    Registered User
    Join Date
    Dec 2003
    Posts
    6
    I got thanks axon and jlou thanks, stupid oversight from when I changed it from a while to a for loop =/

Popular pages Recent additions subscribe to a feed

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