Write a program that will take 7 integers from the user, store them in an array.
ID: 3811581 • Letter: W
Question
Write a program that will take 7 integers from the user, store them in an array. Write three functions which takes the aforementioned array as input and display them on the screen 1) in the order as they were entered, 2) in increasing order and 3) in the decreasing order. For instance if the input is: 13 -23 1 45 -2 16-5 then the call to the first function will display: 13-23 1 45 -2 16-5, call to the second function will display: -23 -5 -2 1 13 16 45 Finally, call to the 3rd function will display 45 16 13 1 -2 -5 -23.Explanation / Answer
code snippet in c :
#include<stdio.h>
void display(int array1[]);
void asc(int array2[]);
void desc(int array3[]);
void main() {
int i,array[7];
printf("Enter the elements one by one ");
for (i = 0; i < 7; i++)
{
scanf("%d", &array[i]);
}
display(array);
printf(" ");
asc(array);
printf(" ");
desc(array);
printf(" ");
}
void display(int array1[]){
int i;
printf("Input array is ");
for (i = 0; i < 7; i++)
{
printf("%d ", array1[i]);
}
}
void asc(int array2[]){
int i, j, temp;
for (i = 0; i < 7; i++)
{
for (j = 0; j < (7 - i - 1); j++)
{
if (array2[j] > array2[j + 1])
{
temp = array2[j];
array2[j] = array2[j + 1];
array2[j + 1] = temp;
}
}
}
printf("Array in Increasing order: ");
for (i = 0; i < 7; i++)
{
printf("%d ", array2[i]);
}
}
void desc(int array3[]){
int i, j, temp;
for (i = 0; i < 7; i++)
{
for (j = 0; j < (7 - i - 1); j++)
{
if (array3[j] < array3[j + 1])
{
temp = array3[j];
array3[j] = array3[j + 1];
array3[j + 1] = temp;
}
}
}
printf("Array in Decreasing order: ");
for (i = 0; i < 7; i++)
{
printf("%d ", array3[i]);
}
}
sample output :
sh-4.2$ gcc -o main *.c
sh-4.2$ main
Enter the elements one by one
-21 -45 -2 -65 12 2 45
Input array is
-21 -45 -2 -65 12 2 45
Array in Increasing order:
-65 -45 -21 -2 2 12 45
Array in Decreasing order:
45 12 2 -2 -21 -45 -65
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.