C Programming Arrays
C Programming Arrays
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.
*
* 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.
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");
printf("Input the number of elements to store in the array :");
scanf("%d",&n);
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);
if(arr3[k]<=arr3[k+1])
{
j=arr3[k+1];
arr3[k+1]=arr3[k];
arr3[k]=j;
}
}
}