0% found this document useful (0 votes)
13 views15 pages

Arrays Comprog 103 Module 3 Codes

The document contains multiple C programming exercises focused on arrays, including functionalities such as reversing an array, finding unique elements, merging arrays, splitting into positive and negative arrays, summing elements at the same index, finding minimum and maximum values, and searching for elements. Each exercise is accompanied by code that demonstrates the required functionality. The exercises are structured to enhance understanding of array manipulation in C.
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)
13 views15 pages

Arrays Comprog 103 Module 3 Codes

The document contains multiple C programming exercises focused on arrays, including functionalities such as reversing an array, finding unique elements, merging arrays, splitting into positive and negative arrays, summing elements at the same index, finding minimum and maximum values, and searching for elements. Each exercise is accompanied by code that demonstrates the required functionality. The exercises are structured to enhance understanding of array manipulation in C.
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

Arrays comprog 103 module 3 codes (feb 11, 2025 finished at 8:34pm,

started ata kang 7pm)

#include <stdio.h> //module 3 arrays module excercise no. 1 (2/11/25)


//Write a program that reads 15 numbers
from an input file (input.txt) and prints them out in the
//reverse order of input. Note: You need to
write a function reverse() that reverses the contents of
//the array. The first line of the file indicates
the number of inputs to be read.

#define MAX_SIZE 20

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


int start = 0, end = size - 1, temp;
while (start < end) {
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}

int main() {
int size, numbers[MAX_SIZE];

printf("Enter the number of inputs: ");


scanf("%d", &size);
if (size > MAX_SIZE || size <= 0) {
printf("Invalid number of inputs!\n");
return 1;
}

printf("Enter %d numbers:\n", size);


for (int i = 0; i < size; i++) {
scanf("%d", &numbers[i]);
}

reverse(numbers, size);

printf("\nIn reverse order:\n");


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

return 0;
}

#include <stdio.h> //module 3 arrays module excercise no. 2 (2/11/25)


#define MAX_SIZE 15

int is_unique(int arr[], int size, int num) {


for (int i = 0; i < size; i++) {
if (arr[i] == num) {
return 0; // Not unique
}
}
return 1; // Unique
}

void validate(int one[], int unique[], int size, int *unique_size) {


*unique_size = 0;
for (int i = 0; i < size; i++) {
if (one[i] != 0 && is_unique(unique, *unique_size, one[i])) {
unique[*unique_size] = one[i];
(*unique_size)++;
}
}
}

int main() {
int one[MAX_SIZE], unique[MAX_SIZE], size = 0, unique_size = 0;

printf("Enter %d numbers:\n", MAX_SIZE);


for (int i = 0; i < MAX_SIZE; i++) {
scanf("%d", &one[i]);
size++;
}
validate(one, unique, size, &unique_size);

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

printf("Unique array:\n");
for (int i = 0; i < unique_size; i++) {
printf("%d ", unique[i]);
}
printf("\n");

return 0;
}

#include <stdio.h> //module 3 arrays module excercise no. 3 (2/11/25)

#define SIZE 10
#define MAX_SIZE 20
void merge(int list1[], int list2[], int list3[], int size1, int size2) {
int temp[MAX_SIZE];
int k = 0;

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


temp[k++] = list1[i];
}

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


temp[k++] = list2[i];
}

// Sorting merged array in ascending order


for (int i = 0; i < k - 1; i++) {
for (int j = i + 1; j < k; j++) {
if (temp[i] > temp[j]) {
int swap = temp[i];
temp[i] = temp[j];
temp[j] = swap;
}
}
}

// Copy sorted values into list3


for (int i = 0; i < k; i++) {
list3[i] = temp[i];
}
}
void input_list(int list[], int size, const char* name) {
printf("Enter 10 numbers for %s:\n", name);
for (int i = 0; i < size; i++) {
scanf("%d", &list[i]);
}
}

int main() {
int list1[SIZE], list2[SIZE], list3[MAX_SIZE];

input_list(list1, SIZE, "list1");


input_list(list2, SIZE, "list2");

printf("\nThe integers of the lists:\n");


printf("list1 = { ");
for (int i = 0; i < SIZE; i++) {
printf("%d%s", list1[i], (i < SIZE - 1) ? ", " : " ");
}
printf("}\n");

printf("list2 = { ");
for (int i = 0; i < SIZE; i++) {
printf("%d%s", list2[i], (i < SIZE - 1) ? ", " : " ");
}
printf("}\n\n");

merge(list1, list2, list3, SIZE, SIZE);

printf("After the merge process, list3 contains the following:\n");


printf("list3 = { ");
for (int i = 0; i < MAX_SIZE; i++) {
printf("%d%s", list3[i], (i < MAX_SIZE - 1) ? ", " : " ");
}
printf("}\n");

return 0;
}

#include <stdio.h> //module 3 arrays module excercise no. 4


(2/11/25)

#define SIZE 20

void split(int list[], int positive[], int negative[], int *posCount, int *negCount) {
*posCount = 0;
*negCount = 0;

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


if (list[i] > 0) {
positive[(*posCount)++] = list[i];
} else if (list[i] < 0) {
negative[(*negCount)++] = list[i];
}
}
}

int main() {
int list[SIZE], positive[SIZE], negative[SIZE];
int posCount, negCount;
printf("Enter 20 postive/negative integers: \n");
for (int i = 0; i < SIZE; i++) {
scanf("%d", &list[i]);
}

printf("\nThe following are the integers of the list: \n");


printf("list = { ");
for (int i = 0; i < SIZE; i++) {
printf("%d%s", list[i], (i < SIZE - 1) ? ", " : " ");
}
printf("}\n\n");

split(list, positive, negative, &posCount, &negCount);

printf("After the split, positive and negative arrays are following: \n");

printf("positive = { ");
for (int i = 0; i < posCount; i++) {
printf("%d%s", positive[i], (i < posCount - 1) ? ", " : " ");
}
printf("}\n");

printf("negative = { ");
for (int i = 0; i < negCount; i++) {
printf("%d%s", negative[i], (i < negCount - 1) ? ", " : " ");
}
printf("}\n");

return 0;
}

#include <stdio.h> //module 3 arrays module excercise no. 5


(2/11/25)

#define SIZE 10

void sum_of_index(int list1[], int list2[]) {


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

int main() {
int list1[SIZE], list2[SIZE];

printf("Enter 10 integers for list 1:\n");


for (int i = 0; i < SIZE; i++) {
scanf("%d", &list1[i]);
}

printf("Enter 10 integers for list 2:\n");


for (int i = 0; i < SIZE; i++) {
scanf("%d", &list2[i]);
}

printf("\nThe sum of integers at the same index:\n");


sum_of_index(list1, list2);
return 0;
}

#include <stdio.h> //module 3 arrays module excercise no. 6


(2/11/25)

#define SIZE 15

void find_min_max(int numbers[], int size) {


int min = numbers[0], max = numbers[0];
int min_index = 0, max_index = 0;

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


if (numbers[i] < min) {
min = numbers[i];
min_index = i;
}
if (numbers[i] > max) {
max = numbers[i];
max_index = i;
}
}

printf("The smallest number is %d. It is at index %d.\n", min, min_index);


printf("The largest number is %d. It is at index %d.\n", max, max_index);
}

int main() {
int numbers[SIZE];
int size;

printf("Enter the number of integers (max 15): ");


scanf("%d", &size);
if (size > SIZE) size = SIZE;

printf("Enter %d integers:\n", size);


for (int i = 0; i < size; i++) {
scanf("%d", &numbers[i]);
}

find_min_max(numbers, size);

return 0;
}

#include <stdio.h> //module 3 arrays module excercise no. 7 (2/11/25)

#define SIZE 15

int sum_of_positive(int numbers[], int size) {


int sum = 0;
for (int i = 0; i < size; i++) {
if (numbers[i] > 0) {
sum += numbers[i];
}
}
return sum;
}

int sum_of_negative(int numbers[], int size) {


int sum = 0;
for (int i = 0; i < size; i++) {
if (numbers[i] < 0) {
sum += numbers[i];
}
}
return sum;
}

int main() {
int numbers[SIZE];
int size;

printf("Enter the number of integers (max 15): ");


scanf("%d", &size);
if (size > SIZE) size = SIZE;

printf("Enter %d integers:\n", size);


for (int i = 0; i < size; i++) {
scanf("%d", &numbers[i]);
}

printf("The sum of all positive numbers is %d.\n", sum_of_positive(numbers,


size));
printf("The sum of all negative numbers is %d.\n", sum_of_negative(numbers,
size));
return 0;
}

#include <stdio.h> //module 3 arrays module excercise no. 8 (2/11/25)

#define SIZE 20

int search_element(int list[], int size, int target) {


int found = 0;
for (int i = 0; i < size; i++) {
if (list[i] == target) {
printf("%d can be found at index %d.\n", target, i);
found = 1;
}
}
return found;
}

int main() {
int list[SIZE];
int size, target;

printf("Enter the number of elements (max %d): ", SIZE);


scanf("%d", &size);
if (size > SIZE) size = SIZE;

printf("Enter %d integers:\n", size);


for (int i = 0; i < size; i++) {
scanf("%d", &list[i]);
}

while (1) {
printf("Enter a number that you want to search from the list: ");
scanf("%d", &target);

if (!search_element(list, size, target)) {


printf("%d is not in the list.\n", target);
break;
}
}

return 0;
}

#include <stdio.h> //module 3 arrays module excercise no. 9


last onee (2/11/25)

#define SIZE 10

int main() {
int list1[SIZE], list2[SIZE], list3[SIZE * 2];

printf("Enter 10 integers for list 1: \n");


for (int i = 0; i < SIZE; i++) {
scanf("%d", &list1[i]);
}

printf("Enter 10 integers for list 2: \n");


for (int i = 0; i < SIZE; i++) {
scanf("%d", &list2[i]);
}

// Merging list1 and list2 into list3


for (int i = 0; i < SIZE; i++) {
list3[i] = list1[i];
}
for (int i = 0; i < SIZE; i++) {
list3[i + SIZE] = list2[i];
}

// Display the lists


printf("List 1 contains: ");
for (int i = 0; i < SIZE; i++) {
printf("%d%s", list1[i], (i < SIZE - 1) ? ", " : "\n");
}

printf("List 2 contains: ");


for (int i = 0; i < SIZE; i++) {
printf("%d%s", list2[i], (i < SIZE - 1) ? ", " : "\n");
}

printf("List 3 contains: ");


for (int i = 0; i < SIZE * 2; i++) {
printf("%d%s", list3[i], (i < SIZE * 2 - 1) ? ", " : "\n");
}

return 0;
}

You might also like