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.
code in C (code to copy)
#include <stdio.h> #include <stdlib.h> #include <time.h> int main(int argc, char **argv){ //initialize N with 100 const int N = 100; //declare an array with N elements int arr[N]; //fill this array with value = index for(int i=0;i<N;i++){ arr[i]=i; } //read pair of input from user until eof which happens when scanf returns -1 int p,q; while (scanf("%d %d", &p, &q) != -1) { arr[p]=q; } //print the array for(int i=0;i<N;i++){ //print a line break at every 10 elements if(i%10==0) printf("\n"); printf("%d ", arr[i]); } }
code screenshot
Console Input/Output Screenshot
i hope it helps..
If you have any doubts please comment and please don't dislike.
PLEASE GIVE ME A LIKE. ITS VERY IMPORTANT FOR ME
Get Answers For Free
Most questions answered within 1 hours.