We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- C
- Introduction
- Functions in C
- Discussions
Functions in C
Functions in C
Sort by
recency
|
865 Discussions
|
Please Login in order to post a comment
sample question with answer
For C
I wrote the code from scratch just to get more practice
include
include
using namespace std; struct Student { string name; float marks[3]; }; int main() { Student s[50]; int n = 0, choice; do { cout << "\n1.Add Student 2.Display All 3.Search 4.Topper 5.Exit\n"; cout << "Enter choice: "; cin >> choice; if(choice == 1) { cout << "Enter student name: "; cin >> s[n].name; cout << "Enter marks in 3 subjects: "; for(int i=0;i<3;i++) cin >> s[n].marks[i]; n++; } else if(choice == 2) { for(int i=0;i "; for(int j=0;j<3;j++) cout << s[i].marks[j] << " "; cout << endl; } } else if(choice == 3) { string key; cout << "Enter name to search: "; cin >> key; for(int i=0;i best) { best = avg; topper = s[i].name; } } cout << "Topper: " << topper << " with avg " << best << endl; } } while(choice != 5); }
Linked list
include
using namespace std;
struct Node { int data; Node* next; };
Node* head = nullptr;
void insertEnd(int val) { Node* newNode = new Node{val, nullptr}; if (!head) head = newNode; else { Node* temp = head; while (temp->next) temp = temp->next; temp->next = newNode; } }
void deleteNode(int val) { if (!head) return; if (head->data == val) { Node* temp = head; head = head->next; delete temp; return; } Node* temp = head; while (temp->next && temp->next->data != val) temp = temp->next; if (temp->next) { Node* del = temp->next; temp->next = del->next; delete del; } }
void display() { Node* temp = head; while (temp) { cout << temp->data << " -> "; temp = temp->next; } cout << "NULL\n"; }
int main() { insertEnd(10); insertEnd(20); insertEnd(30); display(); deleteNode(20); display(); } Stack
include
using namespace std;
define MAX 5
int stack[MAX], top = -1;
void push(int val) { if (top == MAX - 1) cout << "Stack Overflow\n"; else stack[++top] = val; }
void pop() { if (top == -1) cout << "Stack Underflow\n"; else cout << "Popped: " << stack[top--] << endl; }
void display() { if (top == -1) cout << "Stack Empty\n"; else { for (int i = top; i >= 0; i--) cout << stack[i] << " "; cout << endl; } }
int main() { push(10); push(20); push(30); display(); pop(); display(); } Queue
include
using namespace std;
define MAX 5
int queueArr[MAX]; int front = -1, rear = -1;
void enqueue(int val) { if (rear == MAX - 1) cout << "Queue Overflow\n"; else { if (front == -1) front = 0; queueArr[++rear] = val; } }
void dequeue() { if (front == -1 || front > rear) cout << "Queue Underflow\n"; else cout << "Dequeued: " << queueArr[front++] << endl; }
void display() { if (front == -1 || front > rear) cout << "Queue Empty\n"; else { for (int i = front; i <= rear; i++) cout << queueArr[i] << " "; cout << endl; } }
int main() { enqueue(5); enqueue(10); enqueue(15); display(); dequeue(); display(); }
Binary Tree
include
using namespace std;
struct Node { int data; Node* left; Node* right; Node(int val) : data(val), left(nullptr), right(nullptr) {} };
void inorder(Node* root) { if (!root) return; inorder(root->left); cout << root->data << " "; inorder(root->right); }
int main() { Node* root = new Node(10); root->left = new Node(5); root->right = new Node(15); root->left->left = new Node(3); root->left->right = new Node(7);
} Graph (Adjacency List Representation)
include
include
using namespace std;
class Graph { int V; vector> adj;
public: Graph(int v) { V = v; adj.resize(V); }
};
int main() { Graph g(5); g.addEdge(0, 1); g.addEdge(0, 4); g.addEdge(1, 2); g.addEdge(1, 3); g.addEdge(1, 4); g.display(); }
include
include
using namespace std;
class Graph { int V; vector> adj;
public: Graph(int v) { V = v; adj.resize(V); }
};
int main() { Graph g(5); g.addEdge(0, 1); g.addEdge(0, 4); g.addEdge(1, 2); g.addEdge(1, 3); g.addEdge(1, 4); g.display(); }