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

  • Home
  • SEARCH
  • 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 7020581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:20:13+00:00 2026-05-27T23:20:13+00:00

I have a problem in C. This is the question: Develop a C function

  • 0

I have a problem in C. This is the question:

Develop a C function ADDER that adds two integer arrays together. ADDER should have only two parameters, which are the two arrays to be added. The second array argument will hold the sum of arrays on exit. Both parameters should be passed by reference.

Write a C program to test the ADDER function with the call ADDER (A, A) where A is an array to be added to itself. Array A can be of any size with any values. Write, compile, and execute the program.

Explain the results of the program.


So far I have solved it this way and it works just fine:

#include <stdio.h>

// using namespace std;

const int SIZE = 5;

/* Adds two arrays and saves the result in b
 * Assumes that b is larger than or equal to a in size
 */

void ADDER(int (&a)[SIZE], int (&b)[SIZE]) {
    int aSize, bSize, i; /* variable declaration */
    /* find out the sizes first */
    aSize = sizeof (a) / sizeof (int);
    bSize = sizeof (b) / sizeof (int);
    /* add the values into b now */
    for (i = 0; i < aSize; i++) {
    b[i] = b[i] + a[i];
    }
    /* we have the sum at the end in b[] */
}

/* Test program for ADDER */

int main() {
int i; /* variable declaration */
int a[] = {1, 2, 3, 4, 5}; /* the first array */

/* add them now */
ADDER(a, a);
/* print results */
printf("\nThe sum of the two arrays is: ");
for (i = 0; i < SIZE; i++) {
    printf("%d ", a[i]); /* print each element */
}
return 0;
}

The problem is, I have to use dynamic arrays and use malloc and realloc in the program to compute the size of the array on the fly. Instead of specifying the array size and the elements itself, I want the program to ask the user for input and the user enters the array and the size is determined there. It should all be dynamic. I do not know how this is done. Can anyone please help me out! thanks!

Also I have to explain how the array is added to itself the result is saved in “a” and the original array is lost replaced by the sum. how can I explain this?

  • 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-05-27T23:20:14+00:00Added an answer on May 27, 2026 at 11:20 pm

    Here is how your program would look like

    int size; //global variable
    
    void ADDER(int *a, int *b) {
        int i;
        for (i = 0; i < size; i++) {
            b[i] += a[i];
        }    
    }
    
    int main(){
        //ask the user to input the size and read the size
        int *a = (int *)malloc(size*sizeof(int));
        int *b = (int *)malloc(size*sizeof(int));
    
        //fill up the elements
        Adder(a,b);
        //print out b
        free(a->array);
        free(b->array);
    }
    

    ALthough its not wise to use globals, the bottom line here is that adder somehow needs to know the size of the array and somehow you need to convey the size to the ADDER function. If that can’t be done through parameters, you have to use globals.

    Another option would be to use structures.

    typedef struct myArray{
        int *array;
        int length;
    }ARRAY;
    
    void ADDER(ARRAY *a, ARRAY *b) {
        int i;
        for (i = 0; i < b->length; i++) {
            b->array[i] += a->array[i];
        }    
    }
    
    int main(){
        int size; //local variable
        //ask the user to input the size and read into the 'size' variable
        ARRAY *a, *b;
        a->array = (int *)malloc(size*sizeof(int));
        b->array = (int *)malloc(size*sizeof(int));
        a->length = b->length = size;
    
        //fill up the elements
        Adder(a,b);
        //print out b.array
        free(a->array);
        free(b->array);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(I have a problem that I illustrated in this question but had no correct
Similar to this question: link However I have already mastered that. My problem is
I seem to have the exact opposite problem than this question on stopping dock
I have basically the same problem outlined in this question, however I am using
The responses I've got to this question have solved the problem I had in
I have asked this question before and the problem was half solved in the
I have no good title for this question, but my problem is to set
This is more of a theoretical question than an actual problem I have. If
This is a code review question more then anything. I have the following problem:
This is a very high-level question. I'm looking for insight into this problem that

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.