943,516 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 523
  • C++ RSS
Nov 16th, 2008
0

Homework - Array/Functions

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
StainlessSteelR is offline Offline
17 posts
since Oct 2008
Nov 16th, 2008
0

Re: Homework - Array/Functions

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;
Reputation Points: 10
Solved Threads: 0
Newbie Poster
james_zheng is offline Offline
15 posts
since Nov 2008
Nov 16th, 2008
0

Re: Homework - Array/Functions

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
StainlessSteelR is offline Offline
17 posts
since Oct 2008
Nov 16th, 2008
0

Re: Homework - Array/Functions

just simply modify your findMax function to the code i gave you.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
james_zheng is offline Offline
15 posts
since Nov 2008
Nov 16th, 2008
0

Re: Homework - Array/Functions

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
StainlessSteelR is offline Offline
17 posts
since Oct 2008
Nov 16th, 2008
0

Re: Homework - Array/Functions

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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,371 posts
since Jan 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Generate 6 Random Numbers
Next Thread in C++ Forum Timeline: Real time clock





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC