Question
please edit the code so it prints only the numbers from assending order once. For example, instead of printing 5 3 4 3 4 5,
please edit the code so it prints only the numbers from assending order once. For example, instead of printing 5 3 4 3 4 5, it just prints 3 4 5. So, just the numbers entered by user in assending order once.
#include
int main() { int a[30]; //declares int array 'a' of size 30 int x,y,lastx,num; //declares int variables lastx=0; // lasti to '0' //scans the first number printf("Enter a number "); scanf("%d",&a[0]); //reads int value array 'a' at index 0 from user while (1) //infiite while loop //scans another number { printf("Enter another Number "); scanf("%d",&num); //reads int value into variable num from user for(x=0;x<=lastx;x=x+1) //outer for loop { printf("%d ",a[x]); //prints value at index i from array 'a' initially at index 0 if(a[x] > num) //checks if the num that we eneterd is greater than i { for(y=lastx; y>= x; y--) // inner for loop to sort the array 'a' in assending order { a[y+1]=a[y]; /* 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[x]=num; // a[i] is moved to a[i+1] now a[i] will be replaced with the value of num lastx++; //increments lasti value by 1 break; //exits from outer loop } } /* if the value of num is greater than all the values in array 'a', then this condition gets satisfied and num value will be inserted into array 'a' at end of current values available in 'a' */ if(x>lastx) { a[x]=num; lastx++; } for(x=0;x<=lastx;x++) // will print all the values of array 'a' and while loop will continue { printf(" %d ",a[x]); } } //while loop end } //end of main
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started