Function not working on c program selection sort - c

Why this function is not working here? It's not sorting on output.
Suppose if I enter 1 4 2 the output is always 1 4 2 not 1 2 4.
How can properly implement this selection sort?
Thanks in advance!!
#include <stdio.h>
int selection_sort (int a[],int n, int i, int j,int temp,int min){
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++){
min=i;
for(j=i+1;j<n;j++){
if(a[j]<a[min]){
min=j;
}
}
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
}
int main(){
int i, j, n,a[20], temp,min;
printf("How many elements:\n ");
scanf("%d",&n);
printf("Enter array elements:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Sorted array: ");
for(i=0;i<n;i++)
printf(" %d",a[i]);
return 0;
selection_sort(i,j,n,a,temp,min);
}

There are 3 main issues in your code:
You are getting user input twice. One in main function:
printf("Enter array elements:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
Another in selection_sort function:
for(i=0;i<n;i++)
scanf("%d",&a[i]);
Fix this by removing the one in selection_sort function.
As noted in the comments section, you are returning from main function before calling selection_sort function:
return 0;
Fix this by moving it to the end of main function.
You are printing the expected results from selection_sort function before calling it:
printf("Sorted array: ");
for(i=0;i<n;i++)
printf(" %d",a[i]);
...
selection_sort(i,j,n,a,temp,min);
Fix this by calling selection_sort before printing its results.
Here is the fixed code:
#include <stdio.h>
int selection_sort (int a[], int n, int i, int j, int temp, int min)
{
for(i=0;i<n;i++) {
min=i;
for(j=i+1;j<n;j++) {
if(a[j]<a[min]) {
min=j;
}
}
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
}
int main()
{
int i, j, n,a[20], temp, min;
printf("How many elements: ");
scanf("%d",&n);
printf("Enter array elements: ");
for(i=0;i<n;i++) {
scanf("%d",&a[i]);
}
selection_sort(a, n, i, j, temp, min);
printf("Sorted array:");
for(i=0;i<n;i++) {
printf(" %d", a[i]);
}
printf("\n");
return 0;
}
Here is a test:
$ gcc main.c && ./a.out
How many elements: 3
Enter array elements: 1 4 2
Sorted array: 1 2 4

int main(){
int i, j, n,a[20], temp,min;
printf("How many elements:\n ");
scanf("%d",&n);
printf("Enter array elements:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
selection_sort(a,n,i,temp,min);//Not need to use j,cause j isn't used in your function
for(i=0;i<n;i++)
printf("%d\t",a[i]);//You should call your function before print your data,and also before "return 0"
return 0;
}
AND,"int i,int temp,int min" these don't need to be the parameters.You can just create them in your function

Related

How to write two functions for getting an array and display that array in c

function for get array from the user
#include <stdio.h>
void getArray()
{
printf("Enter size of array: ");
scanf("%d",&n);
printf("Enter %d elements in the array : ", n);
for(i=0;i<n;i++)
{
scanf("%d", &a[i]);
}
}
function for display array
void displayArray(){
printf("\nElements in array are: ");
for(i=0;i<n;i++)
{
printf("%d ", a[i]);
}
}
Both functions are called in the main
int main(){
int a[1000],i,n;
getArray();
displayArray();
return 0;
}
The problem is how to pass the array that we get from the user to the display array function and both functions can be called in the main and also the array want to declare in the main function
An example that does not handle input errors.
In order for your functions to have knowledge of the array, you must send them its address as well as its size.
#include <stdio.h>
int getArray(int a[], int size_max)
{
int n;
printf("Enter size of array: ");
while(1)
{
scanf("%d",&n);
if(n>size_max) printf("The size must be less than %d: ", size_max);
else break;
}
printf("Enter %d elements in the array : ", n);
for(int i=0; i<n; i++) scanf("%d", &a[i]);
return n;
}
void displayArray(int a[], int n)
{
printf("\nElements in array are: ");
for(int i=0; i<n; i++) printf("%d ", a[i]);
}
int main()
{
int a[1000];
int n = getArray(a, 1000);
displayArray(a, n);
return 0;
}
You can pass that shared variable as argument. Also return and use the returned value if reference not having valid data. Or else you need to pass this argument as reference instead of value like this.
#include <stdio.h>
int[] getArray(int a[])
{
printf("Enter size of array: ");
scanf("%d",&n);
printf("Enter %d elements in the array : ", n);
for(i=0;i<n;i++)
{
scanf("%d", &a[i]);
}
return a;
}
function for display array
void displayArray(int a[]){
printf("\nElements in array are: ");
for(i=0;i<n;i++)
{
printf("%d ", a[i]);
}
}
Both functions are called in the main
int main(){
int a[1000],i,n;
a = getArray(a);
displayArray();
return 0;
}

Chef and Dolls MISSP

Here is the link to the codechef problem - Chef and Dolls
Why does my code always print the wrong answer? The output should be the number which doesn't have a match but my code always print the first element. Coudn't get the conditions right, what should be the condition?
Problem Statement
Chef is fan of pairs and he likes all things that come in pairs. He even has a doll collection in which all dolls have paired.One day while going through his collection he found that there are odd number of dolls. Someone had stolen a doll!!!
Help chef find which type of doll is missing..
Input
The first line contains the number of test cases.
Second line of the input contains the number of elements in the array.
The next n lines are the types of each doll that is left.
Output
Find the type of doll that doesn't have a pair
Example
Input:
1
3
1
2
1
Output:
2
#include <stdio.h>
int main(){
int t, N, i, m, k, z, flag=0;
scanf("%d", &t);
while(t--)
{
scanf("%d",&N);
int arr[N];
for(i=0;i<N;i++){
scanf("%d", &arr[i]);
}
int j;
for(j=0;j<N;j++){
m=arr[j];
for(k=0;k<N;k++){
if(m==arr[k] && k!=j)
{
flag=0;
}
else
{
flag=1;
break;
}
printf("%d", m);
}
}
}
return 0;
}
You break out of your loop everytime k==j. You have to check for every array member if it is present somewhere else in the array.
#include <stdio.h>
int main()
{
int t, N, i, j, k, m, z, flag;
scanf("%d", &t);
while(t--)
{
scanf("%d",&N);
int arr[N];
for(i=0;i<N;i++)
{
scanf("%d", &arr[i]);
}
for(j=0;j<N;j++)
{
m=arr[j];
flag=1;
for(k=0;k<N;k++)
{
if(m==arr[k] && k!=j)
{
flag=0;
}
}
if(flag)
{
printf("%d\n", m);
}
}
}
return 0;
}
But be careful as this only looks if an array member has a duplicate in it. You probably want to check if there is an even number of elements in the array.
If you want to check which array members occur an odd number of times
#include <stdio.h>
int main()
{
int t, N, i, j, k, m, z, num, flag;
scanf("%d", &t);
while(t--)
{
scanf("%d",&N);
int arr[N], odds[N], n_odds;
for(i=0;i<N;i++)
{
scanf("%d", &arr[i]);
}
n_odds=0;
for(j=0;j<N;j++)
{
m=arr[j];
num=0;
for(k=0;k<N;k++)
{
if(m==arr[k])
{
num++;
}
}
if(num%2) //occurs an odd number of times
{
flag=1;
for(i=0;i<n_odds;i++)
{
if(m==odds[i]) //has already been checked
{
flag=0;
}
}
if(flag)
{
printf("%d\n", m);
odds[n_odds++]=m;
}
}
}
}
return 0;
}

Error with sorting code in C

I think there was something wrong with the algorithm.
It shows wrong answers.
I changed every line by line, but I didn't get the proper answer.
#include<stdio.h>
void sort(int a[],int n)
{
int gap,i,j,temp;
for(gap=n;gap>0;gap/=2)
{
for(i=gap;i<n;i+=1)
{
temp=a[i];
for(j=i;j>=gap&&a[j]>temp;j=gap)
a[j]=a[j];
a[j]=temp;
}
}
}
int main()
{
int a[20],i,n;
printf("Number of elements:");
scanf("%d",&n);
printf("Enter array elements:\n");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
printf("Array elements before the sort:\n");
for(i=0;i<n;++i)
printf("%d",a[i]);
sort(a,n);
printf("\nAfter sort:\n");
for(i=0;i<n;++i)
printf("%d ",a[i]);
return 0;
}
Please help me with this code.
You're right emely,There were some issues in your algorithm.
In the 6th line the gap should be n/2, not n.
And in the 12th line it should be j-gap.
After correcting those errors,I rewrote the code.
Hope it might help.
Cheers.
#include<stdio.h>
void sort(int a[],int n)
{
int gap,i,j,temp;
for(gap=n/2;gap>0;gap/=2)
{
for(i=gap;i<n;i+=1)
{
temp=a[i];
for(j=i;j>=gap&&a[j-gap]>temp;j-=gap)
a[j]=a[j-gap];
a[j]=temp;
}
}
}
int main()
{
int a[20],i,n;
printf("Number of elements:");
scanf("%d",&n);
printf("Enter array elements:\n");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
printf("Array elements before the sort:\n");
for(i=0;i<n;++i)
printf("%d",a[i]);
sort(a,n);
printf("\nAfter shell sort:\n");
for(i=0;i<n;++i)
printf("%d ",a[i]);
return 0;
}

program to print the position of the smallest number of n numbers using arrays

This code does not give me answer when i enter value 1 in array.
for eg i have taken no. of elements as 5
then i enter them as 2,3,1,6,4
then output gives 2 as smallest number and position number is not always correct
what's the error?
#include<stdio.h>
int main()
{
int n,i,a[10],sum=0;
int small=0,pos=0;
printf("enter no of elements in array:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("a[%d]=",i);
scanf("%d",&a[i]);
}
for(i=1;i<n;i++)
{
small=a[0];
if( a[i] < small)
{
small=a[i];
pos=i;
}
}
printf("smallest no:%d \n",small);
printf("position:%d",pos);
}
"small" variable is overridden with a[0] in each iteration. Just move it outside the loop:
#include<stdio.h>
int main()
{
int n,i,a[10],sum=0;
int small=0,pos=0;
printf("enter no of elements in array:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("a[%d]=",i);
scanf("%d",&a[i]);
}
small=a[0];
for(i=1;i<n;i++)
{
if( a[i] < small)
{
small=a[i];
pos=i;
}
}
printf("smallest no:%d \n",small);
printf("position:%d",pos);
}
You are not that far away - use are initializing small=a[0]; every time whenever loop iterates so just initialize small=a[0]; before loop
Here your corrected code
#include<stdio.h>
int main()
{
int n,i,a[10],sum=0;
int small=0,pos=0;
printf("enter no of elements in array:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("a[%d]=",i);
scanf("%d",&a[i]);
}
small=a[0];
for(i=1;i<n;i++)
{
if( a[i] < small)
{
small=a[i];
pos=i;
}
}
printf("smallest no:%d \n",small);
printf("position:%d",pos);
}
small = a[0] should be before the loop.
You should write small = a[0] before the beginning of a for loop.

Passing an 2d array into a function using a pointer to pointer variable in c

the method which takes in pointer to pointer as argument
int findMax(int **a, int m, int n)
{
int max=**a,i,j;
for(i=0;i<m;i++){
for(j=0;j<n;j++){
if(max<=a[i][j]){
max=a[i][j];
}
}
}
return max;
}
This is the main function from where the findMax method is called.
int main()
{
// Variable Declaration
int m,n,i,j,a[50][50],*arr[50],**arrd;
// User Input
printf("Enter the number of rows in the matrix\n");
scanf("%d",&m);
printf("Enter the number of columns in the matrix\n");
scanf("%d",&n);
printf("Enter the elements in the matrix\n");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
scanf("%d",&a[i][j]);
}
}
// Single Pointer Allocation
for(i=0;i<m;i++){
arr[i]=&a[i][0];
}
arrd=&arr[0];
// Output
printf("The matrix is\n");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("The maximum element in the matrix is %d\n",findMax(arrd,m,n));
return 0;
}
I just want to find out max element in a 2d array using a function which takes in pointer to pointer of the array.
this code works fine but i am looking for a better approach...
#include <stdio.h>
#define NUMCOLUMNS 50
#define NUMROWS 50
int findMax(int (*a)[NUMCOLUMNS], int m, int n)
{
int max=**a,i,j;
for(i=0;i<m;i++){
for(j=0;j<n;j++){
if(max<=a[i][j]){
max=a[i][j];
}
}
}
return max;
}
int main()
{
// Variable Declaration
int m,n,i,j,a[NUMROWS][NUMCOLUMNS];
// User Input
printf("Enter the number of rows in the matrix\n");
scanf("%d",&m);
printf("Enter the number of columns in the matrix\n");
scanf("%d",&n);
printf("Enter the elements in the matrix\n");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
scanf("%d",&a[i][j]);
}
}
// Output
printf("The matrix is\n");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("The maximum element in the matrix is %d\n",findMax(a,m,n));
return 0;
}

Resources