Problem with calculating numbers

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

39 Replies - 2294 Views - Last Post: 20 September 2012 - 07:54 AM Rate Topic: -----

#1 tomnidi  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8902
  • View blog
  • Posts: 33,382
  • 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
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6033
  • View blog
  • Posts: 23,415
  • 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);
}



that you found it somewhere. FORGET ABOUT THIS CODE. It's shit.

And if it was given to you by your teacher, then he/she sucks for doing so.
Was This Post Helpful? 0
  • +
  • -

#4 tomnidi  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 rethc  Icon User is offline

  • D.I.C Head

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

Re: Problem with calculating numbers

Posted 18 September 2012 - 08:23 AM

use while loop
Was This Post Helpful? 0
  • +
  • -

#6 tomnidi  Icon User is offline

  • New D.I.C Head

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

Re: Problem with calculating numbers

Posted 18 September 2012 - 08:27 AM

It doesn't help much.
Was This Post Helpful? 0
  • +
  • -

#7 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8902
  • View blog
  • Posts: 33,382
  • 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..
Was This Post Helpful? 0
  • +
  • -

#8 tomnidi  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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.
Was This Post Helpful? -1
  • +
  • -

#9 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8902
  • View blog
  • Posts: 33,382
  • 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:
read> 1
read> 2
read> 3
read> 4
read> 5
read> -1
?

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!
Was This Post Helpful? 0
  • +
  • -

#10 tomnidi  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#11 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8902
  • View blog
  • Posts: 33,382
  • 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?
Was This Post Helpful? 0
  • +
  • -

#12 tomnidi  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#13 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8902
  • View blog
  • Posts: 33,382
  • 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..
Was This Post Helpful? 0
  • +
  • -

#14 tomnidi  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#15 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8902
  • View blog
  • Posts: 33,382
  • 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?
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3