Write a c program to perform array multiplication? Skip to main content

Write a c program to perform array multiplication?


Question: Write a c program to perform array multiplication?

Array multiplication is a common operation in many programming tasks, such as matrix multiplication, image processing, and linear algebra. In this blog post, we will learn how to write a C program to perform array multiplication using pointers and dynamic memory allocation.


First, let us define what array multiplication is. Given two arrays A and B of the same size n, the array multiplication C = A * B is defined as follows:


C[i] = A[i] * B[i] for i = 0, 1, ..., n-1


For example, if A = [1, 2, 3] and B = [4, 5, 6], then C = [4, 10, 18].


To write a C program to perform array multiplication, we need to do the following steps:


1. Declare three pointers to store the addresses of the arrays A, B, and C.

2. Ask the user to enter the size of the arrays and store it in a variable n.

3. Allocate memory for the arrays A, B, and C using the malloc function and check for errors.

4. Ask the user to enter the elements of the arrays A and B and store them using pointer arithmetic.

5. Perform array multiplication by looping through the arrays A and B and storing the result in C using pointer arithmetic.

6. Display the result array C using pointer arithmetic.

7. Free the memory allocated for the arrays A, B, and C using the free function.


The following is a possible implementation of the above steps in C:


#include <stdio.h>

#include <stdlib.h>


int main()

{

    // Declare three pointers to store the addresses of the arrays

    int *A, *B, *C;

    // Declare a variable to store the size of the arrays

    int n;

    // Ask the user to enter the size of the arrays

    printf("Enter the size of the arrays: ");

    scanf("%d", &n);

    // Allocate memory for the arrays A, B, and C using malloc and check for errors

    A = (int *)malloc(n * sizeof(int));

    if (A == NULL)

    {

        printf("Memory allocation failed for A.\n");

        exit(1);

    }

    B = (int *)malloc(n * sizeof(int));

    if (B == NULL)

    {

        printf("Memory allocation failed for B.\n");

        exit(1);

    }

    C = (int *)malloc(n * sizeof(int));

    if (C == NULL)

    {

        printf("Memory allocation failed for C.\n");

        exit(1);

    }

    // Ask the user to enter the elements of the arrays A and B

    printf("Enter the elements of array A: ");

    for (int i = 0; i < n; i++)

    {

        scanf("%d", A + i); // Equivalent to scanf("%d", &A[i])

    }

    printf("Enter the elements of array B: ");

    for (int i = 0; i < n; i++)

    {

        scanf("%d", B + i); // Equivalent to scanf("%d", &B[i])

    }

    // Perform array multiplication by looping through the arrays A and B

    for (int i = 0; i < n; i++)

    {

        *(C + i) = *(A + i) * *(B + i); // Equivalent to C[i] = A[i] * B[i]

    }

    // Display the result array C

    printf("The result of array multiplication is: ");

    for (int i = 0; i < n; i++)

    {

        printf("%d ", *(C + i)); // Equivalent to printf("%d ", C[i])

    }

    printf("\n");

    // Free the memory allocated for the arrays A, B, and C

    free(A);

    free(B);

    free(C);

    

    return 0;


Comments

Popular posts from this blog

अज्ञेय का वैवाहिक जीवन उतार-चढ़ाव भरा था, वर्णन कीजिए? Aage ka vaivahik jivan utar chadhav bhara tha varnan kijiye

सवाल: अज्ञेय का वैवाहिक जीवन उतार-चढ़ाव भरा था, वर्णन कीजिए? अज्ञेय का वैवाहिक जीवन उतार-चढ़ाव भरा था। उन्होंने दो विवाह किए, लेकिन दोनों विवाह सफल नहीं हो पाए। उनका पहला विवाह 1937 में राजकुमारी रत्नलता से हुआ था। यह विवाह अज्ञेय के परिवार की इच्छा के विरुद्ध था। अज्ञेय के पिता हीरानंद वात्स्यायन संस्कृत के विद्वान थे और वे चाहते थे कि अज्ञेय एक योग्य विदुषी से विवाह करें। रत्नलता एक साधारण परिवार से थीं और वे संस्कृत नहीं जानती थीं। इस कारण से, अज्ञेय के परिवार वालों ने इस विवाह का विरोध किया। अज्ञेय और रत्नलता का विवाह कुछ वर्षों तक चलता रहा, लेकिन अंततः उनका तलाक हो गया। इस तलाक के कई कारण थे, जिनमें अज्ञेय की व्यस्त जीवनशैली, रत्नलता की शिक्षा और सामाजिक पृष्ठभूमि में अंतर, और अज्ञेय के परिवार का विरोध शामिल था। अज्ञेय का दूसरा विवाह 1956 में कपिला मलिक से हुआ था। कपिला एक मशहूर संगीतकार थीं और वे अज्ञेय की साहित्यिक प्रतिभा की प्रशंसक थीं। यह विवाह कुछ वर्षों तक चलता रहा, लेकिन अंततः यह भी विफल हो गया। इस विफलता के कारणों में अज्ञेय के व्यस्त जीवनशैली, कपिला की स्वतंत्रताप्रियता,...

Explain voter apathy indifference among the youth considered a challenge for south africa democracy?

Question: Explain voter apathy indifference among the youth considered a challenge for south africa democracy?  Voter apathy and indifference among the youth pose significant challenges to South Africa's democracy. When young people disengage from the electoral process, it undermines the representativeness and legitimacy of the government. Low youth voter turnout means that the voices and concerns of young South Africans are underrepresented in policymaking, leading to policies that may not adequately address issues crucial to them, such as unemployment, education, and access to healthcare. This lack of participation can also empower special interests and entrenched elites, further marginalizing young people's voices in the political process. Additionally, voter apathy among the youth can contribute to political instability, as a significant portion of the population feels unheard and unrepresented, 

List four social beliefs in south africa that contribute to the high frequency of sexual assault?

Question: List four social beliefs in south africa that contribute to the high frequency of sexual assault? Four societal beliefs in South Africa that contribute to the high frequency of sexual assault include: Gender Inequality and Patriarchy – Deep-rooted patriarchal structures place men in positions of power, leading to the normalization of violence against women and reinforcing male dominance. Victim-Blaming Attitudes – Many believe that victims are responsible for the assault due to their behavior, clothing, or choices, discouraging them from reporting incidents and allowing perpetrators to act without accountability. Cultural Norms Surrounding Masculinity – Traditional views of masculinity emphasize aggression and dominance, leading some men to feel entitled to assert power over women through sexual violence. Silence and Stigma Around Sexual Violence – Many victims fear speaking out due to societal stigma, allowing abuse to go unreported and perpetrators to continue their actions...