Question

Explain the following code, line by line. As well as the is going on over all....

Explain the following code, line by line. As well as the is going on over all.

#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 \n");

  scanf("%d",&num);

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

  {

  printf("%d \n",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\n",a[i]);

  }

  }

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h> // Standard C library header, defines various functions for performing input and output

int main()

{

  int a[30]; // declaration of array variable of size 30.

  int i,j,lasti; // declaration of integer variables

  int num; // declaration of integer variable

lasti=0; //Initializing integer variable to 0

// scan the number

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

while (1) // looping until break.
{
// sacnf the new number
printf("Enter another Number \n");
scanf("%d",&num); // reading another number
for(i=0;i<=lasti;i=i+1) // looping 0 to last number in array a[]
{
printf("%d \n",a[i]); //printing array elements
// we check if the num that we eneterd is greter than i
if(a[i] > num) // comparing ith element with given number entered
{
for(j=lasti; j>= i;j--) // looping from last element to ith element.
{
a[j+1]=a[j]; //swaping grater number to next position to sort array
}
a[i]=num; // acutal assingin of given number
lasti++; // incrementing lasti value
break;
}
}
if(i>lasti) // this condition fails if above for loop is terminated by break
{
a[i]=num; // else assigning ith element with given number
lasti++; // incrementing lasti value
}
  
for(i=0;i<=lasti;i++) // loop for printing sorted array in assendign order.
{
  
printf(" %d\n",a[i]); // prining ith element

}

}

}

Smaple output:

Entered 10 as first number array, it promted for another number given 20, it printed the number 10 which in array and internally it added 20 to array and printed 10 and 20 as elements in array and prompted for another number. gave 30 as input, it printed 10 20 as the elements already in array and internally it added 30 to array and printed 10, 20 and 30 as elements in array and promte for another soon upto 50.

if in case we enter number which is less than any number in existing array, the initail printing of number in array is limit till it encounters the greater number than given new nubmer as illustrated below.

Enter another Number 25 10 20 30 10 20 25 30 40 50 Enter another Number

if we give 25 now, it will print till the nearest nubmer grater than given number and then it swaps all greater number to create gap for 25 and place the 25 in currect position to make the array sorted in assending order. and prints total array.

Add a comment
Know the answer?
Add Answer to:
Explain the following code, line by line. As well as the is going on over all....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Do you have a flowgorithim flow chart for this code? #include <stdio.h> int main() { int num,i=0,sum=0; float aver...

    Do you have a flowgorithim flow chart for this code? #include <stdio.h> int main() { int num,i=0,sum=0; float average; int customerNumbers[num]; int customerSales[num]; printf("How many customers do you want to track?\n"); scanf("%d",&num); while(i<num) { printf("Enter the customer number. "); scanf("%d",&customerNumbers[i]); printf("Enter the sales for the customer "); scanf("%d",&customerSales[i]); i++; } printf("Sales for the Customer"); printf("\nCustomer Customer"); printf("\nNumber Sales"); for(i=0;i<num;i++) { printf("\n %d \t %d",customerNumbers[i], customerSales[i]); sum=sum+customerSales[i]; } average=(int)sum/num; printf("\n Total sales are $%d",sum); printf("\n Average sales are $%.2f",average); printf("\n --------------------------------------------");...

  • Explain the code and analyze the performance of algorithm #include<stdio.h> #include<string.h> #define NUM 1...

    Explain the code and analyze the performance of algorithm #include<stdio.h> #include<string.h> #define NUM 100 #define maxint 10000 void dijkstra(int n,int v,int dist[],int prev[],int c[][NUM]) {    int i,j;    bool s[NUM];    for(i=1; i<=n; i++)    {        dist[i] = c[v][i];        s[i] = false;        if (dist[i]>maxint) prev[i] = 0;        else prev[i] = v;    }    dist[v] = 0;    s[v] = true;    for(i=1; i<n; i++)    {        int tmp = maxint;        int u = v;        for(j=1; j<=n; j++)            if(!(s[j]) && (dist[j]<tmp))            {                u = j;                tmp = dist[j];           ...

  • i need flowchart for the following c code #include <stdio.h> int main() {     int n,...

    i need flowchart for the following c code #include <stdio.h> int main() {     int n, i, sum = 0;     printf("Enter an integer: ");     scanf("%d",&n);     i = 1;     while ( i <=n )     {         sum += i;         ++i;     }     printf("Sum = %d",sum);     return 0; }

  • Trace the following code and enter correct information into the working memory box. Code Memory Working...

    Trace the following code and enter correct information into the working memory box. Code Memory Working Memory #include <stdio.h> // Prototypes double factorial(int); void main() 1 int num; // Number from user double fact; // Get value from user do { printf ("Give an integer greater or equal to zero : "); scanf("%d", &num); if (num < 0) printf ("Value must be larger or equal to zero.\n"); } while (num < 0); fact = factorial (num); printf ("The factorial of...

  • #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { /* Type your code here....

    #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { /* Type your code here. */ int GetNumOfNonWSCharacters(const char usrStr[]) { int length; int i; int count = 0; char c; length=strlen(usrStr); for (i = 0; i < length; i++) { c=usrStr[i]; if ( c!=' ' ) { count++; } }    return count; } int GetNumOfWords(const char usrStr[]) { int counted = 0; // result // state: const char* it = usrStr; int inword = 0; do switch(*it)...

  • Consider the following C codes to compute the gcd of two integers. /// code 1 #include...

    Consider the following C codes to compute the gcd of two integers. /// code 1 #include <stdio.h> int gcd(int a, int b) {     while (a != b) {         if (a > b) a = a - b;         else b = b - a;     }     return a; } /// code 2 #include <stdio.h> int getint() {     int i;     scanf("%d", &i);     return i; } void putint(int i) {     printf("%d\n", i); } int main()...

  • Please explain how these code run for each line #include<stdio.h> #include<string.h> /** * Part A */...

    Please explain how these code run for each line #include<stdio.h> #include<string.h> /** * Part A */ struct myWord{    char Word[21];    int Length; }; int tokenizeLine(char line[], struct myWord wordList[]); void printList(struct myWord wordList[], int size); void sortList(struct myWord wordList[], int size); /** * main function */ int main() {    struct myWord wordList[20];    char line[100];    printf("Enter an English Sentence:\n");    gets(line);    int size = tokenizeLine(line, wordList);    printf("\n");    printf("Unsorted word list.\n");    printList(wordList, size);...

  • This code in C converts infix to postfix and evaluates it. The problem is that it...

    This code in C converts infix to postfix and evaluates it. The problem is that it only evaluates one digit expressions. I need to fix it so that it can evaluate 2 digits expressions as well. #include <stdio.h> #include <ctype.h> #include <string.h> #include <math.h> #define SIZE 100 char s[SIZE]; int top=-1; void infixToPostfix(char *infix, char *postfix); void postfixEvaluation(char *postfix); void push(char elem){ s[++top]=elem; } char pop(){ return(s[top--]); } int pr(char elem){ // Order of precedence switch (elem) { case '(':...

  • Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int...

    Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int *); void replaceAt(int *, int, int); int isEmpty(int *, int); int isFull(int *, int); void removeAt(int *, int); void printList(int *, int); int main() { int *a; int arraySize=0,l=0,loc=0; int choice; while(1) { printf("\n Main Menu"); printf("\n 1.Create list\n 2.Insert element at particular position\n 3.Delete list.\n4. Remove an element at given position \n 5.Replace an element at given position\n 6. Check the size of...

  • please help me asap 1 Determine the output for the following code: #include<stdio.h> int main static...

    please help me asap 1 Determine the output for the following code: #include<stdio.h> int main static int num; fornum : ++ num: num++) -- printf("%d". num): if num ==8) break; [10] COI retum 1 b) #include<stdio.h> int main int test: fortest - 0: tests=5: test); printer test: retum

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT