Design and write a computer program as a Win32 console application in C to store
ID: 3682111 • Letter: D
Question
Design and write a computer program as a Win32 console application in C to store a list of names (the last name first) and ages in parallel arrays, and then later to sort them into alphabetical order, keeping the ages with the correct names. See Programming Projects 6, in Chapter 8 of the textbook for a typical Sample output.
- The name of your source program shall be p5.c and that of your executable program shall be p5.exe.
- Your program shall receive information from the user as a command-line argument. For example, a typical command line to start your program (from a command prompt) might be L:>p5 peoplefile.txt
- Your program shall NOT seek information from the user such as by a call to scanf().
- Your program shall NOT print information to the screen except as required herein.
- If your program is started without any command-line arguments,
it shall print the following line to the screen and terminate execution: EE 233 Spring 2016 P5, <your Blazer id>, <your name>
(Fill in your real name in place of <your name> and be sure to follow the output with a newline.)
- If your program is started with at least one command-line argument, it shall NOT print the line given above but shall proceed with normal execution.
- The first command-line argument (after the program name itself) will be the name of the
text file for your program to read. No line in this file will contain more than 80 characters.
- If the file supplied as a command-line argument cannot be opened, your program shall
write an error message to the screen, and terminate execution. In a similar manner, your program shall properly handle all error conditions that might arise.
- Your program shall read the input text file and scan the names and ages for storage in
separate arrays. The names array(s) shall store strings, and ages shall store integers. A word (string) is defined as a sequence of characters that is bounded by white space (tab, newline, or space characters). A string will be no longer than 40 characters.
- Your program shall be able to scan at least 100 names and the corresponding ages. If
more than 100 names and ages are in the input file, your program shall print an error message saying that more than 100 lines of data have been encountered, and terminate execution, without processing that file.
- After your program has read and processed the entire file, it shall print, with an
appropriate header, the alphabetized list of names and ages (see the textbook for a sample output), only. Be sure to follow the output with a newline, and do not print the original list from the input file.
Explanation / Answer
#include "Student.h"
int main()
{
Student xStudent;
xStudent.inplpl();
// system("Pause");
xStudent.inNameyuyu();
// system("Pause");
xStudent.outOriginyuyuy();
// system("Pause");
xStudent.sortNayuyuuyu();
// system("Pause");
xStudent.outSoruyuyu();
xStudent.outOriginyuyuy();
system("Pause");
return 0;
}
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::left;
#include <iomanip>
using std::setw;
#include <stdio.h>
class Student
{
public:
Student();
void inplpl();
void inNameyuyu();
void outOriginyuyuy();
void sortNayuyuuyu();
void outSoruyuyu();
private:
char sortName[ 50 ][ 22 ];
int sortAge[ 50 ];
char nameHold[ 50 ][ 22 ];
int people;
char name[ 50 ][ 22 ];
int age[ 50 ];
};
#include "Student.h"
Student::Student()
{
for ( int count = 0; count <= 50; count++ )
{
age[ count ] = 0;
for ( int ccount = 0; ccount <= 22; ccount++ )
{
name[ count ][ ccount ] = 0;
}
}
}
void Student::inplpl()
{
cout << "Enter number of people (0 . . 50) > ";
cin >> people;
}
void Student::inNameyuyu()
{
fflush(stdin);
for ( int count = 1; count <= people; count++ )
{
cout << "Enter name " << count << " (lastname, firstname): ";
cin.getline( name[ count ], 22 );
fflush(stdin);
cout << "Enter age " << count << ": ";
cin >> age[ count ];
fflush(stdin);
cout << " ";
}
}
void Student::outOriginyuyuy()
{
cout << "Original list" << endl;
cout << "------------------------------" << endl;
for ( int count = 1; count <= people; count++ )
{
for ( int ccount = 0; ccount <= 21; ccount++ )
{
cout << name[ count ][ ccount ];
}
cout << setw( 8 ) << left << age[ count ] << endl;
}
cout << " ";
}
void Student::sortNayuyuuyu()
{
char sortName[ people ][ 22 ];
char nameHold[ people ][ 22 ];
int sortAge[ people ];
for ( int count = 0; count <= people; count++ )
{
sortAge [ count ] = age[ count ];
for ( int ccount = 0; ccount <= 22; ccount++ )
{
sortName[ count ][ ccount ] = name[ count ][ ccount ];
nameHold[ count ][ ccount ] = 0;
}
}
int smallestOrLargest;
int index;
for ( int i = 0; i <= people; i++ )
{
smallestOrLargest = i;
for ( index = i + 1; index <=people; index++ )
{
if ( sortName[ smallestOrLargest ][ 1 ] < sortName[ index ][ 1 ] )
{
smallestOrLargest = index;
}
}
for ( int c = 0; c <= 22; c++ )
{
nameHold[ smallestOrLargest ][ c ] = sortName[ smallestOrLargest ][ c ];
sortName[ smallestOrLargest ][ c ] = sortName[ index ][ c ];
sortName[ index ][ c ] = nameHold[ smallestOrLargest ][ c ];
}
int ageHold = sortAge[ smallestOrLargest ];
sortAge[ smallestOrLargest ] = sortAge[ index ];
sortAge[ index ] = ageHold;
}
}
void Student::outSoruyuyu()
{
cout << "Alphabetized list" << endl;
cout << "------------------------------" << endl;
for ( int count = 1; count <= people; count++ )
{
for ( int ccount = 0; ccount <= 21; ccount++ )
{
cout << sortName[ count ][ ccount ];
}
cout << setw( 8 ) << left << sortAge[ count ] << endl;
}
cout << " ";
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.