454,952 Members | 1,404 Online
Need help? Post your question and get tips & solutions from a community of 454,952 IT Pros & Developers. It's quick & easy.

# c++-arrays need help.

 P: 9 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.eventNumDays 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
9 Replies

 Expert 100+ P: 469 Your post made my head hurt. This Expand|Select|Wrap|Line Numbers 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. Nov 14 '08 #2

 P: 9 It did not help. the output just came to be 0,0,0,0,... Nov 16 '08 #3

 P: 9 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.eventNumDays 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 #include using namespace std; void menu(); void NumofDays(); void Task(); const int MAX=1000; int main(){ //Create files ifstream inputFile("ProjectInfo_h8.txt"); //Check for error opening file if(inputFile.fail()) { cerr<<"Error opening the file"<>events[i]; inputFile>>tasks[i]; inputFile>>numDays[i]; i++; count++; } for(int j=1;jnumDays[a+1]) max= numDays[a]; else max= numDays[a+1]; a++; } eventNumDays[j]=max; sum+=max; max=0; }inputFile.close(); } Nov 16 '08 #4

 Expert 100+ P: 469 Maybe you should try running it through the debugger to figure it out. Are you initializing i to zero? Expand|Select|Wrap|Line Numbers eventNumDays[j]=max; should be Expand|Select|Wrap|Line Numbers eventNumDays[j - 1]=max; because j starts at 1 but array indexes start at zero. And you did use a greater than sign in Expand|Select|Wrap|Line Numbers 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? Nov 16 '08 #5

 Expert 100+ P: 469 It's as easy as 1 2 3. 1: Expand|Select|Wrap|Line Numbers for(int j=1;jnumDays[a+1]) max= numDays[a]; else max= numDays[a+1]; What I said in your other thread. You need Expand|Select|Wrap|Line Numbers if (numDays[a]>max)     max = numDays[a]; If numDays[a] is greater than max, then replace max with numDays[a]. 3: Expand|Select|Wrap|Line Numbers eventNumDays[j]=max; Again, do what I said in your other thread. Oh, and please don't double post, for these reasons. Thanks. Nov 16 '08 #6

 P: 9 It's as easy as 1 2 3. 1: Expand|Select|Wrap|Line Numbers for(int j=1;jnumDays[a+1]) max= numDays[a]; else max= numDays[a+1]; What I said in your other thread. You need Expand|Select|Wrap|Line Numbers if (numDays[a]>max)     max = numDays[a]; If numDays[a] is greater than max, then replace max with numDays[a]. 3: Expand|Select|Wrap|Line Numbers 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 :( Nov 16 '08 #7

 P: 9 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. Nov 16 '08 #8