Question

In: Computer Science

Write a small C program connect.c that: 1. Initializes an array id of N elements with...

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!!

Solutions

Expert Solution

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;
        }
    
    }
}


Related Solutions

Write a small C program connect.c that: 1. Initializes an array id of N elements with...
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...
In C programming: Write a program that initializes an array-of-double and then copies the contents of...
In C programming: Write a program that initializes an array-of-double and then copies the contents of the array into another arrays. To make the copy, use a function with array notation. This function takes two arguments the name of the target array and the number of elements to be copied. That is, the function calls would look like this, given the following declarations: double source[5] ={1.1, 2.2, 3.3., 4.4, 5.5}; double target1[5]; double target2[5]; copyarr(source, target1, 5);
Program in C: Write a program in C that reorders the elements in an array in...
Program in C: Write a program in C that reorders the elements in an array in ascending order from least to greatest. The array is {1,4,3,2,6,5,9,8,7,10}. You must use a swap function and a main function in the code. (Hint: Use void swap and swap)
Write a program in Java that initializes an array with ten random integers and then print...
Write a program in Java that initializes an array with ten random integers and then print three lines of output, containing: Every element at an odd index Every odd element All elements in reverse order   The program should use three different methods to implement the functionalities above. Call the primary source file ArrayManipulator.java
Write Java program Lab43.java which declares and initializes numeric array of 5 elements (int num[5]) and...
Write Java program Lab43.java which declares and initializes numeric array of 5 elements (int num[5]) and produces as output the sum of integers in an array, the largest and the smallest element in an array. Your program should contain the following methods: public static int sum(int[]) public static int findLargest(int[]) public static int findSmallest(int[])
Write Program in C: Write a program that: program starts; declares and initializes to 7.25% constant...
Write Program in C: Write a program that: program starts; declares and initializes to 7.25% constant float variable NJSALES_TAX; declares and initializes to 1000 an integer variable total; declares and initializes to zero a float variable grand_total; prompt and input on new line total; calculate grand_total to equal total plus (total*NJSALES_TAX); if grand_total <= 1000 print on new line “Grand total is less than or equal to 1000 it is $” and the grand_total to two decimal places; else if...
Write C program that reorders elements of an array of integers such that the new order...
Write C program that reorders elements of an array of integers such that the new order is in descending order (first number being the largest). Must have a main function and a swap function. - int main() will declare an array with the values { 32, 110, 79, 18, 22, 2}. This array will be passed to the swap function. - the void swap function will perform the necessary operations to reorder the elements of the array. - After swap()...
Write a program that initializes an array of 6 random integers and then prints 4 lines...
Write a program that initializes an array of 6 random integers and then prints 4 lines of output, containing the following: 1. Only the first and last element 2. Every element at an odd index 3. Every odd element 4. All elements in reverse order
Write a c program Write a function to swap two elements of an integer array. Call...
Write a c program Write a function to swap two elements of an integer array. Call the function to swap the first element, i[0] with last element i[n], second element i[1] with the last but one element i[n-1] and so on. Should handle arrays with even and odd number of elements. Call the swap function with the following arrays and print results in each case before and after swapping. i. int arr1[] = {0, 1, 2, 3, 30, 20, 10,...
Write a C program to show sum of 10 elements of array and show the average....
Write a C program to show sum of 10 elements of array and show the average. [10]
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT