In: Computer Science
Write a small C program connect.c that:
1. Initializes an array id of N elements with the value of the index of the array.
2. Reads from the keyboard or the command line a set of two integer numbers (p and q) until it encounters EOF or CTL - D
3. Given the two numbers, your program should connect them by going through the array and changing all the entries with the same name as p to have the same name as q.
4. Once the EOF has been reached, your program should print the array with a max of 10 positions per line.
5. For testing purposes, define N as 10 at the beginning, but by sure of running data with at least 100 elements.
This is a C Program. I need specific codes for this assignment. Thank you!!
In the above we are asked to write code in c to that nitializes an array id of N elements with the value of the index of the array.
we are asked to input two number p and q and change content of array where value is p to q till we get EOF Or D key pressed over keyboard.
#include<stdio.h>
#include<conio.h>
#define N 1000
int main()
{
    int n;
    scanf("%d",&n);
    int a[n];
    int i,j;
    for(int i=0;i<n;i++)
     a[i]=i;
    while(1)
    {
        char  key = getch();
        if(key=='D' || key=='d'){//as we press D key program will print content of array atmax 10 in one line and then terminate the program
            int mod = n%10;
/*printfing array content*/
            for(int i=0;i<n-mod;i+=10)
            {
                for(int j=i;j<=i+9;j++)
                printf("%d ",a[j]);
                printf("\n");//line change
            }
            for(int i=n-mod;i<n;i++)
            printf("%d ",a[i]);
            break;
        }
        int p,q;
        scanf("%d %d",&p,&q); // traversing array and changing p to q at each place
        for(int i=0;i<n;i++)
        {
            if(a[i]==p)
            a[i]=q;
        }
    
    }
}
