Question

Write a C++ program to read N students records implementing stack using pointers. Each record has a name field and ID field. Your program should have at least the following functions: main(), Read_record.(..), Push.., pop (...)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

In the below code, i used the funtion push,pop and display(equivalent to Read_record).

the code is quite simple, i used comments in most of the portion, if you still found it difficult to understand, do comment the comment section, i'll reply ASAP.

#include<iostream>
#include<string.h>

using namespace std;

#define MAX 5 // maximum number of student record can be 5
// you can also make this field as user input

int TOP;
struct Student{
int Id;
char Name[100];
};
Student STACK[MAX];


//stack initialization
void initStack(){
TOP=-1;
}
//check it is empty or not
int isEmpty(){
if(TOP==-1)
return 1;
else
return 0;
}

//check stack is full or not
int isFull(){
if(TOP==MAX-1)
return 1;
else
return 0;
}

void push(int Id,char d[]){
if(isFull()){
cout<<"STACK is FULL."<<endl;
return;
}
++TOP;
STACK[TOP].Id=Id;
strcpy(STACK[TOP].Name,d);
cout<<Id<<" has been inserted."<<endl;
}

void display(){
int i;
if(isEmpty()){
cout<<"STACK is EMPTY.No student record found"<<endl;
return;
}
for(i=TOP;i>=0;i--){
cout<<STACK[i].Id<<","<<STACK[i].Name<<endl;
}
cout<<endl;
}

//pop - to remove student record
void pop(){
int temp;
if(isEmpty()){
cout<<"STACK is EMPTY."<<endl;
return;
}

temp=STACK[TOP].Id;
TOP--;
cout<<temp<<" has been deleted."<<endl;
}

int main(){
int Id;
char Name[100];

initStack();

char ch;

do{
int a;
cout<<"Chosse \n1.push\n"<<"2.pop\n"<<"3.display\n";
cout<<"Please enter your choice: ";
cin>>a;
switch(a)
{
case 1:
cout<<"Enter a ID of student: ";
cin>>Id;
cout<<"Enter Name of the student:";
cin>>Name;
push(Id,Name);
break;

case 2:
pop();
break;

case 3:
display();
break;

default :
cout<<"An Invalid Choice!!!\n";


}
cout<<"Do you want to continue ?y for Yes, n for No ";
cin>>ch;   
}while(ch=='Y'||ch=='y');
return 0;
}

Add a comment
Know the answer?
Add Answer to:
Write a C++ program to read N students records implementing stack using pointers. Each record has...
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
  • Need help. write a C program stack-ptr.c that implements a stack using a link list. Below...

    Need help. write a C program stack-ptr.c that implements a stack using a link list. Below is a skeleton code to start with.Jjust edit to make thread friendly. examplpe: push(5, &top); push(10, &top); push(15, &top); int value = pop(&top); value = pop(&top); value = pop(&top); this program currently has a race condition. use Pthread mutex locks to fix the race conditions. test you now thread safe stack by creating 200 concurrent threads in main() that push and pop values. -use...

  • Write a modularized, menu-driven program to read a file with unknown number of records.

    ==============C++ or java================Write a modularized, menu-driven program to read a file with unknown number of records.Create a class Records to store the following data: first and last name, GPA , an Id number, and an emailInput file has unknown number of records; one record per line in the following order: first and last names, GPA , an Id number, and emailAll fields in the input file are separated by a tab (‘\t’) or a blank space (up to you)No error...

  • Write a program that uses a stack to reverse its inputs. Your stack must be generic...

    Write a program that uses a stack to reverse its inputs. Your stack must be generic and you must demonstrate that it accepts both String and Integer types. Your stack must implement the following methods: push, pop, isEmpty (returns true if the stack is empty and false otherwise), and size (returns an integer value for the number of items in the stack). You may use either an ArrayList or a LinkedList to implement your stack. Also, your pop method must...

  • Write a C++ program to store and update students' academic records in a binary search tree....

    Write a C++ program to store and update students' academic records in a binary search tree. Each record (node) in the binary search tree should contain the following information fields:               1) Student name - the key field (string);               2) Credits attempted (integer);               3) Credits earned (integer);               4) Grade point average - GPA (real).                             All student information is to be read from a text file. Each record in the file represents the grade information on...

  • C or C++ I need to create a code with implementing stack using linked list(and should...

    C or C++ I need to create a code with implementing stack using linked list(and should not use static array) and for input, each line should be this following order form: name, id, and email and for output, each line should be this order: id, name, and email here is examples of text files example1.txt Geo, 10, [email protected] Yoa, 13, [email protected] Yon, 19, [email protected] Cpo, 48, [email protected] Apx, 55, [email protected] example2.txt Joh, 50, [email protected] Jea, 20, [email protected] Dav, 194, [email protected]...

  • // Header code for stack // requesting to create source code using C++ #ifndef Stack_h #define...

    // Header code for stack // requesting to create source code using C++ #ifndef Stack_h #define Stack_h #include <stdio.h> #include <string> #include <iostream> using namespace std; class Stack { public: Stack(); ~Stack(); bool empty(); string top(); void push(const string &val); void pop(); void display(ostream &out); private: class Node { public: string word; Node *next; }; Node *tos; }; #endif Header file provided above. PLS create source code for functions declared in the header. If changes are need no make in...

  • Write a C program to compute average grades for a course. The course records are in...

    Write a C program to compute average grades for a course. The course records are in a single file and are organized according to the following format: Each line contains a student’s first name, then one space, then the student’s last name, then one space, then some number of quiz scores that, if they exist, are separated by one space. Each student will have zero to ten scores, and each score is an integer not greater than 100. Your program...

  • C++ Write a program that reads students’ names followed by their test scores from the given...

    C++ Write a program that reads students’ names followed by their test scores from the given input file. The program should output to a file, output.txt, each student’s name followed by the test scores and the relevant grade. Student data should be stored in a struct variable of type StudentType, which has four components: studentFName and studentLName of type string, testScore of type int and grade of type char. Suppose that the class has 20 students. Use an array of...

  • C++ Create an array-based implementation of a stack. Each element of the stack should store a...

    C++ Create an array-based implementation of a stack. Each element of the stack should store a string. The stack class should include 3 private member variables (maximum stack size, top of the stack index, and a pointer to the array that holds the stack elements). Public member methods should include a constructor (with an argument of stack maximum size that is used to create a dynamic array), a destructor (that deletes the dynamic array), a push method (argument is a...

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