Question

I need help solving this C++ problem please. A data file will be provided that contains...

I need help solving this C++ problem please.

A data file will be provided that contains the first names and student IDs for a class. Write a program that stores this information using parallel arrays in C++. Assume that the data file will be named "students.txt" and will contain information with no more than 100 students. You can also assume that none of the first names will contain any spaces.

The program will need to contain four functions:

  • Read data from the file into the parallel arrays. The function should also update the number of students actually read in from the file.
  • Print the contents of the arrays. Column headings should be displayed. Use the setw command to have the names aligned to the left. Use the setfill command to display leading zeroes for IDs so that 8 total digits will be displayed.
  • Sort the arrays alphabetically by students' first names. Whenever two names are swapped, the corresponding IDs should also be swapped.
  • Search for a student based on a given student ID. Return the position of the matching item. If an ID does not exist, -1 should be returned.
  • With a main driver program that will test all of these functions.

students.txt datafile:

Austin 00394872

Brett 00705316

Alex 00860156

Marie 00303380

Alexander 00719414

Josh 00600454

Ash 00443106

Jeff 00440146

Paul 00858506

Sylver 00078130

Nate 00524363

Ryan 00346789

Jeff 00819283

Luke 00586747

Harris 00975479

Justin 00661436

Landon 00745369

Garett 00340421

Dylan 00775204

Amber 00991895

William 00700297

Surya 00346965

Shiv 00511067

Mary 00507019

Trennie 00046832

Hangyang 00099012

Isaac 00198113

Kirstie 00143051

ShengLiang 00953869

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <fstream>
#include <cstring>
#include <iomanip>

using namespace std;

int main() {
    int ids[100];
    string names[100];
    int count = 0;

        ifstream infile ("students.txt");

        if(!infile) {
                cout << "Error! unable to find input file." << endl;
                return 1;
        }
        string line;

        while (infile >> names[count] >> ids[count]) {
        //cout << names[count] << " " << ids[count] << endl;
        count++;
        }

    // sort array
    int startScan, minIndex;
    string minValue;

    for(startScan = 0; startScan < (count - 1); startScan++) {
        minIndex = startScan;
        minValue = names[startScan];
        int minId = ids[startScan];

        for(int index = (startScan + 1); index < count; index++) {
            if(names[index] < minValue) {
                minValue = names[index];
                minIndex = index;
            }
        }

        names[minIndex] = names[startScan];
        names[startScan] = minValue;
        ids[startScan] = ids[minIndex];
        ids[minIndex] = minId;
    }

    cout << endl;

    cout << setw(20) << left << "Name" << setw(20) << left << "Id" << endl;

        for(int i=0; i<count; i++) {
        cout << left << setfill(' ') << setw(20) << names[i];
        cout << right << setfill('0') << setw(8) << ids[i] << endl;
        }

    int search, found = 0;
    cout << "
Enter id to be searched: ";
    cin >> search;
        for(int i=0; i<count; i++) {
        if(ids[i] == search) {
            found = 1;
            cout << "Record found: ";
            cout << left << setfill(' ') << setw(20) << names[i];
            cout << right << setfill('0') << setw(8) << ids[i] << endl;
        }
        }

    if(!found) {
        cout << "No record found!" << endl;
    }

}

Please upvote, as i have given the exact answer as asked in question. Still in case of any concerns in code, let me know in comments. Thanks!

Add a comment
Know the answer?
Add Answer to:
I need help solving this C++ problem please. A data file will be provided that contains...
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
  • Write a program, which reads a list of student records from a file in batch mode....

    Write a program, which reads a list of student records from a file in batch mode. Each student record comprises a roll number and student name, and the student records are separated by commas. An example data in the file is given below. · SP18-BCS-050 (Ali Hussan Butt), SP19-BCS-154 (Huma Khalid), FA19-BSE-111 (Muhammad Asim Ali), SP20-BSE-090 (Muhammad Wajid), SP17-BCS-014 (Adil Jameel) The program should store students roll numbers and names separately in 2 parallel arrays named names and rolls, i.e....

  • CIS 22A C++ Project Exam Statistics Here is what your program will do: first it welcomes...

    CIS 22A C++ Project Exam Statistics Here is what your program will do: first it welcomes the user and displays the purpose of the program. It then prompts the user to enter the name of an input file (such as scores.txt). Assume the file contains the scores of the final exams; each score is preceded by a 5 characters student id. Create the input file: copy and paste the following data into a new text file named scores.txt DH232 89...

  • Kindly solve this using C PROGRAMMING ONLY. 4. Write a program marks.c which consists of a...

    Kindly solve this using C PROGRAMMING ONLY. 4. Write a program marks.c which consists of a main function and three other functions called. readmarks , changemarks (, and writemarks() The program begins by calling the function readmarks () to read the input file marksin. txt which consists of a series of lines containing a student ID number (an integer) and a numeric mark (a float). Once the ID numbers and marks have been read into arrays (by readmarks () the...

  • You must assume that the data file and your four function subprograms are located inside the working directory (forder). Please write the main program and each of the four function subprograms. 1. Th...

    You must assume that the data file and your four function subprograms are located inside the working directory (forder). Please write the main program and each of the four function subprograms. 1. The data in the following table are to be read from the file "grades.txt"and processed The grades are in percentages. The format of the data is shown below First Name ID Number Last Name Grades 001AA Tam 78.50 oe 86.45 001AB Gabriel Stuart Thus, there are 4 columns...

  • c++ The program will be recieved from the user as input name of an input file...

    c++ The program will be recieved from the user as input name of an input file and and output.file. then read in a list of name, id# and score from input file (inputFile.txt) and initilized the array of struct. find the student with the higher score and output the students info in the output file obtain the sum of all score and output the resurl in output file. prompt the user for a name to search for, when it find...

  • C++ Language A continuation of your grading software will be made you do not need your...

    C++ Language A continuation of your grading software will be made you do not need your other code for this assignment). In this one you will intake a students first name and last name from a file called "students.txt". Next, there is a file called grades.txt that contains the grades in a course. There will be three grades for each student in "grades.txt" (see example file bellow). In "student.txt" there are two students first and last names. In "grades.txt" the...

  • You are asked to define a user-defined data type for students, that will contain the following...

    You are asked to define a user-defined data type for students, that will contain the following information about a student: ID number, first name, last name, major, GPA. Your structure should be cal Student. Based on the defined structure, user name on the command line, show how you will use a loop to search for the given name within the array of 5 students. use an array to record information about 5 students. Then, given a Write a C program...

  • Using the program segment and sample txt file below, write a program that contains a linked...

    Using the program segment and sample txt file below, write a program that contains a linked list of students. The program should allow the user to: 1. initialize list of students 2. add additional student to front of list 3. add additional student to rear of list 4. delete student 5. sort students alphabetically 6. sort students by idNum 7. show number of students in list 8. print students 9. quit program XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX The program should be divided into the...

  • //Done in C please! //Any help appreciated! Write two programs to write and read from file...

    //Done in C please! //Any help appreciated! Write two programs to write and read from file the age and first and last names of people. The programs should work as follows: 1. The first program reads strings containing first and last names and saves them in a text file (this should be done with the fprintf function). The program should take the name of the file as a command line argument. The loop ends when the user enters 0, the...

  • Write a C++ program that asks user number of students in a class and their names....

    Write a C++ program that asks user number of students in a class and their names. Number of students are limited to 100 maximum. Then, it will ask for 3 test scores of each student. The program will calculate the average of test scores for each student and display with their names. Then, it will sort the averages in descending order and display the sorted list with students’ names and ranking. Follow the Steps Below Save the project as A4_StudentRanking_yourname....

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