Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

den ataba This exercise is designed to introduce the concept of gruceuresin C++,

ID: 3826994 • Letter: D

Question

den ataba This exercise is designed to introduce the concept of gruceuresin C++, to give further practice with strings and grrays to review the idea ofsorting and to introduce (in an elementary way the concept of apointer. The data file "prezdat'' contains a chronological listofU.S.Presidents, one name per record. Thus, the data filelooks like: WASHINGTON, GEORGE ADAMS JOHN JEFFERSON BUSH, GEORGE CLINTON, WILLIAM BUSH,GEORGE WALKER OBAMA BARACK TRUMP,DONALD Your task is to write a C++ program to input this file and to buildandoutput the following database: location chronur president's name next prez 2 ADAMS, JOHN 6 ADAMS JOHN QUINCY 21 ARTHUR,CHESTER 15 BUOHANAN AMES 41 BUSH,GEORGE BUSH GEORGE ALKER CARTER JIORY CLEVELAND, GROVER 24 CL (see next page 42 CLINTON, MILLIAM 30 COOLIDGE, CALVIN 34 EI 13 FILLMORE, MILLARD explanation!) 38 FORD,GERALD 13 14 20 GARFIELD, JAMES 18 GRANT ULYSSES 16 29 HARDING, WARREN 23 HARRISON, BENJANIN HARRISON MILLIAUHENRY 18 19 HAYES RUTHERFORD 7 JACKSON, ANDREW EFFERSON,THOMAS. DOHNSON,ANDRE 36 20HNSON LYNDON 25 35 KENNEDY,20HN 16 LINCOLN,ABRAHAM MADISON, JAMES MOKINLEY,WILLIAM 28 29 MONROE, JAMES 37 NIXON, RIOHARD 44 OBAMA BARACK 33 REAGAN,RONALD 35 ROOSEVELT 37 36 26 ROOSEVELT THEODORE 37 27 TAFT MILLIAMLHO ARD 38 12 TAYLOR,ZACHARY 33 TRUMAN HARRY 45 TRUMP,DONALD 10 TYLER JOHN VAN BUREN,MARTIN 42 43 1 WASHINGTON, GEORGE 28 WILSON, OOORO

Explanation / Answer

Here is the code for you:

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
struct president
{
    int chronum;
    string pname;
    int next;
}plist[60];
void printList(int count)
{
    for(int i = 0; i < count; i++)
    {
        cout << setw(7) << i;
        cout << setw(7) << plist[i].chronum;
        cout << setw(30) << plist[i].pname;
        cout << setw(7) << plist[i].next << endl;
    }  
}
void sortList(int count)
{
    for(int i = 0; i < count - 1; i++)
        for(int j = 0; j < count - i - 1; j++)
            if(plist[j].pname > plist[j+1].pname)
            {
               struct president temp = plist[j];
               plist[j] = plist[j+1];
               plist[j+1] = temp;
            }
}
int findIndexOf(int chronum, int count)
{
    for(int i = 0; i < count; i++)
        if(plist[i].chronum == chronum)
            return i;
    return -1;      
}
int main()
{
    ifstream fin;
    fin.open("prez.dat");
    int count = 0;
    while(!fin.eof())
    {
       fin >> plist[count].pname;
       plist[count].chronum = count+1;
       count++;
    }
    sortList(count);
    for(int i = 0; i < count; i++)
        plist[i].next = findIndexOf(plist[i].chronum+1, count);
    printList(count);
}