Question

ASSIGNMENT: Create a program to do the following: BONUS: A bonus of 20 points if you create the p...

ASSIGNMENT: Create a program to do the following:

BONUS: A bonus of 20 points if you create the program in such a way that there is no limit on the number of names to be handled.

1) Read in names to sort until the user types the “enter” key as the first character of a “C-type” string (the maximum number of names is 20, there is not a maximum length to a name), using a two dimensional array of characters.

2) After sorting and displaying the array of “C-type” strings, ask the user for a “Ctype” string to search for (again, no maximum length).

3) Do a binary search to find if the “C-type” string is in the array

4) If the “C-type” string is in the array, tell the user the index of the “C-type” string.

5) If the “C-type” string is not in the array, tell the user that it is missing.

6) Repeat items 3 thru 5 until the user enters the “enter” key as the first character of the “C-type” string, at which time the program will complete.

Please write the code for c programming

copy of the .c and .h files in the project folder as created by Visual Studio.

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

main.c

#include"function.h"

#define MAX_SIZE 20

int main()

{

char names[MAX_SIZE][256],name[256];

int i=0,j=0,index,len;

while(1)

{

printf("Enter name (press enter to stop) :");

fgets(name,sizeof(name),stdin);

if(name[0]=='\n')

break;

len=strlen(name);

name[len-1]='\0';

strcpy(names[i],name);

i++;

}

printf("\nBefore sorting the array is : \n");

print(names,i);

sortArray(names,i);

printf("\nBefore sorting the array is : \n");

print(names,i);

while(1)

{

printf("Enter name to search (press enter to stop) :");

fgets(name,sizeof(name),stdin);

if(name[0]=='\n')

break;

len=strlen(name);

name[len-1]='\0';

index=binarySearch(names, 0, i, name);

if(index>=0)

printf("%s is in the array at index %d \n",name,index+1);

else

printf("%s is not present in the array. \n",name);

}

return 0;

}

function.h

#include<stdio.h>

#include<string.h>

void sortArray(char name[][256],int n)

{

int i,j;

char temp[256];

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

}

}

}

}

void print(char names[][256],int n)

{

int j;

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

printf("%s\n",names[j]);

}

int binarySearch(char arr[][256], int l, int r, char name[256])

{

if (r >= l) {

int mid = l + (r - l) / 2;

  

// If the element is present at the middle

// itself

if (strcmp(arr[mid],name)==0)

return mid;

  

// If element is smaller than mid, then

// it can only be present in left subarray

if (strcmp(arr[mid],name)>0)

return binarySearch(arr, l, mid - 1, name);

  

// Else the element can only be present

// in right subarray

return binarySearch(arr, mid + 1, r, name);

}

  

// We reach here when element is not

// present in array

return -1;

}

output

Enter name (press enter to stop) :Jay Enter name (press enter to stop) Manish Enter name (press enter to stop) Viral Enter naIf you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
ASSIGNMENT: Create a program to do the following: BONUS: A bonus of 20 points if you create the p...
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
  • C++ Visual Studios - Program - Vector - Simple Create a program that utilizes VECTOR for...

    C++ Visual Studios - Program - Vector - Simple Create a program that utilizes VECTOR for the following topic. Topic: Carl's Cab Stand needs a program to keep track of their daily clients. Your program shall allow the user to enter 10 names. You will then retrieve the names, one by one, from the data structure (using the appropriate method of retrieval for each data structure) and present them on-screen so that Carl knows who to service next. Include a...

  • This program should be run on Visual Studio. Please use printf and scanf as input and output. Tha...

    This program should be run on Visual Studio. Please use printf and scanf as input and output. Thank you 6.12 Lab Exercise Ch.6b: C-string functions Create and debug this program in Visual Studio. Name your code Source.c and upload for testing by zyLabs You will write 2 functions which resemble functions in the cstring library. But they will be your own versions 1. int cstrcat(char dstDchar src) which concatenates the char array srcl to char array dstD, and returns the...

  • 2. Searching a String: Write a MIPS assembly language program to do the following: Read a...

    2. Searching a String: Write a MIPS assembly language program to do the following: Read a string and store it in memory. Limit the string length to 100 characters. Then, ask the user to enter a character. Search and count the number of occurrences of the character in the string. The search is not case sensitive. Lowercase and uppercase letters should be equal. Then ask the user to enter a string of two characters. Search and count the number of...

  • Stuck on this computer science assignment Write a program that demonstrates binary searching through an array...

    Stuck on this computer science assignment Write a program that demonstrates binary searching through an array of strings and finding specific values in an corresponding parallel double array. In all cases your output should exactly match the provided solution.o. Provided files: Assignment.cpp - starter assignment with function prototypes companies.txt - file for program to read. earnings.txt - file for program to read. Input1.txt Input2.txt - Test these inputs out manually, or use stream redirection Input3.txt - These inputs are based...

  • Objectives: Use strings and string library functions. Write a program that asks the user to enter...

    Objectives: Use strings and string library functions. Write a program that asks the user to enter a string and output the string in all uppercase letters. The program should then display the number of white space characters in the string. You program should run continuously until the user enters an empty string. The program must use the following two functions: A function called count_spaces that counts the number of white spaces inside a string. int count_space(char str[]); which tell you...

  • Problem #1 Create a program that performs the following functions: Uses character arrays to read a...

    Problem #1 Create a program that performs the following functions: Uses character arrays to read a user's name from standard input Tells the user how many characters are in her name Displays the user's name in uppercase Create a program that uses the strstr() function to search the string "When the going gets tough, the tough stay put! for the following occurrences (display each occurrence found to standard output): "Going" "tou" "ay put!" Build a program that uses an array...

  • For this assignment, you will create a program that tests a string to see if it...

    For this assignment, you will create a program that tests a string to see if it is a palindrome. A palindrome is a string such as “madam”, “radar”, “dad”, and “I”, that reads the same forwards and backwards. The empty string is regarded as a palindrome. Write a recursive function: bool isPalindrome(string str, int lower, int upper) that returns true if and only if the part of the string str in positions lower through upper (inclusive at both ends) is...

  • Please create a program answering the following question in C PROGRAMMING. CorrectFormation Create a program and...

    Please create a program answering the following question in C PROGRAMMING. CorrectFormation Create a program and locally declare in main fname and Iname and completeName. Ask the user for their first and last name. Put the values into fname and Iname. You will create a function that is called as followed oinNames( fname, Iname, completeName ); You will pass the first name array, the last name array, and the array that will contain the complete name. The function named joinNames...

  • *Using C++* You will create a program that uses a Critter class to move around a...

    *Using C++* You will create a program that uses a Critter class to move around a Grid, which is also a class. The Critter and the Grid classes will be in separate files. The Critter class will have a data member to count the number of moves made. It will also need data members to hold the current x and y coordinates. It will have a member function that randomly moves it one space in one of 4 directions. You...

  • in java please Create an array of characters. Ask the user to enter two characters. Tell...

    in java please Create an array of characters. Ask the user to enter two characters. Tell the user which character occurs more frequently For example, suppose the array contains (a, b, c, d, 8, 2, e, e, f, g and the user enterse and then the program will say that occurs more frequently thanL Bonus: Display a list of all characters in the array with their frequencies. Each value should appear only once For example for the array(a, b, c,...

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