[go: up one dir, main page]

0% found this document useful (0 votes)
11 views9 pages

DS Assignment 1

Uploaded by

mdlipta
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)
11 views9 pages

DS Assignment 1

Uploaded by

mdlipta
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
You are on page 1/ 9

NAME: WASAK AL LIPTA

ID : 41230301806
SECTION: 3J

SOLUTION 1
#include <stdio.h>
void shiftLeft(int source[], int size, int k) {
for (int i = 0; i < k; i++) {
int first = source[0];
for (int j = 0; j < size - 1; j++) {
source[j] = source[j + 1];
}
source[size - 1] = 0;
}
}

void printArray(int arr[], int size) {


for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}

int main() {
int source[] = {10, 20, 30, 40, 50, 60};
int k = 3;
int size = sizeof(source) / sizeof(source[0]);
shiftLeft(source, size, k);
printArray(source, size);

return 0;
}

Solution 2

#include <stdio.h>

void shiftRight(int source[], int n, int k) {


int shifted_array[n];
for (int i = 0; i < n; i++) {
if (i >= k) {
shifted_array[i] = source[i - k];
} else {
shifted_array[i] = 0;
}
}

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


source[i] = shifted_array[i];
}
}

int main() {
int source[] = {10, 20, 30, 40, 50, 60};
int n = sizeof(source) / sizeof(source[0]);
int k = 3;

shiftRight(source, n, k);

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


printf("%d ", source[i]);
}

return 0;
}

Solution 3
#include <stdio.h>

void rotateLeft(int source[], int n, int k) {


int rotated_array[n];
for (int i = 0; i < n; i++) {
rotated_array[i] = source[(i + k) % n];
}
for (int i = 0; i < n; i++) {
source[i] = rotated_array[i];
}
}

int main() {
int source[] = {10, 20, 30, 40, 50, 60};
int n = sizeof(source) / sizeof(source[0]);
int k = 3;

rotateLeft(source, n, k);

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


printf("%d ", source[i]);
}

return 0;
}

Solution 4

#include <stdio.h>

void rotateRight(int source[], int n, int k) {


int rotated_array[n];
for (int i = 0; i < n; i++) {
rotated_array[(i + k) % n] = source[i];
}
for (int i = 0; i < n; i++) {
source[i] = rotated_array[i];
}
}

int main() {
int source[] = {10, 20, 30, 40, 50, 60};
int n = sizeof(source) / sizeof(source[0]);
int k = 3;

rotateRight(source, n, k);

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


printf("%d ", source[i]);
}

return 0;
}

Solution 5

#include <stdio.h>

void removeElement(int source[], int size, int idx) {


if (idx >= 0 && idx < size) {
for (int i = idx; i < size - 1; i++) {
source[i] = source[i + 1];
}
source[size - 1] = 0;
}
}

int main() {
int source[] = {10, 20, 30, 40, 50, 0, 0};
int size = 5;
int idx = 2;
removeElement(source, size, idx);

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


printf("%d ", source[i]);
}

return 0;
}

Solution 6

#include <stdio.h>

void removeAll(int source[], int size, int element) {


int temp[size];
int j = 0;

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


if (source[i] != element) {
temp[j] = source[i];
j++;
}
}

for (int i = j; i < size; i++) {


temp[i] = 0;
}
for (int i = 0; i < size; i++) {
source[i] = temp[i];
}
}

int main() {
int source[] = {10, 2, 30, 2, 50, 2, 2, 0, 0};
int size = 7; // Only considering the first 7 elements as the valid size
int element = 2;

removeAll(source, size, element);

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


printf("%d ", source[i]);
}

return 0;
}

Solution 7

#include <stdio.h>

#define MAX_SIZE 100

int countRepetitions(int source[], int size) {


int repetition[MAX_SIZE] = {0};
int repetitionCount[MAX_SIZE] = {0};

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


repetition[source[i]]++;
}

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


if (repetition[i] > 0) {
repetitionCount[repetition[i]]++;
}
}
for (int i = 0; i < MAX_SIZE; i++) {
if (repetitionCount[i] > 1) {
return 1; // True
}
}

return 0; // False
}

int main() {
int source1[] = {4, 5, 6, 6, 4, 3, 6, 4};
int size1 = sizeof(source1) / sizeof(source1[0]);

int source2[] = {3, 4, 6, 3, 4, 7, 4, 6, 8, 6, 6};


int size2 = sizeof(source2) / sizeof(source2[0]);

if (countRepetitions(source1, size1)) {
printf("Output for source1: True\n");
} else {
printf("Output for source1: False\n");
}

if (countRepetitions(source2, size2)) {
printf("Output for source2: True\n");
} else {
printf("Output for source2: False\n");
}

return 0;
}

You might also like