Thread: Math task

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    11
    Rep Power
    0

    Math task


    Hi guys,
    I need to write a simple task that does the following for exmple:

    Input integer varibles with scanf like this:
    3.4 6.5 -9.0 500.0 34.217 -90.4 0.004 -1.0

    while -1.0 indicating the end of the input.

    After this, I need to printf all this stuff while all integers are showing:

    1. The sum of the numbers (Except -1.0)
    2. The average of the numbers (Except -1.0)
    3. The multiplication of the numbers (Except -1.0)

    Any ideas? I completely lost:\
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Location
    Saint-Petersburg, Russia
    Posts
    236
    Rep Power
    28
    Any ideas? I completely lost
    Obvious idea is that you need to compose yourself and start writing the code. Proposed program is extremely trivial, so you need not fear.

    If you still did not open your book on your programming language - it would be wise to start, I think. Since you mentioned "scanf", I believe "C programming language" by Kernighan & Ritchie will do well (you can find it in pdf from google easily). You only need basic operations with variables (addition, multiplication) and simple loops. It is merely two or three chapters.

    Welcome back when you have any specific question.
    Last edited by jamespayne; October 28th, 2013 at 05:03 PM.
  4. #3
  5. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,075
    Rep Power
    1802
    Originally Posted by adirush
    Input integer varibles with scanf like this:
    3.4 6.5 -9.0 500.0 34.217 -90.4 0.004 -1.0
    I see you really do have a maths problem - those are all examples of real numbers not integers

    Originally Posted by adirush
    while -1.0 indicating the end of the input.
    That is not strictly necessary or even desirable. What if the user wanted to enter -1.0 as an actual value? Also comparing binary floating point values for equality to a decimal value is fraught with problems and is bad practice even if it will work in this case. I'd say that was a poorly defined assignment, bit I'll guess you'll have to stick with it. The better method is to use the return value of scanf() which is a count of the number of successful format specifier matches. If the required input are positive values, then testing for < 0 is safer than testing for -1.0 specifically.


    Originally Posted by adirush
    1. The sum of the numbers (Except -1.0)
    2. The average of the numbers (Except -1.0)
    3. The multiplication of the numbers (Except -1.0)
    Any ideas? I completely lost:\
    If its only the maths you have a problem with rather than the programming, show us what you have so far. Although this is just basic arithmetic. How can you be completely lost? If you attended the lessons and have the course material, surely you have something to start on!?
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    11
    Rep Power
    0

    My Code


    Hi Guys, thank you for your intensive push to investigate and explorer.. frankly i'm not totally lost and i have wrote some code, but i'm sure my way isn't that good.

    My issues so far regarding to;
    1. how to break each number to make "multiplication" calc.. as i wrote this, i think there is no legit way without using arrays. problem is, we didn't learn this by now- and i don't wanna use it.

    2. How do I print the result in way that will be up to 5 decimal numbers in max.

    for instance in the example, input was: 3.4 6.5 -9.0 500.0 34.217 -90.4 0.004 -1.0

    output: 444.72100

    and mine is: 444.721008



    Code:
    int main() {
    
    	float num = 0;
    	float s = 0;
    
    	while (num != -1.0) 
    	{
    		scanf("%f", &num);	
    	
    		if (num!=-1.0)
    			s= num+s;
    	
    		continue;
    
    	} 
    
    		printf("\n\n%lf \n", s);
    	
    }
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,709
    Rep Power
    480
    continue statement at end of while loop does not contribute anything useful.

    Why include a space character immediately before a new line?

    Your data type is float. The correct output format specifier is %f not %lf but, oh help, this might depend on c version. Back in the old days I recall that c promoted float to double when passed as a function argument. In which case %lf should work and may be why
    gcc -Wall
    did not complain.

    Your main program should choose the value it returns to the operating system.

    Code:
    #include<stdio.h>  /* inserted for declarations of scanf and printf */
    int main() {
      float num = 0, s = 0;
      while (num != -1.0) {
        scanf("%f", &num);	
        if (num!=-1.0)
          s += num;
        /* continue statement removed */
      } 
      printf("\n\n%.5f\n", s);
      return 0;  /* return statement inserted */
    }
    And finally, in answer to your question, there's quite a lot to learn about printf formatting. In your case you want to show 5 places following the decimal place. That's where the "%.5f" format comes from.

    Oh, and, just because you're a bit careless, you should not expect as output
    444.72100 .
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,075
    Rep Power
    1802
    Originally Posted by adirush
    how to break each number to make "multiplication" calc.. as i wrote this, i think there is no legit way without using arrays. problem is, we didn't learn this by now- and i don't wanna use it.
    What makes you think an array is necessary? You would loose marks for overcomplicating a simple problem. In your loop you simply need:

    Code:
    sum += num ;
    product *= num ;
    count++ ;
    where count is initialised to zero before the loop, and then after the loop;
    Code:
    mean = sum / count ;

    Originally Posted by adirush
    2. How do I print the result in way that will be up to 5 decimal numbers in max.
    Read the documentation for printf(): here for example
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    11
    Rep Power
    0

    (:


    Thank you dear. your help is golden.
    I'll be fine from here(;

    Adir.

IMN logo majestic logo threadwatch logo seochat tools logo