how do I get the maximum, minimum and the average of an array?

this may be different but how do I get to stop inputting number in an array. for example if the array is 20 and i only need 10 of them. i think its something about EOF but im not sure how to use it.

``````void main()
{
int a[20],i,j;
for(i=0;i<10;i++)
{
cout<<"enter value";
cin>>a[i];
}

for(i=0;i<10;i++)
{
cout<<"value="<<a[i];

}
}``````
commented: trash -3

(1) this is C. not C++. check the forum and post appropriately.

(2) your code snippet doesn't even remotely begin to answer the question. obviously you didn't read the forum, but did you even read his question? did you even read the title of the thread?

(3) generally speaking we don't give away code for homework solutions, and we don't appreciate people who do.

(3) generally speaking we don't give away code for homework solutions, and we don't appreciate people who do.

It's so non-standard the kid would get in trouble anyway.

how do I get the maximum, minimum and the average of an array?

sum = 0
min = value of first array element
max = value of first array element

begin loop for each array element, index = 0 to (n-1)

--- increment sum by value of array element at index
--- if array element value is less than min, set min to this value
--- if array element value is more than max, set max to this value

increment index and repeat loop til last index is completed

average = sum / number of elements (n).
max and min will hold their correct values.

.

you mean the index = 0;
i seriously don't know what that means. i have seen it before but i don't know what it does
and which loop do i use? For loop?
and how do i add the contents of my array

here is a snippet of code to start. it's not going to work by itself, and i'm not giving you any secret answers. at least i hope the formula to calculate the average is not a secret to you.

now you do the rest.

``````for (index = 0; index < NUMBER_OF_ELEMENTS; index++)
{
// calculate stuff with your array here
// ...
// ...
// ...
}

avg = sum / NUMBER_OF_ELEMENTS:

printf("minimum: %d, maximum: %d, average: %f\n",min,max,avg);``````

.

you mean the index = 0;
i seriously don't know what that means. i have seen it before but i don't know what it does
and which loop do i use? For loop?
and how do i add the contents of my array

If you don't know what `index = 0;` you seriously need to start reading your book before continuing. I suggest you talk to your instructor...

will i be able to use Quick Sort, to get max and min, and just get the value on the first element as min and the value at the last element to be the max?

if so then can u guys help out on the code for quick sort or give me tips about it. it is totally getting confusing.

oh btw index is just an identifier, forgot about that

index, is exactly what it says it is: an "index". it indexes elements of the array. it's not an identifier, as it doesn't identify anything ... other than the index.

sure, you can use a sorting algorithm, and then your min and max will be at one end or the other.

until you start posting code and asking questions "the right way" i'm going to have to refer you to this site

well my problem is this

``````#include <stdio.h>
#include <stdlib.h>

int main (void)
{

char a[5];
a[0] = EOF;
int sum=0;
while( 1 )
{
gets(a);
if(a[0] == EOF)
break;
else{
for(int i=0; a[i]!='\0'; i++)
{
int val = int(a[i])-48;
sum = sum*10+val;
}
}
a[0] = EOF;
}
}``````

and now i need to insert the max and min in it

and i did look and try on google and searched for any quick sort code that i can try and have no idea where to put them in this code.

anyone know which part of it i need to put the swap() and the quicksort()

also what i meant by it is an identifier is

index was an identifier/name and it was used in for loop to access the elements in the array manually doing them.

ehhh.... i'm tired of answering questions right now. I'm going to ask YOU some questions for a while:

Q1: run your program, enter 0x7F for one of your array entries. now tell me why is your program broken.

Q2: run your program, enter 100000 for one of your array entries. then tell me why your program shits the bed.

the program works, the things you input needs to be less that or equal to 4 characters. mostly numbers i believe. its a homework assignment. the TA checked it and it works. all you have to do it on step debug or F10. if you don't it does nothing. now the next thing i need is sorting the characters you input.

well i thank you for your effort today. im sorry if i added stress to your brain.

``````>gcc -o array array.c
array.c: In function `main':
array.c:16: error: 'for' loop initial declaration used outside C99 mode
array.c:18: error: syntax error before "int"``````

wrong, it does not compile at all. and then when i fix all your declarations, it still doesnt work. your input routine is broken. im trying to tell you why, but youre apparently not in a space to hear it.

so since you insist that your code is working just fine, with , i'll let you go ahead and finish it up.

be sure to tell your instructor that "all you have to do it on step debug or F10" and im sure they'll think it's groovy.

theres no need for a sorting algorithm in this code, just walk through the array one by one with the for loop that jephthah gave to you. and inside the for loop just do if( min > a ) min = a; and something similar to that for max. and for the average just do sum = sum + a... then after the loop just print out sum divided by the number of elements in the array

it seems that his recent request for us to provide him a coded sorting algorithm is yet another stealth requirement being pushed in under the radar.

it's just an attempt to get us to do his work. slightly more sophisticated than the typical "dump my requirements and wander off" method most newly-joined members attempt.

but still, pretty obvious.

.

this if( min > a ) min = a; is supposed to be in a for loop that jeph gave, right? if so, based on the code that i did, will i have to place it before the else function inside the while loop that i did or will it have to be after or before the else? im trying to do the max min sort thing in my code.

and for jeph don't mean to insult u if i did but i just copied that code from what i have and it still works. i don't know why it doesnt compile for you.
when it does, try just entering 3 digit numbers like 426 or something, using F10. i know it doesnt show anything in the command prompt. its not supposed to except for the digits u typed

this looked like i am asking for the code. my bad if it does, im not trying to

i don't know why it doesnt compile for you.

let me ask you something: what compiler are you using?

.

im using visual C++ 2008 Express edition. also i tried it in Visual Studio 2008 in my school.

ok what i did was put this if( min > a ) min = a; after the else of if else statement. ok here is what i did.

``````#include <stdio.h>
#include <stdlib.h>

int main (void)
{

char a[5];
a[0] = EOF;
int sum=0;
while( 1 )
{
gets(a);
if(a[0] == EOF)
break;
else{
for(int i=0; a[i]!='\0'; i++)
{
int val = int(a[i])-48;
sum = sum*10+val;
}
}
int min = 9;
for( int z =0; a[z] != '\0', z++);
{
if( min > a[z])
min = a[z];
}
printf( "Minimum is %c", min);
a[0] = EOF;
}
}``````

it always shows the last number to be the minimum

it still doesnt compile with my standard GCC compiler.

you're not allowed to intially declare variables inside your for loops, for one thing. that only works in C++.

for another thing, `int val = int(a[i])-48;` is illegal in C, as well.

and why are you STILL using gets()?? we've been over this already. now take that out and replace it with something sensible.

.

isnt gets() almost the same as scanf()? im using it because it gets more characters than scanf(). C code works inside C++ right. we use C++ in class and i dont think thats a problem, for me.

no offense but i think by focusing on the code that i have posted up there, and fixing it, the purpose of my thread. I actually did to by asking for quicksort help and im sorry too.

sorry, im just annoyed because i cant ever compile your code, and its taking forever for you to believe me.

aiight, dude. i'm tired of all this grabassery. here's standard c, that will actually work. I'm not going to bulletproof the input or conversion for you, but it's close enough for your needs, and better than what you had before:

``````#include <stdio.h>
#include <stdlib.h>

int main (void)
{

char a[10];
int sum=0, count=0, min, max, val;

while(fgets(a,10,stdin))   // never use "gets()"
{
a[strcspn(a,"\n")]='\0';  //strip newline

if(strlen(a)==0) //  dont loop input on feof() either
break;

val = atol(a); // standard library function

// and i hope to god you can finish this on your own
// ...
// ...
// ...

}

printf( "Min: %d, Max: %d, Avg: %4.2f\n", min, max, (float)sum/count);

return 0;
}``````

.

isnt gets() almost the same as scanf()? im using it because it gets more characters than scanf().

Yes. See this and this. Stop using `gets()` !

C code works inside C++ right. we use C++ in class and i dont think thats a problem, for me.

Then write C++ instead of C. If you want to write C code you must follow the rules of C and stop arguing with those that know the rules.

aiight, dude. i'm tired of all this grabassery. here's standard c, that will actually work.

He finally got to you... Oh well. :icon_wink:

commented: Saying "Noooooooo" to gets() :) +29

He finally got to you... Oh well

i know. i should be better than that. :(

im sorry if you feel bad. thank you for helping. i didnt really intend to asking for the code. i am really serious. thanks you helped so much =)

i will mark it now as solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.