Thread: Need Help on Function In C

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

    Need Help on Function In C

    Hello Smart ones,
    I want to include a function at the (print)output of this code like (void output int) so that i will call it in main function. It is difficult for me. I already have 2 simply functions in it.
    Code:
    #include <stdio.h>#include <math.h>
    #include <stdlib.h>
    int input1(char[]);
    int input2(char[]);
    
    
    
    
    int main(void)
    {
    	int i, prompt1, prompt2;
    	
    	
    	do {
    	prompt1 = input1("please enter length of array A:");
    	}
    	
    	while ( 1 > prompt1 || prompt1 > 20);
    	
    	int arrayA[prompt1];
    	
    	do {
    	prompt2 = input2("please enter length of array B:");
         }
    	
    	while ( 1 > prompt2 || prompt2 > 20);
    	
    	int arrayB[prompt2];
    	printf("\n");
    	
    	for (i = 0; i < prompt1; i++)
    	{ 
    		
    		printf("insert array :A: elements [%d] ", i);
    		scanf("%d", &arrayA[i]);
    	}   printf("\n");
    	for (i = 0; i < prompt2; i++){
    	
    		printf("insert array :B: elements [%d] ", i);
    		scanf("%d", &arrayB[i]);	
    	}
    	printf("\n");
    	int p = prompt1 + prompt2;
    	int arrayC[p];
    	
    	for (i = 0; i < p; i++)
    	{
    		if (i== 0){
    			arrayC[i] = arrayA[i];
    		}
    		else if (i%2 == 0){
    			arrayC[i] = arrayA[i/2];
    		} 
    		else 
    		{ 
    			arrayC[i] = arrayB[(i-1)/2];
    		}
    		
    	}
    	printf("The final result after inter-switching the two array elements \n");
    	for (i = 0; i < p; i++)
    	{
    		printf (" %d \n",arrayC[i]);
    	}		
    	
    		
    		return 0;
    }		
    	int input1(char text []) {
    	int n;
    	printf("%s", text);
    	scanf("%d", &n);
    	return n;
    }	
    
    
    int input2(char text []) {
    	int m;
    	printf("%s", text);
    	scanf("%d", &m);
    	return m;
    }

  2. #2
    Registered User
    Join Date
    Oct 2019
    Posts
    15
    Still waiting

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    If you post code that compiles and try to code your problem you are more likely to get help!
    "...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

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I want to include a function at the (print)output of this code like (void output int) so that i will call it in main function.
    > It is difficult for me. I already have 2 simply functions in it.
    So did you write input1 and input2 yourself?

    You take a block of code, say
    Code:
        printf("The final result after inter-switching the two array elements \n");
        for (i = 0; i < p; i++)
        {
            printf (" %d \n",arrayC[i]);
        }
    You look to see what the inputs are - say an array and a length, and come up with
    Code:
    void print ( p, arrayC ) {
        printf("The final result after inter-switching the two array elements \n");
        for (i = 0; i < p; i++)
        {
            printf (" %d \n",arrayC[i]);
        }       
    }
    Now figure out what types to add to make the definition complete.
    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. Function Prototype, Function Call, and Function definition
    By dmcarpenter in forum C Programming
    Replies: 9
    Last Post: 04-09-2013, 03:29 AM
  2. Replies: 13
    Last Post: 03-20-2012, 08:29 AM
  3. Replies: 2
    Last Post: 02-26-2009, 11:48 PM
  4. Print function: sending a function.. through a function?
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 11-05-2008, 05:03 PM
  5. Replies: 9
    Last Post: 01-02-2007, 04:22 PM

Tags for this Thread