DS Practical Slip 1 Q A

Copying one array into another is one of the simplest and most important programs in C. This program helps students understand arrays and loops clearly.

📝 Problem Statement

Q1. A) Write a C program to copy one array into another array.


💡 Logic (Very Simple Steps)

  1. Declare two arrays.
  2. Read the size of the array.
  3. Read elements into the first array.
  4. Use a for loop to copy each element into the second array.
  5. Print the second array.

👉 We copy using:

arr2[i] = arr1[i];

💻 Source Code (Simple Student Version)

#include <stdio.h>

int main()
{
    int arr1[100], arr2[100];
    int n, i;

    printf("Enter number of elements: ");
    scanf("%d", &n);

    printf("Enter elements:\n");
    for(i = 0; i < n; i++)
    {
        scanf("%d", &arr1[i]);
    }

    for(i = 0; i < n; i++)
    {
        arr2[i] = arr1[i];
    }

    printf("Copied array is:\n");
    for(i = 0; i < n; i++)
    {
        printf("%d ", arr2[i]);
    }

    return 0;
}

🖥️ Sample Output

Enter number of elements: 4
Enter elements:
5
10
15
20
Copied array is:
5 10 15 20

🎓 Viva Questions with Answers

1. What is an array?

An array is a collection of elements of the same data type.

2. Why do we use a loop to copy an array?

Because we need to copy each element one by one.

3. Can we write arr2 = arr1 in C?

No, arrays cannot be directly assigned in C.

4. What is the index of the first element in an array?

The first index is 0.

5. Which loop is commonly used for arrays?

The for loop is commonly used.Q1. A) Write a C program to Copy one array into another array.

Spread the love

Leave a Comment

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

Scroll to Top