Objective:
- 1D Array
- 2D Array
Activity 1:
Write a program that declare an int array of size N inside the main function. Write the following functions for this array:
- void initalArry(int arr[]) This function takes an int array parameter. The function will be used to populate the array by taking input from user.
- void printArry(int arr[]) Print the array data in an efficient manner that each element is separated with appropriate space. Note: Use this function to print all results of below functions.
- int Find2Largest(int arr[])- The function returns the second largest element of an array.
- void findMinMax(int arr[], int &min, int &max). This function should store the min and max value in the relevant variables which are passed by reference to the function.
- searchData(int arr[], int key). This function should search the key using linear search technique and return the index. If key isn't available in the array, then return -1.
- sortData(int arr[], char choice) This function also takes choice variable as parameters and sort the data according to users choice, i.e., ascending or descending. You can use any sorting technique to sort the data in ascending order.
- evenSort(int arr[]). function to separate even and odd numbers of an array of integers. Put all even numbers first, and then odd numbers.
- CheckFrequency(int arr[]) - the function will take an array as input and print the frequency of each element of an array
Solution:
#include <stdio.h>
#define SIZE 6
void initalArry(int arr[])
{
printf("Enter values of Set: \n");
int i;
for (i = 0; i < SIZE; i++)
{
scanf("%d", &arr[i]);
}
}
void printArry(int arr[])
{
printf("The Elements in Set are: ");
for (int n = 0; n < SIZE; n++)
{
printf("%d", arr[n]);
if (n < (SIZE - 1))
printf(" ");
}
printf("\n");
}
int Find2Largest(int arr[])
{
int arr2[SIZE];
int i, max1 = 0, max2 = 0;
for (i = 0; i < SIZE; i++)
{
arr2[i] = arr[i];
}
for (i = 0; i < SIZE; i++)
{
if (arr2[i] > max1)
{
max1 = arr2[i];
}
}
for (i = 0; i < SIZE; i++)
{
if (arr2[i] == max1)
{
arr2[i] = 0;
}
}
for (i = 0; i < SIZE; i++)
{
if (arr2[i] > max2)
{
max2 = arr2[i];
}
}
return max2;
}
void findMinMax(int arr[], int* min, int* max)
{
int i;
*min = arr[0];
for (i = 1; i < SIZE; i++)
{
if (arr[i] < *min)
{
*min = arr[i];
}
}
*max = arr[0];
for (i = 1; i < SIZE; i++)
{
if (arr[i] > *max)
{
*max = arr[i];
}
}
}
void searchData(int arr[], int key, int* found)
{
int i;
for (i = 0; i < SIZE; i++)
{
if (arr[i] == key)
*found = key;
}
}
void sortData(int arr[], char choice)
{
if (choice == 'a')
{
int i, j, temp;
i = 0;
while (i < SIZE)
{
j = i + 1;
while (j < SIZE)
{
if (arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
j++;
}
i++;
}
printf("Elements sorted in the Ascending Order are: ");
for (i = 0; i < SIZE; i++)
{
printf("%d", arr[i]);
if (i < (SIZE - 1))
printf(" ");
}
printf("\n");
}
if (choice == 'd')
{
int i, j, temp;
i = 0;
while (i < SIZE)
{
j = i + 1;
while (j < SIZE)
{
if (arr[i] < arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
j++;
}
i++;
}
printf("Elements sorted in the Descending Order are: ");
for (i = 0; i < SIZE; i++)
{
printf("%d", arr[i]);
if (i < (SIZE - 1))
printf(" ");
}
printf("\n");
}
}
void evenSort(int arr[])
{
printf("Even Elements of Array: ");
for (int i = 0; i < SIZE; i++)
{
if (arr[i] % 2 == 0)
{
printf("%d ", arr[i]);
}
}
printf("\n");
printf("Odd Elements of Array: ");
for (int i = 0; i < SIZE; i++)
{
if (arr[i] % 2 == 1)
{
printf("%d ", arr[i]);
}
}
printf("\n");
}
void CheckFrequency(int arr[])
{
int flag[SIZE] = { 0 };
int count;
int i, j;
printf("Frequency of all Elements of the Array: ");
for (i = 0; i < SIZE; i++)
{
count = 0;
if (flag[i] != 1)
{
for (j = 0; j < SIZE; j++)
{
if (arr[i] == arr[j])
{
count++;
flag[j] = 1;
}
}
if (count > 1 || count == 1)
{
printf("%d occurs %d times \n", arr[i], count);
}
}
}
}
int main()
{
int arr[SIZE];
int key, found = -1, max = 0, min = 0;
char choice;
initalArry(arr);
printf("\n");
printArry(arr);
printf("\n");
printf("The Second Largest in the Array is: %d\n", Find2Largest(arr));
printf("\n");
findMinMax(arr, &min, &max);
printf("The Max. value in the Array is: %d\n", max);
printf("The Min. value in the Array is: %d\n", min);
printf("\n");
printf("Enter value to find: \n");
scanf("%d", &key);
searchData(arr, key, &found);
if (found == -1)
printf("Value not found in the Array.\n");
else
printf("Value found in the Array.\n");
printf("\n");
do {
printf("Type a: For Ascending Order.\n");
printf("Type d: For Descending Order.\n");
scanf(" %c", &choice);
} while (choice != 'a' && choice != 'd');
sortData(arr, choice);
printf("\n");
evenSort(arr);
printf("\n");
CheckFrequency(arr);
printf("\n");
return 0;
}
Activity 2:
Write
a C program to read a sentence from console into a character array. Now
separate each word and find the word with minimum length and display it on
screen. You also have to make integer array to store length of each word in
sentence. Your code should be generic.
*Note:
Your
sentence always ends with full stop.
Solution:
#include <stdio.h>
#include <string.h>
#define SIZE 100
void Min(char arr[])
{
int lengths[SIZE];
int lengthIndex = 0;
int minLength = SIZE;
int currentLength = 0;
for (int i = 0; arr[i] != '\0'; i++)
{
if (arr[i] != ' ' && arr[i] != '.')
{
currentLength++;
}
else if (currentLength > 0)
{
printf("%d ", currentLength);
lengths[lengthIndex] = currentLength;
lengthIndex++;
if (currentLength < minLength)
{
minLength = currentLength;
}
currentLength = 0;
}
}
printf("\nMin: %d\n", minLength);
}
int main()
{
char arr[SIZE];
printf("Enter a Sentence: \n");
fgets(arr, SIZE, stdin);
arr[strcspn(arr, "\n")] = '\0'; // Remove the newline character from fgets
Min(arr);
return 0;
}
Activity 3:
Write a
function in C called Trace(int data[][size]) that calculates and returns the trace
of a square matrix of size 3x3. The trace of a square matrix is the sum of values present
in its diagonal.
Solution:
#include <stdio.h>
#define rows 3
#define column 3
void trace(int data[rows][column])
{
int trace = 0;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < column; j++)
{
if (i == j)
trace += data[i][j];
}
}
printf("Trace = %d\n", trace);
}
int main()
{
int data[rows][column];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < column; j++)
{
printf("Enter (%d) and (%d) value: ", i, j);
scanf("%d", &data[i][j]);
}
}
printf("Given Matrix is: \n");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < column; j++)
{
printf("%d ", data[i][j]);
}
printf("\n");
}
trace(data);
return 0;
}
Activity 4:
Write a C function to compare two do the following tasks:
- Compare two char arrays
- Copy contents of 1 array into other and then compare both
Solution:
#include <stdio.h>
#include <string.h>
#define SIZE 30
void Compare(char arr1[], char arr2[])
{
printf("First String: %s\n", arr1);
printf("Second String: %s\n", arr2);
if (strcmp(arr1, arr2) == 0)
{
printf("Both Strings are Same.\n");
}
else
{
printf("Both Strings are not Same.\n");
}
printf("\n");
}
int main()
{
char arr1[SIZE];
printf("Enter 1st Sentence: \n");
fgets(arr1, SIZE, stdin);
arr1[strcspn(arr1, "\n")] = '\0'; // Remove the trailing newline character from fgets
char arr2[SIZE];
printf("Enter 2nd Sentence: \n");
fgets(arr2, SIZE, stdin);
arr2[strcspn(arr2, "\n")] = '\0'; // Remove the trailing newline character from fgets
Compare(arr1, arr2);
strcpy(arr2, arr1);
printf("After Copying contents of 1st array into the other.\n");
Compare(arr1, arr2);
return 0;
}
Activity 5:
Ask user for an input, store that input in a char array. Count small letters, capital letters, its and special charters in that input.
Solution:
#include <stdio.h>
#include <string.h>
#define SIZE 30
void count(char arr[])
{
int upper = 0, lower = 0, number = 0, special = 0;
for (int i = 0; arr[i] != '\0'; i++)
{
if (arr[i] >= 'A' && arr[i] <= 'Z')
upper++;
else if (arr[i] >= 'a' && arr[i] <= 'z')
lower++;
else if (arr[i] >= '0' && arr[i] <= '9')
number++;
else
special++;
}
printf("Capital Letters: %d\n", upper);
printf("Small Letters : %d\n", lower);
printf("Numbers : %d\n", number);
printf("Special Characters : %d\n", special);
}
int main()
{
char arr[SIZE];
printf("Enter the String: \n");
fgets(arr, SIZE, stdin);
arr[strcspn(arr, "\n")] = '\0'; // Remove the trailing newline character from fgets
count(arr);
return 0;
}