You are on page 1of 11

GURUNANAK INSTITUTE OF

TECHNOLOGY

FYCYS292

Bsc. In Cyber Security

DATA STRUCTURE AND ALGORITHMS


LAB
[EPSHITA MOITRA]

2st. SEMESTER ( 1st YEAR )

REG NO.: 233112410015 OF 2023-24

Roll. NO. : 31140423015


ASSIGNMENT

• Array (1-D Array)

Lab assignments: the following programs using pointer to array

1. Create an array of size 10 such that 0-th element of that array contains the value 1, 1st
element contains the value 2, 2nd element contains the value 3 and so on. Display the
array. Do not initialize the array and do not use any scanf function.

Answer:
#include <stdio.h>

int main() {
int array[10];

// Initialize the array as specified


for (int i = 0; i < 10; i++) {
array[i] = i + 1;
}

// Display the array


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

return 0;
}
OUTPUT:
Array elements:
1 2 3 4 5 6 7 8 9 10

2. Five numbers are entered through the keyboard into an array. Write a C program to
print the square values of them.

Answer:
#include <stdio.h>

int main() {
int numbers[5];
int i;
// Input
printf("Enter five numbers:\n");
for (i = 0; i < 5; i++) {
scanf("%d", &numbers[i]);
}

// Printing square values


printf("Square values:\n");
for (i = 0; i < 5; i++) {
printf("%d ", numbers[i] * numbers[i]);
}
printf("\n");

return 0;
}
OUTPUT:
Enter five numbers:
12345
Square values:
1 4 9 16 25
3. Write a C program to find the largest number of an array of n numbers.

ANSWER:
#include <stdio.h>

int main() {
int n;

// Input the size of the array


printf("Enter the size of the array: ");
scanf("%d", &n);

// Declare an array of size n


int numbers[n];

// Input numbers into the array


printf("Enter %d numbers:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &numbers[i]);
}

// Find the largest number


int largest = numbers[0];
for (int i = 1; i < n; i++) {
if (numbers[i] > largest) {
largest = numbers[i];
}
}

// Print the largest number


printf("The largest number is: %d\n", largest);

return 0;
}
OUTPUT:
Enter the size of the array: 6
Enter 6 numbers:
12
23
45
78
87
90
The largest number is: 90

4. Write a C program to find the largest and smallest number of an array of n


numbers.

ANSWER:
#include <stdio.h>

int main() {
int n;

// Input the size of the array


printf("Enter the size of the array: ");
scanf("%d", &n);

// Declare an array of size n


int numbers[n];

// Input numbers into the array


printf("Enter %d numbers:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &numbers[i]);
}

// Find the largest and smallest numbers


int largest = numbers[0];
int smallest = numbers[0];
for (int i = 1; i < n; i++) {
if (numbers[i] > largest) {
largest = numbers[i];
}
if (numbers[i] < smallest) {
smallest = numbers[i];
}
}

// Print the largest and smallest numbers


printf("The largest number is: %d\n", largest);
printf("The smallest number is: %d\n", smallest);

return 0;
}
OUTPUT:
Enter the size of the array: 5
Enter 5 numbers:
12
67
0
65
34
The largest number is: 67
The smallest number is: 0

5. Write a C program to find the total number of even numbers and total numbers
of odd numbers in an array of n numbers.
ANSWER:
#include <stdio.h>

int main() {
int n;

// Input the size of the array


printf("Enter the size of the array: ");
scanf("%d", &n);

// Declare an array of size n


int numbers[n];

// Input numbers into the array


printf("Enter %d numbers:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &numbers[i]);
}

// Count the number of even and odd numbers


int even_count = 0;
int odd_count = 0;
for (int i = 0; i < n; i++) {
if (numbers[i] % 2 == 0) {
even_count++;
} else {
odd_count++;
}
}

// Print the total number of even and odd numbers


printf("Total number of even numbers: %d\n", even_count);
printf("Total number of odd numbers: %d\n", odd_count);

return 0;
}
OUTPUT:
Enter the size of the array: 5
Enter 5 numbers:
123
1
65
70
34
Total number of even numbers: 3
Total number of odd numbers: 2

6. Write a C program to find the sum of the numbers of an array of n numbers.


ANSWER:
#include <stdio.h>
int main() {
int n;

// Input the size of the array


printf("Enter the size of the array: ");
scanf("%d", &n);

// Declare an array of size n


int numbers[n];

// Input numbers into the array


printf("Enter %d numbers:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &numbers[i]);
}

// Calculate the sum of the numbers


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

// Print the sum


printf("The sum of the numbers is: %d\n", sum);

return 0;
}
OUTPUT:
Enter the size of the array: 5
Enter 5 numbers:
5
4
7
8
9
The sum of the numbers is: 33
7. Write a C program to search whether an element is present or not within an
integer array of size n. Print the position of the key value if it is a successful
search.
ANSWER:
#include <stdio.h>

int main() {
int n, key, found = 0;

// Input the size of the array


printf("Enter the size of the array: ");
scanf("%d", &n);

// Declare an array of size n


int numbers[n];

// Input numbers into the array


printf("Enter %d numbers:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &numbers[i]);
}

// Input the key value to search


printf("Enter the value to search: ");
scanf("%d", &key);

// Search for the key value


for (int i = 0; i < n; i++) {
if (numbers[i] == key) {
printf("Element found at position: %d\n", i + 1);
found = 1;
break;
}
}

// If key value is not found


if (!found) {
printf("Element not found in the array.\n");
}

return 0;
}
OUTPUT:
Enter the size of the array: 5
Enter 5 numbers:
78
90
12
13
45
Enter the value to search: 12
Element found at position: 3

8. Write a C program to sort a series of n numbers in ascending order.


ANSWER:
#include <stdio.h>

void bubbleSort(int arr[], int n) {


int i, j, temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap if the element at j is greater than the next element
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

int main() {
int n;

// Input the size of the array


printf("Enter the size of the array: ");
scanf("%d", &n);

// Declare an array of size n


int numbers[n];

// Input numbers into the array


printf("Enter %d numbers:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &numbers[i]);
}

// Sort the array


bubbleSort(numbers, n);

// Print the sorted array


printf("Sorted array in ascending order: ");
for (int i = 0; i < n; i++) {
printf("%d ", numbers[i]);
}
printf("\n");

return 0;
}
OUTPUT:
Enter the size of the array: 6
Enter 6 numbers:
34
67
90
87
54
21
Sorted array in ascending order: 21 34 54 67 87 90

9. Write a C program to reverse the elements of an array of size n without using


another array.
ANSWER:
#include <stdio.h>

void reverseArray(int arr[], int n) {


int temp;
for (int i = 0; i < n / 2; i++) {
// Swap elements from start and end of the array
temp = arr[i];
arr[i] = arr[n - i - 1];
arr[n - i - 1] = temp;
}
}

int main() {
int n;

// Input the size of the array


printf("Enter the size of the array: ");
scanf("%d", &n);
// Declare an array of size n
int numbers[n];

// Input numbers into the array


printf("Enter %d numbers:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &numbers[i]);
}

// Reverse the array


reverseArray(numbers, n);

// Print the reversed array


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

return 0;
}
OUTPUT:
Enter the size of the array: 5
Enter 5 numbers:
12
34
56
78
90
Reversed array: 90 78 56 34 12

10. Write a C program to convert a positive decimal integer to its equivalent binary.
ANSWER:
#include <stdio.h>

void decimalToBinary(int n) {
if (n == 0) {
printf("Binary equivalent: 0\n");
return;
}

int binary[32]; // Assuming a 32-bit integer


int i = 0;

while (n > 0) {
binary[i] = n % 2;
n /= 2;
i++;
}

printf("Binary equivalent: ");


for (int j = i - 1; j >= 0; j--) {
printf("%d", binary[j]);
}
printf("\n");
}

int main() {
int decimal;

// Input a positive decimal integer


printf("Enter a positive decimal integer: ");
scanf("%d", &decimal);

if (decimal < 0) {
printf("Please enter a positive decimal integer.\n");
return 1; // Exit with error code 1
}

decimalToBinary(decimal);

return 0;
}
OUTPUT:
Enter a positive decimal integer: 67.8
Binary equivalent: 1000011

11. Write a C program to add two arrays containing 5 elements each and store the
result in a third array.
ANSWER:
#include <stdio.h>

#define SIZE 5

void addArrays(int arr1[], int arr2[], int result[]) {


for (int i = 0; i < SIZE; i++) {
result[i] = arr1[i] + arr2[i];
}
}

int main() {
int array1[SIZE], array2[SIZE], result[SIZE];

// Input elements for the first array


printf("Enter %d elements for the first array:\n", SIZE);
for (int i = 0; i < SIZE; i++) {
scanf("%d", &array1[i]);
}

// Input elements for the second array


printf("Enter %d elements for the second array:\n", SIZE);
for (int i = 0; i < SIZE; i++) {
scanf("%d", &array2[i]);
}

// Add arrays
addArrays(array1, array2, result);

// Print the result array


printf("Result array after addition:\n");
for (int i = 0; i < SIZE; i++) {
printf("%d ", result[i]);
}
printf("\n");

return 0;
}

OUTPUT:
Enter 5 elements for the first array:
34
23
56
67
89
Enter 5 elements for the second array:
56
65
43
123
4
Result array after addition:
90 88 99 79 93

You might also like