0

At first i used bubble sort to sort some numbers.It worked.And then again i tried to sort using bubble sort with 2 function with same logic.but it is not working.I am new to work with 2/3 function.So can anyone help me to find the problem in my logic?

First source

#include<stdio.h>
main()
{
    int i,j;
    float num[5], c;

    for(i=0;i<5;i++)
    {
        scanf("%f",&num[i]);
    }
    for(j=0;j<4;j++)
    {
        for(i=0;i<5;i++)
        {
            if(num[i]<num[i+1])
            {
                c=num[i];
                num[i]=num[i+1];
                num[i+1]=c;
            }
        }
    }
    for(i=0;i<5;i++)
    {
        printf("%.2f\n",num[i]);
    }
}

Second source w/function

#include<stdio.h>
void sort(int a[]);
void main()
{
    int i;
    double a[3], A, B, C;

    for(i=0;i<3;i++)
    {
        scanf("%lf",&a[i]);
    }

    sort(a);

    printf("In The Function\n");
    for(i=0;i<3;i++)
    {
        printf("%.2lf\n",a[i]);
    }

    return;
}

void sort(int a[])
{
    int i, n;
    double temp;

    for(n=0;n<2;n++)
    {
        for(i=0;i<3;i++)
        {
            if(a[i]<a[i+1])
            {
                temp = a[i];
                a[i] = a[i+1];
                a[i+1] = temp;
            }
        }
    }
    return;
}
5
  • 4
    Please NO external links for code. Paste it here as text and mark it as code. Commented Aug 18, 2018 at 7:22
  • 1
    Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: minimal reproducible example Commented Aug 18, 2018 at 7:37
  • 1
    ... find the problem in my logic? Check your array indexes, they could be out of bounds. Commented Aug 18, 2018 at 7:44
  • Please read & act on hits googing 'stackexchange homework'. Show that your program calculates what you expect it to as it goes through (sub)expressions--including that arguments passed to each function/opertaor meet its requirements--by saying what that is & showing what it actually does via output. Commented Aug 18, 2018 at 7:51
  • 1
    Beyond the data type problem that kiran points out below, both source lists suffer from invoking undefined behavior by accessing their array boundaries beyond their declared dimensions. You probably want to fix that too. Commented Aug 18, 2018 at 7:54

2 Answers 2

1

Do not ignore the warnings.

warning: passing argument 1 of ‘sort’ from incompatible pointer type [enabled by default]
     sort(a);
     ^
sort.c:2:6: note: expected ‘int *’ but argument is of type ‘double *’
 void sort(int a[]);
  ^

You have declared a as double but your function receives a as int. Change it to as below.

void sort(double a[]);
Sign up to request clarification or add additional context in comments.

4 Comments

sorry i just started working with c.and haven't learnt yet pointer much.so ignored it
@mahinhossen Compiler warnings are nearly always a sign you're doing something wrong, even for seasoned professional. For a beginner working on the tasks beginners work on, you can be assured warnings are a guarantee you're doing something wrong, and should be treated as errors. Don't ignore them; discover what they mean and fix them.
thanks i found it.i didn't used a as double,that was the mistake.thanks #kiran biradar
@mahinhossen That isn't the only mistake. Your indexing is wrong as well. your inner loops are breaching the declared boundaries of your array.
0

You are passing the double

double a;
sort(a);

value in the main function but at the function declaration part your datatype is integer

void sort(int a[])

correct this to

void sort(double a[])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.