Homework Help.

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2006
Posts: 8
Reputation: jessiegirl is an unknown quantity at this point 
Solved Threads: 0
jessiegirl jessiegirl is offline Offline
Newbie Poster

Homework Help.

 
0
  #1
Dec 2nd, 2006
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

  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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Homework Help.

 
0
  #2
Dec 2nd, 2006
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.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,761
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Homework Help.

 
0
  #3
Dec 2nd, 2006
>>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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 8
Reputation: jessiegirl is an unknown quantity at this point 
Solved Threads: 0
jessiegirl jessiegirl is offline Offline
Newbie Poster

Re: Homework Help.

 
0
  #4
Dec 2nd, 2006
Originally Posted by Lerner View Post
>>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.
  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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,867
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 755
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Homework Help.

 
0
  #5
Dec 2nd, 2006
>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.
New members chased away this month: 5
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,131
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Homework Help.

 
0
  #6
Dec 3rd, 2006
Originally Posted by Narue View Post
>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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Homework Help.

 
0
  #7
Dec 3rd, 2006
    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.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,761
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Homework Help.

 
0
  #8
Dec 3rd, 2006
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.

  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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Homework Help.

 
0
  #9
Dec 3rd, 2006
pretty good if you compiled that in your head.
Last edited by iamthwee; Dec 3rd, 2006 at 3:26 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,131
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Homework Help.

 
0
  #10
Dec 3rd, 2006
Originally Posted by Lerner View Post
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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Reply

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




Views: 3462 | Replies: 20
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC