COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### i have a error in this program plz help me

• Follow

```My name is khan
#include <stdio.h>
#include <math.h>
#include <conio.h>

void main()
{
int a,b,c,d,e,sum=0,i;
clrscr();
printf("\nenter five numbers:");
scanf("%d%d%d%d%",&a,&b,&c,&d,&e);
sum=a+b+c+d+e;
for (i=sum;i<=sum;i--)
{
if (i==a||i==b||i==c||i==d||i==e)
{
printf("the largest number:%d",i);
exit();
}
}
getch();
}
```
 0

```khan wrote:

> scanf("%d%d%d%d%",&a,&b,&c,&d,&e);

Four of these "%d"
Five of these (&)

--
pete
```
 0

```khan <mehaboob.khan11@gmail.com> wrote:
> My name is khan
> #include <stdio.h>
> #include <math.h>
> #include <conio.h>

Note: that's a non-standard header file.

> void main()

int main( void )

> {
> int a,b,c,d,e,sum=0,i;
> clrscr();

Note: that's not a standard function

> printf("\nenter five numbers:");
> scanf("%d%d%d%d%",&a,&b,&c,&d,&e);

Pete already pointed out the problem here. And it might
be prudent to check the return value to see if the pro-
gram was able to read the requested five numbers - if
the user e.g. entered a letter instead of number scanf()
will abort and you then use uninitialized variables in
the following calculations.

> sum=a+b+c+d+e;
> for (i=sum;i<=sum;i--)
> {
> if (i==a||i==b||i==c||i==d||i==e)
> {
> printf("the largest number:%d",i);

If one of the numbers was negative then 'sum' might be smaller
than the largest value (e.g. if the user enters 14, -15, 0, 1, 2
'sum' will be 2) and you will never find the largest one this
way.

And if the user entered e.g. "-1 -1 1 0 -1" then it's even
worse since in that case 'sum' will be smaller than any of
the numbers and your loop will fail badly (i.e. 'i' will get
decremented over and over again until the smallest possible
int is reached and what happens then is undefined).

> exit();

The exit() function is supposed to have an argument, which
is used like the return value of main().

> }
> }
> getch();

Note: another non-standard function.

And since main() is supposed to return an int a line with e.g.

return 0;

would be nice.

> }
Regards, Jens
--
\   Jens Thoms Toerring  ___      jt@toerring.de
\__________________________      http://toerring.de
```
 0
Reply jt68 (1134) 8/14/2010 1:09:46 PM

```On Sat, 14 Aug 2010 08:15:09 -0400, khan <mehaboob.khan11@gmail.com> wrote:
....
> for (i=sum;i<=sum;i--)

You have many errors in this program.  This is just one of them.
Look at the initial condition of the loop, the termination condition,
and the "increment" operation.  Does your termination condition, i <= sum,
make ANY logical sense at all?

i.e. some blank lines and some spaces between character.  It would also
comments in your code explaining what the program is supposed to do,
and what each part of the program should be doing.

calls at various places, to verify whether what's happening in the code
is what you think is happening.  In the program in question, the
following would probably have helped you find the problem on your own:

printf("\nenter five numbers:");
scanf("%d%d%d%d%", &a, &b, &c, &d, &e);
printf("The numbers you entered are %d %d %d %d %d\n", a, b, c, d, e);

sum=a + b + c + d + e;
printf("The sum of the numbers is %d\n", sum);

--
Morris Keesan -- mkeesan@post.harvard.edu
```
 0
Reply mkeesan (160) 8/14/2010 3:39:07 PM

```On 14/08/10 13:15, khan wrote:
> #include <stdio.h>
> #include <math.h>
> #include <conio.h>
>
> void main()
> {
> int a,b,c,d,e,sum=0,i;
> clrscr();
> printf("\nenter five numbers:");
> scanf("%d%d%d%d%",&a,&b,&c,&d,&e);
> sum=a+b+c+d+e;
> for (i=sum;i<=sum;i--)
> {
> if (i==a||i==b||i==c||i==d||i==e)
> {
> printf("the largest number:%d",i);
> exit();
> }
> }
> getch();
> }

1. Your scanf is broken. Users will probably enter whitespace between
numbers. The number of addresses and placeholders should match.
Unattached % symbols in the scanf format specifier may not be valid.

2. Your "find the largest number" logic may take some time if all the
numbers are very big. It could be made to run a lot faster.

3. Your "find the largest number" logic may fail completely if the sum
of the 5 integers is greater than the maximum value for an integer. An
integer on a modern PC is typically 32 bits with a range from
-2147483648 to 2147483647. If the five values entered are 2147483647
2147483646 2147483645 2147483644 and 2147483643, the sum is bigger than
the number that can be stored in an integer. With your logic, this may
not find the highest number. On my computer, your logic takes about 2
minutes to fail to find a solution for those 5 values.

4. It's "int main()" and "return 0";

I might code such a program as follows:

/****** code starts ******/
#include <stdio.h>
int main(){int a,b=0x80,i=5;b=b<<(sizeof(int)-1)*8;
printf("Enter five numbers between %d and %d:",b,b-1);
while(i--){scanf("%d",&a);b=(a>b)?a:b;}
printf("the largest number: %d\n",b);return 0;}
/******* code ends *******/

Rgds

Denis McMahon

p.s. Homework sensors tingling, code squashed!
```
 0

```In article <4C6689CD.61DB@mindspring.com>,
pete  <pfiland@mindspring.com> wrote:
> khan wrote:
>
> > scanf("%d%d%d%d%",&a,&b,&c,&d,&e);
>
> Four of these "%d"
> Five of these (&)

Also an extra "%" at the end.  I didn't spot that myself, but
gcc did, with the "-Wall" flag [*], and it also warned about the
mismatch you noticed.

[*] Loosely speaking, "warn [about] all", though as has been
pointed on many times in this group, the "all" shouldn't be taken
to mean "all possible warnings" but ....  Oh well, here's what
the man page on my system says about it:

"This enables all the warnings about constructions that some users
consider questionable, and that are easy to avoid (or modify to
prevent the warning), even in conjunction with macros."

There are other flags that enable still more warnings.  There is
some diversity of opinion in this group about exactly which ones
are valuable.  I lack the time and energy to say more about that
right now.

Anyway, my point was:

I strongly recommend that the OP try to find out whether his(?)
compiler has options to enable additional warnings, and turn them
on.  Just "-Wall" has saved me debugging time on many occasions.

--
B. L. Massingill
ObDisclaimer:  I don't speak for my employers; they return the favor.
```
 0

```On 14 Aug, 13:15, khan <mehaboob.kha...@gmail.com> wrote:

<snip>

> printf("\nenter five numbers:");
> scanf("%d%d%d%d%",&a,&b,&c,&d,&e);

on some systems the prompt won't appear. Either terminate your
printf() string with a newline or call fflush(stdout). The usual
convention is to end a string with an newline rather than start with a
newline.

<snip>

```
 0

```On Sat, 14 Aug 2010 16:53:54 +0100, Denis McMahon

> On 14/08/10 13:15, khan wrote:
<snip most>
> > scanf("%d%d%d%d%",&a,&b,&c,&d,&e);

> 1. Your scanf is broken. Users will probably enter whitespace between
> numbers. The number of addresses and placeholders should match.
> Unattached % symbols in the scanf format specifier may not be valid.
>
Whitespace is not a problem; %d skips whitespace if present and then
parses a (signed, decimal) number. In fact all *scanf conversions
except %c and %[..] skip leading whitespace. (And %n which isn't a
real conversion and %% which isn't a conversion at all.) It might be
clearer to show a space in the format string, but it's not necessary.

Your other subpoints here and following points (snipped) are correct.

> 4. It's "int main()" and "return 0";
>
Better but not (always) required. In C89, the return type isn't
required but is good style; and 'int main (void)' is even better, for
consistency with other routines where prototypes provide a positive
benefit. Lack of a return statement (or exit() call) produces an
unspecified exit status, but nothing worse; and on some systems or in
some situations an unspecified exit status doesn't matter. In C99 the
return type is required, and again '(void)' is better; and the return
statement is not needed at all, a new wart requires as-if return 0,
but having it is good style.

> I might code such a program as follows:
>
> /****** code starts ******/
> #include <stdio.h>
> int main(){int a,b=0x80,i=5;b=b<<(sizeof(int)-1)*8;

There is no guarantee 0x80 << bits gives INT_MIN, or even works at
all. Use INT_MIN, it's much clearer and that's what it's there for.

> printf("Enter five numbers between %d and %d:",b,b-1);

Similary, INT_MIN - 1 doesn't necessarily give INT_MAX.

> while(i--){scanf("%d",&a);b=(a>b)?a:b;}

Should check scanf() return to see if bogus input or EOF encountered.
Formally scanf numeric conversions have Undefined Behavior on
out of range values, so the only officially safe way is to get the
characters, typically with fgets(), and convert with strtol/etc.;
but in practice IME this is rarely a problem.

> printf("the largest number: %d\n",b);return 0;}
> /******* code ends *******/

> p.s. Homework sensors tingling, code squashed!
```
 0

7 Replies
26 Views

Similiar Articles:

7/23/2012 2:35:00 AM