# calculate the average, highest and lowest

Page 1 of 1

## 10 Replies - 15443 Views - Last Post: 14 November 2008 - 01:22 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=71862&amp;s=3f50229d5e541dc13e09aa2f141b8faf&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 angvil

• New D.I.C Head

Reputation: 0
• Posts: 5
• Joined: 14-November 08

# calculate the average, highest and lowest

Posted 14 November 2008 - 05:37 AM

hello guys,,
first of all I want to tanks all the members for putting a great effort in this extremely great site.
i am new joiner here and i wish to find my help between you i am biggener in programming .
my problem is :

this question to enter 20 student marks and to calculate the average, highest and lowest mark.

the part of highest and average mark I solve it but i can't get the lowest mark.
i wish to find my help here ...

this is the code

```
#include<stdio.h>
void main()
{
float marks[20],sum=0,avg=0,low,hig;
int i;
for(i=0;i<20;i++)
{
printf("\n Enter Students marks");
scanf("%f",&marks[i]);
if(hig<marks[i])
{
hig=marks[i];
marks[i]=hig;
}
if(low>marks[i])
{
low=marks[i];
marks[i]=low;
}
sum=sum+marks[i];
avg=sum/20;
}
printf("\n The hihest mark is %0.2f",hig);
printf("\n The lowest mark is %0.2f",low);
printf("\n The Average marks is %0.2f",avg);

}

```

Is This A Good Question/Topic? 0

## Replies To: calculate the average, highest and lowest

### #2 Gloin

• Expert Schmexpert...

Reputation: 235
• Posts: 4,489
• Joined: 04-August 08

## Re: calculate the average, highest and lowest

Posted 14 November 2008 - 05:53 AM

The only differense to finding the highest is that when you declare the variables without assigning them, they become 0 by default (no mark will be less than 0, right?). So before you begin with the for-loop, simply assign low = marks[0] (or some high value) and things will work out for you. You could even assign both high, low and sum this way and then start the loop from 1 instead of 0, your choice.

Also,

hig=marks[i];
marks[i]=hig;

This is redundancy, you don't need to reassign marks[i] as the value will not have changed. Same in the case with low.

This post has been edited by Gloin: 14 November 2008 - 05:59 AM

### #3 AmitTheInfinity

• C Surfing ∞

Reputation: 119
• Posts: 1,565
• Joined: 25-January 07

## Re: calculate the average, highest and lowest

Posted 14 November 2008 - 05:55 AM

Check my comments in code.

```#include<stdio.h>
void main()
{
float marks[20],sum=0,avg=0,low=0,hig = 0; //you forgot to initialize low and hig.
int i;

//however I would prefer this way...
printf("\n Enter Students marks");
scanf("%f",&marks[0]);
low = marks[0];
hig = marks[0];

for(i=1;i<20;i++)
{
printf("\n Enter Students marks");
scanf("%f",&marks[i]);
if(hig<marks[i])
{
hig=marks[i];
//marks[i]=hig;   was that necessary?
}
if(low>marks[i])
{
low=marks[i];
//  marks[i]=low;  was that necessary?
}
sum=sum+marks[i];
// avg=sum/20; you can do this outside loop, after taking sum of all inputs.
}
//here
avg=sum/20;

printf("\n The hihest mark is %0.2f",hig);
printf("\n The lowest mark is %0.2f",low);
printf("\n The Average marks is %0.2f",avg);

}

```

### #4 angvil

• New D.I.C Head

Reputation: 0
• Posts: 5
• Joined: 14-November 08

## Re: calculate the average, highest and lowest

Posted 14 November 2008 - 11:10 AM

AmitTheInfinity, on 14 Nov, 2008 - 04:55 AM, said:

Check my comments in code.

```#include<stdio.h>
void main()
{
float marks[20],sum=0,avg=0,low=0,hig = 0; //you forgot to initialize low and hig.
int i;

//however I would prefer this way...
printf("\n Enter Students marks");
scanf("%f",&marks[0]);
low = marks[0];
hig = marks[0];

for(i=1;i<20;i++)
{
printf("\n Enter Students marks");
scanf("%f",&marks[i]);
if(hig<marks[i])
{
hig=marks[i];
//marks[i]=hig;   was that necessary?
}
if(low>marks[i])
{
low=marks[i];
//  marks[i]=low;  was that necessary?
}
sum=sum+marks[i];
// avg=sum/20; you can do this outside loop, after taking sum of all inputs.
}
//here
avg=sum/20;

printf("\n The hihest mark is %0.2f",hig);
printf("\n The lowest mark is %0.2f",low);
printf("\n The Average marks is %0.2f",avg);

}

```

Thank you very much for the big effort to help me .. i knew i missed some stuff but as i told you Noob

i have another question i was planing to add it to the coding ...

if i want to make the array to be sorted ascendingly,,, i think it will be too long for the 20 mark right ?!

is there a short code to be added so the array can be sorted ascendingly ?!

i know if i use this code will be a lot of coding

```if if ((1>2)&&(1>3)&&(1>4)&&(1>5)&&(1>6)&&(1>7)&&(1>8)&&(1>9)&&(1>10)&&(1>11)&&(1>12)&&(1>13)&&(1>14)&&(1>15)&&(1>16)&&(1>17)&&(1>18)&&(1>19)&&(1>20))
{
p( "the ascending array are: %d",****);   <---------------------- and here what should be the input to show in the output for ascending !
}

```

i don't know if this code right !!

This post has been edited by angvil: 14 November 2008 - 11:14 AM

### #5 Gloin

• Expert Schmexpert...

Reputation: 235
• Posts: 4,489
• Joined: 04-August 08

## Re: calculate the average, highest and lowest

Posted 14 November 2008 - 11:38 AM

Check out some example on bubblesort if you want the information sorted, there are more efficient sorts but bubblesort is easy to understand.

### #6 angvil

• New D.I.C Head

Reputation: 0
• Posts: 5
• Joined: 14-November 08

## Re: calculate the average, highest and lowest

Posted 14 November 2008 - 11:52 AM

Gloin, on 14 Nov, 2008 - 10:38 AM, said:

Check out some example on bubblesort if you want the information sorted, there are more efficient sorts but bubblesort is easy to understand.

Man i tried it but it wont work ...

can u give me some help ?!

### #7 Gloin

• Expert Schmexpert...

Reputation: 235
• Posts: 4,489
• Joined: 04-August 08

## Re: calculate the average, highest and lowest

Posted 14 November 2008 - 12:06 PM

This is not bubblesort but it's another inefficient but very understandable method for sorting.. I made it in java, basically what it does is it compares each value to every value in the array (including itself) and puts the values back in the array on the index that equals the number of times it was bigger or equal (reduced by one since every value is equal to itself). There's a control-function that will make sure the values are inserted on different indexes if there exist non-distinct numbers.

```public static void slowsort(int[] Values) {

int [] Sorted = new int[Values.length];
int i = 0;
int j = 0;
int k = 0;

for (i = 0; i < Values.length; i++) { // For every value
k = 0;
for (j = 0; j < Values.length; j++) { //Calculate how many values are smaller than itself
if (Values[i] >= Values[j])
k++;
}
while (Sorted[k-1] != 0) { // Only needed when there are non-distinct values
k--;
}
Sorted[k-1] = Values[i]; // -1, not counting itself.
}
for (int m = 0; m < Values.length; m++)
System.out.print(Sorted[m]);
}

```

It's pretty much the same in C except System.out.println is called printf and then public stuff at top, well you know what it looks like in C.

This post has been edited by Gloin: 14 November 2008 - 12:12 PM

### #8 angvil

• New D.I.C Head

Reputation: 0
• Posts: 5
• Joined: 14-November 08

## Re: calculate the average, highest and lowest

Posted 14 November 2008 - 12:15 PM

Gloin, on 14 Nov, 2008 - 11:06 AM, said:

This is not bubblesort but it's another inefficient but very understandable method for sorting.. I made it in java, basically what it does is it compares each value to every value in the array (including itself) and puts the values back in the array on the index that equals the number of times it was bigger or equal (reduced by one since every value is equal to itself). There's a control-function that will make sure the values are inserted on different indexes if there exist non-distinct numbers.

```public static void slowsort(int[] Values) {

int [] Sorted = new int[Values.length];
int i = 0;
int j = 0;
int k = 0;

for (i = 0; i < Values.length; i++) { // For every value
k = 0;
for (j = 0; j < Values.length; j++) { //Calculate how many values are smaller than itself
if (Values[i] >= Values[j])
k++;
}
while (Sorted[k-1] != 0) { // Only needed when there are non-distinct values
k--;
}
Sorted[k-1] = Values[i]; // -1, not counting itself.
}
for (int m = 0; m < Values.length; m++)
System.out.print(Sorted[m]);
}

```

It's pretty much the same in C except System.out.println is called printf and then public stuff at top, well you know what it looks like in C.

Thanks man for the effort but i want something easy to be similar with the code that i wrote it ,,,
coz I'm planning to start from the scratch with the coding ..

### #9 Jayman

• Student of Life

Reputation: 421
• Posts: 9,532
• Joined: 26-December 05

## Re: calculate the average, highest and lowest

Posted 14 November 2008 - 12:19 PM

Topic renamed to be more descriptive of the problem.

### #10 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,233
• Joined: 14-September 07

## Re: calculate the average, highest and lowest

Posted 14 November 2008 - 12:22 PM

angvil, on 14 Nov, 2008 - 12:15 PM, said:

Thanks man for the effort but i want something easy to be similar with the code that i wrote it ,,,
coz I'm planning to start from the scratch with the coding ..

It doesn't get any "easier" then a modified bubble sort. Are you saying you need the algorithm to look like you wrote it? I'm all about rewriting for understanding rather then copy/paste. How simpler do you want/need it?

### #11 angvil

• New D.I.C Head

Reputation: 0
• Posts: 5
• Joined: 14-November 08

## Re: calculate the average, highest and lowest

Posted 14 November 2008 - 01:22 PM

KYA, on 14 Nov, 2008 - 11:22 AM, said:

angvil, on 14 Nov, 2008 - 12:15 PM, said:

Thanks man for the effort but i want something easy to be similar with the code that i wrote it ,,,
coz I'm planning to start from the scratch with the coding ..

It doesn't get any "easier" then a modified bubble sort. Are you saying you need the algorithm to look like you wrote it? I'm all about rewriting for understanding rather then copy/paste. How simpler do you want/need it?

i mean i don't know how to add it to my program the ascending part !!!