#include<stdio.h>
int main()
{
int n=1;
int average;
int num;
int sum;
for(n=1;num != 9999;n++){
printf("Enter a integer:\n");
scanf("%d",&num);
sum = sum + num;
}
average = sum / n;
printf("average is %d",average);
return 0;
}
why the program end in a wrong result 8 1453
<yu*********@yahoo.cnwrote:
#include<stdio.h>
int main()
{
int n=1;
int average;
int num;
int sum;
for(n=1;num != 9999;n++){
printf("Enter a integer:\n");
scanf("%d",&num);
sum = sum + num;
}
average = sum / n;
average has been declared as an integer. In general, there are decimal
results in averages so change average to a variable of type double. There
may be other problems as well, I didn't look for them.
printf("average is %d",average);
return 0;
}
why the program end in a wrong result
On Nov 3, 9:05*am, yuanshua...@yahoo.cn wrote:
#include<stdio.h>
int main()
{
* * * * int n=1;
* * * * int average;
* * * * int num;
* * * * int sum;
* * * * for(n=1;num != 9999;n++){
* * * * printf("Enter a integer:\n");
* * * * * * * * scanf("%d",&num);
* * * * * * * * sum = sum + num;
* * * * }
* * average = sum / n;
* * * * printf("average is %d",average);
* * * * return 0;}
why the program end in a wrong result
For one, the variables num and sum are not initialized.
HTH yu*********@yahoo.cn wrote:
#include<stdio.h>
int main()
{
int n=1;
'num' initialised to 1, and reinitialized later in the loop, you may
just intialise it once
int average;
int num;
int sum;
'sum' is not initialised
for(n=1;num != 9999;n++){
printf("Enter a integer:\n");
scanf("%d",&num);
'scanf' return value is not checked for errors
>
sum = sum + num;
}
average = sum / n;
'average' is integer, so the result will be such that:
result<= real average< result+1
printf("average is %d",average);
return 0;
}
why the program end in a wrong result
When you add anything to an undefined value (not initialised) the result
is undefined behaviour. That is, you may even get the result you
expected. Now. But not later, specially if you are showing your program
to another party.
Best regards,
Zara
<yu*********@yahoo.cnwrote in message
news:0f**********************************@v13g2000 pro.googlegroups.com...
for(n=1;num != 9999;n++){
That's almost certainly not the loop you want. Apart from the confusion
of having num in the test, even if that is the test you want, note that you
add numbers until you enter a 9999 - but do add in that 9999.
Juan Antonio Zaratiegui Vallecillo wrote:
yu*********@yahoo.cn wrote:
>#include<stdio.h> int main() { int n=1;
'num' initialised to 1, and reinitialized later in the loop, you may
just intialise it once
No, this comment is wrong.
>
> int average; int num; int sum;
'sum' is not initialised
> for(n=1;num != 9999;n++){
Shouldn't it be:
while ( num != 9999 ) {
?
> printf("Enter a integer:\n"); scanf("%d",&num);
'scanf' return value is not checked for errors
>> sum = sum + num; }
average = sum / n;
'average' is integer, so the result will be such that:
result<= real average< result+1
> printf("average is %d",average);
return 0; } why the program end in a wrong result
When you add anything to an undefined value (not initialised) the result
is undefined behaviour. That is, you may even get the result you
expected. Now. But not later, specially if you are showing your program
to another party.
Best regards,
Zara
yu*********@yahoo.cn wrote:
#include<stdio.h>
int main()
{
int n=1;
int average;
int num;
int sum;
for(n=1;num != 9999;n++){
printf("Enter a integer:\n");
scanf("%d",&num);
sum = sum + num;
}
average = sum / n;
printf("average is %d",average);
return 0;
}
why the program end in a wrong result
Because to terminate the program you are entering 9999, which is added
to the sum?
On Nov 3, 11:55*am, Juha Nieminen <nos...@thanks.invalidwrote:
yuanshua...@yahoo.cn wrote:
#include<stdio.h>
int main()
{
* *int n=1;
* *int average;
* *int num;
* *int sum;
* *for(n=1;num != 9999;n++){
* *printf("Enter a integer:\n");
* * * * * *scanf("%d",&num);
* * * * * *sum = sum + num;
* *}
* * average = sum / n;
* *printf("average is %d",average);
* *return 0;
}
why the program end in a wrong result
* Because to terminate the program you are entering 9999, which is added
to the sum?- Hide quoted text -
- Show quoted text -
Also, n is incorrect when the loop terminates.
--
Fred
On Nov 3, 4:05*pm, yuanshua...@yahoo.cn wrote:
#include<stdio.h>
int main()
{
* * * * int n=1;
* * * * int average;
* * * * int num;
* * * * int sum;
* * * * for(n=1;num != 9999;n++){
Reading 'num' without being initialized yield undefined behavior. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Paul T. Rong |
last post by:
Dear all,
My aim is to compact and repair current database, I got the following code
from http://www.mvps.org/access/general/gen0041.htm
Option Compare Database
' ***** Code Start *****...
|
by: Paul T. RONG |
last post by:
Hello,
I am making a restaurant database (it is much more complicated than I
thought before!), now it comes to the last stage and I come across a
problem. I will explain it in detail. In a...
|
by: Nikola |
last post by:
compiler says: function undeclared how come???
help!
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
struct lista{
int element;
struct lista *next;
}*pocetak;
|
by: Red Dragon |
last post by:
I am self study C program student.
Hope someone can help me with this problem.
This program generates random numbers over a user defined range using call function
I used the call function "...
|
by: bughunter |
last post by:
IMHO, statements like this is mistake typically.
May be more better made this construction - I said about empty WHERE -
invalid? A lot of data will saved... :-)
Andy
|
by: Lad |
last post by:
I use the following code to sort dictionary.
Olddict={'r':4,'c':1,'d':2,'e':3,'f':2}
Newdict={}
i =
i.sort() # by val
i.reverse() # Get largest first.
for (val, key) in i:
print key,val
|
by: Frank-O |
last post by:
Hi ,
Recently I have been commited to the task of "translating" some complex
statistical algorithms from Matlab to C++. The goal is to be three
times as fast as matlab ( the latest) .
I've...
|
by: Winston |
last post by:
Where is the mistake?
I want to make a simple menu. These are two pieces of two files...
function ShowMenu(objeto) {
is_open = document.getElementById(objeto).style.display;...
|
by: |
last post by:
I have learned about compartmentalizing my code base using Class Libraries.
I have my common code such as my ORM framework broken out into their own
Class Libraries, which are referenced as...
|
by: Eglute |
last post by:
Hello. I have a problem. I am a begginer in PHP. I wrote the code:
<?
$variable=5;
$variable<10? {$ans="less";
echo $ans."<br>";
echo "variable=".$variable."<br>";}:
...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |