Question

Write a main function that declares the names, marks, number of elements as well as the...

Write a main function that declares the names, marks, number of elements as well as the value to be searched for and the index of the returned function calls.

  • Read and display the contents of names and marks.
  • Ask the user for a name and using the linear search return the index to the user. If -1 is returned then the name is not in the file. Otherwise write out the name and mark for that student.
  • Sort the arrays and write them out.
  • Ask the user for a name to search for. This time use the binarySearch to return -1 or an index. Display the student’s name and mark if found.
  • Test your program for found and not found for each type of search.

void getNames(string names[], int marks[], int& numElts);

int linearSearch(const string names[], int numElts,string who);

int binarySearch(const string names[], int numElts,string who);

void selectionSort(string names[], int marks[],int numElts);

void displayData(const string names[], const int marks[], int numElts);

const int NUM_Names = 20;

int main()

{

      string names[NUM_NAMES];

int marks[NUM_NAMES];

      int numElts;

int index;

      string searchWho;

// Function calls here

      return 0;

}

File for names and marks:

Collins,Bill 80

Smith,Bart 75

Allen,Jim 82

Griffin,Jim 55

Stamey,Marty 90

Rose,Geri 78

Taylor,Terri 56

Johnson,Jill 77

Allison,Jeff 45

Looney,Joe 89

Wolfe,Bill 63

James,Jean 72

Weaver,Jim 77

Pore,Bob 91

Rutherford,Greg 42

Javens,Renee 74

Harrison,Rose 58

Setzer,Cathy 93

Pike,Gordon 48

Holland,Beth 79

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

If you have any doubts, please give me comment...

#include <iostream>

#include <fstream>

#include <string>

using namespace std;

void getNames(string names[], int marks[], int &numElts);

int linearSearch(const string names[], int numElts, string who);

int binarySearch(const string names[], int numElts, string who);

void selectionSort(string names[], int marks[], int numElts);

void displayData(const string names[], const int marks[], int numElts);

const int NUM_NAMES = 20;

int main()

{

    string names[NUM_NAMES];

    int marks[NUM_NAMES];

    int numElts;

    int index;

    string searchWho;

    getNames(names, marks, numElts);

    cout<<"The names and marks in the file are: "<<endl;

    displayData(names, marks, numElts);

    cout<<"\n\nEnter search Name by using linear search: ";

    cin>>searchWho;

    index = linearSearch(names, numElts, searchWho);

    if(index!=-1)

        cout<<names[index]<<"\t"<<marks[index]<<endl;

    else

        cout<<"Name is not in the file"<<endl;

    

    cout<<"\nAfter sorted: "<<endl;

    selectionSort(names, marks, numElts);

    displayData(names, marks, numElts);

    cout<<"\nEnter search Name by using binary search: ";

    cin>>searchWho;

    index = binarySearch(names, numElts, searchWho);

    if(index!=-1)

        cout<<names[index]<<"\t"<<marks[index]<<endl;

    else

        cout<<"Name is not in the file"<<endl;

    return 0;

}

void getNames(string names[], int marks[], int &numElts){

    numElts = 0;

    ifstream inFile;

    inFile.open("input.txt");

    if(!inFile.fail()){

        while(!inFile.eof()){

            inFile>>names[numElts]>>marks[numElts];

            numElts++;

        }

    }

}

int linearSearch(const string names[], int numElts, string who){

    for(int i=0; i<numElts; i++){

        if(names[i]==who)

            return i;

    }

    return -1;

}

int binarySearch(const string names[], int numElts, string who){

    int l = 0, r = numElts-1;

    while (l <= r) {

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

        if (names[m] == who)

            return m;

        else if (names[m] < who)

            l = m + 1;

        else

            r = m - 1;

    }

    return -1;

}

void selectionSort(string names[], int marks[], int numElts){

    for(int i=0; i<numElts; i++){

        for(int j=i+1; j<numElts; j++){

            if(names[i]>names[j]){

                string temp = names[i];

                names[i] = names[j];

                names[j] = temp;

                int t = marks[i];

                marks[i] = marks[j];

                marks[j] = t;

            }

        }

    }

}

void displayData(const string names[], const int marks[], int numElts){

    for(int i=0; i<numElts; i++){

        cout<<names[i]<<"\t"<<marks[i]<<endl;

    }

}

Add a comment
Know the answer?
Add Answer to:
Write a main function that declares the names, marks, number of elements as well as the...
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 lab is to give you more experience with C++ Searching and Sorting Arrays Given a...

    This lab is to give you more experience with C++ Searching and Sorting Arrays Given a file with data for names and marks you will read them into two arrays You will then display the data, do a linear search and report if found, sort the data, do a binary search. Be sure to test for found and not found in your main program. Read Data Write a function that reads in data from a file using the prototype below....

  • Write and test a function in C++ that uses the binary search algorithm to search an...

    Write and test a function in C++ that uses the binary search algorithm to search an array of sorted strings – use a do..while loop to allow user to perform multiple searches w/o terminating the program – see sample output below. Use this name array: string names[SIZE] = { "Collins, Bill", "Smith, Bart", "Allen, Jim",         "Griffin, Jim", "Stamey, Marty", "Rose, Geri", "Taylor, Terri",         "Johnson, Jill", "Allison, Jeff", "Looney, Joe", "Wolfe, Bill",         "James, Jean", "Weaver, Jim", "Pore, Bob",...

  • SortAndSearch.cpp – Objectives: Binary Search and Selection sort You are given a list of 20 names...

    SortAndSearch.cpp – Objectives: Binary Search and Selection sort You are given a list of 20 names as follows: {"Collins, Bill", "Smith, Bart", "Michalski, Joe", "Griffin, Jim",                         "Sanchez, Manny", "Rubin, Sarah", "Taylor, Tyrone", "Johnson, Jill",                         "Allison, Jeff", "Moreno, Juan", "Wolfe, Bill", "Whitman, Jean",                         "Moretti, Bella", "Wu, Hong", "Patel, Renee", "Harrison, Rose",                   "Smith, Cathy", "Conroy, Pat", "Kelly, Sean", "Holland, Beth"}; Write a program to sort and display the names in alphabet order (use selection sort). The program prompts...

  • Must be written in C++ Display results and comments Write a program that sorts a vector...

    Must be written in C++ Display results and comments 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: 1. void selSort (vector <string> & v): sorts the vector using selection sort 2. void display (const vector <string> & v): displays the vector contents 3. int binSearch (const vector <string> & v, string key): searches...

  • 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...

  • Write a program that has an array of at most 50 strings that hold people’s names...

    Write a program that has an array of at most 50 strings that hold people’s names and phone numbers. You can assume each string’s length is no more than 40. You may make up your own strings, or use the following: "Becky Warren, 555-1223" "Joe Looney, 555-0097" "Geri Palmer, 555-8787" "Lynn Presnell, 555-1212" "Holly Gaddis, 555-8878" "Sam Wiggins, 555-0998" "Bob Kain, 555-8712" "Tim Haynes, 555-7676" "Warren Gaddis, 555-9037" "Jean James, 555-4939" "Ron Palmer, 555-2783" The program should ask the user...

  • C++ with Pseudocode in the beginning This assignment will require you to write a program that...

    C++ with Pseudocode in the beginning This assignment will require you to write a program that will create an array of 10 string objects. The array will be initialized with the strings which contain the person’s name and phone number in one string. The following is an example of test data: “Renee Javens, 678-1223”, “Joe Looney, 586-0097”, “Geri Palmer, 223-8787”, “Lynn Presnell, 887-1212”, “Bill Wolfe, 223-8878”, “Sam Wiggins, 486-0998”, “Bob Kain, 586-8712”, “Tim Haynes, 586-7676”, “John Johnson, 223-9037”, “Jean James,...

  • 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...

  • Write the functions needed to complete the following program as described in the comments. Use the...

    Write the functions needed to complete the following program as described in the comments. Use the input file course.txt and change the mark of student number 54812 to 80. /* File: course.cpp A student's mark in a certain course is stored as a structure (struct student as defined below) consisting of first and last name, student id and mark in the course.  The functions read() and write() are defined for the structure student.   Information about a course is stored as a...

  • You are to write three functions for this lab: mean, remove, display. Download the main.cpp file...

    You are to write three functions for this lab: mean, remove, display. Download the main.cpp file provided to get started. Read the comments in the main function to determine exactly what the main function should do. //include any standard libraries needed // - Passes in an array along with the size of the array. // - Returns the mean of all values stored in the array. double mean(const double array[], int arraySize); // - Passes in an array, the size...

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