473,406 Members | 2,220 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

c++-arrays need help.

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
9 1516
boxfish
469 Expert 256MB
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
README
9
It did not help. the output just came to be 0,0,0,0,...
Nov 16 '08 #3
README
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
469 Expert 256MB
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
469 Expert 256MB
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
README
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
README
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
9,065 Expert Mod 8TB
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
9,065 Expert Mod 8TB
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

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

Similar topics

0
by: Gregory Nans | last post by:
hello, i need some help to 'tree-ify' a string... for example i have strings such as : s = """A(here 's , B(A ) silly test) C(to show D(what kind) of stuff i need))""" and i need to...
0
by: xunling | last post by:
i have a question about answering ..... this topic is "need help" what do i have to write at te topic line, !after i have klicked the "answer message" button ive tried many possibilities,...
2
by: Paul Mendez | last post by:
I really need some help Date Code ConCAT Bal_Fwd NS_Fees Amt_Coll Cur_End_Bal 1/15/2004 KW 11KW2003 $500.00 $250.00 $250.00 2/15/2004 KW 12KW2003 $300.00 $500.00 ...
7
by: Timothy Shih | last post by:
Hi, I am trying to figure out how to use unmanaged code using P/Invoke. I wrote a simple function which takes in 2 buffers (one a byte buffer, one a char buffer) and copies the contents of the byte...
8
by: Elliot M. Rodriguez | last post by:
I am having a heckuva time debugging this, or determining why my page is behaving this way. I have a search form, that when completed, returns a datagrid. When the user selects a row (normal...
3
by: Rich Squid | last post by:
Hello Here's my basic problem: On my asp.net form page I have a DetailsView (default mode=edit) bound to a AccessDataSource control. Users can successfuly update a databound template field,...
2
by: XML Beginner | last post by:
I have an XML file that contains values that my application needs, so it knows which database to connect to. It also contains a configuration option so that I can specify which node to return...
2
by: =?Utf-8?B?am9lb2ppaA==?= | last post by:
Most students do not have enough time to do their homeworks, assignments, college papers, etc alone. Also most of them, even though they know what to do with respect to their writing assignment,...
30
by: carlos123 | last post by:
Ok I am working on a Hall Pass program for my computer programming class. There are 3 things that I am confused on. 1. Reading a file. 2. Taking that data read from the file and putting it into...
1
by: jhaydon | last post by:
First of all, I'm not a CSS expert. If I was, I wouldn't need to be posting for help here. Secondly, I have been doing web design for several years, just not css. Thirdly, I need help and hope...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.