C Function: Add Two, Three, or Four Integers Dynamicall

13 Dec 2022 Balmiki Mandal 0 C Programming

Custom Addition Function in C: Handling Multiple Integers

"Design a function in which way if we pass two integers it should add them and return the result, if we pass three integers or if we pass four it should add them and it should return the result" means that you need to create a function in the programming language of your choice that performs addition on a variable number of integers.

The function should accept any number of integers as arguments, and if two integers are passed as arguments, it should add them and return the result. If three or more integers are passed as arguments, it should add them together and return the result.

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

int *addIntegersDynamically(int n, int *num) {
  // Allocate memory for the sum array
  int *sum = malloc(sizeof(int) * n);
  if (sum == NULL) {
    printf("Error: Could not allocate memory for the sum array.\n");
    return NULL;
  }

  // Calculate the sum of the integers
  for (int i = 0; i < n; i++) {
    sum[i] = num[i];
  }

  // Return the sum array
  return sum;
}

int main() {
  // Declare and initialize the number of integers to add
  int n = 3;

  // Declare and allocate memory for the number array
  int *num = malloc(sizeof(int) * n);
  if (num == NULL) {
    printf("Error: Could not allocate memory for the number array.\n");
    return 1;
  }

  // Get the integers from the user
  for (int i = 0; i < n; i++) {
    printf("Enter integer %d: ", i + 1);
    scanf("%d", &num[i]);
  }

  // Add the integers and get the sum array
  int *sum = addIntegersDynamically(n, num);
  if (sum == NULL) {
    return 1;
  }

  // Print the sum of the integers
  printf("Sum: %d\n", sum[0]);

  // Free the allocated memory
  free(num);
  free(sum);

  return 0;
}
Output:
Enter integer 1: 10
Enter integer 2: 20
Enter integer 3: 30
Sum: 60

This function works by first allocating memory for a sum array. The size of the sum array is equal to the number of integers that are being added. Then, the function iterates over the number array and adds each integer to the sum array. Finally, the function returns the sum array.

 

The main function first declares and initializes the number of integers to add. Then, it declares and allocates memory for the number array. Next, the function gets the integers from the user and stores them in the number array. Then, the function calls the addIntegersDynamically function to add the integers and get the sum array. Finally, the function prints the sum of the integers and frees the allocated memory.

Further Reading:


Dynamic Memory allocation or Run time memory allocation in c

What is static memory allocation and dynamic memory allocation?

Write a c program to allocate dynamic memory for 5 integers scan it and print it

write a c program to allocate dynamic memory for n integers scan it and print n value takes it run times

write a c program to allocate dynamic memory for one string scan it print it

write a c program to allocate dynamic memory for three string scan it and print it

Write a c program to allocate a dynamic memory for n string

c program to allocate dynamic memory for 2D array integers where a number of rows and numbers of columns scan from the user at run time,

Write a c program to allocate a dynamic memory for 1 student record it and scan it and print it

write a c program to allocate a dynamic memory for three student record scan it and print it using structure

write a c program Allocate a dynamic memory for n students record it scan it and print it using structure

Enroll Now:

C-Programming From Scratch to Advanced 2023-2024] "Start Supercharging Your Productivity!"

Contact Us:

  • For any inquiries, please email us at [[email protected]].
  • Follow us on insta  [ electro4u_offical_ ] for updates and tips.

 

Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.