error(logic or run time error) in my program plz help!

Discussion in 'C Programming' started by iskeletor, Dec 25, 2006.

  1. iskeletor

    iskeletor Guest

    i want to have integers from user and take them into a array then
    sorting them but where do i make mistake?thanx from now

    #include <stdio.h>
    #include <stdlib.h>

    int number=0,count=0,numberArray[100];
    int main (){
    int a;
    Sort(numberArray);
    for(a=0;a<count;a++){
    printf("%ld\t",numberArray[a]);
    }
    return 0;
    }
    int Sort(int array[]){
    int temp,z,t;
    printf("Enter a positive integer(To exit enter -1)\n");
    scanf("%ld",&number);
    while(number!=-1){
    numberArray[count]=number;
    //printf("numberArray[%ld]=%ld",count,numberArray[count]);
    count++;
    printf("Enter a positive integer(To exit enter -1)\n");
    scanf("%ld",&number);
    printf("%d\n",count);
    for(t=0;t<count;++t){
    for(z=t+1;t<count+1;++z){
    if(array[t]>array[z]){
    temp=array[t];
    array[t]=array[z];
    array[z]=temp;
    }
    }
    }
    }//while loop ends
    }
    iskeletor, Dec 25, 2006
    #1
    1. Advertising

  2. iskeletor wrote:
    > i want to have integers from user and take them into a array then
    > sorting them but where do i make mistake?thanx from now


    It is useful to explain what goes wrong when you run
    your programme.

    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > int number=0,count=0,numberArray[100];
    > int main (){
    > int a;
    > Sort(numberArray);


    You are calling a function which you have not declared or
    defined up to this point in the programme.

    > for(a=0;a<count;a++){
    > printf("%ld\t",numberArray[a]);


    numberArray[a] is an int not a long int therefore the
    format to use is %d not %ld. You repeat a similar mistake
    at several places either with printf or with scanf.

    > }
    > return 0;
    > }
    > int Sort(int array[]){
    > int temp,z,t;
    > printf("Enter a positive integer(To exit enter -1)\n");
    > scanf("%ld",&number);
    > while(number!=-1){
    > numberArray[count]=number;
    > //printf("numberArray[%ld]=%ld",count,numberArray[count]);
    > count++;
    > printf("Enter a positive integer(To exit enter -1)\n");
    > scanf("%ld",&number);
    > printf("%d\n",count);


    You don't put the number which has just been read
    into the array before you do the sorting.

    > for(t=0;t<count;++t){
    > for(z=t+1;t<count+1;++z){


    Is the condition meant to be z<count+1 ? Even so
    it's wrong because you have not put any useful value
    at numberArray[count].

    > if(array[t]>array[z]){
    > temp=array[t];
    > array[t]=array[z];
    > array[z]=temp;
    > }
    > }
    > }
    > }//while loop ends
    > }


    You have defined Sort as returning int but it doesn't
    actually return anything.
    Spiros Bousbouras, Dec 25, 2006
    #2
    1. Advertising

  3. iskeletor

    the_init Guest

    iskeletor wrote:
    > i want to have integers from user and take them into a array then
    > sorting them but where do i make mistake?thanx from now
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > int number=0,count=0,numberArray[100];
    > int main (){
    > int a;
    > Sort(numberArray);
    > for(a=0;a<count;a++){
    > printf("%ld\t",numberArray[a]);
    > }
    > return 0;
    > }
    > int Sort(int array[]){
    > int temp,z,t;
    > printf("Enter a positive integer(To exit enter -1)\n");
    > scanf("%ld",&number);
    > while(number!=-1){
    > numberArray[count]=number;
    > //printf("numberArray[%ld]=%ld",count,numberArray[count]);
    > count++;
    > printf("Enter a positive integer(To exit enter -1)\n");
    > scanf("%ld",&number);
    > printf("%d\n",count);
    > for(t=0;t<count;++t){
    > for(z=t+1;t<count+1;++z){
    > if(array[t]>array[z]){
    > temp=array[t];
    > array[t]=array[z];
    > array[z]=temp;
    > }
    > }
    > }
    > }//while loop ends
    > }




    Try this


    #include <stdio.h>
    #include <stdlib.h>

    int number=1,count=0,numberArray[100];

    void Sort(int array[]);



    int main ()
    {
    int a;
    Sort(numberArray);

    for(a=0;a<count;a++)
    {
    printf("%ld\t",numberArray[a]);
    }
    return 0;
    }



    void Sort(int array[])
    {
    int temp,z,t;

    while(1)
    {
    printf("Enter a positive integer(To exit enter -1)\n");
    scanf("%ld",&number);
    if(number!=-1)
    {
    numberArray[count]=number;
    count++;
    }
    else
    {
    for(t=0;t<count;++t)
    {
    for(z=t+1; z<count-1; ++z)
    {
    if(array[t]>array[z])
    {
    temp=array[t];
    array[t]=array[z];
    array[z]=temp;
    }
    }
    }
    return;
    }


    }//while loop ends
    }
    the_init, Dec 26, 2006
    #3
  4. On 25 Dec 2006 13:44:50 -0800, "iskeletor" <>
    wrote:

    >i want to have integers from user and take them into a array then
    >sorting them but where do i make mistake?thanx from now
    >
    >#include <stdio.h>
    >#include <stdlib.h>
    >
    >int number=0,count=0,numberArray[100];
    > int main (){
    > int a;
    > Sort(numberArray);


    At this point, Sort is an unknown function. You should either code a
    prototype for it (right after the #include directives) or place the
    code for main after the code for Sort.

    > for(a=0;a<count;a++){
    > printf("%ld\t",numberArray[a]);


    %ld is not suitable for an int in printf.

    > }
    > return 0;
    >}
    > int Sort(int array[]){


    You have confused yourself by having the Sort function also perform
    the input. It would be better to separate the two tasks in different
    functions.

    > int temp,z,t;
    > printf("Enter a positive integer(To exit enter -1)\n");
    > scanf("%ld",&number);


    %ld is not suitable for an int in scanf either.

    > while(number!=-1){
    > numberArray[count]=number;
    > //printf("numberArray[%ld]=%ld",count,numberArray[count]);


    // comments can wrap when posted, preventing others from compiling
    your code.

    > count++;


    After the first number, count will be 1.

    > printf("Enter a positive integer(To exit enter -1)\n");
    > scanf("%ld",&number);


    You probably meant to terminate the while loop at this point. You
    certainly don't want to sort the array after each number is input.
    This is an example of how consistent indenting can really help you.

    > printf("%d\n",count);
    > for(t=0;t<count;++t){


    t is 0, count is 1.

    > for(z=t+1;t<count+1;++z){


    t does not change in this loop so the loop will never end. Did you
    mean z<count+1? As written, z will be 1.

    You might also consider a little horizontal white space to make things
    easier to read.

    > if(array[t]>array[z]){


    array[1] contains 0 (it was initialized because the array is at file
    scope. It does not contain a number input to the program by the user.
    You are sorting numbers you don't have yet.

    > temp=array[t];
    > array[t]=array[z];
    > array[z]=temp;
    > }
    > }
    > }
    > }//while loop ends


    Too late.

    >}



    Remove del for email
    Barry Schwarz, Dec 26, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    353
  2. Replies:
    2
    Views:
    310
    Nick Keighley
    Nov 24, 2006
  3. Replies:
    1
    Views:
    321
  4. pitamber kumar

    plz send logic to write given program.

    pitamber kumar, Mar 2, 2008, in forum: C Programming
    Replies:
    21
    Views:
    670
  5. nocturnal
    Replies:
    1
    Views:
    498
    nocturnal
    Jul 10, 2009
Loading...

Share This Page