Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Operations on Jagged Arrays #1514

Merged
merged 2 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions 2D Arrays/JaggedArrays/Program.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#include <stdio.h>
#include <stdlib.h>

void initializeJaggedArray(int **jaggedArray, int rows, int *columnSizes) {
for (int i = 0; i < rows; i++) {
printf("Enter the number of columns for row %d: ", i);
scanf("%d", &columnSizes[i]);
jaggedArray[i] = (int *)malloc(columnSizes[i] * sizeof(int));
printf("Enter elements for row %d: ", i);
for (int j = 0; j < columnSizes[i]; j++) {
scanf("%d", &jaggedArray[i][j]);
}
}
}

void insertElement(int **jaggedArray, int row, int *columnSizes, int element, int position) {
columnSizes[row]++;
jaggedArray[row] = (int *)realloc(jaggedArray[row], columnSizes[row] * sizeof(int));
for (int i = columnSizes[row] - 1; i > position; i--) {
jaggedArray[row][i] = jaggedArray[row][i - 1];
}
jaggedArray[row][position] = element;
}

void deleteElement(int **jaggedArray, int row, int *columnSizes, int position) {
for (int i = position; i < columnSizes[row] - 1; i++) {
jaggedArray[row][i] = jaggedArray[row][i + 1];
}
columnSizes[row]--;
jaggedArray[row] = (int *)realloc(jaggedArray[row], columnSizes[row] * sizeof(int));
}

int searchElement(int **jaggedArray, int rows, int *columnSizes, int element) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columnSizes[i]; j++) {
if (jaggedArray[i][j] == element) {
printf("Element %d found at Row %d, Column %d\n", element, i, j);
return 1;
}
}
}
printf("Element %d not found.\n", element);
return 0;
}

void displayJaggedArray(int **jaggedArray, int rows, int *columnSizes) {
printf("Jagged Array:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columnSizes[i]; j++) {
printf("%d ", jaggedArray[i][j]);
}
printf("\n");
}
}

int main() {
int rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);

int **jaggedArray = (int **)malloc(rows * sizeof(int *));
int *columnSizes = (int *)malloc(rows * sizeof(int));

initializeJaggedArray(jaggedArray, rows, columnSizes);
displayJaggedArray(jaggedArray, rows, columnSizes);

int row, position, element, choice;
while (1) {
printf("\nChoose an operation:\n1. Insert\n2. Delete\n3. Search\n4. Display\n5. Exit\nEnter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter row, position, and element to insert: ");
scanf("%d %d %d", &row, &position, &element);
insertElement(jaggedArray, row, columnSizes, element, position);
displayJaggedArray(jaggedArray, rows, columnSizes);
break;
case 2:
printf("Enter row and position of element to delete: ");
scanf("%d %d", &row, &position);
deleteElement(jaggedArray, row, columnSizes, position);
displayJaggedArray(jaggedArray, rows, columnSizes);
break;
case 3:
printf("Enter element to search: ");
scanf("%d", &element);
searchElement(jaggedArray, rows, columnSizes, element);
break;
case 4:
displayJaggedArray(jaggedArray, rows, columnSizes);
break;
case 5:
printf("Exiting program.\n");
for (int i = 0; i < rows; i++) {
free(jaggedArray[i]);
}
free(jaggedArray);
free(columnSizes);
return 0;
default:
printf("Invalid choice. Please try again.\n");
}
}
}
56 changes: 56 additions & 0 deletions 2D Arrays/JaggedArrays/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Description

A jagged array (or an array of arrays) is a type of two-dimensional array where the rows can have different numbers of columns. This project covers basic operations on jagged arrays in the C programming language. We provide functions to initialize a jagged array, insert elements, delete elements, search for elements, and display the array in a readable format.

For instance, consider a jagged array where each row has a different number of elements:

Row 0: 1, 2
Row 1: 3, 4, 5
Row 2: 6

With jagged arrays, each row can be customized to fit the data's varying length requirements.

# Operations

1.Initialize Jagged Array: Dynamically allocate memory for each row, allowing each row to have a specified number of columns.
2.Insert Elements: Add elements at specific positions within each row.
3.Delete Elements: Remove elements from particular rows, with subsequent elements shifting to fill the gap.
4.Search Elements: Locate specific elements within the jagged array, returning their position if found.
5.Display Jagged Array: Print the contents of the jagged array in a structured, readable format.

# Example

Consider a jagged array with three rows:

1.Row 0 has 2 columns.
2.Row 1 has 3 columns.
3.Row 2 has 1 column.

Jagged Array:

1 2
3 4 5
6

If we add an element 7 to Row 2, it will look like this:

1 2
3 4 5
6 7

If we delete the element 4 from Row 1, the array will look like this:

1 2
3 5
6 7

# Program Explanation

In this program, we:

1.Prompt the user to input the number of rows and specify the number of columns for each row.
2.Initialize the jagged array based on user input.
3.Insert and delete elements as specified by the user.
4.Search for specific elements within the array.
5.Display the jagged array in a structured format.
6.This functionality allows flexible operations on arrays where rows have variable lengths, enhancing efficiency in memory usage and data handling.
Loading