Week 3
Week 3
Week 3
AUTOMATED FIX
1).
ROHITH
2).
RAJ
LAB EX
1).
SHARON
#include <stdio.h>
#include <stdbool.h>
char items[MAX_SIZE];
int top = -1;
void initialize() {
top = -1;
}
bool isFull() {
return top == MAX_SIZE - 1;
}
bool isEmpty() {
return top == -1;
}
void push(char value) {
if (isFull()) {
printf("Stack is full. Cannot push %c.\n", value);
return;
}
items[++top] = value;
printf("Pushed: %c\n", value);
}
char pop() {
if (isEmpty()) {
printf("Stack is empty. Nothing to pop.\n");
return '\0'; // return a null character to indicate error
}
char popped_value = items[top--];
printf("Popped: %c\n", popped_value);
return popped_value;
}
void display() {
if (isEmpty()) {
printf("Stack is empty.\n");
return;
}
printf("Stack elements: ");
for (int i = top; i >= 0; i--) {
printf("%c ", items[i]);
}
printf("\n");
}
int main() {
initialize();
int choice;
char value;
while (true) {
scanf("%d", &choice);
switch (choice) {
case 1:
scanf(" %c", &value);
push(value);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
return 0;
default:
printf("Invalid choice\n");
}
}
return 0;
}
2).
ALICE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
studentCount++;
}
}
while (1) {
scanf(" %c", &command); // Read command
switch (command) {
case 'a':
scanf(" %[^\n]", name); // Read name (including spaces)
scanf("%d", &age); // Read age
scanf("%f", &gpa); // Read GPA
case 'u':
undo();
break;
case 'p':
printStudents();
break;
case 'q':
exit(0); // Quit the program
default:
printf("Invalid Choice\n");
break;
}
}
return 0;
}
3).
MILTON
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main() {
Stack* stack = createStack();
int choice;
char data;
while (1) {
scanf("%d", &choice);
switch (choice) {
case 1:
scanf(" %c", &data);
if (isalpha(data)) {
push(stack, data);
} else {
printf("Invalid input. Only alphabetic characters are allowed.\
n");
}
break;
case 2:
pop(stack);
break;
case 3:
display(stack);
break;
case 4:
printf("Exiting program\n");
return 0;
default:
printf("Invalid choice\n");
break;
}
}
return 0;
}
4).
SOFTWARE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main() {
char infix[100];
printf("");
scanf("%s", infix);
printf("");
infixToPostfix(infix);
return 0;
}
CHALLENGE YOURSELF
1).
KRISH
#include <stdio.h>
#include <stdlib.h>
// Stack structure
struct Stack {
int items[MAX_SIZE];
int top;
};
while (1) {
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &item);
push(stack, item);
break;
case 2:
pop(stack);
break;
case 3:
display(stack);
break;
case 4:
printf("Exiting the warehouse\n");
freeStack(stack);
return 0;
default:
printf("Invalid choice\n");
break;
}
}
return 0;
}
2).
UNIVERSITY
#include <stdio.h>
#include <stdlib.h>
// Define the node structure for the linked list
struct Node {
int roll_number;
struct Node* next;
};
while (1) {
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &roll_number);
push(stack, roll_number);
break;
case 2:
pop(stack);
break;
case 3:
display(stack);
break;
case 4:
printf("Exiting program\n");
//freeStack(stack);
return 0;
default:
printf("Invalid choice\n");
break;
}
}
return 0;
}
PRACTICE AT HOME
1).
RAJ
#include <stdio.h>
#include <stdlib.h>
// Stack structure
typedef struct Stack {
Node* top;
} Stack;
printf("\n");
}
int choice;
char element;
while (1) {
scanf("%d", &choice); // Read user choice
switch (choice) {
case 1:
scanf(" %c", &element); // Read character input for push
push(&stack, element);
break;
case 2:
if (isEmpty(&stack)) {
printf("Stack is empty. Cannot pop.\n");
} else {
char poppedElement = pop(&stack);
printf("Popped element: %c\n", poppedElement);
}
break;
case 3:
display(&stack);
break;
case 4:
printf("Exiting program\n");
exit(0);
default:
printf("Invalid choice\n");
break;
}
}
return 0;
}
2).
raja
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
switch (ch) {
case '+':
result = operand1 + operand2;
break;
case '-':
result = operand1 - operand2;
break;
case '*':
result = operand1 * operand2;
break;
case '/':
result = operand1 / operand2;
break;
}
push(&stack, result); // Push the result back onto the stack
}
}
int main() {
char expr[MAX];
// Input expression
scanf("%s", expr);
// Evaluate
int result = evaluatePostfix(expr);
// Output result
printf("%d\n", result);
return 0;
}
3).
NAREN
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Stack Structure
typedef struct Stack {
char items[MAX];
int top;
} Stack;
int main() {
char exp[MAX];
// Input expression
scanf("%s", exp);
return 0;
}
4).
RAM
#include <stdio.h>
int main() {
int v1;
return 0;
}