Question

LANGUAGE = C i. Write a program that takes int numbers from user until user gives...

LANGUAGE = C

i. Write a program that takes int numbers from user until user gives a sentinel value (loop terminating condition). Sort the numbers in ascending order using Insertion sort method. Sorting part should be done in different function named insertionSort(). Your code should count the number of swaps required to sort the numbers. Print the sorted list and total number of swaps that was required to sort those numbers. (4 marks)

ii. In this part take another number from user, insert that number in the right place of the sorted array got from part 1. (Insert that number in such a manner that preserve the sorted order).

Write a program that takes a string from user. Then takes a character from user and search the character in the string. The searching part has to be performed inside search() function. If the character is present in the string, then your code should print "Found" and if the character is not found in the string then the code should print "Not found." (3 marks)

Next, if the character is not found in the string, then your code should place that character in the white-space in the string (space between two words) and print the string in reverse order. (3 marks)

Example: Input string: "This is Test!"

Input character to search: 'n'

Your code should print:

Not Found!

!tseTnsinsihT

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>

int insertionsort(int arr[], int s)
{
   int current,i,j,count=0;
   for(i=1;i<s;i++)
   {
       current=arr[i];
       for(j=i-1;j>=0;j--)
       {
           if(current<arr[j])
           {
               arr[j+1]=arr[j];
               count++;
           }
           else
               break;
       }
       arr[j+1]=current;
   }
   return count;
}
int main()
{
int sentinel=-999,num;
   int t,n,i,res;
   int arr[100000];
   printf("Enter a number or -999 to stop: ");
       scanf("%d",&num);
   i=0;
   while(num!=-999)
   {
   arr[i++]=num;
   printf("Enter a number or -999 to stop: ");
       scanf("%d",&num);
      
      
   }
   n=i;
   res=insertionsort(arr,n);
   printf("Array after sorted is ");
   for(i=0;i<n;i++)
   printf("%d ",arr[i]);
   printf("\nNumber of swaps are: %d\n",res);
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
LANGUAGE = C i. Write a program that takes int numbers from user until user gives...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This program is in C++ Write a program that validates charge account numbers read in from...

    This program is in C++ Write a program that validates charge account numbers read in from the file Charges.txt following this assignment (A23 Data). Use a selection sort function on the array to sort the numbers then use a binary search to validate the account numbers. Print out the sorted list of account numbers so you can see the valid ones. Prompt the user to enter an account number and the validate that number. If the account number entered is...

  • Write a program that takes a list of integer numbers from a user and creates a...

    Write a program that takes a list of integer numbers from a user and creates a new sorted in ascending order list. As output you need to print original list and sorted one. use list and while python

  • Task 1: Write a Python program that takes as input from the user the name of a file containing po...

    python code: Task 1: Write a Python program that takes as input from the user the name of a file containing postcode/location information. Each line in the file consists of the postcode followed by a tab followed by a comma-separated list of locations that have that postcode. For example, the file Small.txt contains: 3015 Newport,South Kingsville,Spotswood 3016 Williamstown 3018 Altona,Seaholme 3019 3021 Albanvale,Kealba,Kings Park,St Albans Braybrook, Robinson Your program should create a list of postcode/location pairs with each pair stored...

  • Write a C program that prompts the user for an input string with all lowercase letters....

    Write a C program that prompts the user for an input string with all lowercase letters. Your program should then sort the characters in the string and print the sorted string. Assume that the string contains no spaces and has at most 30 lowercase characters. Your program should loop continually until the user enters “q” to quit.

  • **C++ only, use standard library, no vectors Write a program to generate a list of 5000...

    **C++ only, use standard library, no vectors Write a program to generate a list of 5000 random numbers between 1 and 10,000 stored in an array. Sorts: Print out the middle 50 numbers of the original list to show the results. Now sort the original numbers using bubble sort. Print out the number of swaps made. Now sort the original numbers using selection sort. Print out the number of swaps made. Now sort the original numbers using insertion sort. Print...

  • (C programing, Not C++) Write a program in C that asks the user to input 10...

    (C programing, Not C++) Write a program in C that asks the user to input 10 numbers. Store these numbers in an array. Then ask the user to input a single number. Your program should execute a linear search on the array, trying to find that number. If the number exists in the array, have the program print out which position/element the number was found at. If the target number is not in the array, have the program print out...

  • Write a program that will accept two numbers from the user. The program will then print...

    Write a program that will accept two numbers from the user. The program will then print all the numbers from the lowest number to the highest number. Make sure to include both numbers in the count. Also, it should not matter what order you input the two numbers. c++  

  • in c++ Program 1 Write a program that sorts a vector of names alphabetically using the...

    in c++ Program 1 Write a program that sorts a vector of names alphabetically using the selection sort and then searches the vector for a specific name using binary search. To do that, you need to write three functions I. void selSort (vector string &v: sorts the vector using selection sort 2. void display (const vector <string & v): displays the vector contents . int binSearch (const vector <ing& v, string key): searches the vector for a key returns the...

  • C programing Write a program to sort numbers in either descending or ascending order. The program...

    C programing Write a program to sort numbers in either descending or ascending order. The program should ask the user to enter positive integer numbers one at a time(hiting the enter key after each one) The last number entered by the user should be -1, to indicate no further numbers will be entered. Store the numbers in an array, and then ask the user how to sort the numbers (either descending or ascending). Call a function void sortnumbers ( int...

  • C++ Vectors and Binary Search Trees • Write a program that takes from the user n...

    C++ Vectors and Binary Search Trees • Write a program that takes from the user n integers and stores them a vector of int. Then, create a function insert After that takes first Value and second Value. This function searches for each occurrence of first Value in the vector and insert the second Value after it in the same vector. The first and second values are taken from the user. • Create another function that creates a Binary Search Tree...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT