Menu

can you please hellp ( structure related)

2002-12-07
2012-09-26
  • Nobody/Anonymous

    THIS IS THE PROGRAM TO FIND THE MAX AND MIN USING STRUCTURES, CAN YOU EXPLAIN WHAT TYPEDEF STRUCT MEANS AND WHAT OTHER DECLARATION WE COULD DO FOR A STRUCTURE , PLUS THIS CODE {  minmaxst out;
    }AND {nmaxst mmstr = findmm(argc-1, x)}

    #include <iostream.h>
    #include <stdlib.h>
    #include <math.h>

    typedef struct {
        double *array;
        int numberPoints;
        double maxValue;
        double minValue;
    } minmaxst;

    minmaxst findmm(int numpoints, double *inArray)
    {
        minmaxst out;
        out.array = new double[numpoints];
        out.maxValue = inArray[0];
        out.minValue = inArray[0];
        out.numberPoints = numpoints;
        for (int i = 0; i < numpoints; i++)
        {
            out.array[i] = inArray[i]; //load up array
            if (inArray[i] < out.minValue)
                    out.minValue = inArray[i];
            if (inArray[i] > out.maxValue)
                    out.maxValue = inArray[i];
        }
        return out;
    }

    int main(int argc, char *argv[])
    {
        if (argc < 3)
            return 0;
        double *x = new double[argc - 1];
        for (int i = 1; i < argc; i++)
            x[i-1] = atof(argv[i]);
        minmaxst mmstr = findmm(argc-1, x);
        cout << mmstr.maxValue << "  " << mmstr.minValue << endl;
        delete x;
        delete mmstr.array;
        system ("pause");
        return 0;
    }

     
    • Nobody/Anonymous

      it looks like that program simply sorts the contents of an array, and minmaxst is a buffer, the findmm is the sorting subroutine and the for loop is the sort.

       
    • Nobody/Anonymous

      oh, forgot to answer your question, typedef structure tells the compiler to declare a structure named minmaxst, minmaxst out means to create a minmaxst variable named out,  minmaxst mmstr = findmm(argc-1, x) means run the subroutine findmm, with the variables argc - 1, and x, then return the minmaxst

       
    • Nobody/Anonymous

      thank you very very very much, that really helped me a lot, and thanks for the fast reply

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.