Thread: How to clear or reset values of variables?

  1. #1
    Registered User
    Join Date
    Aug 2011
    Location
    Manila, Philippines, Philippines
    Posts
    2

    How to clear or reset values of variables?

    Hi! Good day to all. I am new here. Anyway, I have been doing my homework a couple of days now and I can't seem to figure out what is wrong with my code. (I am new to C programming by the way.) We are asked to make a program that evaluates the value of a polynomial based on the user's input of N number of terms, value of x, the coefficient of each term and the constant. What bothers me is that when I did some continuous run of the program I found out that the value of the sum every after implementation of the do-while loop does not reset to 0, instead it adds up the value of the last evaluation. Any ideas how to reset it? Here is my code.
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int terms; /*number of terms the polynomial has*/
    int x; /* value of x */
    int i; /* counter and exponent for evaluation using power function */
    int z; /* value of each term every after iteration */
    int coefficient;
    int sum = 0;  /*value of the whole polynomial*/
    
    
    int main(){
    
    do{
    
        printf("Please enter the number of terms (Enter 0 to exit): ");
        scanf("%d", &terms);
    
        if (terms == 0) {
    
            printf("Program terminated. \n");
            system("exit");
    
        } else if (terms < 0){
    
            system ("cls");
            printf("Please enter valid number of terms! \n");
            system ("pause");
    
        } else {
    
        printf("\nPlease enter the value of x: ");
        scanf("%d", &x);
     
    
          for(i = terms-1; i >= 0; i--){
    
                    if (i == 0){
                        printf("\nEnter constant: ");
                        scanf("%d", &coefficient);
                    } else {
                        printf("\nEnter coefficient of x^%d: ", i);
                        scanf("%d", &coefficient);
                    }
                        z = coefficient*pow (x,i);
                        sum = sum + z;
    
                    }
    
                printf("The value of the polynomial is: %d \n", sum);
                system ("pause");
            }
        } while (terms!=0);
    }
    Thanks a lot to all those who will bother to look at my work and help me. I will mean a lot.
    Last edited by geeksz; 08-03-2011 at 05:13 AM.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Well think about it this way. You have a series of sequential tasks you are doing:

    1) Getting the input from the user
    2) Computing the polynomial
    3) Printing the value you obtained
    4) Back to 1)

    Where in this sequence does it make sense to reset the sum to 0?

    HINT: There is more than one correct answer.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    There is nothing complex about the question. You are doing three things in your code, getting input, computing the value of the polynomial based on the input you get and printing out that value. Where do you think you should be resetting the value of the sum?
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    Registered User
    Join Date
    Aug 2011
    Location
    Manila, Philippines, Philippines
    Posts
    2
    Figured it out. Thanks a lot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. printf clear variables
    By phbunyan in forum C Programming
    Replies: 4
    Last Post: 07-03-2010, 06:25 AM
  2. Function to reset variables
    By matrixx333 in forum C Programming
    Replies: 19
    Last Post: 03-21-2009, 12:18 PM
  3. Reset values
    By dpp in forum C++ Programming
    Replies: 30
    Last Post: 01-27-2009, 08:06 AM
  4. Undefined variables have values???
    By Tronic in forum C++ Programming
    Replies: 10
    Last Post: 03-29-2004, 03:15 PM
  5. hex values/variables location
    By threahdead in forum C Programming
    Replies: 10
    Last Post: 02-21-2003, 12:57 PM