Processing the Arrays in C Language

Processing the Arrays – For certain applications, the assignment of initial values to elements of an array is required. This means that the array is defined globally (extern) or locally as a static array.

Let us now see in the following example how the marks in two subjects, stored in two different arrays(Processing the Arrays), can be added to give another array and display the average marks in the below example.

C program to display the average marks of each student, given the marks in 2 subjects for 3 students.

/* Program to display the average marks of 3 students */
# include < stdio.h >
# define SIZE 3
main()
{
int i = 0;
float stud_marks1[SIZE]; /* subject 1array declaration */
float stud_marks2[SIZE]; /*subject 2 array declaration */
float total_marks[SIZE];
float avg[SIZE];
printf(“\n Enter the marks in subject-1 out of 50 marks: \n”);
for( i = 0;i<SIZE;i++)
{
printf(“Student no. =%d”,i+1);
printf(“ Enter the marks= “);
scanf(“%f”,&amp;stud_marks1[i]);
}
printf(“\n Enter the marks in subject-2 out of 50 marks \n”);
for(i=0;i<SIZE;i++)
{
printf(“Student no. =%d”,i+1);
printf(“ Please enter the marks= “);
scanf(“%f”,&amp;stud_marks2[i]);
}
for(i=0;i<SIZE;i++)
{
total_marks[i]=stud_marks1[i]+ stud_marks2[i];
avg[i]=total_marks[i]/2;
printf(“Student no.=%d, Average= %f\n”,i+1, avg[i]);
}
}

OUTPUT

Enter the marks in subject-1out of 50 marks:
Student no. = 1 Enter the marks= 23
Student no. = 2 Enter the marks= 35
Student no. = 3 Enter the marks= 42
Enter the marks in subject-2 out of 50 marks:
Student no. = 1 Enter the marks= 31
Student no. = 2 Enter the marks= 35
Student no. = 3 Enter the marks= 40
Student no. = 1 Average= 27.000000
Student no. = 2 Average= 35.000000
Student no. = 3 Average= 41.000000

C program to search an element using the linear search

Write a program to search an element in a given list of elements using Linear Search.
/* Linear Search.*/
# include<stdio.h>
# define SIZE 05
main()
{
int i = 0;
int j;
int num_list[SIZE]; /* array declaration */
/* enter elements in the following loop */
printf(“Enter any 5 numbers: \n”);
for(i = 0;i<SIZE;i ++)
{
printf(“Element no.=%d Value of the element=”,i+1);
scanf(“%d”,&amp;num_list[i]);
}
printf (“Enter the element to be searched:”);
scanf (“%d”,&amp;j);
/* search using linear search */
for(i=0;i<SIZE;i++)
{
if(j == num_list[i])
{
printf(“The number exists in the list at position: %d\n”,i+1);
break;
}
}
}

OUTPUT

Enter any 5 numbers:
Element no.=1 Value of the element=23
Element no.=2 Value of the element=43
Element no.=3 Value of the element=12
Element no.=4 Value of the element=8
Element no.=5 Value of the element=5
Enter the element to be searched: 8
The number exists in the list at position: 4

C program to sort a list of elements using the selection sort method

/* Sorting list of numbers using selection sort method*/
#include <stdio.h>
#define SIZE 5
main()
{
int j,min_pos,tmp;
int i; /* Loop variable */
int a[SIZE]; /* array declaration */
/* enter the elements */
for(i=0;i<SIZE;i++)
{
printf(“Element no.=%d”,i+1);
printf(“Value of the element: “);
scanf(“%d”,&amp;a[i]);
}
/* Sorting by descending order*/
for (i=0;i<SIZE;i++)
{
min_pos = i;
for (j=i+1;j<SIZE;j++)
if (a[j] < a[min_pos])
min_pos = j;
tmp = a[i];
a[i] = a[min_pos];
a[min_pos] = tmp;
}
/* print the result */
printf(“The array after sorting:\n”);
for(i=0;i<SIZE;i++)
printf("% d\n",a[i]);
}

OUTPUT

Element no. = 1 Value of the element: 23
Element no. =2 Value of the element: 11
Element no. = 3 Value of the element: 100
Element no. = 4 Value of the element: 42
Element no. = 5 Value of the element: 50

The array after sorting:

11
23
42
50
100

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: