Find the answer to your Linux question:
Results 1 to 3 of 3
What I have to do is make a function that will make an array of even numbers out of another array of ints that is inputted by the user. Here ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Sep 2012
    Posts
    35

    Making an array of even numbers out of another array


    What I have to do is make a function that will make an array of even numbers out of another array of ints that is inputted by the user. Here is the function that I have so far... Sorry if it is absolutely horrible. I suck with arrays so badly, and i'm pretty new at it.

    Code:
    void distributeEven(int array[])
    {
    	int counter = 0,
    	    array2[10],
    	    arrayCounter = 0;
    
    	for (counter; counter < 10; counter++)
    	{
    		if (array[counter] % 2 == 0)
    		{
    			array2[arrayCounter] = array[counter];
    		}
    		arrayCounter++;
    	}
    }
    I know its horrible and probably has many problems, one of them being that I don't know how to make it return values into the array when i'm calling it.

    I thank you for any help that you can afford me

  2. #2

  3. #3
    Just Joined!
    Join Date
    Sep 2012
    Location
    Bangalore
    Posts
    22
    This is the program (in C) for creating arrays of even nos :

    Code:
    #include <stdio.h>
    
    int main()
    {
            int i,j,count=0;
            printf("Enter the size of an array: ");
            scanf("%d",&i);
            int array[i],array_even[i];
            printf("Enter the elements of an array :\n ");
            for(j=0;j<i;j++)
            {
                    scanf("%d",&array[i]);
                    if(array[i]%2==0)
                    {
                            array_even[count]=array[i];
                            count++;
                    }
    
            }
    
            printf("Array of even elements(from given array):\n");
            for(j=0;j<count;j++)
            printf("%d\n",array_even[j]);
    
            return 0;
    }
    The output of the program looks like this :

    Code:
    shantanu@{Just Do It !}:~/C_progs$ vi forum_array.c 
    shantanu@{Just Do It !}:~/C_progs$ gcc forum_array.c 
    shantanu@{Just Do It !}:~/C_progs$ ./a.out
    Enter the size of an array: 10
    Enter the elements of an array :
     15
    69
    83
    26
    48
    3
    128
    9
    742
    588
    Array of even elements(from given array):
    26
    48
    128
    742
    588
    shantanu@{Just Do It !}:~/C_progs$
    I hope this is what you need & it address your query !

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •