Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Develop a flow diagram and write a C program that prompts the user for 5 integer

ID: 3794262 • Letter: D

Question

Develop a flow diagram and write a C program that prompts the user for 5 integers, sorts them in place, and then outputs them. You must include a function with the declaration int sort(int *array, int size); That takes an integer array array and the number of elements in the array size (constant for this task, but your function must be able to handle the general case), and returns the largest element in the array. The function must sort the array of integers in ascending order and correctly sum each of the numbers. You may not use any built in sorting algorithms to sort the array; you must create one yourself.

Explanation / Answer

#include<stdio.h>
#include<conio.h>

int sort(int *array, int size)
{
   int i, j;
   int temp;
   for (i = 1; i<size; i++)
   {
       for (j = 0; j<size - i; j++)
       {
           if (*(array + j)>*(array + j + 1))
           {
               temp = *(array + j);
               *(array + j) = *(array + j + 1);
               *(array + j + 1) = temp;
           }
       }

   }

   return 0;
}


void main()
{
   int arr[10];
   int i;
   int size;


   printf(" Enter the size of array :");
   scanf("%d", &size);

   printf(" Enter the elements ");
   for (i = 0; i<size; i++)
       scanf("%d", &arr[i]);

   printf(" Unsorted array ");
   for (i = 0; i<size; i++)
       printf("%d ", arr[i]);

   sort(arr, size);

   printf(" Sorted array ");
   for (i = 0; i<size; i++)
       printf("%d ", arr[i]);

   printf(" Highest Element = %d ", arr[size - 1]);

   getch();
}