C Programming, Sorting array - c

What am I doing wrong? the program run but the output isn't in the right order.
#include <stdio.h>
int main () { /*program to ask user to input array values and sort them in ascending order.*/
int n,j,i,temp;
printf ("how many numbers?\n");
scanf ("%d",&n);
int a[n];
for (i=0;i<n;i++){
printf ("enter number");
scanf ("%d",&a[i]);
}
for (i=0;i<n;i++); {
for (j=i+1;j<n;j++){
if (a[i]>a[j]){
temp=a[i];
a[i]=a[j];
a[j]=temp; }
} }
printf("The numbers arranged in ascending order are given below \n");
for (i = 0; i < n; ++i)
printf("%d\n", a[i]);
}

Change
for (i=0;i<n;i++); {
To
for (i=0;i<n-1;i++) {
Semicolon was executed instead of following błock {}

Element form right/end cant propagate to left, because for example first element of array can be swapped only at first check with second element.

Related

Why is only the first element of the output wrong?

I have recently started practicing on C. The output is reversed except for first element. Can anyone tell me what is wrong here?
I also tried using for loop but that is displaying the array as it is.
#include<stdio.h>
#include<conio.h>
int main()
{
int temp;
int i,n;
printf("Enter value of n:");
scanf("%d",&n);
int array1[n];
int k=0,j=n;
printf("Enter the five numbers\n");
for(i=0;i<n;i++)
{
printf("Enter the number %d:",i+1);
scanf("%d",&array1[i]);
}
printf("The array is:\n");
for(i=0;i<n;i++)
{
printf("%d ",array1[i]);
}
while(k<j)
{
temp=array1[k];
array1[k]=array1[j];
array1[j]=temp;
j--;
k++;
}
printf("\nThe array after reversing is:\n");
for(i=0;i<n;i++)
{
printf("%d ",array1[i]);
}
}
You are assigning array1[k] to array1[j] and you initialized j to n, so it swaps the value in array1[k] with array1[j] => in the first iteration, the value j = n, however the index of the final element is n-1. (indexes are 0,1,2,3,4,...n-1). So it tries to get the value of position n, where there is actually no value stored ( it is outside the array ). Change j to n-1 in int k=0,j=n;

In C language why my selection-sort code doesn't work

I wrote a selection-sort algorithm but didn't work. I cannot find my wrong. Anyone can help me ?
#include <stdio.h>
int main () {
int array[100],i,j,position,size,swap;
printf("Enter number of integers\n");
scanf("%d",&size);
printf("Enter %d integers\n",size);
for(i=0;i<size;i++){
scanf("%d",&array[i]);
}
for(i=0;i<size;i++){
position=i;
for(j=i;j<size;j++){
if(array[position]>array[j]){
position=j;
}
if(position!=i){
swap=array[i];
array[i]=array[position];
array[position]=swap;
}
}
}
printf("Sorted list in ascending order\n");
for(i=0;i<size;i++){
printf("%d\n",array[i]);
}
return 0;
}
The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from the unsorted part and putting it at the beginning.
So the swap needs to happen after you have found the correct position i.e. happening after your for loop is over. So if you switch the if block after the j loop ends, the code should work.
for(i=0;i<size;i++){
position=i;
for(j=i;j<size;j++){
if(array[position]>array[j]){
position=j;
}
}
if(position!=i){
swap=array[i];
array[i]=array[position];
array[position]=swap;
}
}

Problem in printing inverse array of an input array

#include <stdio.h>
#include <conio.h>
void main()
{
int arr[5], new[5], i, j;
printf("ENTER ANY FIVE NUMBERS:");
scanf("%d%d%d%d%d", &arr[0], &arr[1], &arr[2], &arr[3], &arr[4]);
for(i=0; i<5; i++)
{
for(j=5; j>=0 ;--j)
{
new[i] = arr[j];
printf("%d", new[i]);
printf(" ");
}
}
getch();
}
The above code is of a simple problem, which asks to take inputs of numbers in an array and show the inverse array of the input. I tried to solve it myself and wrote the above code. But the compiler is showing the result multiple times, I mean, the result should have only 5 numbers but the output shows series of numbers.
Here is one problem:
for(j=5;j>=0;--j){
new[i]=arr[j];
^ out of bounds access to arr[5]
Change it to
for(j=4;j>=0;--j){ // 4 instead of 5
new[i]=arr[j];
That said, if all you want is to print an array in reverse order, simply do:
for(j=4;j>=0;--j){
printf("%d", arr[j]);
printf(" ");
}
printf("\n");
No need for two loops and no need for the extra array new
If you really want a "reversed copy" do:
for(j=4;j>=0;--j){
new[4-j] = arr[j];
printf("%d", arr[j]); // or printf("%d", new[4-j]);
printf(" ");
}
printf("\n");

C Bubble Sort algorithm

I have the following code which sorts the strings using bubble sort logic. The part that I am confused about is the for loop which Im not sure why i is set to one and why J is iterated until it is less than n-j:
#include <stdio.h>
#include <string.h>
void main()
{
char name[25][50],temp[25];
int n, i, j;
printf("\n\nSorts the strings of an array using bubble sort :\n");
printf("-----------------------------------------------------\n");
printf("Input number of strings :");
scanf("%d",&n);
printf("Input string %d :\n",n);
for(i=0; i<=n; i++)
{
fgets(name[i], sizeof name, stdin);
}
/*Logic Bubble Sort*/
for(i=1; i<=n; i++)
{
for(j=0; j<=n-i; j++)
{
if (strcmp(name[j],name[j+1])>0)
{
strcpy(temp,name[j]);
strcpy(name[j],name[j+1]);
strcpy(name[j+1],temp);
}
}
}
printf("The strings appears after sorting :\n");
for(i=0;i<=n;i++)
printf("%s\n",name[i]);
}
There are a few subtle things wrong with this code
You are running the for loop one more than necessary.
for(i=0;i<=n;i++)
{
fgets(name[i], sizeof name, stdin);
}
The reason that is the line above scanf("%d",&n);
Here the end of line remains in the input stream and is being added to name[0]. You should change it to scanf("%d ",&n);
This will consume the end of line in the same statement. Also the for loop should now run from for(i=0;i< n;i++).
Additionally, the fgets function receives a size of name which is too large.
It should receive a size of name[0] i.e. fgets(name[i], sizeof name[0], stdin);
Now, your strings are being stored from name[0] to name[n-1] and the rest of your sort algorithm can be fixed.
for(i=0;i< n;i++){
for(j=0;j< n-i;j++)
{
if(strcmp(name[j],name[j+1])>0)
{
strcpy(temp,name[j]);
strcpy(name[j],name[j+1]);
strcpy(name[j+1],temp);
}
}
}
printf("The strings appears after sorting :\n");
for(i=0;i< n;i++)
printf("%s",name[i]);
The reason for the inner for loop going from 0 to n-i is that at the end of 1 outer loop the largest element is in names[n-1]
After 2 outer loops, 2 elements are sorted [n-2] and [n-1]. So there is no need to check these elements.
And so on.

Segmentation fault in my program

My sorting program results in a "segmentation fault 11":
#include <stdio.h>
int main()
{
// Asking user for number of inputs in an array
int n;
do {
printf ("enter the number of intigers you want to sort\n");
scanf("%d",&n);
}while (n<=1);
int sort [n];
printf ("please enter %d numbers\n",n);
for (int i=0; i<n; i++) {
scanf("%d",&sort[i]);
}
printf("you entered\n ");
for (int i=0; i<n; i++) {
printf(" %d ",sort[i]);
}
printf("\n");
int k,c,i,x;
for (i=0;i<n;i++) {
if (sort[i]<sort[i-1]){
k=i-2;
while (sort[k]>sort[i]){
k--;
}
k++;
x =sort[i];
c=i;
for (c=i;c>k;c++){
sort[c-1]=sort[c];
}
sort[k]=x;
}
}
printf ("Sorted numbers :-\n");
for (int i=0; i<n; i++) {
printf ("%d ",sort[i]);
}
printf ("\n");
return 0;
}
Now I have looked up the internet and found that it is caused because a variable has value that exceeds the system's memory limit. But cannot understand that concept.
for (i=0;i<n;i++)
{
if (sort[i]<sort[i-1])
You are accessing array out of its bounds.Maybe you want to start your loop from 1.Also
k=i-2;
while (sort[k]>sort[i])
will access index beyond 0 for example if i is 0 or 1 or 2
k = i - 2; looks unstable for low values of i.
sort[i - 1] is undefined when i is zero.
The thing causes the behaviour of sort[k] to be undefined.
Moral of the story: check all the array indexes before attempting to retrieve an array element. Your debugger will help you here.
In addition to all said before:
c=i;
for (c=i;c>k;c++){
sort[c-1]=sort[c];
}
sort[k]=x;
i can be 0 -> c can be 0 -> sort[c-1] would also result in accessing array out of bounds.
There is no need to initialize c=i twice. It is enough to do it in the loop-declaration

Resources