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; }

  • Need help, i can't get a infinite loop to work, that terminates when a blank line...

    Need help, i can't get a infinite loop to work, that terminates when a blank line is inputted for the firstName. note: C programing langauge and using microsoft visual studios . #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> #include <math.h> #include <stdbool.h> int main() {    //int arraySalaries[50];    int calculate_avg = 0;    //int calculate_max;    //int calculate_min;    int salary[50];    //int length;    //int average_salary;    char firstName[50][50];    char lastName[50][50];    int i...

  • Hello, I need help with the following C code. This program asks the user to enter...

    Hello, I need help with the following C code. This program asks the user to enter three numbers and outputs the sum on the screen , all im trying to do is have the program reprompt the user for a number if anything other than a number is entered for each of the entries. I wanted to use do while for each of the functions but that did not work for example: Enter Number 1: 2 Enter Number 2: Hello...

  • 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)...

  • I'm having trouble getting my program to print shapes For example: Which shape (L-line, T-triangle, R-rectangle):...

    I'm having trouble getting my program to print shapes For example: Which shape (L-line, T-triangle, R-rectangle): L Enter an integer length between 1 and 25: 13 ************* Which shape (L-line, T-triangle, R-rectangle): t Enter an integer base length between 3 and 25: 5 * ** *** **** ***** Which shape (L-line, T-triangle, R-rectangle): r Enter an integer width and height between 2 and 25: 4 5 **** **** **** **** **** Here's my code: #include <stdio.h> #include <ctype.h> const int...

  • 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);...

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