0% found this document useful (0 votes)
86 views

C Programming Arrays

The program reads integers from the user into two separate arrays of the same size. It then merges the two arrays into a third array, sorts the third array in descending order, and prints out the merged and sorted array. The key steps are: 1. Read integers from the user to populate two arrays 2. Merge the two arrays into a third array by iterating through and copying elements 3. Sort the third array in descending order using a for loop comparison 4. Print the merged and sorted third array

Uploaded by

Kirankumar Naidu
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views

C Programming Arrays

The program reads integers from the user into two separate arrays of the same size. It then merges the two arrays into a third array, sorts the third array in descending order, and prints out the merged and sorted array. The key steps are: 1. Read integers from the user to populate two arrays 2. Merge the two arrays into a third array by iterating through and copying elements 3. Sort the third array in descending order using a for loop comparison 4. Print the merged and sorted third array

Uploaded by

Kirankumar Naidu
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 8

/*

 * C program to read N integers into an array A and


 * a) Find the sum of all numbers
 * b) Find the average of all numbers
 * Display the results with suitable headings
 */
 
#include <stdio.h>
 
int main()
{
int i, num;
float total = 0.0, average;
printf ("Enter the value of N \n");
scanf("%d", &num);
int array[num];
 
printf("Enter %d numbers (-ve, +ve and zero) \n", num);
 
for (i = 0; i < num; i++)
{
scanf("%d", &array[i]);
}
 
printf("Input array elements \n");
 
for (i = 0; i < num; i++)
{
printf("%+3d\n", array[i]);
}
 
/* Summation starts */
 
for (i = 0; i < num; i++)
{
total+=array[i];/* this means total=total+array[i]; */
}
 
average = total / num;
 
printf("\n Sum of all numbers = %.2f\n", total);
 
printf("\n Average of all input numbers = %.2f\n", average);
 
}

Program Explanation
1. Take a number num as input, which will indicate the number of elements in the array.
2. Create an array of integer with user-defined size.
3. Iterating through for loops (from 0 to N)), take integers as input from the user and print them.
These inputs are the elements of the array.
4. Now, start summation by iterating through all the elements and adding numbers to calculate
the sum of the array.
5. To calculate the average, the overall sum is divided by the total number of elements in the
array.
6. Print the sum and average values calculated.

Runtime Test Cases


Here is the runtime output of the C program where the user is reading an array of 5 integers
with values 10,20,30,40 and 50 and the program is calculating and displaying the sum and
average of the elements of the array.

Enter the value of N


5
Enter 5 numbers (-ve, +ve and zero)
10
20
30
40
50
 
Input array elements
10
20
30
40
50
 
Sum of all numbers = 150 
Average of all input numbers = 30

*
 * C program to read elements into an array and find the
 * largest two elements in a given array.
 */
 
#include <stdio.h>
int main ()
{
int n = 0, i = 0, largest1 = 0, largest2 = 0, temp = 0;
 
printf ("Enter the size of the array\n");
scanf ("%d", &n);
int array[n];
printf ("Enter the elements\n");
for (i = 0; i < n; i++)
{
scanf ("%d", &array[i]);
}
 
printf ("The array elements are : \n");
for (i = 0; i < n; i++)
{
printf ("%d\t", array[i]);
}
 
printf ("\n");
 
largest1 = array[0];
largest2 = array[1];
 
if (largest1 < largest2)
{
temp = largest1;
largest1 = largest2;
largest2 = temp;
}
 
for (int i = 2; i < n; i++)
{
if (array[i] > largest1)
{
largest2 = largest1;
largest1 = array[i];
}
else if (array[i] > largest2 && array[i] != largest1)
{
largest2 = array[i];
}
}
 
printf ("The FIRST LARGEST = %d\n", largest1);
printf ("THE SECOND LARGEST = %d\n", largest2);
 
return 0;
}
Program Explanation
1. Declare an array of user-defined size.
2. Using for loop, define the elements of the array.
3. Consider the first element of array to be the first largest number (store it in a variable,  largest1).
4. Consider the second element of array to be the second-largest number (store it in a variable,  largest2).
5. Now interchange the values if the value of largest1 is smaller than the largest2.
6. Run a for loop from the third element of the array till the last element of the array, wherein each
element will be compared to the largest1.
i) If the value of the current element is larger than largest1 then, we put the value of the current
element to largest1 and value of largest1 to largest2.
ii) else if, the value of the current element is larger than the largest2 and is not equal to largest1,
then we put the value of the current element to largest2.
7. Running loop to the end will give us the actual first largest and second-largest number.
8. Exit
Runtime Test Cases
Here is the runtime output of the C program with 2 different test cases.

Test case 1: Here, the elements are unique. We are reading an array of 5 elements with unique values
2,4,5,8 and 7. The program is displaying the largest 2 numbers.

Enter the size of the array


5
Enter the elements
2
4
5
8
7
The array elements are :
2 4 5 8 7
The FIRST LARGEST = 8
THE SECOND LARGEST = 7
Test case 2: Here, the elements are recurring. We are reading an array of 5 elements with recurring
values 2,1,1,2,1. The program is displaying the largest 2 numbers.

Enter the size of the array


6
Enter the elements
2
1
1
2
1
2

Write a program in C to read n number of values in an array and display it in reverse order.
#include <stdio.h>
void main()
{
int i,n,a[100];

printf("\n\nRead n number of values in an array and display it in


reverse order:\n");

printf("---------------------------------------------------------------------
---\n");
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]);
}
printf("\n\n");
}
Read n number of values in an array and display it in reverse order:
----------------------------------------------------------------------
--
Input the number of elements to store in the array :3
Input 3 number of elements in the array :
element - 0 : 2
element - 1 : 5
element - 2 : 7

The values store into the array are :


2 5 7

The values store into the array in reverse are :


7 5 2
Write a program in C to find the sum of all elements of an array.
#include <stdio.h>
void main()
{
int a[100];
int i, n, sum=0;
printf("\n\nFind sum of all elements of array:\n");
printf("--------------------------------------\n");
printf("Input the number of elements to be stored in the array :");
scanf("%d",&n);

printf("Input %d elements in the array :\n",n);


for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&a[i]);
}

for(i=0; i<n; i++)


{
sum += a[i];
}
printf("Sum of all elements stored in the array is : %d\n\n", sum);
}

Copy
Sample Output:
Find sum of all elements of array:
--------------------------------------
Input the number of elements to be stored in the array :3
Input 3 elements in the array :
element - 0 : 2
element - 1 : 5
element - 2 : 8
Sum of all elements stored in the array is : 15
Write a program in C to merge two arrays of same size sorted in decending order.
#include <stdio.h>

void main()
{
int arr1[100], arr2[100], arr3[200];
int s1, s2, s3;
int i, j, k;
printf("\n\nMerge two arrays of same size sorted in decending
order.\n");

printf("------------------------------------------------------------\n");
printf("Input the number of elements to be stored in the first
array :");
scanf("%d",&s1);

printf("Input %d elements in the array :\n",s1);


for(i=0;i<s1;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
}

printf("Input the number of elements to be stored in the second


array :");
scanf("%d",&s2);
printf("Input %d elements in the array :\n",s2);
for(i=0;i<s2;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr2[i]);
}
/* size of merged array is size of first array and size of second array
*/
s3 = s1 + s2;
/*----------------- insert in the third
array------------------------------------*/
for(i=0;i<s1; i++)
{
arr3[i] = arr1[i];
}
for(j=0;j<s2; j++)
{
arr3[i] = arr2[j];
i++;
}
/*----------------- sort the array in decending order
---------------------------*/
for(i=0;i<s3; i++)
{
for(k=0;k<s3-1;k++)
{

if(arr3[k]<=arr3[k+1])
{
j=arr3[k+1];
arr3[k+1]=arr3[k];
arr3[k]=j;
}
}
}

/*--------------- Prints the merged array


------------------------------------*/
printf("\nThe merged array in decending order is :\n");
for(i=0; i<s3; i++)
{
printf("%d ", arr3[i]);
}
printf("\n\n");
}
Sample Output:
Merge two arrays of same size sorted in decending order.
------------------------------------------------------------
Input the number of elements to be stored in the first array :3
Input 3 elements in the array :
element - 0 : 1
element - 1 : 2
element - 2 : 3
Input the number of elements to be stored in the second array :3
Input 3 elements in the array :
element - 0 : 1
element - 1 : 2
element - 2 : 3

The merged array in decending order is :


3 3 2 2 1 1

You might also like