Thread: newbie --secant method loop question

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    2

    newbie --secant method loop question

    Hello,
    I am new to programming in C and am taking my first class on programming now. As a project I am required to write a program that uses the secant method to find the roots of an equation, which I have done. The only issue is I'm only allowed to call my function f once. My program calls it twice and I don't know how to cut it to once. I'm sure I need a loop that calls the function f then reassigns variables as it loops but I am really clueless how to do it. Any suggestions or advice would be greatly appreciated.
    newbie --secant method loop question-capture-png

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You mean that the function f <!--Maybe you should think again about the name of the function...It does not inform the reader of what this function is designed to do --> is called twice inside the loop.In the program it is going to be called 2 times the number of loops that are going to be executed.
    If you only want to call the function once inside the loop, you could modify the function to receive two pointers to x1 and x2.
    In code this will be something like this
    Code:
    void f(float* x1, float* x2)
    {
          /* Careful when the asterisk operator */
          /* is used for multiply or as a pointer!*/
          /* In order to have the code cleaner I */
          /* will write pointer to x enclosed in   */
          /* parentheses.E.g. *x == (*x)         */
    
          *x1 = (*x1) * ( (*x1) * (*x1) - 3.0 ) + 1.0;
          *x2 = (*x2) * ( (*x2) * (*x2) - 3.0 ) + 1.0;
    }
    General Tip : Give your functions (and variables) names that denote their usage.This is increasing the readability of your code

    Example
    I will write two pieces of code, with no comments and i won't inform you for what this code is meant to do.
    Read both and try to find out what is the goal of this code..Or to be more exact find out which one you would prefer to read.
    Code:
    #include <stdio.h>
    
    int aTodayWrittenFunction(int* aVar , int anotherVar);
    int aFunction(int var1, int var2);
    
    int main(void)
    {
      int a0 = 5;
      int a1[a0],a2,a3,a4;
    
      for( a2 = 0 ; a2 < a0 ; a2++ )
         a1[a2] = a2;
    
      a3 = aTodayWrittenFunction(a1 , a0);
    
      a4 = aFunction(a3 , a0);
    
      printf("Value computed : %d\n" , a4);
    
      return 0;
    }
    
    int aTodayWrittenFunction(int* aVar , int anotherVar)
    {
      int someVar,abcd = 0;
    
      for(someVar = 0 ; someVar < anotherVar ; someVar++)
            abcd += aVar[someVar];
    
      return abcd;
    }
    
    int aFunction(int var1, int var2)
    {
      int a0;
    
      a0 = var1/var2;
    
      return a0;
    }
    The same code, but with different names of functions and variables
    Code:
    #include <stdio.h>
    
    int computeSum(int* array , int length);
    int computeAverage(int sum, int length);
    
    int main(void)
    {
      int length = 5;
      int array[length],i,sum,average;
    
      for( i = 0 ; i < length ; i++ )
         array[i] = i;
    
      sum = computeSum(array , length);
    
      average = computeAverage(sum , length);
    
      printf("Value computed : %d\n" , average);
    
      return 0;
    }
    
    int computeSum(int* array , int length)
    {
      int i,sum = 0;
    
      for(i = 0 ; i < length ; i++)
            sum += array[i];
    
      return sum;
    }
    
    int computeAverage(int sum , int length)
    {
      int average;
    
      average = sum/length;
    
      return average;
    }
    I think that you will agree that the second one is "better" than the first one.Of course I am not saying that it is the best names that can be given.You might also think of other, more explanative names if you want.The ideal is to have the minimum length of names that fully explain the purpose of the variable or the function.As always in programming (and also life maybe) ,everything is a trabe of ( here length of word and how much info you include in it)

    Also another tip.It is very useful for the others to see your code wrapped in code tags.
    It's easy
    [key]
    /* YOUR CODE HERE */
    [/key]
    Replace key with the word code in order this to work.Try it out

    Welcome to the forum,
    G.Samaras

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    2
    Thanks for the tips. You were correct I only want to call the function in the loop once. I've changed the function name to equation to hopefully make it clearer. It is simply a math equation. I've also included comments to try to clear things up as well. Sorry I'm new to this . Thanks for the tip about the pointers, I will mess with that a bit and see if it doesn't get me somewhere. Should I use code tags even when I'm inserting a screen shot like I'm doing here?
    newbie --secant method loop question-capture-png

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by brlaswell View Post
    Should I use code tags even when I'm inserting a screen shot like I'm doing here?
    newbie --secant method loop question-capture-png
    You shouldn't be inserting a screen shot of text in the first place. Copy and paste the text (as plain text) inside code tags, and the forum software will add syntax highlighting and line numbers automatically.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about an incrimenting method in for loop
    By jack41 in forum C Programming
    Replies: 2
    Last Post: 07-17-2011, 05:49 AM
  2. help: nonlinear secant method program
    By lady_lyssa in forum C++ Programming
    Replies: 5
    Last Post: 10-09-2009, 12:40 PM
  3. Newbie Loop Question.
    By SlyMaelstrom in forum C++ Programming
    Replies: 2
    Last Post: 05-10-2004, 03:47 PM
  4. newbie question: the for loop
    By Panopticon in forum C++ Programming
    Replies: 2
    Last Post: 01-10-2003, 03:25 AM
  5. KEYSCANNING within a LOOP, Newbie Question
    By Robert_Ingleby in forum C++ Programming
    Replies: 8
    Last Post: 11-23-2001, 06:49 AM