Question

In: Computer Science

I need specific codes for this C program assignment. Thank you! C program question: Write a...

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:

  • P to be switched with Q (Once done will remain as it is in all the rows)
  • If the user enters the same P again, the program must not make any changes

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.

Solutions

Expert Solution

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


Related Solutions

in c++ you need to write a program that maintains the gradebook for a specific course....
in c++ you need to write a program that maintains the gradebook for a specific course. For each student registered in this course, the program keeps track of the student’s name, identification number (id), four exam grades, and final grade. The program performs several functionalities such as: enrolling a student in the course, dropping a student from the course, uploading student’s exam grades, displaying the grades of a specific student, displaying the grades of all students in the course, and...
For this assignment, you need to write a parallel program in C++ using OpenMP for vector...
For this assignment, you need to write a parallel program in C++ using OpenMP for vector addition. Assume A, B, C are three vectors of equal length. The program will add the corresponding elements of vectors A and B and will store the sum in the corresponding elements in vector C (in other words C[i] = A[i] + B[i]). Every thread should execute an approximately equal number of loop iterations. The only OpenMP directive you are allowed to use is:...
For this assignment, I need to write a c program named stick which plays a matchstick-picking...
For this assignment, I need to write a c program named stick which plays a matchstick-picking game. Given an initial number of sticks, players take turns picking either 1, 2, 3 or 4 sticks from a pile. Whoever picks the last stick wins. Usage The user can run stick with or without command line arguments. i.e. somebody can enter the number of sticks to begin play when the program is launched or they can be prompted after the program begins...
For this assignment, I need to write a c program named stick which plays a matchstick-picking...
For this assignment, I need to write a c program named stick which plays a matchstick-picking game. Given an initial number of sticks, players take turns picking either 1, 2, 3 or 4 sticks from a pile. Whoever picks the last stick wins. Usage You run stick with or without command line arguments. i.e. somebody can enter the number of sticks to begin play when the program is launched or they can be prompted after the program begins running. Print...
For this assignment, I need to write a c program named stick which plays a matchstick-picking...
For this assignment, I need to write a c program named stick which plays a matchstick-picking game. Given an initial number of sticks, players take turns picking either 1, 2, 3 or 4 sticks from a pile. Whoever picks the last stick wins. Usage You run stick with or without command line arguments. i.e. somebody can enter the number of sticks to begin play when the program is launched or they can be prompted after the program begins running. Print...
I need to update this program to follow the these requirements please and thank you :...
I need to update this program to follow the these requirements please and thank you : Do not use packages Every class but the main routine class must have a toString method . The toString method for Quadrilateral should display its fields: 4 Points. All instance variables should be declared explicitly private . Area incorrect : enter points no: 1 1 3 enter points no: 2 6 3 enter points no: 3 0 0 enter points no: 4 5 0...
Program this in C thank you PROBLEM DESCRIPTION: Write a program to implement the following requirement:...
Program this in C thank you PROBLEM DESCRIPTION: Write a program to implement the following requirement: The program will read from standard input two things - a string str1 on the first line of stdin (this string may be an empty string) - a string str2 on the second line of stdin (this string may be an empty string) Note that stdin does not end with '\n'. The program will output a string that is the concatenation of string str1...
C# language Question: You need to write a program for students who receive bursaries in the...
C# language Question: You need to write a program for students who receive bursaries in the department, managing the free hours they have to do. For each recipient you store the recipient’s name and the number of hours outstanding. All recipients start with 90 hours. Implement class Recipients which has the private attributes Name and Hours. In addition to the constructor, the class has the following methods: public String getName() // Returns the name of the recipient public int getHours()...
I need a scholarship essay for my nursing program. please and thank you.
I need a scholarship essay for my nursing program. please and thank you.
In C++ For this assignment, you will write a program to count the number of times...
In C++ For this assignment, you will write a program to count the number of times the words in an input text file occur. The WordCount Structure Define a C++ struct called WordCount that contains the following data members: An array of 31 characters named word An integer named count Functions Write the following functions: int main(int argc, char* argv[]) This function should declare an array of 200 WordCount objects and an integer numWords to track the number of array...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT