Exp 5
Exp 5
5
Usage of Arrays Reg.No: URK22CS5043
01.11.2022
a) Write a C program to find the smallest and largest number in an array with index value
Aim:
To find the smallest and largest number in an array with index value
Algorithm:
Step 1: Start the program.
Step 2: int n, k=0, l=0
Step 3: Read the Input values n
Step 4: int I =0, i<n, ++i
Step 5: Print smallest and largest number with index value
Step 6: Stop the Program.
Program:
#include <stdio.h>
int main() {
int n, k=0, l=0, i, j;
double arr[100];
printf("Enter the number of elements (1 to 100): ");
scanf("%d", &n);
for (i = 0; i < n; ++i) {
printf("Enter number%d: ", i + 1);
scanf("%lf", &arr[i]);
}
for (i = 0; i < n - 1; ++i) {
for (j = 0; j < n - i - 1; ++j) {
if (arr[i] > arr[i + 1]) {
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
}
printf("smallest element = %.2lf and its position is %d ", arr[0],l);
for(i = 0; i < n; i++) {
if(arr[0] > arr[i]) {
arr[0] = arr[i];
}
}
printf("Largest element = %.2lf and its position is %d ", arr[n-1],n-1);
return 0;
}
Output:
Result:
This program is executed successfully and the reverse of an array is obtained
b) Write a C program to read a n number of values in a array and display it in reverse order
Aim:
To read a n number of values in a array and display it in reverse order
Algorithm:
include <stdio.h>
void main ()
{
int i, n, a[100];
printf ("Input the number of elements to store in the array :");
scanf ("%d", &n);
printf ("Input %d number of elements in the array :\n", n);
for (i = 0; i < n; i++)
{
printf ("element - %d : ", i);
scanf ("%d", &a[i]);
}
printf ("\nThe values store into the array are : \n");
for (i = 0; i < n; i++)
{
printf ("% 5d", a[i]);
}
printf ("\n\nThe values store into the array in reverse are :\n");
for (i = n - 1; i >= 0; i--)
{
printf ("% 5d", a[i]);
}
}
Output:
Result:
This program is executed successfully and the reverse of an array is obtained.