Hi, I am new to C++ and I tried creating a program but it didn't turn out so well..... Before I explain my problems, I will show you the program:

#include <iostream>using namespace std;int main() { double num[99], min_val, max_val, avg, range; int i; char ch; do { cout << "Enter: "; cin >> num[i]; } while ( num[i] != 'e' ); for ( i = 0;; i++) { avg = 0; avg += num[i]; } avg /= i; cout << "\n\nAverage: " << avg << "\n"; i = 0; min_val = max_val = i; for ( i = 0; num[i]; i++) { if (min_val > num[i]) min_val = num[i]; if (max_val < num[i]) max_val = num[i]; } range = max_val - min_val; cout << "Minimum: " << min_val << "\n"; cout << "Maximum: " << max_val << "\n"; cout << "Range: " << range << "\n"; system("PAUSE"); return 0; }

I don't know exactly how to end input for the numbers....I also do not know how to get the median and mode into the program....I have thought about it for a few days now but nothing clicks.

I have tried have a char value end the input but I didn't know how to....When I press 'e', the program goes crazy..... I am trying to get that fixed..... If anyone can help me or give me suggestions. I would very well appreciate it.

Thanks in advance~!!

Sorry if this doesn't exactly work - I'm not a C++'er... but I'll try my best

Everyone else - feel free to point out what I'm saying wrong!

A few things here:

If you use void instead of int, you won't have to

return 0;You defined num as a double - there's gonna be some problems if you try and put a string in there... Maybe you could instead use -1 as your end value?

I don't think you're incrementing i in the input loop, you should probably increment it and make sure it doesn't exceed 99

for ( i = 0;; i++) {might cause errors, or an infinite loop, or it might just skip past - you need something that it tests (i <= 99 maybe?)

for ( i = 0; num; i++) {will terminate if a 0 turns up, and it might act weirdly wheniexceeds 99.

If your range is -5 to -3, max will record as 0. You should probably define max as the most negative a double can get.

If your range is 3 to 5, min will record as 0. You should probably define min as the most positive a double can get.

It might help to define a one byte integer (which is a char I assume) as the number of items you have - I'll call it c for simplicity. You could have it as your first input (not part of the loop), then in your input loop (as well as incrementing i) you test that i < c. And you can do the same test in your for loops!

Hey guys, thanks for the help but if I put a number to end the input, it would count the number as part of the input....and this is not homework.... I self study, my school doesn't teach C++. I am a MS student.

Not if you add the following code after your do-while statement:if I put a number to end the input, it would count the number as part of the input....

num[i]=null;

hmmm....on my compiler, it says that null is undefined....

OK tran...As I promised you here is the better and easiest version of your program to find minimum, maximum and average.

Since you are at the starting stage of learing C++ I eliminated char values etc...to make the program easy to understand.

And my porgram will be C as I already told you I don't know C++ and you told me you can understand C. And my program is just to teach you the simple logic behind calculating these. You should make changes to

printfandscanfthings tocoutandcinto make them work in C++ or save the program with.cextension and compile as usual. Remove clrscr() and getch() if they give any trouble.

OK, here is the program. First I will give the whole program and then, I will explain each part.

void main(){ int number[50]; int i,j,n,total=0,min=0,max=0,temp; float avg; clrscr();//It clears the screen just like cls command printf("How many numbers you want to enter: "); scanf("%d",&n); for(i=0;i<n;i++) { printf("Enter the number: "); scanf("%d",&number[i]); } printf("The numbers you entered are \n"); for(i=0;i<n;i++) { printf("%d ",number[i]); } /*this is sorting section */ for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(number[i]>number[j]) { temp=number[i]; number[i]=number[j]; number[j]=temp; } } }/*calculating the total */ for(i=0;i<n;i++) { total=total+number[i]; }/* calculating and printing our min max and avgerage */ min=number[0]; max=number[n-1]; avg=(float)total/n; printf("\n The minimum number is: %d \n",min); printf("The maximum number is: %d \n",max); printf("The average is: %f",avg); getch();}Ok, lets see some important parts of the code. First of all, I eliminated the char part. Instead your program will first ask you how many numbers you want to enter. It reads the entered value and stores it in

n;The i,j are used as increment values in for loops, temp is temporary variable to use in swapping.

This above code asks you to enter numbers. IT read the numbers and put them int arrayfor(i=0;i<n;i++) { printf("Enter the number: "); scanf("%d",&number[i]); }number[]uptontimes.

The code below prints the entire array you entered.

for(i=0;i<n;i++){ printf("%d ",number[i]);}

The next part is very important part, and you should understand it carefully. This is calling sorting. That is the following part of the code will sort all numbers in the array in minimum(smallest number) to maximum(biggest number) order.As

galexcdtold, this is calledbubble sorting. The simplest of all.

for(i=0;i<n;i++){ for(j=i+1;j<n;j++) { if(number[i]>number[j]) { temp=number[i]; number[i]=number[j]; number[j]=temp; } }}

we used nested for loops to achieve sorting. The condition in theif blockwill check if the first number is greater than second number. If greater it swaps two numbers. Else it continues. And then the first number will then compare to third number so on...This process will continue until the entire array is in min to maximum order (until the end of the loop actually).

Next part will calculate the sum of all numbers in the array.

And the last part calculates the average and print minimum, maximum and average.for(i=0;i<n;i++){ total=total+number[i];}I added float before total because we explicitly converting the integer value into float.avg=(float)total/n;

I think this program made everything clear to you. If you have any doubts about the program ask it here or IM me...

There are many better versions for this program. But at starting stage this is best for you. I left those range,median things. You can do them yourself.

Wow....I can't believe I understanded all of that....... YAY for me~!! Thanks galexcd and xpress..... The bubble sorting took a while but I got it..

VIEW DESKTOP VERSION | REGISTER | GET FREE HOSTING |

Xisto.com offers Free Web Hosting to its Members for their participation in this Community. We moderate all content posted here but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy. Copyright 2001-2019 by Xisto Corporation. All Rights Reserved. |