Time limit exceeded in bubble sort [closed] - arrays

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Problem-To sort array in ascending order
Algorithm used-Bubble sort
Error-Time limit exceeded
Compiler-ideone online editor /Codeblocks
What could be the possible alternative for this?
int a[5];
int i,t,j;
for(i=0;i<=4;i++) //for initialising the elements
{
printf("Enter 5 numbers");
scanf("%d",&a[i]);
}
for(j=0;j<5;i++) //for sorting
{
for(i=0;i<5;i++)
{
if(a[i]>a[i+1])
{
t=a[i+1];
a[i+1]=a[i];
a[i]=t;
}
}
}
for(i=0;i<=4;i++) //for printing the sorted array
{
printf("%d\n",a[i]);
}

Your loop:
for(j=0;j<5;i++) //for sorting
should say j++, so it should be
for(j=0;j<5;j++)
Your second loop:
for(i=0;i<5;i++)
should be
for(i=0;i<4;i++)

Related

Sum of numbers occurring in the multiplication table of 8. What's wrong with my code? Desirable output is 440, and I'm getting 32303304 [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 months ago.
Improve this question
#include<stdio.h>
int main(){
int num,i,sum=0;
printf("Enter the number:");
scanf("%d",&num);
for(i=1; i<=10; i++)
{
num = num*i;
sum += num;
}
printf("%d",sum);
return 0;
}
this is the code i have written and i don't know why this is not working?
you should not change the num which is 8 in your case.
do it like this:
#include<stdio.h>
int main(){
int num,i,sum=0;
printf("Enter the number:");
scanf("%d",&num);
for(i=1; i<=10; i++)
{
sum += num*i;
}
printf("%d",sum);
return 0;
}
You’re changing num each time through the loop, so you’re not actually adding the multiples of the input you think you are. Add some logging or run it in a debugger for good visibility into the problem.

Program to sort numbers not in descending order [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have written a program to sort numbers in ascending order.
I successfully compiled and executed it, but the problem is that at first 'for' loop its taking one more input than than the entered.
Suppose if I enter a value of t equal to 5 , then below loop takes 6 inputs.
for(i=0;i<t;i++){
scanf("%d\n",&a[i]);
}
I debugged this code, and observed that i=0 is taking 2 inputs, but how do I solve it.
Code
#include<stdio.h>
int main()
{
int t,i;
printf("Enter the number of numbers:");
scanf("%d",&t);
int a[t],temp;
printf("Enter the numbers\n");
for(i=0;i<t;i++)
{
scanf("%d\n",&a[i]);
}
for(i=0;i<t-1;i++)
{
for(int j=i+1;j<t;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("Sorted\n");
for(int k=0;k<t;k++)
{
printf("%d\n",a[k]);
}
}
scanf syntax for integer is as follows:
int i;
scanf("%d", &i);
It does not need \n like in printf
Change your for loop into this
for(i=0; i<t; i++)
{
scanf("%d", &a[i]);
}
Actually when you are printing into your screen the symbol "\n" , it means that the outputstream takes a command to change line. This is being used only in the printing process. When you are trying to input values via scanf its actually useless and problematic thus breaking your code. So you have to write:
scanf(" %d", &a[i]);

What's wrong with my first 'c' bubble sort programme? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I just want to know what is the wrong with it, and how to fix it.
It looks like every thing is right, but when you run the .exe it crash every time
#include <stdio.h>
#include <stdlib.h>
// first list filling function:
void T_filling(int T[],int n){
int i;
for(i=1 ;i<=n ;i++){
printf("enter the number:",i+1);
scanf("%d",&T[i]);
}
}
//then the main algorithm:
int main()
{
int j,k,l;
int n,x;
// you can order up to 100 integer number
int T[100];
printf("This program is to order numbers decreasingly\n");
printf("how many numbers you want to order?\n");
// scanning the number of elements in the list
scanf(n);
//filling the list
T_filling(T[100],n);
//bubble sort Algorithm
for(j=1;j<=n-1;j++){
for(k=1;k<=n-j;k++){
if(T[k+1]>T[k]){
x=T[k];
T[k]=T[k+1];
T[k+1]=x;
}
}
}
for(l=1;l<=n;l++){
//printing the result on screen
printf("%d;",T[l]);
}
printf("\n");
system("pause");
return 0;
}
You are not using scanf and passing the array to function properly.
Please modify these lines in your code as follows and your program will work as expected.
scanf("%d", &n);
//filling the list
while(n > 100)
{
printf("Exceeding size, please re enter the size");
scanf("%d", &n);
}
T_filling(T,n);
Hope this helps.

Ask user to enter an integer value for each element of the array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
• Declare an array type of int and size of 5
• Ask user to enter an integer value for each element of the array
• Then display every element of the array user entered
please help me where to start i don't know much about array.
One advice, if you don't learn for yourself, then nobody can teach you. So, please try researching a bit on the internet, or reading a good book before you post a question.
Assuming this is your first mistake, and you wont repeat this without working a little on your own, i'll provide the code and the explanation..
#include<stdio.h>
int main(void)
{
int arr[5], i;
printf("please enter the numbers one by one");
for(i = 0; i < 5; i++)
{
scanf("%d", &arr[i]);
}
printf("The numbers you entered are\n");
for(i = 0; i < 5; i++)
{
printf("%d ", arr[i]);
}
}

I am getting garbage values for this prob in c [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am trying to execute the code below, but along with my answer I am getting some garbage values. Please help me find where I made an error.
int main()
{
int n,i,j,k=0;
int a[100];
printf("Enter a number:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(n%i==0)
{
a[++k]=i;
}
}
for(j=0;a[j]!='\0';j++)
{
printf("\t%d",a[j]);
}
}
int a[100];
C does not initialize the array elements by default. So all the elements that are not assigned in your first loop will have garbage.
What you can do is:
int a[100] = {0};
This will initialize all elements to 0
In C array indexing starts from 0. Preincrement ++k will cause to start array indexing from 1. Change it to k++. Also change
for(j=0;a[j]!='\0';j++)
to
for(j=0;j < k;j++)
to print the only values you have entered.
your code should be
for(i=1;i<=n;i++)
{
if(n%i==0)
{
a[k++]=i;
}
}
for(j=0; j < k;j++)
{
printf("\t%d",a[j]);
}
Here how you can fix this. Next time please describe what you were trying to achive

Resources