Arrays
Question 1: Traversal
Scenario: You have an array representing the daily earnings of a store over a week. You
need to find out the total earnings
Array:
int daily_earnings[] = {150, 180, 160, 200, 170, 250, 220};
int n = 7;
Task: Write a function to traverse the array and calculate the total earnings for the first three
days.
Expected Output:
Total earnings for the first three days: 490
Question 2: Insertion
Scenario: You have an array representing the scores of a game. A new score needs to be
added to the end of the array.
Array:
int scores[] = {85, 90, 88};
int n = 3;
int new_score = 92;
Task: Write a function to insert the new score into the array.
Expected Output:
Updated scores list: [85, 90, 88, 92]
Question 3: Deletion
Scenario: You have an array representing a series of task durations in minutes. One of the
tasks is cancelled, and you need to remove its duration from the array.
Array:
int task_durations[] = {30, 45, 50, 40};
int n = 4;
int task_to_remove = 50;
Task: Write a function to delete the specified task duration from the array.
Expected Output:
Updated task durations list: [30, 45, 40]
Question 4: Insertion at Specific Position
Scenario: You have an array of temperatures recorded hourly over a day. A new reading
needs to be inserted at a specific hour.
Array:
int temperatures[] = {22, 24, 26, 25};
int n = 4;
int new_temperature = 23;
int position = 2;
Task: Write a function to insert the new temperature at the specified position in the array.
Expected Output:
Updated temperatures list: [22, 24, 23, 26, 25]
Question 5: Traversal for Specific Condition
Scenario: You have an array of exam scores. You need to find the indices of scores that are
above 75.
Array:
int exam_scores[] = {70, 80, 65, 90, 75};
int n = 5;
Task: Write a function to traverse the array and print the indices where the scores are above
75.
Expected Output:
Indices of scores above 75: [1, 3]
Linked Lists
You are managing a linked list representing the scores of players in a game. Perform various
operations on this linked list based on the given scenarios.
Base Code for Linked List Creation
#include <stdio.h>
#include <stdlib.h>
// Define the structure for a Node
struct Node {
int data;
struct Node* next;
};
// Function to create a new Node
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// Function to print the linked list
void printList(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main() {
// Initial linked list creation (empty)
struct Node* head = NULL;
// Add your function calls here
return 0;
}
Question 1: Create and Traverse the Linked List
Task: Create a linked list with the scores: 50, 60, 70, 80, 90. Write a function to traverse and
print the linked list.
Example:
int main() {
struct Node* head = createNode(50);
head->next = createNode(60);
head->next->next = createNode(70);
head->next->next->next = createNode(80);
head->next->next->next->next = createNode(90);
printList(head);
return 0;
}
Expected Output:
50 -> 60 -> 70 -> 80 -> 90 -> NULL
Question 2: Insert a New Score at the End
Task: Insert a new score, 100, at the end of the linked list.
void addScore(struct Node** head, int newScore) {
struct Node* newNode = createNode(newScore);
struct Node* last = *head;
if (*head == NULL) {
*head = newNode;
return;
}
while (last->next != NULL) {
last = last->next;
}
last->next = newNode;
}
int main() {
// Initial linked list creation
struct Node* head = createNode(50);
head->next = createNode(60);
head->next->next = createNode(70);
head->next->next->next = createNode(80);
head->next->next->next->next = createNode(90);
addScore(&head, 100);
printList(head);
return 0;
}
Expected Output:
50 -> 60 -> 70 -> 80 -> 90 -> 100 -> NULL
Question 3: Delete a Specific Score
Task: Remove the score 70 from the linked list.
Example:
void removeScore(struct Node** head, int scoreToRemove) {
struct Node* temp = *head, *prev = NULL;
if (temp != NULL && temp->data == scoreToRemove) {
*head = temp->next;
free(temp);
return;
}
while (temp != NULL && temp->data != scoreToRemove) {
prev = temp;
temp = temp->next;
}
if (temp == NULL) return;
prev->next = temp->next;
free(temp);
}
int main() {
// Initial linked list creation
struct Node* head = createNode(50);
head->next = createNode(60);
head->next->next = createNode(70);
head->next->next->next = createNode(80);
head->next->next->next->next = createNode(90);
head->next->next->next->next->next = createNode(100);
removeScore(&head, 70);
printList(head);
return 0;
}
Expected Output:
50 -> 60 -> 80 -> 90 -> 100 -> NULL
Question 4: Insert a Score at a Specific Position
Task: Insert a new score, 75, at the third position (0-based index) in the linked list.
Example:
void insertScoreAtPosition(struct Node** head, int newScore, int position) {
struct Node* newNode = createNode(newScore);
struct Node* current = *head;
if (position == 0) {
newNode->next = *head;
*head = newNode;
return;
}
for (int i = 0; current != NULL && i < position - 1; i++) {
current = current->next;
}
if (current == NULL) return;
newNode->next = current->next;
current->next = newNode;
}
int main() {
// Initial linked list creation
struct Node* head = createNode(50);
head->next = createNode(60);
head->next->next = createNode(80);
head->next->next->next = createNode(90);
head->next->next->next->next = createNode(100);
insertScoreAtPosition(&head, 75, 2);
printList(head);
return 0;
}
Expected Output:
50 -> 60 -> 75 -> 80 -> 90 -> 100 -> NULL
Question 5: Traverse and Sum Scores Above a Threshold
Task: Write a function to traverse the linked list and return the sum of scores that are above
70.
Example:
int sumScoresAboveThreshold(struct Node* head, int threshold) {
int sum = 0;
struct Node* current = head;
while (current != NULL) {
if (current->data > threshold) {
sum += current->data;
}
current = current->next;
}
return sum;
}
int main() {
// Initial linked list creation
struct Node* head = createNode(50);
head->next = createNode(60);
head->next->next = createNode(75);
head->next->next->next = createNode(80);
head->next->next->next->next = createNode(90);
head->next->next->next->next->next = createNode(100);
printf("Sum of scores above 70: %d\n", sumScoresAboveThreshold(head, 70));
return 0;
}
Expected Output:
Sum of scores above 70: 345