0% found this document useful (0 votes)
26 views23 pages

Debugging Ds File

The document provides an overview of debugging in C++ with data structures, detailing common errors and effective debugging techniques. It includes examples of programs with errors related to stack, queue, and hash table implementations, along with corrected versions. Additionally, it discusses logical errors in a product prices analysis program and presents a corrected version of that program.

Uploaded by

sathvikkummari6
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views23 pages

Debugging Ds File

The document provides an overview of debugging in C++ with data structures, detailing common errors and effective debugging techniques. It includes examples of programs with errors related to stack, queue, and hash table implementations, along with corrected versions. Additionally, it discusses logical errors in a product prices analysis program and presents a corrected version of that program.

Uploaded by

sathvikkummari6
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

index

[Link] TITLE PAGE

1 Introduction to debugging 2

2 PROGRAM-4 4

3 PROGRAM-7 8

4 PROGRAM-11 12

5 PROGRAM-20 18

1
WHAT IS DEBUGGING?
Debugging in C++ with data structures is the systematic process of
identifying, analyzing, and correcting errors in programs that use
structures such as arrays, stacks, queues, linked lists, trees, and graphs,
and it is especially important because these structures involve pointers,
memory allocation, index manipulation, and boundary conditions that
can easily lead to bugs; common errors include syntax errors caught by
the compiler, logical errors where the program runs but produces
incorrect results, and runtime errors such as segmentation faults, stack
overflow or underflow, and invalid memory access; typical debugging
issues in data structures include array index out-of-bounds, incorrect
loop conditions, improper pointer handling in linked lists, failure to
check empty or full conditions in stacks and queues, and memory leaks
due to improper use of dynamic memory; effective debugging
techniques include using cout statements to track variable values and
program flow, manually tracing code step by step to understand how
data changes, carefully checking boundary and edge cases, initializing
variables properly, paying attention to compiler warnings, and using IDE
debuggers with breakpoints and watch windows to observe execution
in real time, all of which help ensure that C++ programs using data
structures are correct, efficient, and reliable.

2
Questions :-
4. Stack Implementation Using Array
7. Queue Implementation Using Array
11. hash table with linear probing
20. . Product Prices Analysis

3
Program with errors:-
4. Stack Implementation Using Array

#include <iostream>

using namespace std;

#define MAX 5

int main()

int stack[MAX];

int top = 1;

int choice, value;

do

cout << "\[Link]\[Link]\[Link]\[Link]\n";

cout << "Enter choice: ";

cin >> choice;

switch(choice)

case 1:

if (top == MAX)

cout << "Stack Overflow\n";

else

cout << "Enter value: ";

cin >> value;

top++;

stack[top] == value;

break;

case 2:
4
if (top = -1)

cout << "Stack Underflow\n";

} else

cout << "Popped: " << stack[top];

top--;

break;

case 3:

cout << "Stack Elements: ";

for (int i = 0; i <= top; i++);

cout << stack[i] << " ";

break;

} while (choice != 5);

Errors:-

Logical Errors :-
• int top = 1; → int top = -1;
• if (top == MAX) → if (top == MAX - 1)
• stack[top] == value; → stack[top] = value;
• if (top = -1) → if (top == -1)
• for (int i = 0; i <= top; i++); → for (int i = 0; i <= top; i++) (remove semicolon)
• cout << stack[i] << " "; (outside loop) → move inside the loop
• while (choice != 5); → while (choice != 4);

Program without errors:-


#include <iostream>

using namespace std;

#define MAX 5

5
int main()

int stack[MAX];

int top = -1;

int choice, value;

do

cout << "\[Link]\[Link]\[Link]\[Link]\n";

cout << "Enter choice: ";

cin >> choice;

switch(choice)

case 1:

if (top == MAX)

cout << "Stack Overflow\n";

else

cout << "Enter value: ";

cin >> value;

top++;

stack[top] = value;

break;

case 2:

if (top == -1)

cout << "Stack Underflow\n";

} else

cout << "Popped: " << stack[top];

top--;

6
}

break;

case 3:

cout << "Stack Elements: ";

for (int i = 0; i <= top; i++)

cout << stack[i] << " ";

break;

case 4:

cout <<"exiting the loop";

break;

default :

cout <<"you chosen option does not exsts";

break;

} while (choice != 4);

return 0;

Output:-

7
Program with errors:-
7. . Queue Implementation Using Array

#include <iostream>

using namespace std;

#define SIZE 5

int main()

int q[SIZE];

int front = 0, rear = 0

int choice, value;

do

cout << "\[Link] [Link] [Link] [Link]\n";

cin >> choice;

switch (choice)

case 1:

if (rear == SIZE)

cout << "Queue Full\n";

else

cout << "Enter value: ";

cin >> value;

q[++rear] = value;

break;

case 2:

if (front = rear)

cout << "Queue Empty\n";


8
}

else

cout << "Deleted: " << q[front];

front++;

break;

case 3:

cout << "Queue Elements: ";

for (int i = front; i <= rear; i++);

cout << q[i] << " ";

break;

while (choice != 7);

Errors :-

Logical errors :-
• int front = 0, rear = 0 → int front = 0, rear = 0;

• if (rear == SIZE) → if (rear == SIZE - 1)

• q[++rear] = value; → q[rear] = value; rear++;

• if (front = rear) → if (front == rear)

• for (int i = front; i <= rear; i++); → for (int i = front; i < rear; i++)

choice != 7 → choice != 4

program without errors:-


#include <iostream>

using namespace std;

#define SIZE 5

9
int main() {

int q[SIZE];

int front = 0, rear = -1;

int choice, value;

do {

cout << "\[Link]\[Link]\[Link]\[Link]\n";

cout << "Enter choice: ";

cin >> choice;

switch (choice) {

case 1:

if (rear == SIZE - 1) {

cout << "Queue Full\n";

} else {

cout << "Enter value: ";

cin >> value;

rear++;

q[rear] = value;

break;

case 2:

if (front > rear) {

cout << "Queue Empty\n";

} else {

cout << "Deleted: " << q[front];

front++;

break;

case 3:

if (front > rear) {

cout << "Queue Empty\n";

} else {

cout << "Queue Elements: ";

for (int i = front; i <= rear; i++) {

10
cout << q[i] << " ";

break;

case 4:

cout << "Exiting...\n";

break;

default:

cout << "Invalid choice\n";

} while (choice != 4);

return 0;

Output:-

11
Program with errors:-
11. hash table with linear probing

#include <iostream>

using namespace std;

class HashTable

private:

int size;

int *table;

public:

HashTable(int s)

size = s;

table = new int[size];

for (int i = 0; i <= size; i++)

table[i] = -1;

int hashFunction(int key)

return key % (size - 1);

void insert(int key)

int index = hashFunction(key);

int i = 0;

while (table[index] != -1 && i < size)

index = index + 1;

i++;

if (i >= size)

cout << "Hash Table Full\n";


12
else

table[index] = 0;

cout << "Inserted: " << key << endl;

int search(int key)

int index = hashFunction(key);

int i = 0;

while (table[index] != key && i <= size)

index = (index + 1);

i++;

if (table[index] == key)

return index;

return -1;

void display()

cout << "Hash Table:\n";

for (int i = 0; i < size - 1; i++)

cout << i << ": " << table[i] << endl;

};

int main()

HashTable ht(10);

[Link](25);

[Link](35);

[Link](15);

int pos = [Link](35);

13
if (pos == -1)

cout << "Key not found\n";

else

cout << "Key found at index: " << pos << endl;

[Link]();

return 0;

Logical errors :-
• for (int i = 0; i <= size; i++) → for (int i = 0; i < size; i++)

• return key % (size - 1); → return key % size;

• index = index + 1; → index = (index + 1) % size;

• table[index] = 0; → table[index] = key;

• while (table[index] != key && i <= size) → while (table[index] != key && i < size)

• index = (index + 1); → index = (index + 1) % size;

. for (int i = 0; i < size - 1; i++) → for (int i = 0; i < size; i++)

Program without errors:-


#include <iostream>

using namespace std;

class Node {

public:

int data;

Node *next;

Node(int val) {

data = val;

next = NULL;

};

class LinkedList {

private:

Node *head;

public:

LinkedList() {

14
head = NULL;

void insertAtEnd(int val) {

Node *newNode = new Node(val);

if (head == NULL) {

head = newNode;

return;

Node *temp = head;

while (temp->next != NULL) {

temp = temp->next;

temp->next = newNode;

void deleteNode(int val) {

if (head == NULL) {

cout << "List empty\n";

return;

if (head->data == val) {

Node *del = head;

head = head->next;

delete del;

return;

Node *temp = head;

while (temp->next != NULL && temp->next->data != val) {

temp = temp->next;

if (temp->next == NULL) {

cout << "Value not found\n";

return;

15
Node *del = temp->next;

temp->next = del->next;

delete del;

void display() {

Node *temp = head;

cout << "List: ";

while (temp != NULL) {

cout << temp->data << " ";

temp = temp->next;

cout << endl;

};

int main() {

LinkedList list;

[Link](10);

[Link](20);

[Link](30);

cout << "Linked List before deletion:\n";

[Link]();

[Link](20);

cout << "Linked List after deletion:\n";

[Link]();

return 0;

Output:-

16
17
Program with errors:-
20. Product Prices Analysis

#include <iostream>

using namespace std;

int main()

int n;

cout << "Enter number of products: ";

cin >> n;

float prices[50];

cout << "Enter product prices:" << endl;

for(int i = 1; i <= n; i++)

cin >> price[i];

float maxPrice = prices[10];

float minPrice = prices[n];

float sum;

int aboveAvg = 0, belowAvg = 0;

for(int i = 0; i <= n; i++) {

if(prices[i] > maxprice)

maxPrice = prices[0];

if(prices[i] < minPrice)

minPrice = prices[1];

sum = sum + price[i]; }

float avg = sum / 0;

cout << "Average price = " << avrg << endl;

for(int i = 0; i <= n; i++)

if(prices[i] > avg)

aboveAvg == aboveAvg + 1;

else

belowAvg++;;
18
}

for(int i = 0; i < n; i++)

prices[i] = prices[i] - prices[i] * 10;

for(int i = 0; i <= n; i++)

for(int j = 0; j <= n; j++)

if(prices[j] < prices[j-1])

float temp = price[j]; prices[j] = prices[j+1];

prices[j+1] == temp; }

cout << "\n---- PRICE REPORT ----\n";

cout << "Maximum Price = " << minPrice << endl;

cout << "Minimum Price = " << maxPrice << endl;

cout << "Average Price = " << avg << endl;

cout << "Products above average = " << above << endl;

cout << "Products below average = " << belowAvg << endl;

cout << "Sorted Prices: ";

for(int i = 0; i <= n; i++)

cout << prices[i] << " ";

return 0

Errors:-

19
Logical Errors :-
o Loop uses i = 1 to n, but arrays should be filled from index 0 to n-1.

o maxPrice is set to a fixed index (prices[10]) instead of the first entered value.

o minPrice is set to prices[n], which is outside valid data range.

o sum is used before assigning an initial value (should start from 0).

o Using i <= n accesses one extra element beyond the entered prices

o When a larger value is found, maxPrice is wrongly assigned prices[0] instead of the current element.

o When a smaller value is found, minPrice is wrongly assigned prices[1] instead of the current element.

o Average is calculated using sum / 0, which is logically invalid.

aboveAvg == aboveAvg + 1 does not increase the count.

o Prices are reduced by price * 10 instead of price * 0.10 (10%).

Sorting loops use <= n, causing invalid index access

o Sorting compares prices[j] with prices[j-1] but swaps with prices[j+1]

o Uses comparison (==) instead of assignment while swapping values

o Prints minPrice as maximum and maxPrice as minimum

o Uses i <= n instead of i < n.

Program without errors:-


#include <iostream>

using namespace std;

int main()

int n;

cout << "Enter number of products: ";

cin >> n;

float prices[50]; // maximum 50 products

cout << "Enter product prices:" << endl;

// Read prices (correct indexing)

for (int i = 0; i < n; i++)

cin >> prices[i];

20
float maxPrice = prices[0];

float minPrice = prices[0];

float sum = 0;

int aboveAvg = 0, belowAvg = 0;

for (int i = 0; i < n; i++)

if (prices[i] > maxPrice)

maxPrice = prices[i];

if (prices[i] < minPrice)

minPrice = prices[i];

sum += prices[i];

float avg = sum / n;

cout << "Average price = " << avg << endl;

for (int i = 0; i < n; i++)

if (prices[i] > avg)

aboveAvg++;

else

belowAvg++;

for (int i = 0; i < n; i++)

prices[i] = prices[i] - prices[i] * 0.10;

for (int i = 0; i < n - 1; i++)

for (int j = 0; j < n - i - 1; j++)

if (prices[j] > prices[j + 1])

float temp = prices[j];

prices[j] = prices[j + 1];

21
prices[j + 1] = temp;

cout << "\n---- PRICE REPORT ----\n";

cout << "Maximum Price = " << maxPrice << endl;

cout << "Minimum Price = " << minPrice << endl;

cout << "Average Price = " << avg << endl;

cout << "Products above average = " << aboveAvg << endl;

cout << "Products below average = " << belowAvg << endl;

cout << "Sorted Prices (after discount): ";

for (int i = 0; i < n; i++)

cout << prices[i] << " ";

return 0;

Output:-

22
23

You might also like