Thread: struggling to implement function

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    68

    struggling to implement function

    I am trying to write the code for my hardware whose flow would be something like this


    struggling to implement function-function-flow-jpg

    Code:
    #include<stdio.h>
    
    void start()
    {
    	printf("Start \n");
    }
    void send(int D)
    {
    	int i;
    	for (i=0; i<8; i++)
    	    {     
    	      printf("%d\n");
    		}
    }
    int get()
    {
    	if(1==0)
    		return 1;
    	else
    		return 0;
    )
    
    
    #include<stdio.h>
    int main ()
    {  
       int p; 
       int A = 0;
       int n = 5
       arry[4] = (1, 2, 3, 4);
       p = SendD(A, n, array);   
    
    
    }
    I am struggling with this function

    Code:
    int SendD ( A, n, *pointer){
    	int value;
    	start();
        send(A);
    	value = get();	
    }
    How to make function according to drawing ?

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,742
    Given the flowchart I'm not surprised that you're having trouble implementing it.

    What's it supposed to do? Who made the drawing/flowchart?

    Aside from mystery of the flowchart (incrementing (*pointer)... really?), I have the following questions:

    Code:
    if(1==0) /* under what circumstances will 1 equal 0? */
    int SendD ( A, n, *pointer) /* Why do none of these function parameters have a type? */
    arry[4] = (1, 2, 3, 4); /* What are the parenthesis () for? That's not how you initialize an array */
    p = SendD(A, n, array); /* What is 'array'? I see 'arry', but no 'array' */
    int n = 5 /* why is n 5 when the array only has 4 elements? */
    for (i=0; i<8; i++) /* What is 8? */
    Maybe the first step is to fix the flowchart. After that get your skeleton code to at least compile (and make sense... 1 can never equal 0 for a start). I don't think it's wise to proceed before you do these things and answer the above questions
    Last edited by Hodor; 12-23-2019 at 04:28 AM.

  3. #3
    Registered User
    Join Date
    Oct 2019
    Posts
    68
    Quote Originally Posted by Hodor View Post
    Given the flowchart I'm not surprised that you're having trouble implementing it.

    What's it supposed to do? Who made the drawing/flowchart?

    Aside from mystery of the flowchart (incrementing (*pointer)... really?), I have the following questions:

    Maybe the first step is to fix the flowchart. After that get your skeleton code to at least compile (and make sense... 1 can never equal 0 for a start). I don't think it's wise to proceed before you do these things and answer the above questions
    forget all that just think that is only box and need to take decision

    take a look this portion

    can you guess what basic c need to complete following behavior
    Attached Images Attached Images struggling to implement function-bottom-jpg 
    Last edited by gajya; 12-23-2019 at 06:30 AM.

  4. #4
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,742
    Nah, I cannot guess what "basic C" is needed unfortunately (apart from a loop). Maybe it would be easier to guess if your current code actually compiled (I didn't even try because it's obvious it doesn't).

    The new flowchart is a bit more sensible than the first but it still gives no indication of what you're trying to do. Maybe forget about flowcharts for now and first describe what you want to do using English. The new flowchart is kind of useless because it's just showing a loop and not what is supposed to happen in the loop. The termination of the loop (value == 1) is weird. I really have no idea what the flowchart represents (apart from a loop) because it shows... nothing.

  5. #5
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,742
    Maybe start by understanding the code below. It probably has nothing to do with your flowchart but at least it makes sense and compiles.

    Code:
    #include <stdio.h>
    
    #define NUM_OF_ELEMENTS 4
    
    int function(int *A, int n);
    
    int main(void)
    {
        int array[NUM_OF_ELEMENTS] = {1, 2, 3, 4};
        
        printf("Sum is %d\n", function(array, NUM_OF_ELEMENTS));
        
        return 0;
    }
    
    int function(int *A, int n)
    {
        int i;
        int sum;
        
        sum = 0;
        
        for (i = 0; i < n; i++) {
            printf("Element value is %d. Adding to 'sum'.\n", A[i]);
            sum = sum + A[i];
        }
        
        return sum;
    }

  6. #6
    Registered User
    Join Date
    Oct 2019
    Posts
    68
    Quote Originally Posted by Hodor View Post
    Maybe start by understanding the code below. It probably has nothing to do with your flowchart but at least it makes sense and compiles.
    In this case, I would like to work on my code

    When ever get function return success first pass A then array content other print stop message

    Code:
    #include<stdio.h>
    
    #define failure  1
    #define success  0
    
    
    int SendD (unsigned int A, unsigned int n, int *pointer)
    
    
    int main ()
    {
       int p; 
       int A = 0;
       int n = 5;  
       int i;
       int array[4] = {1, 2, 3, 4};
       p = SendD(A, n, array);    
       
       return 0;
    }
    
    
    int get()
    {
        int Value;
        printf("Enter digit : ");
        scanf("%d", Value);
        if( Value == 1)
           return failure;
        else
           return success;         
    }
    
    
    void send(int D)
    {
        int i;
        for (i=0; i<8; i++)
            {     
              printf("%d\n", D);
            }
    }
            
    int SendD (unsigned int A, unsigned int n, int *pointer)
    {
        int value, a;
        printf("Start \n");
        send(A);
        value = get();
        if (value == failure)
            printf("Stop \n");
        else
            if (n == 0)
                printf("Stop \n");
            else
                a = *pointer;
                send(a);
                printf("%d ", a);
                value = get();
                if (value == failure)
                    printf("Stop \n"); 
                else
                ++pointer;
                n--;    
        return value;            
    }
    Start
    0
    0
    0
    0
    0
    0
    0
    0
    Enter digit : 0
    When get function return 0, pass value pointed by pointer that is array[0] = 1;

    line 58 it should be display 1 that is array[0] = 1;
    Last edited by gajya; 12-23-2019 at 08:47 AM.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,749
    > scanf("%d", Value);
    Your compiler should be telling you that you forgot the &

    Where's the loop in your sendD code?


    Code:
    start();
    send(A);
    value = get();
    if ( value != 1 ) {
      while ( n != 0 ) {
        send(*pointer);
        value = get();
        if ( value == 1 ) break;
        ++*pointer;
        n--;
      }
    }
    stop();
    return value;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I implement this bool function?
    By XavierPacheco in forum C Programming
    Replies: 4
    Last Post: 03-14-2018, 06:07 AM
  2. Replies: 7
    Last Post: 09-12-2015, 01:34 AM
  3. Replies: 5
    Last Post: 03-26-2013, 02:17 PM
  4. how to implement a function
    By BMWE in forum C Programming
    Replies: 8
    Last Post: 03-25-2012, 02:25 PM
  5. Struggling w/ string_replacement function
    By cwafavre in forum C Programming
    Replies: 2
    Last Post: 10-31-2007, 07:08 AM

Tags for this Thread