Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8091397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:03:21+00:00 2026-06-05T20:03:21+00:00

I have to do the following: Given a vector with random numbers, separate it

  • 0

I have to do the following:

Given a vector with random numbers, separate it in 2 other vectors, odd and even. But in the function, all vectors must be passed via parameter (can’t use global).
Here is my code:

#include <stdio.h>
#include <stdlib.h>

void vetores(int *vetor, int *A, int *B)
{
    int i = 0,aux;
    for(i = 0;i < 100; i++)
    {
        if(vetor[i] % 2 == 0)
        {
            aux = vetor[i];
            A[i] = aux;
        }else{
            aux = vetor[i];
            B[i] = aux;
        }
    }
}

int main()
{
    int vetor[101], a[51], b[51],i;

    /*a = (int)malloc(sizeof(int)*51);
    b = (int)malloc(sizeof(int)*51);*/

    for(i = 0; i < 100; i++)
    {
        vetor[i] = i;
    }

    vetores(vetor,a,b);

    for(i = 0; i < 50; i++)
    {
        if(vetor[i] % 2 == 0)
        {
            printf("%d",a[i]);
        }else
        {
            printf("%d",b[i]);
        }
    }

    return 0;
}

If you test that code, the vector is being altered (it is not supposed to) and the A vector is not receiving the even numbers!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T20:03:23+00:00Added an answer on June 5, 2026 at 8:03 pm

    Your main problem is that you’re indexing your outputs based on the position in the input.

    For example, if your first even number is at input[3], then you’re writing it to even_output[3], where is should (probably) be at even_output[0].

    You probably want something like:

    if (input[i] %2 == 0)
        *A++ = input[i];
    else
        *B++ = input[i];
    

    You probably also need to do something to tell the caller how many odd and even numbers you found (and the caller may have to allocate memory a bit differently unless those counts are equal — right now, you have 101 inputs, and 51 spots allocated for each output. If you had (for example) 60 even and 40 odd numbers, you’ll write beyond end of the space you’ve allocated for the even numbers. With the inputs you’ve provided, that problem won’t arise, but in almsot any other case, it’s not only possible, but in fact likely.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

given i have the following block of code (function(){ var mb = { abc:function(){
I have the following code (generates a quadratic function given the a, b, and
I have the following SQL query I was given, but I am not sure
A different question inspired the following thought: Does std::vector<T> have to move all the
I have the following function, which generates an integer in a given range: 18
I have the following code for a vector of vectors of integers (i.e. an
I have the following problem, given the following service contract, data contract, and service
I have the following html code (in the given order) <div id=content>...</div> <div id=footer>...</div>
Given that I have the following WCF service: class LookUpService { public List<County> GetCounties(string
I have the following problem: I am given a tree with N apples, for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.