Programming with C

⌘K
  1. Home
  2. Docs
  3. Programming with C
  4. Arrays and Strings
  5. Additional Program Arrays

Additional Program Arrays

Program: Implementation of descending order

#include<stdio.h>
int main()
{
	int a[100],n,i,j,temp;
	printf("Enter array elements\n");
	scanf("%d",&n);
	printf("Enter elements \n");
	for(i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
	}
	// loop for sorting
	for(i=0;i<n;i++)
	{
		for(j=i+1;j<n;j++)
		{
			if(a[i]<a[j])
			{
				temp = a[i];
				a[i] = a[j];
				a[j] = temp;
			}
	
		}
	}
	printf("\nAfter sorting in descending order \n");
	for(i=0;i<n;i++)
	{
		printf("%d\t",a[i]);
	}
	return 0;
}

Output

Enter array elements
5
Enter elements
4 5 3 9 10
After sorting
10      9       5       4       3

Program: Implementation of ascending order

#include<stdio.h>
int main()
{
	int a[100],n,i,j,temp;
	printf("Enter array elements\n");
	scanf("%d",&n);
	printf("Enter elements \n");
	for(i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
	}
	// loop for sorting
	for(i=0;i<n;i++)
	{
		for(j=i+1;j<n;j++)
		{
			if(a[i]>a[j])
			{
				temp = a[i];
				a[i] = a[j];
				a[j] = temp;
			}
	
		}
	}
	printf("\nAfter sorting in ascending order \n");
	for(i=0;i<n;i++)
	{
		printf("%d\t",a[i]);
	}
	return 0;
}

Output

Enter array elements
5
Enter elements
45 32 1 67 40
After sorting in ascending order
1       32      40      45      67

Program: To find Second biggest number in array

#include<stdio.h>
int main()
{
	int a[100],n,i,j,temp;
	printf("Enter array elements\n");
	scanf("%d",&n);
	printf("Enter elements \n");
	for(i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
	}
	// loop for sorting
	for(i=0;i<n;i++)
	{
		for(j=i+1;j<n;j++)
		{
			if(a[i]<a[j])
			{
				temp = a[i];
				a[i] = a[j];
				a[j] = temp;
			}
	
		}
	}
	printf("Second biggest number is %d",a[1]);
	return 0;
}

Output

Enter array elements
5
Enter elements
45 67 90 1 40
Second biggest number is 67

Program: To find mean, median and mode

How to find the mean

  • Add up all numbers
  • Divide the sum by number of values

How to find the median

  • Arrange the numbers from smallest to largest
  • The number in the middle is the median, If there are two middle numbers, add them and divide by two.

How to find the mode

The most frequent number

#include <stdio.h>

// function to sort the array in ascending order
void Array_sort(int *array , int n)
{ 
    // declare some local variables
    int i=0 , j=0 , temp=0;

    for(i=0 ; i<n ; i++)
    {
        for(j=i+1 ; j<n ; j++)
        {
            if(array[i]>array[j])
            {
                temp        = array[i];
                array[i]    = array[j];
                array[j]  = temp;
            }
        }
    }

    printf("\nThe array after sorting is..\n");
    for(i=0 ; i<n ; i++)
    {
        printf("\narray_1[%d] : %d",i,array[i]);
    }
}

float Find_mean(int arr[], int n){
    float sum = 0;
	int i=0;
    for(i = 0;i < n; i++)
        sum += arr[i];
    
    return sum/n;
}

// function to calculate the median of the array
float Find_median(int array[] , int n)
{
    float median=0;
    
    // if number of elements are even
    if(n%2 == 0)
        median = (array[(n-1)/2] + array[n/2])/2.0;
    // if number of elements are odd
    else
        median = array[n/2];
    
    return median;
}
//finding mode of ungrouped data
float Find_mode( int arr[], int n){

    
    //finding max frequency  
    int max_count = 1, res = arr[0], count = 1; 

    for (int i = 1; i < n; i++) { 
        if (arr[i] == arr[i - 1]) 
            count++; 
        else { 
            if (count > max_count) { 
                max_count = count; 
                res = arr[i - 1]; 
            } 
            count = 1; 
        } 
    } 
  
    // when the last element is most frequent 
    if (count > max_count) 
    { 
        max_count = count; 
        res = arr[n - 1]; 
    } 
    
    return res;
}

int main()
{
    // declare  int arrays
    int array_1[30] = {0};
    // declare some local variables
    int i=0 ,n=0;
    float median=0;
	float mean =0;
	float mode =0;

    printf("\nEnter the number of elements for the array : ");
    scanf("%d",&n);
   
    printf("\nEnter the elements for array_1..\n");
    for(i=0 ; i<n ; i++)
    {
        printf("array_1[%d] : ",i);
        scanf("%d",&array_1[i]);
    }
    mean = Find_mean(array_1,n);
    // Sort the array in ascending order
    Array_sort(array_1 , n);
    
    // Now pass the sorted array to calculate
    // the median of your array.
    median = Find_median(array_1 , n);
	
	mode = Find_mode(array_1,n);
    
	printf("\n\n The mean is:%f\n",mean);
    printf("\n\nThe median is : %f\n",median);
	printf("\n\nThe mode is : %f\n",mode);
    return 0;
}

Output

Enter the number of elements for the array : 6

Enter the elements for array_1..
array_1[0] : 9
array_1[1] : 3
array_1[2] : 1
array_1[3] : 8
array_1[4] : 3
array_1[5] : 6

The array after sorting is..
array_1[0] : 1
array_1[1] : 3
array_1[2] : 3
array_1[3] : 6
array_1[4] : 8
array_1[5] : 9

The mean is:5.000000
The median is : 4.500000
The mode is : 3.000000

Views: 0

How can we help?

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments