# Problem with calculating numbers

• (3 Pages)
• 1
• 2
• 3

## 39 Replies - 3270 Views - Last Post: 20 September 2012 - 07:54 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=292346&amp;s=1428caf2ea0f34ccf40ef2b9aef99e13&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 tomnidi

Reputation: -1
• Posts: 36
• Joined: 18-September 12

# Problem with calculating numbers

Posted 18 September 2012 - 07:57 AM

Hello, I've been lately given a task and I just can't find a satisfying solution. It goes pretty much like this-
I need to write a program (using C) which receives an unknown amount of numbers (last one would be -1). The program calculates the amount of numbers which are higher than the total average. Now the main problem is, that we're not allowed to use any sort of lists,stacks,arrays or recursions. We can use loops though, that's what this is all about. This is our third lesson and we have absolutely no idea how to use the rest of the options, nor we're allowed to. The teacher wants us to practice loops as much as possible, hence we were given this task.

I'd be very glad if any of you could give me a hand with this, I've been smashing my head against the wall the whole day, trying to figure it out. I have absolutely no idea how to even begin writing my code, absolutely none ideas. All I need is a way or explanation, it is very logical and a bit hard for me.

Thank you very much.

Here's a brief example:
For the next input -
1 2 3 4 5 -1
The output shout be 2, because only 4 and 5 are higher than the average, which is 3 in our case.
I can't find any way to store the numbers without using stuff which I'm not supposed to use.
I'd be very glad to be given a tip on how to get to this or anything like that, there's nothing much I can add to this post so please refrain from locking it once again, I really need some help with this.

This is what I came with so far:
```#include <stdio.h>
void main()
{
int sum = 0, idx, num;
int count = 0;
for(scanf("%d", &num),idx=1;num>0;scanf("%d", &num),idx++)
(num>sum/idx)?(count++,sum+=num):(sum+=num);
printf("%d", count);
}
```

That's not how it should be done though. It calculates the avg in each step within my code, that's not the real solution.
Any ideas?

Is This A Good Question/Topic? 0

## Replies To: Problem with calculating numbers

### #2 modi123_1

• Suitor #2

Reputation: 10849
• Posts: 42,530
• Joined: 12-June 08

## Re: Problem with calculating numbers

Posted 18 September 2012 - 07:59 AM

Look - being stuck at the "I don't know where to start" shouldn't be this large of a problem, but if it is why not start with what you do know?

How about just reading in a value. You know you need that functionality so get that down.
How about just reading in an unknown number of values and printing them to the console? A loop would do that, right? Not so bad, right?

Then move to tackling try the math bit. If you did have six variables with the values of '1 2 3 4 5 -1' in each - what would your private methods look like to find the average and what would it look like to find the ones above said average?

Okay so you have all the major parts out of the way except for the housing structure.. think about that.

If you don't know where to start then get going with breaking the problem down - things you do know how to do and things you do not know how to do. Work what you do know (input, variable storage, loops, etc) and then save the rest of the, in this case, time reviewing the structures you have gone over and get down a way to store

• Saucy!

Reputation: 6219
• Posts: 23,965
• Joined: 23-August 08

## Re: Problem with calculating numbers

Posted 18 September 2012 - 08:05 AM

I can only assume, given the obfuscated nature of this code

```#include <stdio.h>
void main()
{
int sum = 0, idx, num;
int count = 0;
for(scanf("%d", &num),idx=1;num>0;scanf("%d", &num),idx++)
(num>sum/idx)?(count++,sum+=num):(sum+=num);
printf("%d", count);
}

```

And if it was given to you by your teacher, then he/she sucks for doing so.

### #4 tomnidi

Reputation: -1
• Posts: 36
• Joined: 18-September 12

## Re: Problem with calculating numbers

Posted 18 September 2012 - 08:13 AM

This code is mine, I was calculating the avg at each step within the loop, I know it's wrong hence I'm asking here for help.

Quote

Look - being stuck at the "I don't know where to start" shouldn't be this large of a problem, but if it is why not start with what you do know?

How about just reading in a value. You know you need that functionality so get that down.
How about just reading in an unknown number of values and printing them to the console? A loop would do that, right? Not so bad, right?

Then move to tackling try the math bit. If you did have six variables with the values of '1 2 3 4 5 -1' in each - what would your private methods look like to find the average and what would it look like to find the ones above said average?

Okay so you have all the major parts out of the way except for the housing structure.. think about that.

If you don't know where to start then get going with breaking the problem down - things you do know how to do and things you do not know how to do. Work what you do know (input, variable storage, loops, etc) and then save the rest of the, in this case, time reviewing the structures you have gone over and get down a way to store

I tried splitting it into a few steps to make it easier for me, but still - no luck for me.

```#include <stdio.h>
void main()
{
int sum = 0, idx, num;
int count = 0;

// printing each number

/*for(scanf("%d", &num);num>0;scanf("%d", &num))
printf("%d", num);
*/

// printing the avg

/*
for(scanf("%d", &num),idx=0;num>0;scanf("%d", &num),idx++) // printing each number
sum+=num;
printf("%d", sum/idx);
*/

// checking how many numbers are above the avg

/*for(loop which goes through the numbers, somehow)
if(number>sum/idx)
count++;
printf("%d", count);
*/
}
```

I can't find a way to get back to the numbers after the avg is calculated. I can't check if the number is higher than the avg before I've got the final avg, right? Then I need to get back to the original numbers somehow.

This code is mine, I was calculating the avg at each step within the loop, I know it's wrong hence I'm asking here for help.

Quote

Look - being stuck at the "I don't know where to start" shouldn't be this large of a problem, but if it is why not start with what you do know?

How about just reading in a value. You know you need that functionality so get that down.
How about just reading in an unknown number of values and printing them to the console? A loop would do that, right? Not so bad, right?

Then move to tackling try the math bit. If you did have six variables with the values of '1 2 3 4 5 -1' in each - what would your private methods look like to find the average and what would it look like to find the ones above said average?

Okay so you have all the major parts out of the way except for the housing structure.. think about that.

If you don't know where to start then get going with breaking the problem down - things you do know how to do and things you do not know how to do. Work what you do know (input, variable storage, loops, etc) and then save the rest of the, in this case, time reviewing the structures you have gone over and get down a way to store

I tried splitting it into a few steps to make it easier for me, but still - no luck for me.

```#include <stdio.h>
void main()
{
int sum = 0, idx, num;
int count = 0;

// printing each number

/*for(scanf("%d", &num);num>0;scanf("%d", &num))
printf("%d", num);
*/

// printing the avg

/*
for(scanf("%d", &num),idx=0;num>0;scanf("%d", &num),idx++) // printing each number
sum+=num;
printf("%d", sum/idx);
*/

// checking how many numbers are above the avg

/*for(loop which goes through the numbers, somehow)
if(number>sum/idx)
count++;
printf("%d", count);
*/
}
```

I can't find a way to get back to the numbers after the avg is calculated. I can't check if the number is higher than the avg before I've got the final avg, right? Then I need to get back to the original numbers somehow.

### #5 rethc

Reputation: 12
• Posts: 76
• Joined: 23-April 12

## Re: Problem with calculating numbers

Posted 18 September 2012 - 08:23 AM

use while loop

### #6 tomnidi

Reputation: -1
• Posts: 36
• Joined: 18-September 12

## Re: Problem with calculating numbers

Posted 18 September 2012 - 08:27 AM

It doesn't help much.

### #7 modi123_1

• Suitor #2

Reputation: 10849
• Posts: 42,530
• Joined: 12-June 08

## Re: Problem with calculating numbers

Posted 18 September 2012 - 08:36 AM

He's right - stop thinking in terms of a finite set of a for-loop, but more general and abstract of a while loop. I don't see you tracking how many numbers are entered.. I don't see you taking in user input (until the stop command is sent.. which I am guessing is -1)... Come on... basic bits need to be worked out and not just a spew of commented out green what-evers..

### #8 tomnidi

Reputation: -1
• Posts: 36
• Joined: 18-September 12

## Re: Problem with calculating numbers

Posted 18 September 2012 - 08:57 AM

The user input is being scanned into the var 'num'. I can check how many numbers the user entered because I've got the var 'idx', which is being increased by one each time a new number is being scanned. I can't see the different between a while and a for loop in this task, I can't seem to find a way to restore my original numbers.

I managed to calculate the avg, that means I do know how many numbers are entered, right? How about you direct me to something I can use, because so far I haven't been given a single tip but to use a while loop instead of a for loop, which doesn't help me much in my case, not that I see it.

### #9 modi123_1

• Suitor #2

Reputation: 10849
• Posts: 42,530
• Joined: 12-June 08

## Re: Problem with calculating numbers

Posted 18 September 2012 - 09:08 AM

Quote

How about you direct me to something I can use, because so far I haven't been given a single tip but to use a while loop instead of a for loop,

I am always a big fan of dialing down on the tone, and perhaps start a discussion on options for holding data and the specifics of the homework assignment.

How is entry happening? In one single string of "1 2 3 4 5 -1" or in individual segments like:
?

Then there's checking that you are certain you cannot use a structure?

Oh..oh so many things not quite discussed or made clear in the problem set!

### #10 tomnidi

Reputation: -1
• Posts: 36
• Joined: 18-September 12

## Re: Problem with calculating numbers

Posted 18 September 2012 - 09:17 AM

I can not use any structures because we haven't learned them yet. The input is an unknown amount of numbers until the last number (-1) is being scanned. The program needs to calculate the amount of numbers which are above the avg. Here's the problem - you don't have an access to those numbers after you scan a new one, that means you've got a problem. You need to calculate the avg first and then check which numbers are higher than the avg, but you can't store them using arrays, structures, lists, stacks or recursions.

### #11 modi123_1

• Suitor #2

Reputation: 10849
• Posts: 42,530
• Joined: 12-June 08

## Re: Problem with calculating numbers

Posted 18 September 2012 - 09:19 AM

Yes, you have repeated that a few times. My question was is the input one long string of numbers separated by spaces or someone typing in a number, hitting enter, typing in another, hitting enter, etc, until -1 is entered?

### #12 tomnidi

Reputation: -1
• Posts: 36
• Joined: 18-September 12

## Re: Problem with calculating numbers

Posted 18 September 2012 - 09:21 AM

num always scan one number, that means the numbers are being scanned one by one.

### #13 modi123_1

• Suitor #2

Reputation: 10849
• Posts: 42,530
• Joined: 12-June 08

## Re: Problem with calculating numbers

Posted 18 September 2012 - 09:34 AM

That's not quite answering the question. If you were to use a straight up plain ol' CIN to get the input. Does the input come in as one giant character array with numbers, white spaces, etc, or would the user have to type in each

example of input for the second:
```Int32 input = 0;
while (input != -1)
{
cin >> input;
cout << input;
}

```

Again, what I am trying to get at is if you have a character array, or string, of numbers and spaces then you can work off that array..

### #14 tomnidi

Reputation: -1
• Posts: 36
• Joined: 18-September 12

## Re: Problem with calculating numbers

Posted 18 September 2012 - 10:06 AM

The user have to type in each.

### #15 modi123_1

• Suitor #2

Reputation: 10849
• Posts: 42,530
• Joined: 12-June 08

## Re: Problem with calculating numbers

Posted 18 September 2012 - 10:11 AM

Wheew.. wow. Okay.. now we are moving. What section of your book/lecture is this homework tied to? Perhaps pointers?