lowest number (array)

lowest number (array)

Page 1 of 1

2 Replies - 3398 Views - Last Post: 11 October 2008 - 04:44 AM Rate Topic: -----

#1 ziamie19  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-October 08

lowest number (array)

Posted 10 October 2008 - 10:29 PM

for(a=0;a<5;a++) 
				  { low = b[0];
					if (b[a] <= low)
					{ low = b[a]; }
				  }  
				  cout<<"\n\nThe lowest entered number/s is "<<low;


what's wrong with this code??? the result is wrong. . .
plz help me. . . . . :)

Is This A Good Question/Topic? 0
  • +

Replies To: lowest number (array)

#2 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: lowest number (array)

Posted 11 October 2008 - 12:55 AM

Check what happens as the first instruction of each iteration of your for block -

For each element within your array b, you have set your low variable to to the first element in the array (The array will not change, therefore low will be the same each time it comes to the comparison). As a result, the comparison statement always compares each element to the one at position 0 (Which may or may not be the lowest depending on the data)

Setting low to the first element once is a good idea, you would typically need it to happen before the for statement begins

This post has been edited by Bench: 11 October 2008 - 01:00 AM

Was This Post Helpful? 0
  • +
  • -

#3 Soura  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 24-September 08

Re: lowest number (array)

Posted 11 October 2008 - 04:44 AM

View Postziamie19, on 10 Oct, 2008 - 10:29 PM, said:

plz help me. . . . . :)


The prog. to find the lowest between 10 No.s is-
#include<stdio.h>
#include<conio.h>
#include<iostream.h>

main()
{
	  int a[10],i, min;
	  for(i=0; i<=9; i++)
	  {
	  printf("Input the marks%d=",i+1);
	  scanf("%d", &a[i]);
	  }
	  min=a[0];
			for(i=0; i<=9; i++)
	  {
			if(a[i]<min)
			min=a[i];
	  }
	  printf("The Lowest marks is=%d",min);
		  getch();
}
/*end of main*/


Please try this.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1