Thread: round() function

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    28

    round() function

    im sorry to have to post for something like this but i am desperate.


    in what .h can the round() function be found? again, im sorry to have to make a new thread for something like this :/

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    28
    while you're at it ( )
    how can i, for example, define an array with an user-defined size?

    as in, i need a proggy in which the user inputs the number of "digits" in the array, and then defines each

    only thing that comes to mind is make something like:
    1. tell the user to enter the # of values he wants the array to consist of.
    2. tell the user to enter all the values, and each time he enters a value, lets say, i use a counter to count the # of values he used.

    only problem is, how then do i arrange those values in the array?

    ive tried declaring a variable n so the user lets the proggy know how many values he will use, but then i cant declare an array like w[n] cause it says:
    [quote]expected constant expression
    cannot allocate an array of constant size 0
    'w' : unknown size]

    :/ any help, id appreciate it
    the early bird gets the worm but the second mouse gets the cheese

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    compiler?

    but most likely you will need to use new and delete to allocate memory.

    for example:
    Code:
    int *array;
    int size;
    
    std::cin >> size;
    
    array = new int[size];

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    28
    using MS Visual c++
    this is what i got so far:
    Code:
    #include <iostream.h>
    #include <math.h>
    
    
    
    int main() {
    
        int n;
    
    	
    	cout << "Enter the number of values to process:\n";
    	cin >> n;
    	while(n<0){
    		cout << "Enter the number of values to process:\n";
    		cin >> n;
    	}
    	if(n==0)
    		cout << "No values to process.\n";
      
    	if(n>0){
    		cout << "First Value:\n";
    		cin >> w[0];
    	}
    	for(int i=1;i<n;i++){
    		cout << "Next Value:\n";
    		cin >> w[i];
    	}
    
    
    
    
    	return 0;
    }
    the early bird gets the worm but the second mouse gets the cheese

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    you haven't declared your array.

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    28
    whoa
    you helped
    the early bird gets the worm but the second mouse gets the cheese

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    28
    yeah i took it out to show the code: i didnt want peeps to mock me for my n00b skillz ;/
    the early bird gets the worm but the second mouse gets the cheese

  8. #8
    Registered User
    Join Date
    Feb 2003
    Posts
    28
    Code:
    #include <iostream.h>
    #include <math.h>
    
    
    
    int main() {
    
        int n, *w;
    
    	
    	cout << "Entrar el numero de datos a procesar:\n";
    	cin >> n;
    	w = new int[n];
    	while(n<0){
    		cout << "Entrar el numero de datos a procesar:\n";
    		cin >> n;
    	}
    	if(n==0)
    		cout << "No hay datos para procesar.\n";
      
    	if(n>0){
    		cout << "Primer Numero:\n";
    		cin >> w[0];
    	}
    	for(int i=1;i<n;i++){
    		cout << "Proximo Numero:\n";
    		cin >> w[i];
    	}
    
    
    
    
    	return 0;
    }
    it worked
    the early bird gets the worm but the second mouse gets the cheese

  9. #9
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Originally posted by vege^
    yeah i took it out to show the code: i didnt want peeps to mock me for my n00b skillz ;/
    we're not here to mock you. we understand that you and others may be newbies. I'm still sortof a newbie. we're here to help.

    I'm glad I could help

  10. #10
    Registered User
    Join Date
    Feb 2003
    Posts
    28
    oh i almost forgot: where's the round() function

    i mean, what #include do i have to do in order to use it :/
    the early bird gets the worm but the second mouse gets the cheese

  11. #11
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    on your above code, add this line before return 0;
    Code:
    delete []w;
    as for the round() fxn. i don't know. have you tried msdn

    actually, there are some round fxns in math.h .

    edit: i hope its what you're looking for. so #include <math.h>

  12. #12
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    to round a float/double to nearest int try.....

    int x = floor( your_float_or_double + 0.5 );

    That will round to nearest int. You can wrap that in its own function no problem.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  13. #13
    Registered User
    Join Date
    Feb 2003
    Posts
    28

    Unhappy

    the round function im looking for rounds a number to 2 decimal places
    the professor gave us this example:
    Code:
    round(x*100)/100
    but when i try to make a function like this
    Code:
    int round(int x){
     int r;
    r=(x*100)/100;
    return r;
    }
    it says something about double/int conversions, but even if i change it to double, when i summon the function in the program it still gives me the double/int error :/
    any thoughts?

  14. #14
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    You can still use what i gave you with some creative multiplication and division no problem. Its easy to move a decimal point isnt it.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  15. #15
    Registered User
    Join Date
    Feb 2003
    Posts
    28
    i am teh dumb :/

    if i have:
    Code:
    a=minValue(w,n)-.01;
    	b=maxValue(w,n)+.01;
    	d=(b-a)/5;
    how then would i round d to 2 decimal places using what you gave me? sorry im bothering so much :/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  3. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  4. Replies: 4
    Last Post: 11-23-2003, 07:15 AM
  5. I need some quick help with code.
    By stehigs321 in forum C Programming
    Replies: 35
    Last Post: 10-30-2003, 10:07 PM