Like Tree1Likes
  • 1 Post By c99tutorial

What is wrong with this program

This is a discussion on What is wrong with this program within the C Programming forums, part of the General Programming Boards category; Okai this is the program, or at least I think so . What the fiddlesticks is wrong with it? Code: ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    20

    What is wrong with this program

    Okai this is the program, or at least I think so . What the fiddlesticks is wrong with it?
    Code:
    #include <stdio.h>
    
    
    void double_trouble(int *p,int y);
    void trouble(int *x,int*y);
    
    
    
    
    int main(void)
    {
    	int x,y;
    	trouble(&x,&y);
    	printf("x=%d,y=%d\n",x,y);
    	return(0);
    }
    
    
    void double_trouble(int *p,int y)
    {
    	int x;
    	x=10;
    	*p=2*x-y;
    }
    void trouble(int *x,int *y)
    {
    	double_trouble(x,7);   //don't they have to have *x and *y specified inside the function
    	double_trouble(y,*x);
    }
    This is basic C-free programming so I don't need complex ways of fixing it... Ooh and I need to know the parameters of the two function like what kinds are they. So I made out that
    Double_trouble = *p: Input and output
    Y: Input
    Trouble = *x: Input
    *Y:?
    So where am I going wrong here?

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,070
    Quote Originally Posted by Moody Barrawi View Post
    Ooh and I need to know the parameters of the two function like what kinds are they. So I made out that
    Double_trouble = *p: Input and output
    Y: Input
    Trouble = *x: Input
    *Y:?
    So where am I going wrong here?
    I think the problem is of naming. Each function has its own local name that is unrelated to the others. Let's introduce a simple renaming scheme:

    1. main keeps the names x, y for its variables.
    2. trouble appends the number "1" to each of its local variable names.
    3. double_trouble appends the number "2" to each of its local variable names.

    By doing this, the program is not changed at all, but it may become easier to track the different symbols

    Code:
    #include <stdio.h>
    
    void double_trouble(int *, int);
    void trouble(int *, int *);
    
    int main(void)
    {
        int x, y;
        trouble(&x, &y);
        printf("x=%d,y=%d\n",x, y);
        return(0);
    }
    
    void trouble(int *x1, int *y1)
    {
        double_trouble(x1, 7);
        double_trouble(y1, *x1);
    }
    
    void double_trouble(int *p2, int y2)
    {
        int x2 = 10;
        *p2 = 2 * x2 - y2;
    }

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    20
    So I wrote this
    Code:
    #include <stdio.h>
     
    void double_trouble(int *p2, int y2);
    void trouble(int *x1, int *y1);
     
    int main(void)
    {
        int x1, y1;
        trouble(&x1, &y1);
        printf("x=%d,y=%d\n",x1, y1);
        return(0);
    }
     
    void trouble(int *x1, int *y1)
    {
        double_trouble(x1, 7);
        double_trouble(y1, *x1);
    }
     
    void double_trouble(int *p2, int y2)
    {
        int x2 = 10;
        *p2 = 2 * x2 - y2;
    }
    And the program still wouldn't run. What the F is wrong?

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,070
    Quote Originally Posted by Moody Barrawi View Post
    And the program still wouldn't run. What the F is wrong?
    It ran fine for me and gave an answer. And there is no `F' in that program, only x1, y1, etc.

    If you're having trouble you have to say what didn't work exactly. Did the compiler give you an error message or what

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,678
    The code seems good. What's the error you get?

    Don't say the word that starts with F :O
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  6. #6
    Registered User
    Join Date
    Jan 2013
    Location
    127.0.0.1
    Posts
    3
    I don't see anything wrong with the code...

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,678
    Quote Originally Posted by QuantumNut View Post
    I don't see anything wrong with the code...
    That was an echo :P

    //mine was just typed simultaneously with the one of the tutorial guy :P
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  8. #8
    Registered User
    Join Date
    Dec 2012
    Posts
    289
    program ran for me, got x=13,y=7, althoug i have no clue what the point of it is!!! lol

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,678
    Quote Originally Posted by Crossfire View Post
    program ran for me, got x=13,y=7, althoug i have no clue what the point of it is!!! lol
    I got the same results. It can be for training purposes, or just for fun
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's wrong with this program?
    By KiRa11Love in forum C Programming
    Replies: 5
    Last Post: 08-08-2012, 01:56 PM
  2. what is wrong with my program?
    By Farnaz in forum C++ Programming
    Replies: 29
    Last Post: 02-28-2011, 01:18 PM
  3. wrong with my FFT program
    By rpbear in forum C Programming
    Replies: 4
    Last Post: 04-05-2010, 11:27 PM
  4. What is wrong with this program?
    By cuba06 in forum C Programming
    Replies: 4
    Last Post: 11-24-2007, 01:45 AM
  5. What's wrong with my program?
    By Infuriate in forum C Programming
    Replies: 7
    Last Post: 12-03-2005, 03:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21