In: Computer Science
I need specific codes for this C program assignment. Thank you!
C program question:
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.
Deliver your work by either meeting Case-1 or Case-2 requirements given below:
Case 1: The given snapshot in the assignment instructions checks for the following:
For instance, given elements are 0123456789
3 4 0 0124456789
2 5 1 0154456789 (Keeping the change in Row 0 (input for row 1); 2 is switched to 5)
1 6 2 0654456789 (Keeping the change in row1 (input for row 2); 1 has been replaced with 6)
This implies that for every other row, the input array elements gets changed to its previous row elements.
Case 2: Keep initial array elements to be the input for all the rows of the matrix and make necessary switching.
For instance, given elements are 0123456789
3 4 0 0124456789
2 5 1 0153456789 (Discarding the change in Row 0; 2 is switched to 5)
1 6 2 0623456789
Use any one case to solve the assessment problem and submit the same output before deadlines.
Code in c for Case 1
#include <stdio.h>
#include <string.h>
//Case 1
int main(void)
{
//declare size;
const int n = 10;
//create array
int arr[n];
//initia;ize elements of array
for(int i=0;i<n;i++){
arr[i]=i;
}
//take input from user until end of file
int p,q;
while(scanf("%d %d", &p, &q)!=EOF){
//go through the array and
//change all the entries with the same name as p to have the same name as q
for(int i=0;i<n;i++){
// replace p qith q
if(arr[i]==p){
arr[i]=q;
}
}
}
//print the array with maximum 10 positions per line
int pos=0;
int i=0;
while(i<n){
//print a new line on 10 numbers
if(pos==10){
printf("\n");
pos=0;
}
printf("%d", arr[i]);
i++;
}
printf("\n");
}
Code screenshot for case 1
sample output for case 1
Code in c for Case 2
#include <stdio.h>
#include <string.h>
//Case 1
int main(void)
{
//declare size;
const int n = 10;
//create array
int arr[n];
//take input from user until end of file
int p,q;
while(scanf("%d %d", &p, &q)!=EOF){
//initialize array each time we take input
for(int i=0;i<n;i++){
arr[i]=i;
}
//no need to go through the entire array as we know arr[i]=i
//change all the entry with the same name as p to have the same name as q
arr[p]=q;
}
//print the array with maximum 10 positions per line
int pos=0;
int i=0;
while(i<n){
//print a new line on 10 numbers
if(pos==10){
printf("\n");
pos=0;
}
printf("%d", arr[i]);
i++;
}
printf("\n");
}
Code screenshot for Case 2
Sample output for Case 2