c++-arrays need help.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • README
    New Member
    • Nov 2008
    • 9

    c++-arrays need help.

    so what i need to do is implement a code that will take the maximum number in a array and put it into another array. events[] is an array that contains numbers that was extracted from a file. Count is the number of numbers in the array events[ ] because i did not know how many numbers i was reading. numDays is also an array.eventNumD ays is the array for which i need to put the maximun number that numDays [] contains. but this is just not the any regular max number i need to copy. an example is the following:( note that i put dashes so you can see it better. but the number under th events[] is for events[]. and the number under numDays[] is for numDays.

    events[]:contains----------------------numDays[] contains:
    1----------------------------------------------1
    1----------------------------------------------3
    1----------------------------------------------2
    2----------------------------------------------5
    3----------------------------------------------1
    3----------------------------------------------8
    4----------------------------------------------4
    6----------------------------------------------1

    so the maximum number for events[] which contains the number 1 is 3 because 3 is greater that 1 or 2. The maximum number for event[] which contains the number 2 is 5. the maximum number for the event[] which contains the number 3 is 8. and so on. i hope you see what i have to do. maybe looking at my algorithm could help you realize what i need to do. anyway, this is too tricky for me, thus i turned to the c++ community. if there is any question ask.

    for(int j=1;j<events[count-1];j++)
    {

    while(events[i]==j)
    {
    if (numDays[i]<numDays[i+1])
    {
    max=numDays[i+1];

    }
    i++;

    }
    eventNumDays[j]=max;
    sum+=max;
    max=0;
    }
  • boxfish
    Recognized Expert Contributor
    • Mar 2008
    • 469

    #2
    Your post made my head hurt.
    This
    Code:
    if (numDays[i]<numDays[i+1])
    {
    max=numDays[i+1];
    
    }
    seems wrong. How about changing it to
    Code:
    if (numDays[i]>max)
    {
    max=numDays[i];
    
    }
    What happens if there is no max for a particular j?
    By the way, it would be helpful if you used code tags around your code. Put [CODE] before the code and [/CODE] after it, so it shows up in a code box and the indentation isn't wrecked. Thanks.
    Hope this helps.

    Comment

    • README
      New Member
      • Nov 2008
      • 9

      #3
      It did not help. the output just came to be 0,0,0,0,...

      Comment

      • README
        New Member
        • Nov 2008
        • 9

        #4
        help please . stuck on this stupid array algorithm. any c++ speacialist?

        so what i need to do is implement a code that will take the maximum number in a array and put it into another array. events[] is an array that contains numbers that was extracted from a file. Count is the number of numbers in the array events[ ] because i did not know how many numbers i was reading. numDays is also an array.eventNumD ays is the array for which i need to put the maximun number that numDays [] contains. but this is just not the any regular max number i need to copy. an example is the following:( note that i put dashes so you can see it better. but the number under th events[] is for events[]. and the number under numDays[] is for numDays.

        events[]:contains----------------------numDays[] contains:
        1----------------------------------------------1
        1----------------------------------------------3
        1----------------------------------------------2
        2----------------------------------------------5
        3----------------------------------------------1
        3----------------------------------------------8
        4----------------------------------------------4
        6----------------------------------------------1

        so the maximum number for events[] which contains the number 1 is 3 because 3 is greater that 1 or 2. The maximum number for event[] which contains the number 2 is 5. the maximum number for the event[] which contains the number 3 is 8. and so on. i hope you see what i have to do. here is my code. try running it on visual studio, if needed. Just to be clear i need to get the maximum number corresponding the the same event[] number. like event 1 corresponds the numDays[0]= 1, numDays[1]=3,numDays[2] = 2. and so on. this might be unclear but try looking at my code. PLEASE HELP ME . thanks

        #include<iostre am>
        #include<fstrea m>
        using namespace std;
        void menu();
        void NumofDays();
        void Task();
        const int MAX=1000;
        int main(){

        //Create files
        ifstream inputFile("Proj ectInfo_h8.txt" );
        //Check for error opening file
        if(inputFile.fa il())
        { cerr<<"Error opening the file"<<endl;
        exit(1);
        }

        ofstream outputFile("Tim etable.txt");

        //Declare variables and arrays
        int i(0),a=0,max(0) ,count(0), events[MAX]={0}, tasks[MAX],numDays[MAX], eventNumDays[MAX],sum(0);

        while(!inputFil e.eof())
        {
        inputFile>>even ts[i];

        inputFile>>task s[i];
        inputFile>>numD ays[i];
        i++;

        count++;


        }
        for(int j=1;j<events[count-1];j++)
        {

        while(events[a]==j)
        {
        if(numDays[a]>numDays[a+1])
        max= numDays[a];
        else
        max= numDays[a+1];
        a++;

        }
        eventNumDays[j]=max;
        sum+=max;
        max=0;


        }inputFile.clos e();
        }

        Comment

        • boxfish
          Recognized Expert Contributor
          • Mar 2008
          • 469

          #5
          Maybe you should try running it through the debugger to figure it out. Are you initializing i to zero?
          Code:
          eventNumDays[j]=max;
          should be
          Code:
          eventNumDays[j - 1]=max;
          because j starts at 1 but array indexes start at zero. And you did use a greater than sign in
          Code:
          if (numDays[i]>max)
          right?
          I hope you can get this working. Maybe you could post the new code, just so I can be sure you got it right?

          Comment

          • boxfish
            Recognized Expert Contributor
            • Mar 2008
            • 469

            #6
            It's as easy as 1 2 3.
            1:
            Code:
            for(int j=1;j<events[count-1];j++)
            replace the less-than here with a less-than-or-equal-to.
            2:
            Code:
            if(numDays[a]>numDays[a+1])
            max= numDays[a];
            else
            max= numDays[a+1];
            What I said in your other thread. You need
            Code:
            if (numDays[a]>max)
                max = numDays[a];
            If numDays[a] is greater than max, then replace max with numDays[a].
            3:
            Code:
            eventNumDays[j]=max;
            Again, do what I said in your other thread.
            Oh, and please don't double post, for these reasons.
            Thanks.

            Comment

            • README
              New Member
              • Nov 2008
              • 9

              #7
              Originally posted by boxfish
              It's as easy as 1 2 3.
              1:
              Code:
              for(int j=1;j<events[count-1];j++)
              replace the less-than here with a less-than-or-equal-to.
              2:
              Code:
              if(numDays[a]>numDays[a+1])
              max= numDays[a];
              else
              max= numDays[a+1];
              What I said in your other thread. You need
              Code:
              if (numDays[a]>max)
                  max = numDays[a];
              If numDays[a] is greater than max, then replace max with numDays[a].
              3:
              Code:
              eventNumDays[j]=max;
              Again, do what I said in your other thread.
              Oh, and please don't double post, for these reasons.
              Thanks.

              Its still not working :(

              Comment

              • README
                New Member
                • Nov 2008
                • 9

                #8
                Originally posted by oler1s
                Sorry to hear that. Good luck fixing your code. (Did you have a question?)
                yes i do. i need to implement an algorithm, such stated above. please assist me. i went to t tutor but she did not know the solution either. please help me fix my code.

                Comment

                • Banfa
                  Recognized Expert Expert
                  • Feb 2006
                  • 9067

                  #9
                  README,

                  Please do not double post your questions (I have merged them now), please do use &#91;code] ... [/code] tags round the code you post (it makes it easier to read) and please understand that the Experts on this site can not do your work for you. You have to do your own work, but they will be more than willing to help you resolve any problems you have with your code.

                  Please do read our posting guidelines

                  Banfa
                  Administrator

                  Comment

                  • Banfa
                    Recognized Expert Expert
                    • Feb 2006
                    • 9067

                    #10
                    As to your code, it appears you have tried to start writing your code without knowing what algorithm you are using to solve the problem.

                    From this stand point I suggest you try actually solving the problem on paper and noting how you did it, then create a algorithm for the computer that does the sme thing. You can not get the computer to solve a problem that you do not know how to solve.

                    As to you actual code numDays appears to be an array of ints where it would be better as a structure (every 2 entries in the array are related) and better off declared as a vector, list or set (depending on how you want to use it later) rather than an array.

                    Comment

                    Working...