10 Replies - 5518 Views - Last Post: 14 November 2008 - 01:22 PM Rate Topic: -----

#1 angvil  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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 :D.
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  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • 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);

}



I hope this will help you. :)
Was This Post Helpful? 0
  • +
  • -

#4 angvil  Icon User is offline

  • New D.I.C Head

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

Re: calculate the average, highest and lowest

Posted 14 November 2008 - 11:10 AM

View PostAmitTheInfinity, 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);

}



I hope this will help you. :)




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

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

Was This Post Helpful? 0
  • +
  • -

#5 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#6 angvil  Icon User is offline

  • New D.I.C Head

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

Re: calculate the average, highest and lowest

Posted 14 November 2008 - 11:52 AM

View PostGloin, 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 ?!
Was This Post Helpful? 0
  • +
  • -

#7 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#8 angvil  Icon User is offline

  • New D.I.C Head

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

Re: calculate the average, highest and lowest

Posted 14 November 2008 - 12:15 PM

View PostGloin, 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 ..
Was This Post Helpful? 0
  • +
  • -

#9 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#10 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3106
  • View blog
  • Posts: 19,145
  • Joined: 14-September 07

Re: calculate the average, highest and lowest

Posted 14 November 2008 - 12:22 PM

View Postangvil, 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?
Was This Post Helpful? 0
  • +
  • -

#11 angvil  Icon User is offline

  • New D.I.C Head

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

Re: calculate the average, highest and lowest

Posted 14 November 2008 - 01:22 PM

View PostKYA, on 14 Nov, 2008 - 11:22 AM, said:

View Postangvil, 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 !!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1