Question

Programming language C Please go through this carefully. Needs function void add(int *a1, int n, int...

Programming language C

Please go through this carefully. Needs function void add(int *a1, int n, int *a2)

Write a program addition.c that reads in an array (a1) of numbers, and creates a new array (a2) of numbers such that the first and last numbers of a1 are added and stored as the first number, the second and second-to-last numbers are added and stored as the second number, and so on.

You need to check for even and odd length of arrays. In case of an odd number of integers, leave the central integer in the original list as it is.

As part of the solution, write and call the function add() with the following prototype. The add function should add the numbers of a1 as described above.

void add(int *a1, int n, int *a2);

The add() function should use pointer arithmetic – not subscripting – to visit array elements. In other words, eliminate the loop index variables and all use of the [] operator in the function.

In the main function, ask the user to enter the length of the array, declare the array with the length, call the add function, and display the output array.

Example input/output #1: Enter the length of the array: 5 Enter the elements of the array: 8 4 1 3 9 Output: 17 7 1

Example input/output #2: Enter the length of the array: 4 Enter the elements of the array: 3 4 1 7 Output: 10 5

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Source code:

#include<stdio.h>
#include<stdlib.h>
void add(int *a1,int n,int *a2)
{
   int i,j,k=0;
   if(n%2==0)
   {
       for(i=0,j=n-1;i<j;i++,j--)
       {
           *(a2+k)=*(a1+i)+*(a1+j);
           k++;
       }
   }
   else
   {
       for(i=0,j=n-1;i!=j;i++,j--)
       {
           *(a2+k)=*(a1+i)+*(a1+j);
           k++;
       }
       *(a2+k)=*(a1+i);
       k++;
   }
}
main()
{
   int *a1,*a2,n,i;
   printf("Enter the total array elements of a1: ");
   scanf("%d",&n);
   a1=(int*)malloc(sizeof(int)*n);
   if(n%2==0)
       a2=(int*)malloc(sizeof(int)*(n/2));
   else
       a2=(int*)malloc(sizeof(int)*(n/2+1));
   for(i=0;i<n;i++)
   {
       printf("Enter element %d: ",(i+1));
       scanf("%d",(a1+i));
   }
   add(a1,n,a2);
   printf("\nArray 1 elements are: ");
   for(i=0;i<n;i++)
   {
       printf("%d ",*(a1+i));
   }
   printf("\nArray 2 elements are: ");
   if(n%2==0)
   {
       for(i=0;i<n/2;i++)
       {
           printf("%d ",*(a2+i));
       }
   }
   else
   {
       for(i=0;i<=n/2;i++)
       {
           printf("%d ",*(a2+i));
       }
   }
}

Output:

Add a comment
Know the answer?
Add Answer to:
Programming language C Please go through this carefully. Needs function void add(int *a1, int n, int...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C program convert.c that converts each number in an array by the sum of...

    Write a C program convert.c that converts each number in an array by the sum of that number plus 6 modulus 10. A sample input/output: Enter the length of the array: 5 Enter the elements of the array: 3 928 4 14 77 Output: 9 4 0 0 3 The program should include the following function: void convert(int *a1, int n, int *a2) The function converts every element in array a1 of length n to an output array a2. The...

  • In C language Write a program that includes a function search() that finds the index of...

    In C language Write a program that includes a function search() that finds the index of the first element of an input array that contains the value specified. n is the size of the array. If no element of the array contains the value, then the function should return -1. The program takes an int array, the number of elements in the array, and the value that it searches for. The main function takes input, calls the search()function, and displays...

  • 1. Write a program that reads in two arrays (a1 and a2) of numbers and creates...

    1. Write a program that reads in two arrays (a1 and a2) of numbers and creates a new array (a3) of numbers such that the new array contains all the unique numbers of a1 and a2. Assume the input array elements are unique elements. In the main function, ask the user to enter the length of each array, declare and reads in the numbers for each array, and calculate and display the output array. Example input/output #1: Enter the length...

  • 1. Write a function that converts a string into an int. Assume the int is between...

    1. Write a function that converts a string into an int. Assume the int is between 10 and 99. Do not use the atoi() or the stoi() function. 2. Write a function prototype for problem 1. 3. Write a function call for the function you defined in problem 1. 4. Write a function that converts an int between 10 and 99 into a string. 5. Write a function prototype for problem 4. 6. Write a function call for function you...

  • pls help Write a method void remove(int *a, int index) that will remove the number at...

    pls help Write a method void remove(int *a, int index) that will remove the number at the given index and shift all remaining numbers one position to the left in the array a. Assume 1that the last element of the array is -1. Now, write a main function that will define an array int A[40]=[3, 5, 9, 17, 24, -1]; read from user input an index; and call the method remove passing array A and the index given by the...

  • sort.c #include <stdlib.h> #include <stdio.h> #include "libsort.h" int main() {     int* array;     int size,...

    sort.c #include <stdlib.h> #include <stdio.h> #include "libsort.h" int main() {     int* array;     int size, c;     float median;     printf("Enter the array size:\n");     scanf("%d", &size);     array = (int*) malloc(size * sizeof(int));     printf("Enter %d integers:\n", size);     for (c = 0; c < size; c++)         scanf("%d", &array[c]);     sort(array, size);     printf("Array sorted in ascending order:\n");     for (c = 0; c < size; c++)         printf("%d ", array[c]);     printf("\n");     median = find_median(array,...

  • c++ LUILIIR Exercise #3: The xOr Cipher Write the fol lowing functions: void integerToBinary(int, int [0):...

    c++ LUILIIR Exercise #3: The xOr Cipher Write the fol lowing functions: void integerToBinary(int, int [0): // Convers an integer number to binary and stores the binary // form in an array. I/ Converts a string to binary by converting the ASCII value of I/ each character into binary. l/ Calculates the xor of two binary numbers. void stringToßinary (string, int Il): void xOr(int, int O. int, int [I, int [l): The ASCII values of the characters 'A, 'B', C,...

  • IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are...

    IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are developing a program that works with arrays of integers, and you find that you frequently need to duplicate the arrays. Rather than rewriting the array-duplicating code each time you need it, you decide to write a function that accepts an array and its size as arguments. Creates a new array that is a copy of the argument array, and returns a pointer to the...

  • Using C programming

    Using C, create a data file with the first number being an integer. The value of that integer will be the number of further integers which follow it in the file. Write the code to read the first number into the integer variable how_many.Please help me with the file :((This comes from this question:Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int...

  • Consider the following program that reads a number of nonnegative integers into an array and prints...

    Consider the following program that reads a number of nonnegative integers into an array and prints the contents of the array.   Complete the missing parts, add new function prototypes and function definitions, and test the program several times. Add the following to the program: Write a void function that prints the list of nonnegative integers in reverse. Write a void function that prints all the numbers stored in the list that are greater than 10. It will also print the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT