Question

In: Computer Science

The assignment: C++ program or Java You need to use the following programming constructs/data structures on...

The assignment: C++ program or Java

You need to use the following programming constructs/data structures on this assignment.

1. A structure named student which contains:  

a. int ID; student id;

b. last name; // as either array of char or a string

c. double GPA;   

2. An input file containing the information of at least 10 students.

3. An array of struct: read the student information from the input file into the array.

4. A stack: you can use the standard library template for the stack or you can write your own code for the different stack operations. The stack stores elements of type student.

5. Output file containing a duplicate of all output sent to the console.

What to do:

1. Read all students rows from the input file into an array.

2. Store the information about seven different students in the stack: This is intended to initialize the stack.

3. Generate a random number between 1 and 20.

4. If the randomly generated number is even, read one row from the array and store it (push it) into the stack.

5. If the randomly generated number is odd then check If the number is divisible by 3, if so ask the user if they want to quit.

6. If the user selects to continue or the odd number is not divisible by 3 then pop one element from the stack and send it to the output file.,

7. If the stack is not empty and you quit: it will display the number of the students left in the stack, and display the information of each student left in the stack.

8. If you choose to quit, and the stack is empty when a pop is needed, or the array is empty when a push is required then do the following:

a. Display on the console and send to the output file: “The reason for quitting”, how many elements are left on the stack when quitting, and the content of each student record found in the stack when quitting.

Solutions

Expert Solution

//C++ CODE TO COPY//

#include<iostream>
#include<fstream>
#include<string>
#include<sstream>
#include<stack>
using namespace std;

//student struct to keep student data
struct student{
   int ID; //student id
   string last_name;
   double cgpa;
};

//function decleration
void read_std_file(student *std);
void store_std_into_stack(stack <student> ss, student std[]);
void wite_output_file(student std);

//function to read data from file into array
void read_std_file(student *std)
{
   ifstream file;
   file.open("student.txt");
   if (file.fail())
   {
       cout << "\nFile opening error!!";
       exit(1);
   }
   string line;
   int counter = 0;
   int index = 0;
   while (getline(file, line))
   {
       stringstream ss(line);
       string token;
       counter = 0;
       while (getline(ss, token, ','))
       {
           if (counter == 0)
               std[index].ID = stoi(token);
           else if (counter == 1)
               std[index].last_name = token;
           else if (counter == 2)
               std[index].cgpa = stod(token);
           counter++;
       }
       index++;
   }
   file.close();
}

//function to store data from array to stack for 7 students
void store_std_into_stack(stack <student> ss, student std[])
{
   int rand_num = 0;
   char choice;
   for (int i = 0; i < 7; i++)
   {
       rand_num = rand() % 20;
       if (rand_num%2==0)
           ss.push(std[i]);
       if (rand_num%2!=0)
       if (rand_num % 3 == 0)
       {
          
           cout << "\npress y to quit n for not: ";
           cin >> choice;
           if (choice == 'y')
           {
               if (!ss.empty())
               {
                   cout << "\nNumber of students on stack: " << ss.size()<<endl;
                   cout << "ID\t\t" << "last name\t\t" << "CGPA" << endl;
                   for (int i = 0; i <= ss.size(); i++)
                   {
                       student s = ss.top();
                       cout << s.ID << "\t\t" << s.last_name << "\t\t" << s.cgpa<<endl;
                       ss.pop();
                   }
               }
               cout << endl << endl;
               exit(0);
           }  
       }
       else
       {
           if (!ss.empty())
           {
                   wite_output_file(ss.top());
                   ss.pop();
           }
       }
   }
}


//writing data to output file
void wite_output_file(student std)
{
   ofstream file;
   file.open("output_student.txt");
   if (file.fail())
   {
       cout << "\nError Opening output file to write!!";
       exit(1);
   }
   file << std.ID << "," << std.last_name << "," << std.cgpa<<"\n";
   file.close();
}

//main driver function
int main()
{
   //array for 10 students
   student std[10];
   //stack to store student data from array
   stack <student> ss;

   //calling function to read student data from student.txt file
   //and store it in std array
   read_std_file(std);

   //calling function to store data from array to stack
   store_std_into_stack(ss, std);

   cout << endl << endl;
}

//OUTPUT//

Comment down for any queries!
Please give a thumbs up if you are satisfied and helped with the answer :)


Related Solutions

Your task is to modify the program from the Java Arrays programming assignment to use text...
Your task is to modify the program from the Java Arrays programming assignment to use text files for input and output. I suggest you save acopy of the original before modifying the software. Your modified program should: contain a for loop to read the five test score into the array from a text data file. You will need to create and save a data file for the program to use. It should have one test score on each line of...
Java. Part 1 of 4 - Amusement Park Programming Project Requirements: Use the Java selection constructs...
Java. Part 1 of 4 - Amusement Park Programming Project Requirements: Use the Java selection constructs (if and if else). Use the Java iteration constructs (while, do, for). Use Boolean variables and expressions to control iterations. Proper error handling. Class: Ticket – models admission tickets. Instance Fields: number : long – identify the unique ticket. category : String – store the category of the ticket. holder : String – store the name of the person who purchased the ticket. date...
C PROGRAMMING – Steganography In this assignment, you will write an C program that includes processing...
C PROGRAMMING – Steganography In this assignment, you will write an C program that includes processing input, using control structures, and bitwise operations. The input for your program will be a text file containing a large amount of English. Your program must extract the “secret message” from the input file. The message is hidden inside the file using the following scheme. The message is hidden in binary notation, as a sequence of 0’s and 1’s. Each block of 8-bits is...
Java program In this assignment you are required to create a text parser in Java/C++. Given...
Java program In this assignment you are required to create a text parser in Java/C++. Given a input text file you need to parse it and answer a set of frequency related questions. Technical Requirement of Solution: You are required to do this ab initio (bare-bones from scratch). This means, your solution cannot use any library methods in Java except the ones listed below (or equivalent library functions in C++). String.split() and other String operations can be used wherever required....
c++ program You are to use a Heap data structure for this assignment I currently work...
c++ program You are to use a Heap data structure for this assignment I currently work for an investment/insurance company and I’m looking for clients to call, ones with funds.  I need to have a schedule that shows the list of customers to call and the order to be called.  The list of customers names and phone numbers are in the file ‘NamesAndPhoneV2.txt’.  A second file contains a net worth value for each client.  The files are separated for security and protection reasons, but...
I need specific codes for this C program assignment. Thank you! C program question: Write a...
I need specific codes for this C program assignment. Thank you! C program question: Write a small C program connect.c that: 1. Initializes an array id of N elements with the value of the index of the array. 2. Reads from the keyboard or the command line a set of two integer numbers (p and q) until it encounters EOF or CTL - D 3. Given the two numbers, your program should connect them by going through the array and...
Program in Java using Inheritence The purpose of this assignment is to practice OOP programming covering...
Program in Java using Inheritence The purpose of this assignment is to practice OOP programming covering Inheritance. Core Level Requirements (up to 6 marks) The scenario for this assignment is to design an online shopping system for a local supermarket (e.g., Europa Foods Supermarket or Wang Long Oriental Supermarket). The assignment is mostly concentrated on the product registration system. Design and draw a UML diagram, and write the code for the following classes: The first product category is a fresh...
Programming assignment 4 : C++ Write a program to do the following: 1.Define a structure to...
Programming assignment 4 : C++ Write a program to do the following: 1.Define a structure to store a date, which includes day(int), month(int), and year(int). 2.Define a structure to store an address, which includes address(house number and street)(string), city(string), state(string), zip code (string). 3.Define a class to store the following information about a student. It should include private member variables: name(string), ID (int), date of birth (the first structure), address (the second structure), total credit earned (int), and GPA (double)....
This assignment uses data structures, selection, use, control structures, logic, computation, file I/O, etc. In C++...
This assignment uses data structures, selection, use, control structures, logic, computation, file I/O, etc. In C++ There should be 3 files for the project: theMain.cpp, dateClass.h, dateClass.cpp (or similar file names) For input/output. Read at least 10 dates in a data file set up this way: The first number is the number of data items in the file. Then after that, each row is one date in the format month day year. here is a sample data file of 6...
In this programming assignment, you will write a program that reads in the CSV file (passenger-data-short.csv),...
In this programming assignment, you will write a program that reads in the CSV file (passenger-data-short.csv), which contains passenger counts for February 2019 on 200 international flights. The data set (attached below) is a modified CSV file on all International flight departing from US Airports between January and June 2019 reported by the US Department of Transportation. You write a program that give some summary statistics on this data set. Create a header file named flights.h. In this file, you...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT