1. Array In C

C Program to Compute the Sum of two One-Dimensional Arrays

Method 1 using malloc

#include <stdio.h>
 
 
#include <malloc.h>
 
#include <stdlib.h>
 
void main()
 
{
        int i, n;
 
    int *a, *b, *c;
 
        printf("How many Elements in each array...\n");
 
        scanf("%d", &n);
 
        a = (int *)malloc(n * sizeof(int));
 
        b = (int *)malloc(n * sizeof(int));
 
    c = (int *)malloc(n * sizeof(int));
 
        printf("Enter Elements of First List\n");
 
        for (i = 0; i < n; i++)
 
        {
 
        scanf("%d", a + i);
 
    }
 
        printf("Enter Elements of Second List\n");
         
        for (i = 0; i < n; i++)
 
        {
 
        scanf("%d", b + i);
 
    }
 
        for (i = 0; i < n; i++)
 
        {
 
        *(c + i) = *(a + i) + *(b + i);
 
    } 
 
        printf("Resultant List is\n");
 
        for (i = 0; i < n; i++)
 
        {
        printf("%d\n", *(c + i));
    }
}

Output

How many Elements in each array...
5
Enter Elements of First List
23
45
67
12
90
Enter Elements of Second List
87
56
90
45
10
Resultant List is
110
101
157
57
100
Comments to: C Program to Compute the Sum of two One-Dimensional Arrays

Your email address will not be published. Required fields are marked *

Attach images - Only PNG, JPG, JPEG and GIF are supported.

Login

Welcome to WikiCoders

Join the site and store the save posts for later, share your own articles
Join WikiCoder
Registration is closed.