# Control Structures

Page 1 of 1

## 11 Replies - 316 Views - Last Post: 14 December 2011 - 09:36 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=259812&amp;s=c4fcaf9f2a1c64d9fbc6a73c7a16ece8&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 sxintiasz21

Reputation: 0
• 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

• Saucy!

Reputation: 5676
• 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)
```

### #3 sxintiasz21

Reputation: 0
• 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

### #4 azemyth

Reputation: 2
• 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.

```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

### #5 sxintiasz21

Reputation: 0
• 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.

### #6 azemyth

Reputation: 2
• 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..).

### #7 CTphpnwb

• D.I.C Lover

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

## Re: Control Structures

Posted 14 December 2011 - 06:54 AM

### #8 azemyth

Reputation: 2
• 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.

• Saucy!

Reputation: 5676
• 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.

### #10 jimblumberg

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

## Re: Control Structures

Posted 14 December 2011 - 07:06 AM

azemyth, 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

### #11 azemyth

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

## Re: Control Structures

Posted 14 December 2011 - 07:06 AM

JackOfAllTrades, 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.

### #12 CTphpnwb

• D.I.C Lover

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

## Re: Control Structures

Posted 14 December 2011 - 09:36 AM

azemyth, 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.