Thread: howto make a function read from array A, but put select parts in array B?

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

    howto make a function read from array A, but put select parts in array B?

    im trying to read in 1 array and get 2 as outputs from 3 different functions.

    my read array is easy enough were im getting confused is how to read that array, separate it and take out only the parts i want and place them into a 2nd, then again a 3rd array.
    i have the following so far:

    Code:
    #include<stdlib.h>#include<stdio.h>
    #include<unistd.h>
    #define SIZE 10
    
    
    void Input_Array(int A[], int size);
    void Even_Array(int B[], int size);
    void Odd_Array(int C[], int size);
    
    
    int main(void)
    {
    	int A[SIZE], B[SIZE], C[SIZE], size = SIZE;
    
    
    	Input_Array (A, SIZE);
    	Even_Array (B, SIZE);
    	Odd_Array (C, SIZE);
    
    
    return(0);
    }
    
    
    // code to gather input from user for the 10 elements in the array
    void Input_Array(int A[], int size)
    {
    	int i;
    	printf("Please enter %d integer numbers separated by spaces:\n", size);
    	for (i = 0; i < size; i++)
    		scanf("%d", &A[i]);
    }
    
    
    void Even_Array(int B[], int size)
    {
    	int i, A[SIZE];
    	for (i = 0; i < size; i++)
    	{
    		if(A[i]%2 == 0)
    		{
    			scanf("%d", &B[i]);
    		}
    	}
    	
    	printf("The Even number Array is as follows: \n");
    	for (i = 0; i < size; i++)
    	{
    		printf("%d\t", B[i]);
    	}
    
    
    }
    
    
    void Odd_Array(int C[], int size)
    {
    	int i, A[SIZE];
    	for (i = 0; i < size; i++)
    	{
    		if(A[i]%2 != 0)
    		{
    			scanf("%d", &C[i]);
    		}
    	}
    	printf("The Even number Array is as follows: \n");
    	for (i = 0; i < size; i++)
    	{
    		printf("%d\t", C[i]);
    	}
    
    
    }
    this compiles without a complaint, but when i go to run it no longer responds after taking the 10th element (well 9th if counting from 0).

    I think i have the if correct for the even odd section, but when i try to populate B or C array with the output of that if statement from A is were i think things are dying...

    some guidance would be great.

    Thank you.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Get rid of all the extra local A arrays declared in functions Even_Array and Odd_Array.
    Pass in the A array to functions Even_Array and Odd_Array.

    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
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    If I understand you correctly, you want the user to fill up an array A with numbers of their choosing. Then, you wish to put all the even numbers from A into B, and all the odd numbers from A into C. Is that correct? If so:

    First, A, B and C are not very good names, since they don't say anything about what the variable is for. Single letter variable names should be used sparingly, mostly as loop counters, array indexes and in mathematical funcitons where that letter has significance. Rename them something more descriptive, like all_numbers, odd_numbers and even_numbers. Don't sacrifice clarity to save a few keystrokes.

    You don't want to call scanf in the Even_Array or Odd_Array functions. scanf is for reading user input (from the keyboard). You want to copy elements from all_numbers into even_numbers and odd_numbers. You will need something like:
    Code:
    odd_numbers[j] = all_numbers[i];
    Note that I used two different indexes. Otherwise, there will be "gaps" in your odd array where you skipped even numbers, and vice versa. I will leave it to you to work out what to start i and j at, and when to increment each.

    As Tim suggested, you need to pass in your all_numbers array to the functions, so they have access to the actual array. Simply reusing the same name does not suffice, it creates a separate variable with different data. So something like:
    Code:
    // This function iterates through all_numbers and copies any even
    // numbers into the even_numbers array.  It returns the count of
    // even numbers.
    int Even_Array(int even_numbers[], int all_numbers[], int size)
    Also, a function should do one thing and do it well. A function that separates the odd numbers should not print anything. Make a separate print function, to which you pass the array to print and how many elements you want to print. You can use it in main like so:
    Code:
    void print_array(int array[], int size)
    {
        // your code here
    }
    
    int all_numbers[SIZE], even_numbers[SIZE], odd_numbers[SIZE];
    // store the count of even/odd numbers since there may not be 10 and we don't want to print garbage at the end
    int n_even_numbers, n_odd_numbers;
    ...
    n_even_numbers = Even_Array(even_numbers, all_numbers, SIZE);
    printf("The Even number Array is as follows: \n");
    print_array(even_numbers, n_even_numbers);
    Once you get that all sorted out, the corollary for odd numbers should be simple.

  4. #4
    Registered User
    Join Date
    May 2013
    Posts
    66
    Quote Originally Posted by anduril462 View Post
    If I understand you correctly, you want the user to fill up an array A with numbers of their choosing. Then, you wish to put all the even numbers from A into B, and all the odd numbers from A into C. Is that correct? If so:
    Yes that is correct.

    First, A, B and C are not very good names, since they don't say anything about what the variable is for. Single letter variable names should be used sparingly, mostly as loop counters, array indexes and in mathematical funcitons where that letter has significance. Rename them something more descriptive, like all_numbers, odd_numbers and even_numbers. Don't sacrifice clarity to save a few keystrokes.

    You don't want to call scanf in the Even_Array or Odd_Array functions. scanf is for reading user input (from the keyboard). You want to copy elements from all_numbers into even_numbers and odd_numbers. You will need something like:
    Code:
    odd_numbers[j] = all_numbers[i];
    Note that I used two different indexes. Otherwise, there will be "gaps" in your odd array where you skipped even numbers, and vice versa. I will leave it to you to work out what to start i and j at, and when to increment each.
    Ok that make sense on the variable names.

    I think i follow you with the i j thing, but I do have a question about the results later in my code

    As Tim suggested, you need to pass in your all_numbers array to the functions, so they have access to the actual array. Simply reusing the same name does not suffice, it creates a separate variable with different data. So something like:
    Code:
    // This function iterates through all_numbers and copies any even
    // numbers into the even_numbers array.  It returns the count of
    // even numbers.
    int Even_Array(int even_numbers[], int all_numbers[], int size)
    Also, a function should do one thing and do it well. A function that separates the odd numbers should not print anything. Make a separate print function, to which you pass the array to print and how many elements you want to print. You can use it in main like so:
    Code:
    void print_array(int array[], int size)
    {
        // your code here
    }
    
    int all_numbers[SIZE], even_numbers[SIZE], odd_numbers[SIZE];
    // store the count of even/odd numbers since there may not be 10 and we don't want to print garbage at the end
    int n_even_numbers, n_odd_numbers;
    ...
    n_even_numbers = Even_Array(even_numbers, all_numbers, SIZE);
    printf("The Even number Array is as follows: \n");
    print_array(even_numbers, n_even_numbers);
    Once you get that all sorted out, the corollary for odd numbers should be simple.
    So I thought i was following what you were saying. I can get my program to compile, but not run. Im getting a segmentation fault error:

    Code:
    #include<stdlib.h>#include<stdio.h>
    #include<unistd.h>
    #define SIZE 10
    
    
    void Input_Array(int all_numbers[], int size);
    int Even_Array(int even_numbers_only[], int all_numbers[], int size);
    int Odd_Array(int odd_numbers_only[], int all_numbers[], int size);
    
    
    int main(void)
    {
        int all_numbers[SIZE], even_numbers_only[SIZE], odd_numbers_only[SIZE], new_even_numbers, new_odd_numbers, size = SIZE;
    
    
        new_even_numbers = Even_Array(even_numbers_only, all_numbers, SIZE);
        printf("\nThe even numbers in your array are: %d\n", new_even_numbers);
    
    
    return(0);
    }
    
    
    // code to gather input from user for the 10 elements in the array
    void Input_Array(int A[], int size)
    {
        int i;
        printf("Please enter %d integer numbers separated by spaces:\n", size);
        for (i = 0; i < size; i++)
            scanf("%d", &A[i]);
    }
    
    
    int Even_Array(int even_numbers_only[], int all_numbers[], int size)
    {
        int i, j, result_even;
        for (i = 0; i < size; i++)
        {
            if(all_numbers[i]%2 == 0)
            {
                even_numbers_only[j] = all_numbers[i];
                return result_even;
            }
        }
    }
    with the following results:

    Code:
    ssma-imac:ENG-3211 ssma$ gcc -o even_odd even_odd.c 
    ssma-imac:ENG-3211 ssma$ ./even_odd
    Segmentation fault

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Add a printf like the following:
    Code:
    int Even_Array(int even_numbers_only[], int all_numbers[], int size)
    {
        int i, j, result_even;
        for (i = 0; i < size; i++)
        {
            if(all_numbers[i]%2 == 0)
            {
                printf("Adding %d to even_numbers_only[%d]\n", all_numbers[i], j);  // print the number being added and the position adding it to
                even_numbers_only[j] = all_numbers[i];
                return result_even;
            }
        }
    }
    That should tip you off to the problem.

    Note, you don't need a result_even variable. You are returning the number of items in even_numbers_only when the loop is finished. Which other variable contains that value?

  6. #6
    Registered User
    Join Date
    May 2013
    Posts
    66
    hmm, ok were is this number coming from?

    Code:
    ssma-imac:ENG-3211 ssma$ gcc -o even_odd even_odd.c 
    ssma-imac:ENG-3211 ssma$ ./even_odd
    Please enter 10 integer numbers separated by spaces:
    1 2 3 4 5 6 7 8 9 0
    Adding 2 to even_numbers_only[1606424274]
    Segmentation fault
    Code:
    #include<stdlib.h>#include<stdio.h>
    #include<unistd.h>
    #define SIZE 10
    
    
    void Input_Array(int all_numbers[], int size);
    int Even_Array(int even_numbers_only[], int all_numbers[], int size);
    int Odd_Array(int odd_numbers_only[], int all_numbers[], int size);
    
    
    int main(void)
    {
    	int all_numbers[SIZE], even_numbers_only[SIZE], odd_numbers_only[SIZE], new_even_numbers, new_odd_numbers, size = SIZE;
    
    
    	Input_Array(all_numbers, SIZE);
    	new_even_numbers = Even_Array(even_numbers_only, all_numbers, SIZE);
    	printf("\nThe even numbers in your array are: %d\n", new_even_numbers);
    
    
    return(0);
    }
    
    
    // code to gather input from user for the 10 elements in the array
    void Input_Array(int all_numbers[], int size)
    {
    	int i;
    	printf("Please enter %d integer numbers separated by spaces:\n", size);
    	for (i = 0; i < size; i++)
    		scanf("%d", &all_numbers[i]);
    }
    
    
    int Even_Array(int even_numbers_only[], int all_numbers[], int size)
    {
    	int i, j, result_even;
    	for (i = 0; i < size; i++)
    	{
    		if(all_numbers[i]%2 == 0)
    		{
    			printf("Adding %d to even_numbers_only[%d]\n", all_numbers[i], j);
    			even_numbers_only[j] = all_numbers[i];
    			//return result_even;
    		}
    	}
    }
    also fixed my error in not updating the read array from A to all_numbers.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by lleb View Post
    hmm, ok were is this number coming from?

    Code:
    ssma-imac:ENG-3211 ssma$ gcc -o even_odd even_odd.c 
    ssma-imac:ENG-3211 ssma$ ./even_odd
    Please enter 10 integer numbers separated by spaces:
    1 2 3 4 5 6 7 8 9 0
    Adding 2 to even_numbers_only[1606424274]
    Segmentation fault
    Maybe not the best way to word it, but:
    Quote Originally Posted by anduril462 View Post
    I will leave it to you to work out what to start i and j at, and when to increment each.
    You might want to consider picking a sensible starting value for j (hint: arrays in C start at index ____). You will also need to find the right place to increment j, so you don't put every even number in the same spot.

  8. #8
    Registered User
    Join Date
    May 2013
    Posts
    66
    just so im clear, is this a loop inside of a loop like in a multidimensional array?

    arrays start at element 0, thus is why i started i at 0

  9. #9
    Registered User
    Join Date
    May 2013
    Posts
    66
    slowly making progress:

    Code:
    ssma-imac:ENG-3211 ssma$ ./even_oddPlease enter 10 integer numbers separated by spaces:
    1 2 3 4 5 6 7 8 9 0
    Adding 2 to even_numbers_only[0]
    Adding 4 to even_numbers_only[1]
    Adding 6 to even_numbers_only[2]
    Adding 8 to even_numbers_only[3]
    Adding 0 to even_numbers_only[4]
    
    
    The even numbers in your array are: 10
    why is the value of the Even_Array 10? i at last have the correct code for adding the even numbers to even_numbers_only array, so getting that for odd will be simple enough, but now to figure out why its not printing properly... thats the next step.

    duh, and here are the fixes with the still remaining error:

    Code:
    int main(void){
    	int all_numbers[SIZE], even_numbers_only[SIZE], odd_numbers_only[SIZE], new_even_numbers, new_odd_numbers, size = SIZE;
    
    
    	Input_Array(all_numbers, SIZE);
    	new_even_numbers = Even_Array(even_numbers_only, all_numbers, SIZE);
    	printf("\nThe even numbers in your array are: %d\t\n", new_even_numbers);
    
    
    return(0);
    }
    
    
    // code to gather input from user for the 10 elements in the array
    void Input_Array(int all_numbers[], int size)
    {
    	int i;
    	printf("Please enter %d integer numbers separated by spaces:\n", size);
    	for (i = 0; i < size; i++)
    		scanf("%d", &all_numbers[i]);
    }
    
    
    int Even_Array(int even_numbers_only[], int all_numbers[], int size)
    {
    	int i, j, result_even;
    
    
    	j=0;
    
    
    	for (i = 0; i < size; i++)
    	{
    		if(all_numbers[i]%2 == 0)
    		{
    			printf("Adding %d to even_numbers_only[%d]\n", all_numbers[i], j);
    			even_numbers_only[j] = all_numbers[i];
    			j++;
    		}
    	}
    }
    Last edited by lleb; 07-30-2013 at 10:55 PM. Reason: to add the new improved code

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Not a loop inside a loop, and not a multidimensional array. It's two arrays in parallel, but they are "moving at different speeds". i increments every time through the loop, j does not increment every time, but it much increment some of the time. You're were right to start i at 0. But what did you start j at? You should probably start it at the first element of the array, and arrays in C start at index 0. So...

    Work through this on paper. If you can't do it yourself, you can't program a computer to do it. Draw an array of 10 elements, with some "random" numbers in them. That's your all_numbers array. Below that, draw an array with 10 empty spots, that is your even_numbers_only array:
    Code:
    all:  [ 3 ][ 5 ][ 8 ][ 1 ][ 2 ][ 4 ][ 7 ][ 6 ][ 9 ][ 0 ]
    even: [   ][   ][   ][   ][   ][   ][   ][   ][   ][   ]
    Figure out how you would fill in the even array by hand, on paper. As you step through the all array, that will be like incrementing i. Stepping through the even array is like incremenenting j. Pay careful attention to each little step, like when you increment j.those steps will become your algorithm.

  11. #11
    Registered User
    Join Date
    May 2013
    Posts
    66
    buggerz, big time new question ive never thought of before. my new array is no longer 10 elements in size. how do i tell a print function how long the array is properly so o dont get junk out for values that are not set?

    Code:
    void Print_Even_Array(int even_numbers_only[], int size);// new function
    
    int main(void)
    {
    	int all_numbers[SIZE], even_numbers_only[SIZE], odd_numbers_only[SIZE], new_even_numbers, new_odd_numbers, size = SIZE;
    
    
    	Input_Array(all_numbers, SIZE);
    	Even_Array(even_numbers_only, all_numbers, SIZE);
    	Print_Even_Array(even_numbers_only, SIZE);
    	//new_even_numbers = Even_Array(even_numbers_only, all_numbers, SIZE);
    	//printf("\nThe even numbers in your array are: %d\t\n", new_even_numbers);
    
    
    return(0);
    }
    
    
    // code to gather input from user for the 10 elements in the array
    void Input_Array(int all_numbers[], int size)
    {
    	int i;
    	printf("Please enter %d integer numbers separated by spaces:\n", size);
    	for (i = 0; i < size; i++)
    		scanf("%d", &all_numbers[i]);
    }
    
    
    int Even_Array(int even_numbers_only[], int all_numbers[], int size)
    {
    	int i, j, result_even;
    
    
    	j=0;
    
    
    	for (i = 0; i < size; i++)
    	{
    		if(all_numbers[i]%2 == 0)
    		{
    			printf("Adding %d to even_numbers_only[%d]\n", all_numbers[i], j);
    			even_numbers_only[j] = all_numbers[i];
    			j++;
    		}
    	}
    }
    
    
    void Print_Even_Array(int even_numbers_only[], int size)
    {
    	int i;
    	
    	printf("The even numbers are: \n\n");
    	for (i = 0; i < size; i++)
    	{
    		printf("%d\t", even_numbers_only[i]);
    	}
    }
    sadly it works, but i forgot all about the SIZE = 10 above, thus im getting junk out:

    Code:
    ssma-imac:ENG-3211 ssma$ ./even_odd
    Please enter 10 integer numbers separated by spaces:
    1 2 3 4 5 6 7 8 9 0 
    Adding 2 to even_numbers_only[0]
    Adding 4 to even_numbers_only[1]
    Adding 6 to even_numbers_only[2]
    Adding 8 to even_numbers_only[3]
    Adding 0 to even_numbers_only[4]
    The even numbers are: 
    
    
    2	4	6	8	0	32767	1606420320	32767	0	ssma-imac:ENG-3211 ssma$

  12. #12
    Registered User
    Join Date
    May 2013
    Posts
    66
    Thank you for all of your help. that was fantastic. I LEARNED and I succeeded in creating what I needed:

    Code:
    #include<stdlib.h>#include<stdio.h>
    #include<unistd.h>
    #define SIZE 10
    
    
    void Input_Array(int all_numbers[], int size);
    int Even_Array(int even_numbers_only[], int all_numbers[], int size);
    int Odd_Array(int odd_numbers_only[], int all_numbers[], int size);
    void Print_Even_Array(int even_numbers_only[], int size);
    void Print_Odd_Array(int odd_numbers_only[], int size);
    
    int main(void)
    {
        int all_numbers[SIZE], even_numbers_only[SIZE], odd_numbers_only[SIZE], size = SIZE;
    
    
        Input_Array(all_numbers, SIZE);
        Even_Array(even_numbers_only, all_numbers, SIZE);
        Print_Even_Array(even_numbers_only, Even_Array(even_numbers_only, all_numbers, SIZE));
        Print_Odd_Array(odd_numbers_only, Odd_Array(odd_numbers_only, all_numbers, SIZE));
    
    
    return(0);
    }
    
    
    // code to gather input from user for the 10 elements in the array
    void Input_Array(int all_numbers[], int size)
    {
        int i;
        printf("Please enter %d integer numbers separated by spaces:\n", size);
        for (i = 0; i < size; i++)
            scanf("%d", &all_numbers[i]);
    }
    
    
    int Even_Array(int even_numbers_only[], int all_numbers[], int size)
    {
        int i, j, result_even, *new_size;
    
    
        j=0;
    
    
        for (i = 0; i < size; i++)
        {
            if(all_numbers[i]%2 == 0)
            {
                even_numbers_only[j] = all_numbers[i];
                j++;
            }
        }
        return j;
    }
    
    
    void Print_Even_Array(int even_numbers_only[], int size)
    {
        int i;
        
        printf("The even numbers are: \n\n");
        for (i = 0; i < size; i++)
        {
            printf("%d\t", even_numbers_only[i]);
        }
            printf("\nThere are [%d] elements in this array\n\n", i);
    }
    
    
    int Odd_Array(int odd_numbers_only[], int all_numbers[], int size)
    {
        int i, j;
    
    
        j=0;
    
    
        for (i = 0; i < size; i++)
        {
            if(all_numbers[i]%2 != 0)
            {
                odd_numbers_only[j] = all_numbers[i];
                j++;
            }
        }
        return j;
    }
    
    
    void Print_Odd_Array(int odd_numbers_only[], int size)
    {
        int i;
        
        printf("The odd numbers are: \n\n");
        for (i = 0; i < size; i++)
        {
            printf("%d\t", odd_numbers_only[i]);
        }
            printf("\nThere are [%d] elements in this array\n\n", i);
    }
    with flawless results:

    Code:
    ssma-imac:ENG-3211 ssma$ ./even_odd
    Please enter 10 integer numbers separated by spaces:
    1 2 3 4 5 6 7 8 9 0
    The even numbers are: 
    
    
    2    4    6    8    0    
    There are [5] elements in this array
    
    
    The odd numbers are: 
    
    
    1    3    5    7    9    
    There are [5] elements in this array
    
    
    ssma-imac:ENG-3211 ssma$ ./even_odd
    Please enter 10 integer numbers separated by spaces:
    2 2 2 2 2 2 2 2 2 2
    The even numbers are: 
    
    
    2    2    2    2    2    2    2    2    2    2    
    There are [10] elements in this array
    
    
    The odd numbers are: 
    
    
    
    
    There are [0] elements in this array
    
    
    ssma-imac:ENG-3211 ssma$ ./even_odd
    Please enter 10 integer numbers separated by spaces:
    1 1 2 3 5 6 18 2222222 1111111 3
    The even numbers are: 
    
    
    2    6    18    2222222    
    There are [4] elements in this array
    
    
    The odd numbers are: 
    
    
    1    1    3    5    1111111    3    
    There are [6] elements in this array
    
    
    ssma-imac:ENG-3211 ssma$
    Thank you again. FYI it was just a wild guess on my part to put the function inside of the function for the size of the print functions... not sure if that is standard, but it seems to be working.

    Is there a better way to do that?
    Last edited by lleb; 07-30-2013 at 11:56 PM.

  13. #13
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You're welcome, glad you got it figured out.
    Quote Originally Posted by lleb View Post
    Thank you again. FYI it was just a wild guess on my part to put the function inside of the function for the size of the print functions... not sure if that is standard, but it seems to be working.

    Is there a better way to do that?
    Ahh, you're referring to these two lines:
    Code:
    Print_Even_Array(even_numbers_only, Even_Array(even_numbers_only, all_numbers, SIZE));
    Print_Odd_Array(odd_numbers_only, Odd_Array(odd_numbers_only, all_numbers, SIZE));
    In this case, I find it difficult to read and follow what is going on, especially since you pass the array to the outer function and the inner. Plus, you can't save odd_len or even_len this way, should you want to reuse them. I would do
    Code:
    int even_len;
    
    even_len = Even_Array(even_numbers_only, all_numbers, SIZE);
    Print_Even_Array(even_numbers_only, even_len);
    Sometimes I will use that technique, however, if the function calls are short and it's easy to reason about what will happen. Something like:
    Code:
    char  *s = malloc(strlen(some_str) + 1);
    The one other thing I would suggest is that, since your print functions are so similar, move the common code into one function:
    Code:
    void Print_Array(int array[], int size, char *message)
    {
        int i;
         
        printf("%s", messsage);
        for (i = 0; i < size; i++)
        {
            printf("%d\t", odd_numbers_only[i]);
        }
            printf("\nThere are [%d] elements in this array\n\n", i);
    }
    Then, you only have one print function to write, debug and maintain. You can reuse it like so:
    Code:
    Print_Array(even_numbers_only, even_len, "The even numbers are: \n\n");

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 12-07-2012, 10:44 PM
  2. Replies: 35
    Last Post: 12-01-2011, 08:31 PM
  3. array parts
    By taurus in forum C Programming
    Replies: 18
    Last Post: 10-06-2007, 12:21 AM
  4. Replies: 5
    Last Post: 08-01-2007, 06:17 AM