944,129 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3864
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 2nd, 2006
0

Homework Help.

Expand Post »
Hi everyone and thanks for looking to help me. I am working with a program involving arrays. I needed to write one that involves the use to
have the program take
about 10 numbers, list the minimum, the maximum, and the average.
Now the program that I have below runs fine which finds the average. But when I try to loop the program to find the minimum and maximum numbers it just repeats.
How do I loop in a loop to find the minimum and maximum nubers.
Please let me know. TY

cpp Syntax (Toggle Plain Text)
  1. #include
  2. using namespace std;
  3.  
  4.  
  5. const int arraymax = 10;
  6.  
  7. typedef int arraytype[arraymax];
  8.  
  9.  
  10.  
  11. float findaverage(arraytype numarray, int count);
  12.  
  13. void array2(arraytype numarray, int & count);
  14.  
  15.  
  16. int main(void)
  17. {
  18. int array1;
  19. arraytype array3;
  20. float avg;
  21.  
  22. array2(array3, array1);
  23. avg = findaverage(array3, array1);
  24. cout << endl << "The average is: " << avg << endl << endl;
  25.  
  26. return 0;
  27. }
  28.  
  29.  
  30. void array2(arraytype numarray, int & count)
  31. {
  32. int k;
  33.  
  34. cout << "How many numbers would you like to enter? ";
  35. cin >> count;
  36.  
  37. for (k = 0; k < count; k++)
  38. {
  39. cout << "Enter your numbers " << k << " ";
  40. cin >> numarray[k];
  41. }
  42. }
  43.  
  44.  
  45. float findaverage(arraytype numarray, int count)
  46. {
  47. int k;
  48. float sum;
  49.  
  50. sum = 0.0;
  51. for (k = 0; k < count; k++)
  52. sum=sum + numarray[k];
  53.  
  54. if (count > 0)
  55. return sum / count;
  56. else
  57. return 0.0;
  58. }
Last edited by WaltP; Dec 3rd, 2006 at 12:27 am. Reason: Please use code tags. Read the Announcement about BBCodes
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jessiegirl is offline Offline
8 posts
since Oct 2006
Dec 2nd, 2006
0

Re: Homework Help.

Ah this is what you must do.

1. Write a flow chart first.
2. Try to do this without functions they will only confuse you.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Dec 2nd, 2006
0

Re: Homework Help.

>>How do I loop in a loop to find the minimum and maximum nubers

Let the first element be the largest (or smallest) element in the array. If the array only has one element then that must be true. If the array has more than one element it may or may not be true so look at each element in the array after the first element. If that element is larger/smaller than the current largest/smallest, then let the current element be the largest/smallest. At the end of the loop the current largest/smallest is the answer.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Dec 2nd, 2006
0

Re: Homework Help.

Click to Expand / Collapse  Quote originally posted by Lerner ...
>>How do I loop in a loop to find the minimum and maximum nubers

Let the first element be the largest (or smallest) element in the array. If the array only has one element then that must be true. If the array has more than one element it may or may not be true so look at each element in the array after the first element. If that element is larger/smaller than the current largest/smallest, then let the current element be the largest/smallest. At the end of the loop the current largest/smallest is the answer.
Okay with hours of work I came up with this.
cpp Syntax (Toggle Plain Text)
  1. //Jessica Moore
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. void getStats( double * min,
  6. double * max,
  7. double * average,
  8. int numsNum );
  9.  
  10. int main()
  11. {
  12. double min, max, average;
  13. int nums;
  14. do ;
  15. {
  16. cout << "How many Number will you Enter? " << flush;
  17. cin >> nums;
  18. }
  19. {
  20. while (nums <0);
  21.  
  22.  
  23.  
  24. getStats( &min, &max, &average, nums );
  25. cout <<"The Minimum is: " << min << endl
  26. <<"The Maximum is: " << max <<endl
  27. <<"The Average is: " << average <<endl;
  28.  
  29. return 0;
  30. }
  31. void getStats( double * min, double * max, double * average, int numsNum )
  32. {
  33. int *myArray=new int[numsNum];
  34. int minimum = myArray[0];
  35. int maximum = myArray[0];
  36. int sum = 0;
  37. for (int i = 0; i < numsNum; i++)
  38. {
  39. cout << "Enter you number[" << i << "]: ";
  40. cin >> myArray[numsNum];
  41. for (int i = 0; i < numsNum; i++)
  42. if (myArray[numsNum] < minimum)
  43. {
  44. minimum = myArray[numsNum];
  45. *min = minimum;
  46.  
  47. for (int i =0; i < numsNum; i++)
  48. if (myArray[numsNum] > minimum)
  49. {
  50. maximum = myArray[numsNum];
  51. *max = maximum;
  52. for (int i = 0; i < numsNum; i++)
  53. {
  54. sum = ((sum + myArray[numsNum])/numsNum);
  55. *average = sum;
  56. }
  57. }
  58. }
  59. }
  60. }
  61.  
  62. }


I still have some errors and I can't seem to figure out the mistakes TY for all your help
Last edited by WaltP; Dec 3rd, 2006 at 12:28 am. Reason: Please use BB Code and Inlinecode tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jessiegirl is offline Offline
8 posts
since Oct 2006
Dec 2nd, 2006
0

Re: Homework Help.

>I still have some errors and I can't seem to figure out the mistakes TY for all your help
I refuse to read your code until you format it and put it in code tags.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Dec 3rd, 2006
0

Re: Homework Help.

Click to Expand / Collapse  Quote originally posted by Narue ...
>I still have some errors and I can't seem to figure out the mistakes TY for all your help
I refuse to read your code until you format it and put it in code tags.
And don't let us try to figure out what the errors are. Tell us. It's not our program.
Moderator
Reputation Points: 3281
Solved Threads: 895
Posting Sage
WaltP is online now Online
7,749 posts
since May 2006
Dec 3rd, 2006
0

Re: Homework Help.

    do ;
    {
        cout << "How many Number will you Enter? " << flush;
        cin >> nums;
    }
{
  while (nums <0);
Your loop is really screwed up. I don't think you want that semicolon after do, and that opening brace before the while statement. Here's how do...while statements are supposed to look like:
  1. do {
  2. // do stuff here
  3.  
  4. } while (expression == true);

And by the way you've got 1 too many closing braces at the end of your program. Proper indentation would help you to see problems like that.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Dec 3rd, 2006
0

Re: Homework Help.

Here's what I think is a correct version of your function. Look at this version versus yours. Identify differences and explain why this version is more likely to work than yours. Adapt this version to your needs if need be. Note: I haven't compiled this version, so there may still be some errors.

I always use reference variables in preference to pointer variables if possible because the syntax is so much easier.

C++ Syntax (Toggle Plain Text)
  1. void getStats( double & min, double & max, double & average, int numsNum )
  2. {
  3. int * myArray=new int[numsNum];
  4. int sum;
  5.  
  6. for (int i = 0; i < numsNum; i++)
  7. {
  8. cout << "Enter you number[" << i << "]: ";
  9. cin >> myArray[i];
  10. }
  11.  
  12. //set min to be first element entered in myArray
  13. min = myArray[0];
  14.  
  15. //for each element in myArray after first element
  16. for (int i = 1; i < numsNum; i++)
  17. {
  18. if (myArray[i] < min)
  19. min = myArray[i];
  20. }
  21.  
  22. //set max to be first element of myArray
  23. max = myArray[0];
  24.  
  25. //look at all elements in myArray after first element
  26. for (int i = 1; i < numsNum; i++)
  27. {
  28. if (myArray[i] > max)
  29. max = myArray[i];
  30. }
  31.  
  32. //set sum to zero
  33. sum = 0;
  34. for (int i = 0; i < numsNum; i++)
  35. {
  36. //add each value of myArray to sum;
  37. sum = sum + myArray[i];
  38. }
  39.  
  40. //now calculate average
  41. average = sum/numsNum;
  42.  
  43. delete [] myArr;
  44. }
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Dec 3rd, 2006
0

Re: Homework Help.

pretty good if you compiled that in your head.
Last edited by iamthwee; Dec 3rd, 2006 at 3:26 pm.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Dec 3rd, 2006
0

Re: Homework Help.

Click to Expand / Collapse  Quote originally posted by Lerner ...
Here's what I think is a correct version of your function. Look at this version versus yours.
I disagree on principal. She already has a function called findeaverage() so why would you want to make a do-all function? They are harder to debug and maintain. Take each piece you want to accomplish and make a function out of it. Return one value from each with the return statement.

And change the name of array1. It's not an array. Call it cout.
Moderator
Reputation Points: 3281
Solved Threads: 895
Posting Sage
WaltP is online now Online
7,749 posts
since May 2006

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: Reversing a string
Next Thread in C++ Forum Timeline: ClassView is not showing my classes (VC++, namespace, headers) ?!?!?





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


Follow us on Twitter


© 2011 DaniWeb® LLC