| | |
Homework Help.
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2006
Posts: 8
Reputation:
Solved Threads: 0
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
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)
#include using namespace std; const int arraymax = 10; typedef int arraytype[arraymax]; float findaverage(arraytype numarray, int count); void array2(arraytype numarray, int & count); int main(void) { int array1; arraytype array3; float avg; array2(array3, array1); avg = findaverage(array3, array1); cout << endl << "The average is: " << avg << endl << endl; return 0; } void array2(arraytype numarray, int & count) { int k; cout << "How many numbers would you like to enter? "; cin >> count; for (k = 0; k < count; k++) { cout << "Enter your numbers " << k << " "; cin >> numarray[k]; } } float findaverage(arraytype numarray, int count) { int k; float sum; sum = 0.0; for (k = 0; k < count; k++) sum=sum + numarray[k]; if (count > 0) return sum / count; else return 0.0; }
Last edited by WaltP; Dec 3rd, 2006 at 12:27 am. Reason: Please use code tags. Read the Announcement about BBCodes
•
•
Join Date: Jul 2005
Posts: 1,761
Reputation:
Solved Threads: 283
>>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.
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.
•
•
Join Date: Oct 2006
Posts: 8
Reputation:
Solved Threads: 0
•
•
•
•
>>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.
cpp Syntax (Toggle Plain Text)
//Jessica Moore #include <iostream> using namespace std; void getStats( double * min, double * max, double * average, int numsNum ); int main() { double min, max, average; int nums; do ; { cout << "How many Number will you Enter? " << flush; cin >> nums; } { while (nums <0); getStats( &min, &max, &average, nums ); cout <<"The Minimum is: " << min << endl <<"The Maximum is: " << max <<endl <<"The Average is: " << average <<endl; return 0; } void getStats( double * min, double * max, double * average, int numsNum ) { int *myArray=new int[numsNum]; int minimum = myArray[0]; int maximum = myArray[0]; int sum = 0; for (int i = 0; i < numsNum; i++) { cout << "Enter you number[" << i << "]: "; cin >> myArray[numsNum]; for (int i = 0; i < numsNum; i++) if (myArray[numsNum] < minimum) { minimum = myArray[numsNum]; *min = minimum; for (int i =0; i < numsNum; i++) if (myArray[numsNum] > minimum) { maximum = myArray[numsNum]; *max = maximum; for (int i = 0; i < numsNum; i++) { sum = ((sum + myArray[numsNum])/numsNum); *average = sum; } } } } } }
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
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
do ; { cout << "How many Number will you Enter? " << flush; cin >> nums; } { while (nums <0);
do, and that opening brace before the while statement. Here's how do...while statements are supposed to look like: c Syntax (Toggle Plain Text)
do { // do stuff here } 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.
All my posts may be freely redistributed under the terms of the MIT license.
•
•
Join Date: Jul 2005
Posts: 1,761
Reputation:
Solved Threads: 283
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.
I always use reference variables in preference to pointer variables if possible because the syntax is so much easier.
C++ Syntax (Toggle Plain Text)
void getStats( double & min, double & max, double & average, int numsNum ) { int * myArray=new int[numsNum]; int sum; for (int i = 0; i < numsNum; i++) { cout << "Enter you number[" << i << "]: "; cin >> myArray[i]; } //set min to be first element entered in myArray min = myArray[0]; //for each element in myArray after first element for (int i = 1; i < numsNum; i++) { if (myArray[i] < min) min = myArray[i]; } //set max to be first element of myArray max = myArray[0]; //look at all elements in myArray after first element for (int i = 1; i < numsNum; i++) { if (myArray[i] > max) max = myArray[i]; } //set sum to zero sum = 0; for (int i = 0; i < numsNum; i++) { //add each value of myArray to sum; sum = sum + myArray[i]; } //now calculate average average = sum/numsNum; delete [] myArr; }
•
•
•
•
Here's what I think is a correct version of your function. Look at this version versus yours.
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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
![]() |
Similar Threads
- We only give homework help to those who show effort (Computer Science)
- Need help with Computer Science homework (Computer Science)
- Dynamic memory allocation homework (C++)
- Homework Help!! Priority Queue ?? (Computer Science)
Other Threads in the C++ Forum
- Previous Thread: Reversing a string
- Next Thread: ClassView is not showing my classes (VC++, namespace, headers) ?!?!?
Views: 3462 | Replies: 20
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






