By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,952 Members | 1,404 Online
Bytes IT Community
+ Ask a Question
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<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;
}
Nov 14 '08 #1
Share this Question
Share on Google+
9 Replies


boxfish
Expert 100+
P: 469
Your post made my head hurt.
This
Expand|Select|Wrap|Line Numbers
  1. if (numDays[i]<numDays[i+1])
  2. {
  3. max=numDays[i+1];
  4.  
  5. }
seems wrong. How about changing it to
Expand|Select|Wrap|Line Numbers
  1. if (numDays[i]>max)
  2. {
  3. max=numDays[i];
  4.  
  5. }
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<iostream>
#include<fstream>
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"<<endl;
exit(1);
}

ofstream outputFile("Timetable.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(!inputFile.eof())
{
inputFile>>events[i];

inputFile>>tasks[i];
inputFile>>numDays[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.close();
}
Nov 16 '08 #4

boxfish
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
  1. eventNumDays[j]=max;
should be
Expand|Select|Wrap|Line Numbers
  1. 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
  1. 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

boxfish
Expert 100+
P: 469
It's as easy as 1 2 3.
1:
Expand|Select|Wrap|Line Numbers
  1. for(int j=1;j<events[count-1];j++)
replace the less-than here with a less-than-or-equal-to.
2:
Expand|Select|Wrap|Line Numbers
  1. if(numDays[a]>numDays[a+1])
  2. max= numDays[a];
  3. else
  4. max= numDays[a+1];
What I said in your other thread. You need
Expand|Select|Wrap|Line Numbers
  1. if (numDays[a]>max)
  2.     max = numDays[a];
If numDays[a] is greater than max, then replace max with numDays[a].
3:
Expand|Select|Wrap|Line Numbers
  1. 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
  1. for(int j=1;j<events[count-1];j++)
replace the less-than here with a less-than-or-equal-to.
2:
Expand|Select|Wrap|Line Numbers
  1. if(numDays[a]>numDays[a+1])
  2. max= numDays[a];
  3. else
  4. max= numDays[a+1];
What I said in your other thread. You need
Expand|Select|Wrap|Line Numbers
  1. if (numDays[a]>max)
  2.     max = numDays[a];
If numDays[a] is greater than max, then replace max with numDays[a].
3:
Expand|Select|Wrap|Line Numbers
  1. 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

Banfa
Expert Mod 5K+
P: 8,916
README,

Please do not double post your questions (I have merged them now), please do use [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
Nov 16 '08 #9

Banfa
Expert Mod 5K+
P: 8,916
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.
Nov 16 '08 #10

Post your reply

Sign in to post your reply or Sign up for a free account.