Homework - Array/Functions

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2008
Posts: 17
Reputation: StainlessSteelR is an unknown quantity at this point 
Solved Threads: 0
StainlessSteelR StainlessSteelR is offline Offline
Newbie Poster

Homework - Array/Functions

 
0
  #1
Nov 16th, 2008
I'm having some problems with a program that invovles arrays. I have a program that is suppose to read data from a file fish.txt about fisherman. The a function is suppose to print the number of fish that need to be thrown back because their is a maximum of 14 fish allowed. Then finally I need a function that is suppose to print the fisherman number with the most fish and how many they caught. Something is wrong with the math for the number of maximum fish and a problem with the fisherman who caught the most it goes through each one which and doesn't display their array number.
Any help would be appreciate it. Thank you.

  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4.  
  5. void getData (int[]);
  6. void throwback (int [], int);
  7. void findMax (int[], int);
  8.  
  9. int main()
  10. {
  11. const int NUM_FISH=20;
  12. int fish[NUM_FISH];
  13. int badfish;
  14. int max;
  15.  
  16. // print fish data
  17. cout << "Fish Story Data\n"
  18. << "---------------\n";
  19. for (int i=0; i < NUM_FISH; i++ )
  20. {
  21. cout << setw(2) << i << setw(6) << fish[i] << endl;
  22. }
  23. // read fish data
  24.  
  25. getData (fish);
  26. throwback (fish, badfish);
  27. findMax (fish, max);
  28.  
  29.  
  30. return 0;
  31. }
  32.  
  33. void getData (int fish [])
  34. {
  35. int NUM_FISH = 20;
  36. for (int i=0; i < NUM_FISH; i++ )
  37. {
  38. // cout << "enter number of fish caught by person: " << i << " ";
  39. cin >> fish[i];
  40. }
  41. }
  42.  
  43. void throwback (int fish [],int badfish)
  44. {
  45. int goodfish = 14;
  46. int NUM_FISH = 20;
  47. for (int i=0; i < NUM_FISH; i++ )
  48. {
  49. if(fish[i]>goodfish)
  50. badfish=fish[i]-goodfish;
  51. }
  52. }
  53.  
  54. void findMax (int fish [], int max)
  55. {
  56. int NUM_FISH = 20;
  57. max = 0;
  58. for (int i=0; i<NUM_FISH; i++)
  59. {
  60. if (fish[i]>max)
  61. max=fish[i];
  62. cout<<"The maxmum number of fish caught was " <<max <<"by "
  63. <<fish[i]<<endl;
  64. }
  65. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 15
Reputation: james_zheng is an unknown quantity at this point 
Solved Threads: 0
james_zheng james_zheng is offline Offline
Newbie Poster

Re: Homework - Array/Functions

 
0
  #2
Nov 16th, 2008
int max_id = -1;
for (int i=0; i<NUM_FISH; i++)
{
if (fish[i]>max) {
max=fish[i];
max_id = i;
}

}
cout<<"The maxmum number of fish caught was " <<max <<"by "
<<max_id<<endl;
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 17
Reputation: StainlessSteelR is an unknown quantity at this point 
Solved Threads: 0
StainlessSteelR StainlessSteelR is offline Offline
Newbie Poster

Re: Homework - Array/Functions

 
0
  #3
Nov 16th, 2008
I need a function and that code produced alot of errors when I tried to make it a function or add it into the main.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 15
Reputation: james_zheng is an unknown quantity at this point 
Solved Threads: 0
james_zheng james_zheng is offline Offline
Newbie Poster

Re: Homework - Array/Functions

 
0
  #4
Nov 16th, 2008
just simply modify your findMax function to the code i gave you.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 17
Reputation: StainlessSteelR is an unknown quantity at this point 
Solved Threads: 0
StainlessSteelR StainlessSteelR is offline Offline
Newbie Poster

Re: Homework - Array/Functions

 
0
  #5
Nov 16th, 2008
Okay I got it to work halfway. I had to put the cout statement inside the last } because of an "error: expected constructor, destructor, or type conversion before '<<' token". Don't know what that means....
Theirs just one problem with the findMax function the max_id= i; produces the last array number not the array number that corresponds with the maximum number of fish. That doesn't max since to me because it shouldn't enter the for loop correct?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,819
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Homework - Array/Functions

 
0
  #6
Nov 16th, 2008
Originally Posted by StainlessSteelR View Post
Okay I got it to work halfway. I had to put the cout statement inside the last } because of an "error: expected constructor, destructor, or type conversion before '<<' token". Don't know what that means....
Theirs just one problem with the findMax function the max_id= i; produces the last array number not the array number that corresponds with the maximum number of fish. That doesn't max since to me because it shouldn't enter the for loop correct?
Post your updated code.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC