Question

In: Computer Science

Pet [0] = "dog"  Pet[1] = "cat"    Pet[2] = "bird"   Pet[3] = "snake"   Pet[4] = "duck"   Pet[5]...

Pet [0] = "dog"  Pet[1] = "cat"    Pet[2] = "bird"   Pet[3] = "snake"   Pet[4] = "duck"   Pet[5] = "fish"   Pet[6} = "rabbit'   Pet[7] = "mouse"   Pet[8] = "pony" Pet[9] = "frog"                       SA 18.) Write a program segment to sort the given array in alphabetical order using the bubble sort method. Raptor Format (Prelude to Programing 6th ed.)

Solutions

Expert Solution

C program to read N names, store them in the form of an array and sort them in alphabetical order using Bubble Sort.

    #include <stdio.h>

    #include <string.h>

   

    void main()

    {

        char name[10][8], tname[10][8], temp[8];

        int i, j, n;

     //The number of names in the array

        printf("Enter the value of n :- \n");

        scanf("%d", &n);
      
    //Enter all the names in the array and they will be stord in a 2-D array

        printf("Enter %d names \n", n);

        for (i = 0; i < n; i++)

        {

            scanf("%s", name[i]);

            //copy name[i] to tnam[i] so tname[i] contains the data we entered
            strcpy(tname[i], name[i]);

        }


      //Sort the array using Bubble Sort Mechanism
        for (i = 0; i < n - 1 ; i++)

        {

            for (j = i + 1; j < n; j++)

            {

                if (strcmp(name[i], name[j]) > 0)

                {

                    strcpy(temp, name[i]);

                    strcpy(name[i], name[j]);

                    strcpy(name[j], temp);

                }

            }

        }
      
        //now name array will contain the sorted information
      
        //display the input names and the sorted information

        printf("Input Names\tSorted names\n");

        printf("------------------------------------------\n");

        for (i = 0; i < n; i++)

        {

            printf("%s\t\t%s\n", tname[i], name[i]);

        }

    }


Output:-

Enter the value of n:-  10
Enter 10 names 
Input Names     Sorted names
------------------------------------------
dog             bird
cat             cat
bird            dog
snake           duck
duck            fish
fish            frog
rabbit          mouse
mouse           pony
pony            rabbit
frog            snake

Related Solutions

exampleInput.txt 1 2 3 0 2 3 4 0 1 3 5 0 1 2 6...
exampleInput.txt 1 2 3 0 2 3 4 0 1 3 5 0 1 2 6 1 5 6 8 2 4 6 7 3 4 5 9 10 5 8 9 4 7 9 6 7 8 6 How can I detect when 'cin' starts reading from a new line. The amount of numbers in each row is unknown. I need them in type 'int' to use the data.
DATA 3 8 2 15 2 2 0 0 4 5 2 7 0 1 5...
DATA 3 8 2 15 2 2 0 0 4 5 2 7 0 1 5 3 0 2 5 4 1 6 9 5 3 1 2 10 6 1 1 2 1 19 6 6 6 7 0 4 1 1 1 0 1 9 2 2 2 1 16 10 10 5 2 3 1 4 4 4 3 6 2 8 5 2 7 1 6 4 0 3 1 1 1 Background: A group of...
x (Bins) frequency 0 0 1 0 2 0 3 2 4 5 5 8 6...
x (Bins) frequency 0 0 1 0 2 0 3 2 4 5 5 8 6 13 7 33 8 42 9 66 10 77 11 105 12 103 13 110 14 105 15 84 16 70 17 51 18 40 19 27 20 27 21 15 22 5 23 7 24 2 25 2 26 1 27 0 28 0 29 0 30 0 (7) On the Histogram worksheet, calculate all frequencies of the distribution using the table shown....
Find the distances: A) Between ?1=〈2+2?,−1+?,−3?〉and ?2=〈4,−5−3?,1+4?〉 . B) Between the planes 2?−?+5?=0 and 2?−?+5?=5 ....
Find the distances: A) Between ?1=〈2+2?,−1+?,−3?〉and ?2=〈4,−5−3?,1+4?〉 . B) Between the planes 2?−?+5?=0 and 2?−?+5?=5 . C) From the point (1,2,3) to the line ?=〈−?,4−?,1+4?〉 .
Developing an Equation from Average Costs The America Dog and Cat Hotel is a pet hotel...
Developing an Equation from Average Costs The America Dog and Cat Hotel is a pet hotel located in Las Vegas. Assume that in March, when dog-days (occupancy) were at an annual low of 500, the average cost per dog-day was $14. In July, when dog-days were at a capacity level of 4,000, the average cost per dog-day was $7. (a) Develop an equation for monthly operating costs. (Let X = dog-days per month) Total cost = Answer + Answer *...
Out of 82 people surveyed about what kind of pet they have ( dog, cat or...
Out of 82 people surveyed about what kind of pet they have ( dog, cat or parrot ), 47 people people had a dog, 40 people had a cat and 11 people had a parrot, these are totals and are not mutually exclusive.. The 47 people with a dog may have also had other animals.    There were 5 people that had all 3 pets. There were 15 people who had a dog and a cat, not mutually exclusive. There were...
A = (1 −7 5 0 0 10 8 2 2 4 10 3 −4 8...
A = (1 −7 5 0 0 10 8 2 2 4 10 3 −4 8 −9 6) (1) Count the number of rows that contain negative components. (2) Obtain the inverse of A and count the number of columns that contain even number of positive components. (3) Assign column names (a,b,c,d) to the columns of A. (4) Transform the matrix A into a vector object a by stacking rows. (5) Replace the diagonal components of A with (0,0,2,3). Hint:...
Given the following dataset x   1   1   2   3   4   5 y   0   2   4   5  ...
Given the following dataset x   1   1   2   3   4   5 y   0   2   4   5   5   3 We want to test the claim that there is a correlation between xand y. The level of cretaine phosphokinase (CPK) in blood samples measures the amount of muscle damage for athletes. At Jock State University, the level of CPK was determined for each of 25 football players and 15 soccer players before and after practice. The two groups of athletes are trained...
Given the following dataset x 1 1 2 3 4 5 y 0 2 4 5...
Given the following dataset x 1 1 2 3 4 5 y 0 2 4 5 5 3 We want to test the claim that there is a correlation between xand y. (a) What is the null hypothesis Ho and the alternative hypothesis H1? (b) Using α= 0.05, will you reject Ho? Justify your answer by using a p-value. (c) Base on your answer in part (b), is there evidence to support the claim? (d) Find r, the linear correlation...
A= 1 2 4 0 1 -2 -1 0 1 2 0 3 8 1 4...
A= 1 2 4 0 1 -2 -1 0 1 2 0 3 8 1 4 . Let W denote the row space for A. (a) Find an orthonormal basis for W and for W⊥. (b) Compute projW⊥(1 1 1 1 1 ).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT