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...
1. a. In C++, Write a program that creates an array of 20 integers and initializes...
1. a. In C++, Write a program that creates an array of 20 integers and initializes it with the even values starting from 200. i.e. 200, 202, 204…. b. Write the elements of the array to the file even.txt, each element on a separate line. Try to use a single for loop for initializing the array and writing to file. You will need a separate counter for initializing the array. 2. a. Write another program that opens the file even.txt...
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)
In C, write a program that initializes a 3D array (the exact size doesn't matter) that...
In C, write a program that initializes a 3D array (the exact size doesn't matter) that contain what ever arbitrary integers and print out the array including the elements and use different functions so that the main only contains variables and function calls.
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
(8%) Write a C/C++ program that takes an input (array) from 1 to n (say n...
(8%) Write a C/C++ program that takes an input (array) from 1 to n (say n = 50) and displays the string representations of those numbers with following conditions If the current number is divisible by 2, then print CSU If the current number is divisible by 5, then print SB If the current number is divisible by both 2 and 5, then print CSUSB If the number is neither divisible by 2 nor 5, then print the number Example:...
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()...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT