Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.5 kB
1
Indexable
Never
#include <stdio.h>

int main()
{
    printf("Welcome to LAP 4 program 2! // creating arrays.\n");
    int size1 = 10;
    int size2 = size1 * 2;
    int arr1[size1], arr2[size1], arr3[size2], i, j;
    //  --- entering the values of array 1 ---
    printf("\nArray 1:\n\n");
    for(i = 0; i < 10; ++i){
      printf("Enter a number:");
      scanf("%d", &arr1[i]);
    }
    //  --- entering the values of array 2 ---
    printf("\nArray 2:\n\n");
    for(i = 0; i < 10; ++i){
      printf("Enter a number:");
      scanf("%d", &arr2[i]);
    }
    printf("---------------------\n");
    //  --- setting the values of array 3 ---
    printf("\nCopying the elements of array 1 & 2 into array 3 (in an interleaved order)...\n\n");
    //  --- array 1 into 3 ---
    for(i = 0, j = 0; i < size1; ++i, j += 2) {
          arr3[j] = arr1[i];
    }
    //  --- array 2 into 3 ---
    for(i = 0, j = 1; i < size1; ++i, j += 2) {
          arr3[j] = arr2[i];
    }
    //  --- printing the results ---
    printf("---------------------\n");
    printf("Results of Array 1:\n");
    for(i = 0; i < size1; ++i) {
      printf("%d", arr1[i]);
      if(i != 9) printf(", ");
    }
    printf("\nResults of Array 2:\n");
    for(i = 0; i < size1; ++i) {
      printf("%d", arr2[i]);
      if(i != 9) printf(", ");
    }
    printf("\nResults of Array 3:\n");
    for(i = 0; i < size2; ++i) {
      printf("%d", arr3[i]);
      if(i != 19) printf(", ");
    }
    return 0;
}