2. Write a C program that reads integers from the keyboard and place them into a
ID: 3810885 • Letter: 2
Question
2. Write a C program that reads integers from the keyboard and place them into an array. The program will then sort the array into ascending and descending order and 41 print the sorted lists. The program must not change the original array Ascending Descending or create any other integer arrays. Before Sorting After Sorting Use two pointer arrays. The first pointer array is rearranged so that it points to the data in ascending sequence. The second pointer array is rearranged so that it points to the data in descending sequence. Output should be formatted with the three arrays printed as vertical lists next to each other. Must use pointer notation for full credit. Output should look similar to below. Sample Run: original Ascending Descending 14 26 26 41 41 57 41Explanation / Answer
#include <stdio.h>
int main(void)
{
int a[10], i=0, j=0, n, t;
printf (" Enter the no. of elements: ");
scanf ("%d", &n);
printf (" ");
for (i = 0; i <n; i++)
{
printf (" Enter the %dth element: ", (i+1));
scanf ("%d", &a[i]);
}
for (j=0 ; j<(n-1) ; j++)
{
for (i=0 ; i<(n-1) ; i++)
{
if (a[i+1] < a[i])
{
t = a[i];
a[i] = a[i + 1];
a[i + 1] = t;
}
}
}
printf (" Ascending order: ");
for (i=0 ; i<n ; i++)
{
printf (" %d ", a[i]);
}
printf (" Descending order: ");
for (i=n ; i>0 ; i--)
{
printf (" %d ", a[i-1]);
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.