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

PLease help me finish the rest of this code... C language and the this seem to b

ID: 3881830 • Letter: P

Question

PLease help me finish the rest of this code... C language and the this seem to be like add a number and make it in the right order ( small to big).

#include <stdio.h>
main()
{
int a[30];
int i, j, lasti;
int num;
lasti = 0;

printf("Enter an integer: ");
scanf("%d", &a[0]);
printf("array[0] is: %d ", a[0]);

while(1)
{

printf("Enter an integer to be sorted: ");
scanf("%d", &num);
  
for(i = 0; i<=lasti; i++)
{
// if num is bigger than a[i]
if( num < a[i] )
{
// But we are sorting as a number_come_in bases,
// a[0] < a[1] < a[2] < a[3] < ..... < a[lasti]
// For example;
// a[0] a[1] a[2] a[3] a[4] a[5]
// 7 < 12 < 30 < 35 < 64 < 77
//
// if a new comming_in number num is 20,
// 20 <= 7 (a[0]) Nooooo
// 20 <= 12 (a[1]) Nooooo
// 20 <= 30 (a[2]) Ya ya ya! 20 should go in a[2]
// When i is 2, 20<=30 is true ! That is the slot.
// now, we have to move 77,64,35, and 30 to insert 20
//
// a[0] a[1] a[2] a[3] a[4] a[5] a[6]
// 7 < 12 < ____ 30 < 35 < 64 < 77
// This moving process can be done by the loop:
//a[j+1] = a[j];
// Let's think about j's range
// First one to move is a[lasti] to a[lasti+1]
// So, we set j = lasti
//a[j+1] = a[j]; // this moves a[lasti] to a[lasti+1]
// Next, set j = j-1
//a[j+1] = a[j]; // this moves a[lasti-1] to a[lasti]
// Next, set j = j-1
//a[j+1] = a[j]; // this moves a[lasti-2] to a[lasti-1]
// Well, how long do we have to do this moving business?
// When we finish moving a[i] to a[i+1]. a[i] is the slot.
// In the above example, when 30 (a[2]) moved into a[3]
// So, j goes from lasti down as long as j >= i
for(j=lasti; j >= i; j--){
a[j+1] = a[j];
}
// After the above moving business,
// insert num into a[i] and get out the loop and


a[i] = num;

}

// if num is bigger than anyone in the array a[],
// that means, "if( num < a[i] )" never be true,
// then num should be placed in the end.
// This case is detected when i is bigger than lasti
if( i > lasti )
{
a[i] = num;
lasti++;
}
// print array
{
int i;
for(i=0; i <= lasti; i++)
printf("%d ", a[i]);
printf("---------lasti=%d ", lasti);

}
}

}
}

Explanation / Answer

Ans: In my program I am sorting the array every time after adding the a element to the list.So it very sort.

#include<stdio.h>

int main() {
int array[30];
int num=30;
int i, j, temp;
int Noofturn=0;

while(num>Noofturn)
{

printf("Enter an integer to be sorted: ");
scanf("%d", &num);
array[Noofturn]=num;
printf("array[0] is: %d ", a[Noofturn]);
//Sort the array
for (i = 0; i < num; i++)
{
for (j = 0; j < (num - i - 1); j++)
{
if (array[j] > array[j + 1])
{
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
  
printf("Sorted array is... ");
for (i = 0; i < num; i++)
{
printf("%d ", array[i]);
}
  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote