Page 1 of 1

Reputation:

# getting odd,even,max,min,sum and average in array

Posted 03 February 2011 - 01:39 PM

```#include <stdio.h>
02
03
04
05
06
07	int main()
08	{
09
10	    int i;
11	    int a[10]={1,2,3,4,5,6,7,8,9,10};
12	    float sum=0;
13	    float average=0;
14	    int max=0,min=a[0];
15	    int odd,even;
16
17	    for(i=0;i<5;i++){
18	    printf("enter no: ");
19	    scanf("%d",&a[i]);
20	    sum+=a[i];
21	    }
22	    for(i=0;i<5;i++){
23	    if(a[i]%2==0)
24	          printf("even: %d",even);
25	    if(a[i]%2==1)
26	          printf("odd: %d",odd);
27	        }
28	    for(i=0;i<5;i++){
29	          if(a[i]>max){
30	              max=a[i];
31	      }
32	          else if(a[i]<=min){ /** my problem is this part,it should give the minimum
integer as what the user enter but it always give
min=0 can anyone edit this part or the code in getting
max and min**/
33	              min=a[i];
34	       }
35	       }
36	    printf("max: %d\n",max);
37	    printf("min: %d\n",min);
38
39	    printf("sum: %.1f",sum);
40	    average=sum/10;
41	    printf("average: %1.f",average);
42
43
44
45	     return(EXIT_SUCCESS);
46	 }
```

This post has been edited by ishkabible: 03 February 2011 - 01:58 PM
Reason for edit:: use code tags and dont copy the line numbers

Is This A Good Question/Topic? 0

## Replies To: getting odd,even,max,min,sum and average in array

### #2 Dogstopper

• The Ninjaducky

Reputation: 2965
• Posts: 11,222
• Joined: 15-July 08

## Re: getting odd,even,max,min,sum and average in array

Posted 03 February 2011 - 02:08 PM

Please don't copy the line numbers into your code if possible it really is a pain to deal with. Also, please post your question and/or errors along with the code or nobody will be able to help you. Thanks!
Was This Post Helpful? 0

### #3 newclearner

• D.I.C Regular

Reputation: 103
• Posts: 302
• Joined: 29-September 10

## Re: getting odd,even,max,min,sum and average in array

Posted 03 February 2011 - 07:24 PM

Dogstopper: He wrote his query with in the code using comments.

Knix: Please try to write your queries/concerns clearly so that they are easily noticebale/readable and understanable.

Btw doesn't the code print garbage valaues for printf at line 24 & 26 because you haven't initialized variables even and odd and nor you are assigning any values to them. Infact you should print the a[i] at both the lines, like this:
```printf("even: %d", a[i]);

```

So, if the number is even it gets printed with text even and if its odd it gets printed with text odd. No need of variables odd and even. What say?
Was This Post Helpful? 0

Reputation:

## Re: getting odd,even,max,min,sum and average in array

Posted 03 February 2011 - 10:24 PM

newclearner, on 03 February 2011 - 07:24 PM, said:

Dogstopper: He wrote his query with in the code using comments.

Knix: Please try to write your queries/concerns clearly so that they are easily noticebale/readable and understanable.

Btw doesn't the code print garbage valaues for printf at line 24 & 26 because you haven't initialized variables even and odd and nor you are assigning any values to them. Infact you should print the a[i] at both the lines, like this:
```printf("even: %d", a[i]);

```

So, if the number is even it gets printed with text even and if its odd it gets printed with text odd. No need of variables odd and even. What say?

aw sorry i mistaken, it should be printf("even: %d",a[i]) its look like this not the one i post earlier. yes i use that code but always display minimum=0
Was This Post Helpful? 0

Reputation:

## Re: getting odd,even,max,min,sum and average in array

Posted 03 February 2011 - 10:28 PM

Dogstopper, on 03 February 2011 - 02:08 PM, said:

Please don't copy the line numbers into your code if possible it really is a pain to deal with. Also, please post your question and/or errors along with the code or nobody will be able to help you. Thanks!

aw hehehe sorry, i will next time. by the way its my first time posting forums like this. site of intelligent people eheeehehe
Was This Post Helpful? 0

### #6 newclearner

• D.I.C Regular

Reputation: 103
• Posts: 302
• Joined: 29-September 10

## Re: getting odd,even,max,min,sum and average in array

Posted 03 February 2011 - 10:37 PM

1. Stop giving extra spaces/lines in your post.
2. No need to quote others post completely. Just write their name in bold as I did and respond to them.
3. I really dont see how it will print 0 for minimun value. Since you have initialized to min to a[0] which is set to 1 initially, and you are accepting only positive integers(I assume), I think min should always print 1 untill and unless you give 0 as one of the input values.
Makes sense?

Something questions for you:
1. Did you code compile successfully?
2. Show use some actual input & output samples. What input you gave & what output you recieved.

This post has been edited by newclearner: 03 February 2011 - 10:41 PM

Was This Post Helpful? 0

### #7 Slumdog

• D.I.C Head

Reputation: 34
• Posts: 116
• Joined: 26-November 10

## Re: getting odd,even,max,min,sum and average in array

Posted 03 February 2011 - 10:45 PM

First you need to sort them numerically and then proceed to find the variables you want.

Here is a simple pseudocode:
```//sort the array here using any algorithm.
std::cout << arr[0] << " is the minimum.\n" << arr[10] << " is the maximum\n";
for(int i = 0; i < 10; i++)
{
if(arr[i] % 2 == 0)
std::cout << arr[i] << " is even\n";
else
std::cout << arr[i] << " is odd\n";
sum += arr[i];
counter++;

}
std::cout << "Sum is " << sum << std::endl;
std::cout << "Average is: " << sum / counter;
```

Don't compile it... you'll get errors. The code is fairly simple to understand.

This post has been edited by Slumdog: 03 February 2011 - 10:46 PM

Was This Post Helpful? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }