Thread: Adding a constant to multiple variables

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    2

    Adding a constant to multiple variables

    Hi,

    I was hoping someone could help me with this problem.

    Instead of using:
    Code:
    x=x+k
    y=y+k
    z=z+k
    Is there a more elegant method of adding the same constant to many variables?

    Something like:
    Code:
    (x, y, z) = (x, y, z) + k  ??
    I tried searching for a solution but couldn't find what I was looking for.

    Thanks for any help
    Last edited by melia789; 05-20-2013 at 12:16 PM.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    3,824
    This C code
    Code:
    x=x+k;
    y=y+k;
    z=z+k;
    Can instead be
    Code:
    x+=k;
    y+=k;
    z+=k;
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    May 2013
    Posts
    2
    Thanks Tim, would that be considered the most elegant?

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    There is no "short cut" that I'm aware of to accomplish this. The best case scenario would be if those "many variables" were elements in an array - in which case you could just use a loop.

    You could also write your own elegant solution - therein lies the beauty of 'C'.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    3,824
    Quote Originally Posted by melia789 View Post
    Thanks Tim, would that be considered the most elegant?
    Depending on the problem, writing a function could be the most elegant solution.

    If you have to do it three or more times; I say write a function.
    Once only, not really worth a function.
    Twice, it is had to decide what is better.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,876
    You could define x,y,z in a structure and make a function which adds the values

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct point_t
    {
      int x;
      int y;
      int z;
    };
    
    /* Add 'k' to x, y, z */
    void add_k(struct point_t *p, int k);
    
    int main(int argc, char *argv[])
    {
        struct point_t point;
    
        int k = 4;
    
        point.x = 1;
        point.y = 1;
        point.z = 1;
    
        printf("First Values:\tx=%d\ty=%d\tz=%d\n", point.x, point.y, point.z);
    
        add_k(&point, k);
    
        printf("Results:\tx=%d\ty=%d\tz=%d\n", point.x, point.y, point.z);
    
        return EXIT_SUCCESS;
    }
    
    void add_k(struct point_t *p, int k)
    {
        p->x += k;
        p->y += k;
        p->z += k;
    }
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. about constant functions and variables
    By student111 in forum C++ Programming
    Replies: 5
    Last Post: 06-17-2012, 10:46 AM
  2. Replies: 9
    Last Post: 01-29-2006, 06:57 PM
  3. Adding variables
    By Mckooy in forum C Programming
    Replies: 4
    Last Post: 01-15-2006, 10:22 AM
  4. Adding to char variables together
    By maxorator in forum C++ Programming
    Replies: 3
    Last Post: 10-02-2005, 10:27 AM
  5. Replies: 2
    Last Post: 12-25-2001, 04:18 PM

Tags for this Thread