473,387 Members | 1,532 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

entering specific amount of integers

I am trying to write the code so it only enters a specific amount of numbers to average using a sentinel value.

[code]#include <stdio.h>
int main ()
{
/* variable definition: */
int count, value, sum; avg;
/* Initialize */
count = 0;
sum = 0; avg = 0.0;
// Loop through to input values
while(1){
printf("Enter the number of integers you want to add and find average of: ");
scanf("%d", &value);
if (value == 10)
break;
sum = sum + value;
count = count + 1; } else {
printf("Value must be positive\n"); }
}
// Calculate avg. of two integers
avg = sum/count;
printf("average is %ld\n ", avg );
return 0;
}
Mar 24 '16 #1
1 3092
donbock
2,426 Expert 2GB
Some comments.
  1. avg is an int so you cannot set it to 0.0.
  2. count = count + 1;} -- What block do you intend to terminate with "}"?
  3. else { printf("Value must be positive\n");} -- What if does this else go with?
  4. What happens if the very first number entered is 10? This will cause a divide-by-zero exception when avg is computed.
  5. print("average is %ld\n",avg); -- %ld is not the correct format specifier for an int like avg.

By the way, you need "[/code]" at the end of the listing in order for your code snippet to be formatted like code. Code formatting would have provided line numbers that I could have used to identify which lines my comments pertain to.

By the way, if you intend to disallow negative input numbers, then a negative value would make an excellent sentinel value to terminate data entry.
Mar 24 '16 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Jeff Melvaine | last post by:
I note that I can write expressions like "1 << 100" and the result is stored as a long integer, which means it is stored as an integer of arbitrary length. I may need to use a large number of...
2
by: Michelle | last post by:
I am a new user and am trying to create a query to show the renewals that will come up within the next two months. I already have: Now()And I am not sure how to input "two months" Please Help
2
by: Rose | last post by:
I have a transactions table with a field named Transaction. I want Transaction to be positive for input and negative for outflow. How do I set up the form for entering outflow so for data entry,...
2
by: Craig | last post by:
I am very new to access and I am trying to get a database set up to gather time data. Most, if not all of my data is in the nn:ss format or minutes and seconds. I am trying to set up a form to...
11
by: H. | last post by:
I'm writing a driver for something I wrote, and can't remember how to do this very simple thing. The code I have at this point in time: int myArr; int temp; int ind = 0; cout << "enter some...
4
by: Lisa | last post by:
Hi. I have an ASP app that allows administrators to add articles to a database for viewing by the public. What I want to do is display a summary of the article and give the user the visitor to the...
4
by: nickcleary1 | last post by:
I need to pass 4 integers between two forms but am having trouble passing more than one.I am hoping to use the contructor approach as i need the variables straight away Any help would be great ...
39
by: Juha Nieminen | last post by:
I was once taught that if some integral value can never have negative values, it's a good style to use an 'unsigned' type for that: It's informative, self-documenting, and you are not wasting half...
1
by: dada1 | last post by:
I was wondering is there any way to make some html element like div, image map or any other linkable elemente to fill only specific amount of pixels. Example. If I click within document I will get...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
marktang
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,...
0
Oralloy
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.