Thread: needs help in c programming!!!!!

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    24

    needs help in c programming!!!!!

    I want write a program that convert the decimal
    number to the nearest integer. Round up if the decimal place(s) is greater than or equal to 0.5.
    Otherwise, round down the number.
    After converting to the integer, calculate and print out all factors of the number followed by the
    total number of factors found.
    After that, your program should ask the user to enter a positive number (less than 1000) with
    decimal place(s) and to repeat the process as described above. In addition, your program should ask
    the user whether he/she wants to run the program again. If so, prompt the user to enter a valid
    positive number again and so on. There is no preset number of times that the user can enter a
    number. The program will be terminated when it receives appropriate input from the user.


    Code:
    #include <stdio.h>
    #include<math.h>
    main(void)
    {
    	char choose;
    	float num_unround;
    	printf("*****     Student Name : Kan Ka Him *****\n");
    	printf("*****     Student Num. : 10022177A  *****\n");
    	printf("*****   C compiler Used : Dev-C++ *****\n");
    	printf("The decimal number formed by the student number is: \n");
    	scanf("%f", &num_unround);
    	int to_round(float num_unround);
    	int is_factor(int x);
    	printf ("Would you like to enter the number again?  (y/n)");
    	scanf("\n%c",&choose);
    	while (choose == 'Y' || choose == 'y')
    	{
              printf("Enter a positive number with decimal place(s)\n");
              printf("[Value less than 1000]:");
              scanf("\n%f",&num_unround);
              if (num_unround <0)
              {
              printf("Please enter a number greater than 0 : ");
              scanf("\n%f",num_unround);
              }
              if (num_unround > 1000)
              {
              printf("Please enter a number less than 1000 : ");
              scanf("\n%f",num_unround);
              }
    	{
    		int to_round;
    		int is_factor;
    		printf ("Would you like to enter the number again?  (y/n)");
    	    scanf("\n%c",&choose);
    	}
    	while (choose == 'N' || choose == 'n')
    	{
    		printf("<<<<<Program Completed>>>>>");
    	}
    	}
    	int to_round (float num_unround)
    	{
            int num_round;
    		if (num_unround <10 || num_unround > 0)
    		{
    			num_unround = num_unround*10;
    		    num_unround = num_unround+5;
    		    num_round = (num_unround/10) ;
    		    printf("After rounding, the numer is : %d \n\n", num_round);
    		    
    		}
    		if (num_unround <100 || num_unround >= 10)
    		{
    			num_unround = num_unround*100;
    		    num_unround = num_unround+50;
    		    num_round = (num_unround/100);
    		    printf("After rounding, the numer is : %d \n\n", num_round);
    		}
    		if (num_unround <1000 || num_unround >=100)
    		{
    			num_unround = num_unround*1000;
    		    num_unround = num_unround+500;
    		    num_round = (num_unround/1000);
    		    printf("After rounding, the numer is : %d \n\n", num_round);
    		}
    		if (num_unround > 1000)
    		{
    			printf("Please enter a value less than 1000 : ");
    			scanf("%f", &num_unround);
    		}
    		if (num_unround < 0)
    		{
    			printf("Please enter a positive number : ");
    			scanf("%f", &num_unround);
    		}
    	}
    	int is_factor(int num_round)
    	{
    		int num, i;
        printf ("The factors of %d are:\n",num_round);
        scanf ("%d",&num);
        printf ("The factors of %d =",num);
        for(i=1; i<=num; i++)
    {
        if(num%i==0)
             printf("Factor = %d",i);
    }
    }
    }
    Also, what is the meaning of"static declaration of 'to_round' follows non-static declaration " and " previous declaration of 'to_round' was there"
    THX!!!

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    It means to_round is declared twice in the program. Once near the top, and once just below "program completed".

    Although your compiler allows you to declare variables throughout your program (it appears), it may not be wise to do so. Keeping a short list of them when they are all declared at the top of your function, helps eliminate this error, and others as well.

    Welcome to the forum, Fail50!

    I have some suggestions for you:

    main always return int, to the OS, in C so:
    "int main(void)", at the start of your main function, and "return 0", at the end of it.


    You have nice indentation, but still the program has:

    }
    }

    Which shouldn't be there. The compiler won't care, but indenting ALL the subordinate code lines, makes it 10X easier to debug LOTS of program errors.

    That's a very long one-function program. Try to break up your program into smaller functions. Then, as you go forward in your programming. Check each function for correctness of syntax, and accuracy. It makes the big problems, much smaller, and easier to find and fix.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    24
    Quote Originally Posted by Adak View Post
    It means to_round is declared twice in the program. Once near the top, and once just below "program completed".

    Although your compiler allows you to declare variables throughout your program (it appears), it may not be wise to do so. Keeping a short list of them when they are all declared at the top of your function, helps eliminate this error, and others as well.

    Welcome to the forum, Fail50!

    I have some suggestions for you:

    main always return int, to the OS, in C so:
    "int main(void)", at the start of your main function, and "return 0", at the end of it.


    You have nice indentation, but still the program has:

    }
    }

    Which shouldn't be there. The compiler won't care, but indenting ALL the subordinate code lines, makes it 10X easier to debug LOTS of program errors.

    That's a very long one-function program. Try to break up your program into smaller functions. Then, as you go forward in your programming. Check each function for correctness of syntax, and accuracy. It makes the big problems, much smaller, and easier to find and fix.
    and I also want to ask...
    how can I call the function??
    thx

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    A brief intro to functions...

    your function declaration goes between your C libraries and the body of main. This simply lets the compiler know that it is going to bump into a function at some point later in the program....

    The function declaration takes in as many parameters as you decide to pass into it from main...

    suppose you want a function that simply subtracts the points taken off of an exam and returns the students grade...

    FUNCTION DECLARATION:
    Code:
    int studentGrade(int perfectTestScore, int pointTakenOff);

    reading it from left to right,
    the first int is the return type... since in the function above I am trying to figure out what a student grade is, I am going to return an integer to main...

    studentGrade is the name of the function... and you can call it anything you want

    int perfectTestScore is a perfect grade, it can be 10, 100, 105, etc... this value is assigned in main.

    int pointsTakenOff is the points counted off which is also assigned in main...


    once you decide to call the function in main, this is how you do it:

    FUNCTION CALL:
    Code:
    studentGrade(perfectScore, pointsOff);
    It is important to know that the variable names in the function declaration are completely independent of the function call in main. If you pass in perfectScore as your first parameter in your function call then once it reaches the actual function body, perfectScore is going to be treated as perfectTestScore.


    if you wanted to print out the score in MAIN for example, you would treat your function call as an INTEGER since its return type is int.

    Code:
    printf("Your score is %d.", studentGrade(perfectScore, pointsOff));
    Last edited by matthayzon89; 10-19-2010 at 10:57 AM.
    Linklists use recursion to store what? ..... floats!

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Say you want a function to actually do the rounding in. Call it
    roundIt();

    What else will it need? Student number (which should be put into a string maybe, because it might be very long), I believe, change it to:
    Code:
    void roundIt(char *student_num) {
    }
    
    Which makes rounding easy. 
    
    So declare a char array:
    char student_num[20];
    
    And put the digits into that char array:
    fgets(student_num, sizeof(student_num), stdin);
    
    remove the newline char from student_num:
    length = strlen(student_num);
    if((*student_num[length - 1])=='\n')
      *student_num[length-1]=='\0';   //overwrite the newline with end of string char
    
    then round the last digit up or down:
    if(*student_num[length-1] >4)
       //increment (*student_num[length-2])++
    *student_num[length-1]=='0';
    something like that. You don't need to return anything, because you're changing the actual student_num, array.

    You could print out the rounded "number" in the char array student_num, or you could change the char array back into a number:

    long int num = atol(student_num);

    There are also similar functions to change strings to double: strtod(), strtoul() for unsigned longs, etc.

    questions?
    Last edited by Adak; 10-19-2010 at 11:04 AM.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    24
    Quote Originally Posted by matthayzon89 View Post
    A brief intro to functions...

    your function declaration goes between your C libraries and the body of main. This simply lets the compiler know that it is going to bump into a function at some point later in the program....

    The function declaration takes in as many parameters as you decide to pass into it from main...

    suppose you want a function that simply subtracts the points taken off of an exam and returns the students grade...

    FUNCTION DECLARATION:
    Code:
    int studentGrade(int perfectTestScore, int pointTakenOff);

    reading it from left to right,
    the first int is the return type... since in the function above I am trying to figure out what a student grade is, I am going to return an integer to main...

    studentGrade is the name of the function... and you can call it anything you want

    int perfectTestScore is a perfect grade, it can be 10, 100, 105, etc... this value is assigned in main.

    int pointsTakenOff is the points counted off which is also assigned in main...


    once you decide to call the function in main, this is how you do it:

    FUNCTION CALL:
    Code:
    studentGrade(perfectScore, pointsOff);
    It is important to know that the variable names in the function declaration are completely independent of the function call in main. If you pass in perfectScore as your first parameter in your function call then once it reaches the actual function body, perfectScore is going to be treated as perfectTestScore.


    if you wanted to print out the score in MAIN for example, you would treat your function call as an INTEGER since its return type is int.

    Code:
    printf("Your score is %d.", studentGrade(perfectScore, pointsOff));
    What should I type if I want to call the function 'to_round' and 'is_factor' between
    Code:
    scanf("%f", &num_unround);
    and
    Code:
    printf ("Would you like to enter the number again?  (y/n)");
    and also print it out like this
    http://i1128.photobucket.com/albums/...g?t=1287644969

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    24
    Can anyone can help me??

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Not at 4 a.m. (our time)

    This is a version of your program, that I can compile without errors. What is it supposed to do (I don't know what numbers to enter right off), and what errors does it produce?

    Work with this version, or replace it with a version that WILL compile, or I can't help you. I will NOT reformat / redo your program, so it will compile, again.

    Code:
    #include <stdio.h>
    #include<math.h>
    
    int to_round(float num_unround);
    int is_factor(int x);
    
    int main(void)
    {
    	char choose;
      int is_factor;
    	float num_unround;
    	printf("*****     Student Name : Kan Ka Him *****\n");
    	printf("*****     Student Num. : 10022177A  *****\n");
    	printf("*****  C compiler Used : Dev-C++    *****\n");
    	printf("The decimal number formed by the student number is: \n");
    	scanf("%f", &num_unround);
    	printf ("Would you like to enter the number again?  (y/n)");
    	scanf("\n%c",&choose);
    	while (choose == 'Y' || choose == 'y') 	{
        printf("Enter a positive number with decimal place(s)\n");
        printf("[Value less than 1000]:");
        scanf("\n%f",&num_unround);
        if (num_unround <0)  {
          printf("Please enter a number greater than 0 : ");
          scanf("\n%f",num_unround);
        }
        if (num_unround > 1000)  {
          printf("Please enter a number less than 1000 : ");
          scanf("\n%f",num_unround);
        }
    		printf ("Would you like to enter the number again?  (y/n)");
        scanf("\n%c",&choose);
    	}
    	while (choose == 'N' || choose == 'n')
    	{
    		printf("<<<<<Program Completed>>>>>");
    	}
      return 0;
    }
    int to_round (float num_unround)
    	{
        int num_round;
    		if (num_unround <10 || num_unround > 0)
    		{
    			num_unround = num_unround*10;
    		    num_unround = num_unround+5;
    		    num_round = (num_unround/10) ;
    		    printf("After rounding, the numer is : %d \n\n", num_round);
    		    
    		}
    		if (num_unround <100 || num_unround >= 10)
    		{
    			num_unround = num_unround*100;
    		    num_unround = num_unround+50;
    		    num_round = (num_unround/100);
    		    printf("After rounding, the numer is : %d \n\n", num_round);
    		}
    		if (num_unround <1000 || num_unround >=100)
    		{
    			num_unround = num_unround*1000;
    		    num_unround = num_unround+500;
    		    num_round = (num_unround/1000);
    		    printf("After rounding, the numer is : %d \n\n", num_round);
    		}
    		if (num_unround > 1000)
    		{
    			printf("Please enter a value less than 1000 : ");
    			scanf("%f", &num_unround);
    		}
    		if (num_unround < 0)
    		{
    			printf("Please enter a positive number : ");
    			scanf("%f", &num_unround);
    		}
       return num_round; //??
    }
    int is_factor(int num_round) 	{
    	int num, i;
      printf ("The factors of %d are:\n",num_round);
      scanf ("%d",&num);
      printf ("The factors of %d =",num);
      for(i=1; i<=num; i++) {
        if(num%i==0)
          printf("Factor = %d",i);
      }
      return i;
    }

  9. #9
    Registered User
    Join Date
    Oct 2010
    Posts
    24
    Code:
    #include <stdio.h>
    #include<math.h>
    int to_round(float num_unround);
    int is_factor(int x);
    main()
    {
    	char choose;
    	float num_unround;
    	printf("*****     Student Name : Kan Ka Him *****\n");
    	printf("*****     Student Num. : 10022177A  *****\n");
    	printf("*****   C compiler Used : Dev-C++ *****\n");
    	printf("The decimal number formed by the student number is: ");
    	scanf("%f", &num_unround);
    	to_round;
    	is_factor;
        printf ("Would you like to enter the number again?  (y/n)");
    	scanf("\n%c",&choose);
    	while (choose == 'Y' || choose == 'y')
    	{
              printf("Enter a positive number with decimal place(s)\n");
              printf("[Value less than 1000]:");
              scanf("\n%f",&num_unround);
              if (num_unround <0)
              {
              printf("Please enter a number greater than 0 : ");
              scanf("\n%f",num_unround);
              }
              if (num_unround > 1000)
              {
              printf("Please enter a number less than 1000 : ");
              scanf("\n%f",num_unround);
              }
    	{
    		int to_round;
    		int is_factor;
    		printf ("Would you like to enter the number again?  (y/n)");
    	    scanf("\n%c",&choose);
    	}
    	if (choose == 'N' || choose == 'n')
    	{
    		printf("<<<<<Program Completed>>>>>");
    	}
    	}
    	int to_round (float num_unround)
    	{
            int num_round;
    		if (num_unround <10 || num_unround > 0)
    		{
    			num_unround = num_unround*10;
    		    num_unround = num_unround+5;
    		    num_round = (num_unround/10) ;
    		    printf("After rounding, the numer is : %d \n\n", num_round);
    		    
    		}
    		if (num_unround <100 || num_unround >= 10)
    		{
    			num_unround = num_unround*100;
    		    num_unround = num_unround+50;
    		    num_round = (num_unround/100);
    		    printf("After rounding, the numer is : %d \n\n", num_round);
    		}
    		if (num_unround <1000 || num_unround >=100)
    		{
    			num_unround = num_unround*1000;
    		    num_unround = num_unround+500;
    		    num_round = (num_unround/1000);
    		    printf("After rounding, the numer is : %d \n\n", num_round);
    		}
    		if (num_unround > 1000)
    		{
    			printf("Please enter a value less than 1000 : ");
    			scanf("%f", &num_unround);
    		}
    		if (num_unround < 0)
    		{
    			printf("Please enter a positive number : ");
    			scanf("%f", &num_unround);
    		}
    		return num_round; 
    	}
    	int is_factor(int num_round)
    	{
    		int counter, i;
        printf ("The factors of %d :",num_round);
        scanf("%d",&num_round);
        printf("The factors of %d : ",num_round);
        for(i=1; i<=num_round; i++)
    {
        if(num_round%i==0)
        counter++;
        printf("%d\n", i);
    }
             printf("The total count of factors is : %d",counter);
             return i;
    }
    }
    I still don't know how to print the result.
    also I don't know whether the function could run= =

  10. #10
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    why don't you use the math library functions?

    Code:
    float rounded = (floorf(number+0.5f));
    It's actually that simple.
    Devoted my life to programming...

  11. #11
    Registered User
    Join Date
    Oct 2010
    Posts
    24
    Quote Originally Posted by Sipher View Post
    why don't you use the math library functions?

    Code:
    float rounded = (floorf(number+0.5f));
    It's actually that simple.
    this is my assignment. it is not allowed me to use the build-in function

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If you have a number - say 123.45678, and you need to round it off (up in this case). Here's one way to do it:

    Multiply the number * 100: (number = 12345.678.

    divide the number by the integer 1, knocking off the digits on the right hand side of the decimal point: 12345.

    Now
    Code:
    digit = number % 10; (digit = 5)
    if(digit >4) {
      number +=  (10-digit) (number= 12350)
    else
      number -= digit. (number = 12340) 
    
    number /= 10;
    Number is now rounded off to the nearest tenth: number = 123.40 or 123.50

    If you need to round off to the one's place, it's the same idea:
    Code:
    digits = number % 100 (digits = 45)
    if(digits >49) {  //if number greater than 123.49
      number += (100-digits) (number = 12400)
    else
      number -= digits (number = 12300)
    
    number /= 100; (number = 123.00 or 124.00), and is rounded to the one's place.
    Test that out thoroughly, and let me know if you get errors you can't solve. As for ==, you should test it by running it.

  13. #13
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by fail50 View Post
    this is my assignment. it is not allowed me to use the build-in function
    Ok then. You can also do this:
    Code:
    float rounded = (float)(long)(number+0.5f);
    It is exactly the same with my previous post
    Devoted my life to programming...

  14. #14
    Registered User
    Join Date
    Oct 2010
    Posts
    24
    I want to ask that how can I bring the result of a function to another??
    for example, after rounding off a number how can this number enter to another function which can be factorized.
    Also, what makes some strange number to be appeared such as 36 before "The"
    2293672 after 'of' etc.
    http://i1128.photobucket.com/albums/...g?t=1287891635

    THX!!

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The way you return a number from a function is like this:

    Code:
    #include <stdio.h>
    
    int add_one(int *);  //function prototype agrees with function 
    
    int main(void) {
      int num1 = 5;
      int num2 = 0;
    
      printf("\n Before going to add_one(), the values are: ");
      printf("\n Num1=%d     Num2=%d\n", num1, num2);
    
      num2 = add_one(&num1);  //the call to add_one function
    
      /* num1 is sent to the add_one function, where one is added to it, and then
          it is returned from add_one, and "caught" immediately, by num2.
    
         I have to include the & so that I am sending the address of num1,
         and not just a copy of num1. That allows any change I make to num1,
         in the add_one function, to be permanent, even after the program returns
         to main(), from add_one(). 
      */
    
      printf("\n After going to add_one(), the values are: ");
      printf("\n Num1=%d     Num2=%d\n", num1, num2);
    
      printf("\n\t\t\t    press enter when ready");
      (void) getchar();
      return 0;
    }
    
    int add_one(int *num1) { //This is OUTSIDE the main() function
      *num1 = *num1 + 1;
      return *num1+1;
    }
    You need to put every function in your program, OUTSIDE your main() functiion, like add_one() is outside main() here.

    Study hard!

Popular pages Recent additions subscribe to a feed