Thread: How to define a formula in struct

  1. #1
    Registered User
    Join Date
    Sep 2015
    Posts
    2

    How to define a formula in struct

    Hello people,

    This is my first post, I am just learning C and I want to ask a simple question...

    Suppose there is a

    Code:
    typedef struct {
        int a,b;
    }x;
    and I want to make a formula where y=a/b. What should I do and where should I put the code?

    Thank you,

  2. #2
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Quote Originally Posted by Raden View Post
    I want to make a formula where y=a/b.
    What do you mean by that?

    You could write a function that takes an x as a parameter (which itself contains the integer members a and b), and returns y. I suspect this is what you want to do.

    In advanced programming topics, things like abstract syntax trees are discussed. Programs like Mathematica and Maple use such syntax trees to represent symbolic calculations -- much like compilers and interpreters use them to represent the parsed program code. So, technically, you might mean that you want to represent simple equations using tree nodes, and are asking what kind of structures you'd use for those, and how to split the code. This is unlikely, because you'd use separate nodes to describe a and b in the equation y=a/b.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Nominal Animal View Post
    You could write a function that takes an x as a parameter (which itself contains the integer members a and b), and returns y. I suspect this is what you want to do.
    Newbies are usually surprised that a/b for integers a and b is 0 (or lacking a fractional part). To get 1/2 = 0.5 for instance, OP, you'd have to compute (double)a / (double)b at a minimum.

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Quote Originally Posted by whiteflags View Post
    Newbies are usually surprised that a/b for integers a and b is 0 (or lacking a fractional part). To get 1/2 = 0.5 for instance, OP, you'd have to compute (double)a / (double)b at a minimum.
    Actually, you only need to cast one, the other will be promoted automatically. Providing that y is a double:
    Code:
    y = (double)a / b;

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I'm aware.

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Quote Originally Posted by Raden View Post
    Hello people,

    This is my first post, I am just learning C and I want to ask a simple question...

    Suppose there is a

    Code:
    typedef struct {
        int a,b;
    }x;
    and I want to make a formula where y=a/b. What should I do and where should I put the code?

    Thank you,
    There are a number of ways to do this...

    This is as simple a description as possible, I think...

    Code:
    //first declare the struct
    //the struct has to be declared before you use it 
    //in a function or as a variable
    //I would use names that have some meaning (not just 'x', 'a', etc)
    
    typedef struct {
        int a,b;
    }xStruct;
    
    //now declare the function
    //the function has to be declared before you use it 
    //return will be decimal (ie 1/2 returns 0.5)
    
    double DivideX(xStruct input)
    {
    	double y = 0;
    	//validate input (look for divide by zero errors)
    	if(input.y == 0)
    	{
    		return -1;//return an error ???
    	}
    	//do formula using the x struct passed into the function
    	y = (double)input.a / (double)input.b; //cast to doubles (see note)
    	//return result
    	return y;
    }
    
    //now we have told the compiler what the struct is and how to do the formula
    //we can use them in the program
    
    int main()
    {
    	//create a struct (initialising to 0)
    	xStruct theInput = {0};
    	//set the elements 
    	theInput.a = 1;
    	theInput.b = 2;
    	
    	//create the variable to hold the result
    	double theResult = 0;
    
    	//call the function to do the formula
    	theResult = DivideX(theInput);
    
    	//use the result....
    
    	//end program
    	return 0;
    }
    //Not tested, checked or thought about in any meaningful way, and so should not, in any way, be misrepresented to depict working code...
    Note: if you divide 2 'int' variables you get an 'int' returned as the result is rounded down to the nearest integer.
    So 1/2 = 0, 3/2 = 1, 5/2 = 2

    To get a decimal returned you need to tell the compiler to treat the int variables as a decimal (floating point type).

    This can be done with a 'cast', which temporarily converts the variable to another type (in this case an int -> double)
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  7. #7
    Registered User
    Join Date
    Sep 2015
    Location
    Australia
    Posts
    63
    Hi ..

    Not to sure about Structures but would this be how its done,, I don't think you can actually put a calculation in a structure as such.

    Code:
    #include <stdio.h>
    
    typedef struct {
     int a,b ;
    }x;
    
    int main() {
     int y;
     x calc;
        calc.a = 8;
     calc.b = 4;
        y = calc.a / calc.b ;
        printf("\n This it ? %d",y);
    
     return (0);

    John

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    > Hi ..
    Hello.

    > Not to sure about Structures but would this be how its done
    novacain's answer is the most complete, whereas your attempt has all of the problems already discussed.

    > I don't think you can actually put a calculation in a structure as such.
    It is important to try to interpret questions from people in such a way that an answer is possible. Many times, just explaining what you can do (as novacain did) or asking for clarification (as Nominal Animal did) moves the thread in a productive direction.

  9. #9
    Registered User
    Join Date
    Sep 2015
    Posts
    2
    Quote Originally Posted by whiteflags View Post
    > Hi ..
    Hello.


    > Not to sure about Structures but would this be how its done
    novacain's answer is the most complete, whereas your attempt has all of the problems already discussed.


    > I don't think you can actually put a calculation in a structure as such.
    It is important to try to interpret questions from people in such a way that an answer is possible. Many times, just explaining what you can do (as novacain did) or asking for clarification (as Nominal Animal did) moves the thread in a productive direction.

    I'm sorry my bad, I'm in a hurry when I post...
    What I want really to do is to create a database, an employee database specifically, containing:
    1. ID Number
    2. Name
    3. How many hour the employee works overnight
    4. How many hour the employee has been absent
    5. Bonus -> where Bonus = overnight-((2/3)*absent)


    And I should be able to:
    1. Insert a record
    2. Display the records
    3. Modify a record
    4. Delete a record


    The problem is I want the program to automatically calculate the "Bonus" when the records are displayed. Here is the code so far
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    typedef struct employee
    {
      char name[30];
      int empid,overnight,absent;  
    }emp;
    
    
    void insert(emp e[50],int n)
    {
      printf("\nEnter the name of the employee: ");
      scanf("%s",&e[n].name);       
      printf("Enter the Employee ID: ");
      scanf("%d",&e[n].empid);
      printf("Enter how many hour(s) the employee works overnight: ");
      scanf("%d",&e[n].overnight);
      printf("Enter how many hour(s) the employee has been absent: ");
      scanf("%d",&e[n].absent);  
    }
    
    
    void display(emp e[50],int n)
    {
      int i;
         
      printf("\nEMP ID\tNAME\tOVERNIGHT\tABSENT\tBONUS");
      printf("\n**************************************\n");
      for(i=0;i<n;i++)
      {
        printf("\n%d\t%s\t%d hour\t%d hour\t???\n",e[i].empid,e[i].name,e[i].overnight,e[i].absent);
        printf("\n**************************************\n");
      }
    }
    
    
    void search(emp e[50],int n)
    {
      int i,id;
      printf("\nEnter the Employee ID to be searched: ");
      scanf("%d",&id);
      for(i=0;i<n;i++)
      {
        if(e[i].empid==id)
        {
          printf("\nRecord present.");
          printf("\n>> %d\t%s\t%d\t%d",e[i].empid,e[i].name,e[i].overnight,e[i].absent);
          return;
        }
      }
      printf("\nRecord not present.\n");
    }
    
    
    void modify(emp e[50],int n)
    {
      int i,id;
      printf("\nEnter the Employee ID to be modified: ");
      scanf("%d",&id);
      for(i=0;i<n;i++)
      {
        if(e[i].empid==id)
        {
          printf("\nRecord present.\n");
          printf("\nEnter the new name: ");
          scanf("%s",&e[n].name);
          printf("Enter the new overnight hour: ");
          scanf("%d",&e[i].overnight);
          printf("Enter the new absent hour: ");
          scanf("%d",&e[i].absent);
          printf("\nRecord modified.\n");
          return;
        }
      }
      printf("\nRecord not present.\n");
    }
    
    
    int delrec(emp e[50],int n)
    {
      int i,id;
      printf("\nEnter the Employee ID to be deleted: ");
      scanf("%d",&id);
      for(i=0;i<n;i++)
      {
        if(e[i].empid==id)
        {
          break;
        }
      }
      if(i==n)
      {
        printf("\nRecord not present.\n");
      }
      else
      {
        printf("\nRecord present and deleted.\n");
        while(i<n)
        {
          e[i]=e[i+1];
          i++;
        }
        n=n-1;
      }
      return(n);
    }
    
    
    int main(int argc, char **argv)
    {
      emp e[50];
      int n=0,ch;
      printf("=================\n");
      printf("EMPLOYEE DATABASE\n");
      printf("=================\n");
      
      while(1)
      {
        printf("\n1] Insert Data\n2] Display Data\n3] Search Data\n4] Modify Data\n5] Delete Data\n6] Exit");
        printf("\nPlease enter your choice: ");
        scanf("%d",&ch);
        switch(ch)
        {
          case 1:insert(e,n);
             n=n+1;
             break;
          case 2:display(e,n);
             break;
          case 3:search(e,n);
             break;
          case 4:modify(e,n);
             break;
          case 5:n=delrec(e,n);
             break;
          case 6:exit(0);
          default:printf("\nPlease enter the correct choice.\n");
        }
      }
    }
    
    
    /* This is the condition of the bonus
      
      int bonus;
      
      if(e[n].b>40) {
          bonus = 5000;
      }
      else if((e[n].b>30)&&(e[n].b<=40)) {
          bonus = 4000;
      }
      else if((e[n].b>20)&&(e[n].b<=30)) {
          bonus = 3000;
      }
      else if((e[n].b>10)&&(e[n].b<=20)) {
          bonus = 2000;
      }
      else {
          bonus = 1000;
      } 
    */

    Thank you all for your answers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Special use of #define with a struct
    By himiker in forum C Programming
    Replies: 3
    Last Post: 07-27-2015, 02:59 PM
  2. define string to struct var
    By siperi in forum C Programming
    Replies: 4
    Last Post: 10-22-2010, 08:28 AM
  3. Newbee: Define a struct with scoop outside file?
    By Marcux in forum C Programming
    Replies: 2
    Last Post: 07-18-2007, 12:46 PM
  4. #define within struct definition?
    By fanoliv in forum C Programming
    Replies: 11
    Last Post: 06-23-2006, 06:24 PM
  5. struct and define problem
    By hurry up in forum C Programming
    Replies: 1
    Last Post: 03-19-2003, 11:37 AM