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

please explain what is going on in the following code, step by step and in detai

ID: 3726623 • Letter: P

Question

please explain what is going on in the following code, step by step and in detail.

#include <stdio.h>

int main() {   

  int a[30];

  int i,j,lasti;

  int num;

  lasti=0;

  // scanf the number

  scanf("%d",&a[0]);

  while (1)

  {

  // sacnf the new number

  printf("Enter another Number ");

  scanf("%d",&num);

  for(i=0;i<=lasti;i=i+1)

  {

  printf("%d ",a[i]);

  // we check if the num that we eneterd is greter than i

  if(a[i] > num)

  {

  for(j=lasti; j>= i;j--)

  {

  a[j+1]=a[j];

  }

  a[i]=num;

  lasti++;

  break;

  }

  }

  if(i>lasti)

  {

  a[i]=num;

  lasti++;

  }

  for(i=0;i<=lasti;i++)

  {

  printf(" %d ",a[i]);

  }

  }

}

Explanation / Answer

Answer:

Please find the comments in the code below,

#include <stdio.h>

int main() {   

int a[30]; // Declared intiger array 'a' of size 30

int i,j,lasti; // Declared intiger variables i, j and lasti

int num; // Declared intiger variables num

lasti=0; // Assigning lasti to value '0'

// scanf the number

scanf("%d",&a[0]); // reading intiger value into array 'a' at index 0 from console

while (1) // Infinity loop of while

{

// sacnf the new number

printf("Enter another Number ");

scanf("%d",&num); // Reading intiger value into variable num from console

for(i=0;i<=lasti;i=i+1) // outer for loop

{

printf("%d ",a[i]); // printing the value at index i from array 'a' initially at index 0

// we check if the num that we eneterd is greter than i

if(a[i] > num)

{

for(j=lasti; j>= i;j--) // inner for loop to sort the array 'a' in assending order

{

a[j+1]=a[j]; /* Switching each index values of array 'a' to next index starting from the index i
i.e, the value of a[i] wille be stored in a[i+1], a[i+1] will be moved to a[i+2]
continue the same till end of array 'a'
*/

}

a[i]=num; // Since the a[i] value is moved to a[i+1] now a[i] will be replaced with the value of num

lasti++; // increment lasti value by 1

break; // exit from outer for loop

}

}

/* if the value of num is greater than all the values in array 'a',
then this condition will gets satisfied and num value will be
inserted into array 'a' at end of current values available in 'a'
*/
if(i>lasti)
{   
  
a[i]=num;
lasti++;

}
// This for loop will print the all the values of array 'a' and While loop will continue
for(i=0;i<=lasti;i++)
{

printf(" %d ",a[i]);

}

}//end of while

}//end of main