read integers that returns a negative or prints the average


Posted on 16th Feb 2014 07:03 pm by admin

Im trying to finish this homework assignment by tommarow afternoon and I keep getting stuck.
Write a Function that reads integers from the keyboard. If any are negative, it returns a negative. If all are positive, it returns an average of the numbers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include #include /* Prototype Declarations */ bool anyPositiveEOF (void); float readAverage (void); int main (void) { /* Statements */ if (anyPositiveEOF()) printf("nAll numbers are positiven"); if (readAverage()) printf("nAverage: %fn", readAverage()); else printf("nAll numbers negativen"); getch(); return 0; } /* main */ bool anyPositiveEOF (void) { // Local Declarations bool anyPositive = false; int numIn; // Statements printf("Determine if any number are positiven"); printf("Please enter any integers: n"); while (scanf("%d", &numIn) != EOF) { anyPositive = numIn > 0; if (anyPositive) continue; printf("next number: n"); } // while return false; } float readAverage (void) { // Local Declarations bool breakFlag = true; int x = 0; int y = 0; int z = 0; int count = 0; int n; float sum = 0; while(scanf("%d",&n) != EOF) { if (n != 0) { sum += n; count++; } // if } // while return (sum / count); }

No comments posted yet

Your Answer:

Login to answer
75 Like 12 Dislike
Previous forums Next forums
Other forums

Get Total From While Loop
I'm trying to get the total for each product and add them for a Grand Total to list outside the loop

Add code to enable passing of checkbox variable on login
Ok, I have a login page, but want to add a checkbox that when checked and successful login is made i

CHMOD script
Hi,

I need a script to read all files in a folder and set to 777.

Can anyone help out

function to return url
Hi,

Can you guys help me giving a function that takes all the contents in a particular field

PHP webpage & array print issue
I have this code running, and it works perfectly … however, see my bottom bit about what I see

multiple recipients + dbuser mail recall
Hello,

I am building a php login script.

When the user registers the script will send

urldecode question
How would I format this line of code properly?

<?php echo urldecode($_GET['Title']); ?

how do i display data on a page from mysql
1 - do i use this code at the top of each of my page i wish to only alow access if there as been a s

Redirecting Admin
In my members table, I have a field called "perm" and it's set to zero for all members. Ho

Wierd echo error?
Hi, i got the most wierd php error ever and i don't know why..
Code: echo "<t

Sign up to write
Sign up now if you have flare of writing..
Login   |   Register
Follow Us
Indyaspeak @ Facebook Indyaspeak @ Twitter Indyaspeak @ Pinterest RSS



Play Free Quiz and Win Cash