11 Replies - 316 Views - Last Post: 14 December 2011 - 09:36 AM Rate Topic: -----

#1 sxintiasz21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 05-September 10

Control Structures

Posted 14 December 2011 - 04:18 AM

Can someone guide me guys?

Im trying to determine the highest and the lowest number from the series of numbers input by using control structures.
im ok with the highest but im having a problem when it comes to lowest. here is the code.
my lowest ( c ) its value is always -29291
if i declare it as c = 0; its output will always be 0.
hope you cn explain me with this. thanks in advance :)
#include<stdio.h>
#include<conio.h>
void main()
{
loop:
int c,x,y,z,a=0;
char yesno;
x=0;z=0;
clrscr();
printf("\nEnter Range:");
scanf("%d",&x);

for(y=1; y<=x; y++)
{
printf("\nEnter a Number:");
scanf("%d",&z);

 if(z>=a)
 {
 a=z;
 }
 if(z<=c)
 {
 c=z;
 }
}
printf("\nHighest is:%d",a);
printf("\nLowest is:%d",c);

printf("\nTry Again?[Y/N]:");
yesno=getche();
if(yesno=='Y' || yesno=='y')
goto loop;
else if(yesno=='N' || yesno=='n')
getch();
}


Is This A Good Question/Topic? 0
  • +

Replies To: Control Structures

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5676
  • View blog
  • Posts: 22,538
  • Joined: 23-August 08

Re: Control Structures

Posted 14 December 2011 - 04:44 AM

First, it's NOT void main(), it's int main(). And then you must add a return 0; to the end of the main function.

Second, don't you think you should give the variable c an initial value before you start using it in this comparison?

if(z<=c)

Was This Post Helpful? 0
  • +
  • -

#3 sxintiasz21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 05-September 10

Re: Control Structures

Posted 14 December 2011 - 04:58 AM

thanks.
whenever i put an initial value to c, like 0 or 1, the lowest always become 0 or 1. if not when i try to rerun it again, after i entered series of number it will get the highest one but it will print the lowest number of the last value of c in the previous execution of the program. btw sorry for my bad english
Was This Post Helpful? 0
  • +
  • -

#4 azemyth  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 25
  • Joined: 12-December 11

Re: Control Structures

Posted 14 December 2011 - 05:38 AM

1. delete line 34. this line is clearly non functional.
2. start giving your variables appropriate names. Like MIN, MAX etc..
3. stop using curly brackets when you are dealing with only one statement after a conditional expression.

your code is highly unreadable

int min, max;

printf("Enter any number: ");
	scanf("%d",&anyNumber);


for (y=0; y < x; y++)
	{
	printf("Enter any number: ");
	scanf("%d",&anyNumber);
	 
	 if(anyNumber > max)
	    max = anyNumber;
	
	 if(anyNumber < min)
	    min = anyNumber;
	}
	printf("\nHighest is:%d",a);
	printf("\nLowest is:%d",c);


ignore the previous code

is that what you seeking for?

int min, max;

printf("Enter any number: ");
scanf("%d",&anyNumber);
min = max = anyNumber;

for (y=0; y < x; y++)
{
	printf("Enter any number: ");
		scanf("%d",&anyNumber);

	if(anyNumber > max)
		max = anyNumber;

	if(anyNumber < min)
		min = anyNumber;
}

printf("\nHighest is:%d",a);
printf("\nLowest is:%d",c); 


This post has been edited by jimblumberg: 14 December 2011 - 06:59 AM
Reason for edit:: Added missing Code Tags

Was This Post Helpful? 0
  • +
  • -

#5 sxintiasz21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 05-September 10

Re: Control Structures

Posted 14 December 2011 - 06:34 AM

thanks a lot for your effort though its still has some problem when it comes to anyNumber.

if anyNumber has a value of 4 so both min and max also have the same value as well.
 min = max = anyNumber; 


and their value will will check if the condition is true right?
if(anyNumber > max)
        max = anyNumber;
       if(anyNumber < min)
        min = anyNumber; 


but what if the value of min and max = 4 and u input a 4 series of numbers greater than (4) there is a problem with the lowest. but determining the highest has no problem. sorry i cnt really explain well.
for example

Enter Series of Number: 4
Enter Number: 5
Enter Number: 6
Enter Number: 12
Enter Number: 9
Enter Number: 34
Highest is: 34
Lowest is: 4

it is also same as the opposite when it comes to highest.
sorry for my bad english.
Was This Post Helpful? 0
  • +
  • -

#6 azemyth  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 25
  • Joined: 12-December 11

Re: Control Structures

Posted 14 December 2011 - 06:51 AM

The first scanning for anyNumber is needed to initialize min and max values.
And then if you wish to go thru the for() loop, you have to scan for the x value. Just as in your original code. (enter the range.. etc..).
Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2486
  • View blog
  • Posts: 8,533
  • Joined: 08-August 08

Re: Control Structures

Posted 14 December 2011 - 06:54 AM

http://www.dreaminco...hy-is-goto-bad/
Was This Post Helpful? 0
  • +
  • -

#8 azemyth  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 25
  • Joined: 12-December 11

Re: Control Structures

Posted 14 December 2011 - 06:56 AM

The very first scanning of anyNumber is the preparation for the for() loop in the future. Actually in somehow its part of it.
Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5676
  • View blog
  • Posts: 22,538
  • Joined: 23-August 08

Re: Control Structures

Posted 14 December 2011 - 06:58 AM

Disagree with this:

Quote

3. stop using curly brackets when you are dealing with only one statement after a conditional expression.


Code is much clearer when EVERY block executed by a conditional expression is enclosed in brackets.
Was This Post Helpful? 2
  • +
  • -

#10 jimblumberg  Icon User is online

  • member icon

Reputation: 3058
  • View blog
  • Posts: 9,305
  • Joined: 25-December 09

Re: Control Structures

Posted 14 December 2011 - 07:06 AM

View Postazemyth, on 14 December 2011 - 06:38 AM, said:

3. stop using curly brackets when you are dealing with only one statement after a conditional expression.


I recommend new users always use the braces {} for every conditional expression. If the braces are always used they will have fewer problems, caused by thinking multiple lines are part of the conditional, instead of just the one line. While using braces for every control statement is never wrong, not using them can cause problems.

Jim
Was This Post Helpful? 1
  • +
  • -

#11 azemyth  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 25
  • Joined: 12-December 11

Re: Control Structures

Posted 14 December 2011 - 07:06 AM

View PostJackOfAllTrades, on 14 December 2011 - 06:58 AM, said:

Disagree with this:

Quote

3. stop using curly brackets when you are dealing with only one statement after a conditional expression.


Code is much clearer when EVERY block executed by a conditional expression is enclosed in brackets.


A matter of taste actually. When your code has 10 IF's in a sequence and each one with only one statement, you wouldn't see anything except a curly bracket forest.
Was This Post Helpful? 0
  • +
  • -

#12 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2486
  • View blog
  • Posts: 8,533
  • Joined: 08-August 08

Re: Control Structures

Posted 14 December 2011 - 09:36 AM

View Postazemyth, on 14 December 2011 - 10:06 AM, said:

A matter of taste actually. When your code has 10 IF's in a sequence and each one with only one statement, you wouldn't see anything except a curly bracket forest.

That's usually a case for switch statements. ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1