Thread: Have a question on my program

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    15

    Have a question on my program

    I am suppose to

    "Create a program to read an array of characters from the keyboard terminated by EOF. Count the number of characters that are entered. The characters should be stored into an array of integers. Display the array of characters as characters. Call shellsort (pp 62) to sort the array and display the sorted array."

    Here is the thing I almost have it all done but I am trying to print out the letters for each character it stores in the array and then sort the array and display it. The problem is that I just tested the while loop without the shellsort program and it will not show the letters I store in the array.

    Am I just not thinking my logic is correct or am I naming a variable wrong?

    Also am I using the wrong names for the variables in the shellsort? I am getting a "error: expected expression before âintâ" and a " error: too few arguments to function âshellsortâ"

    Here is my code I hope someone can point me in the right direction im so confused.
    Code:
    #include <stdio.h>
    
     
    void shellsort(int v[], int n);
    
    main()
    {
        char str[100]="abcdefgh";
        int num[100];
        int v[100];
        int i, c, n;
        i=0;
        
        printf("%s\n", str);
        
        while ((c = getchar()) != EOF)
        {
            num[i++] = c;
                for (i=c; i != EOF; i++)
                    printf("%c", num[i]);
        }
    
        shellsort(int v[i], int n);
        printf("%c", v[i]);
        
    }
    
    void shellsort(int v[], int n)
    {
        int gap, i, j, temp;
        for (gap = n/2; gap > 0; gap /= 2)
            for (i = gap; i < n; i++)
                for (j=i-gap; j>=0 && v[j]>v[j+gap]; j-=gap)
                {
                    temp = v[j];
                    v[j] = v[j+gap];
                    v[j+gap] = temp;
                }
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You've made your call for shellsort(address of array, n), into a re-prototyping of the shellsort function.

    Your call should NOT be the same as the prototype for shellsort, which is above main(). Remove the "int" from your call to shellsort.

    If you put your error codes into English, I can understand them a lot better!

    You may have other errors, but that's the first one I saw. Fix that and see how it goes.

    And welcome to the forum Zach - thanks for using code tags.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    15
    Now it says with the int taken out of the shellsort call " warning: passing argument 1 of âshellsortâ makes pointer from integer without a cast" so confused also I tried this code and it debugs fine, the problem is that after it displays my original string it just waits there instead of doing the rest of the program

    Code:
    #include <stdio.h>
    
    void shellsort(int v[], int n);
    
    main()
    {
        char str[100]="abcdefgh";
        int num[100];
        int v[100];
        int i, c, n;
        i=0;
        
        printf("%s\n", str);
        
        while ((c = getchar()) != EOF && c !='\0')
        {
            num[i++] = c;
                for (i=c; i != EOF; i++)
                    printf("%c", num[i]);            
        }
        
        shellsort(num,4);
        printf("%c", num);
    
    }
    
    void shellsort(int v[], int n)
    {
        int gap, i, j, temp;
        for (gap = n/2; gap > 0; gap /= 2)
            for (i = gap; i < n; i++)
                for (j=i-gap; j>=0 && v[j]>v[j+gap]; j-=gap)
                {
                    temp = v[j];
                    v[j] = v[j+gap];
                    v[j+gap] = temp;
                }
    }

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    On the printf() call, you're trying to print num (which is an array, as a char %c). That's a problem.

    Code:
    for (i=c; ...
    Is that a c right there, instead of a zero?

    c is an int, and likely to have a value totally unrelated to what you want. Try zero instead.

    And you're program is waiting for i to equal EOF. Is that possible?
    Code:
    ... i != EOF; ...
    What COULD equal EOF, besides i?
    Last edited by Adak; 10-12-2011 at 01:20 AM.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    15
    I fixed my code just wanted to post the correct code only took me another 4 hours of debugging and checking -_-;; Cheers to the next lucky guy who needs it! >.<

    Code:
    #include <stdio.h>
    
    int v[100]; 
    void shellsort(int v[], int n);
    
    main()
    {
            int num[100];
            int count = 0;
            int i, c;
            i=0;
    
            printf("\nEnter the characters: ");
    
            while ((c = getchar()) != EOF)
            
                    num[count++] = c;
                            
            
    
            printf("\nArray before sorting: ");
            for (i=0; i < count ; i++)
                 printf("%c", num[i]);
    
            shellsort(num, count);
    
            printf("\nArray after sorting: ");
            for (i=0; i < count ; i++)
                 printf("%c", num[i]);
          printf("\n");
    
    }
    
    void shellsort(int v[], int n)
    {
            int gap, i, j, temp;
            for (gap = n/2; gap > 0; gap /= 2)
                    for (i = gap; i < n; i++)
                            for (j=i-gap; j>=0 && v[j]>v[j+gap]; j-=gap)
                            {
                                    temp = v[j];
                                    v[j] = v[j+gap];
                                    v[j+gap] = temp;
                            }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question on C program
    By tekkiram in forum C Programming
    Replies: 6
    Last Post: 04-19-2010, 06:45 AM
  2. Question regarding a this C program
    By cnb in forum C Programming
    Replies: 10
    Last Post: 10-11-2008, 04:43 AM
  3. Tic Tac Toe Program Question
    By Unknowntoyou000 in forum C++ Programming
    Replies: 24
    Last Post: 04-10-2008, 08:55 PM
  4. Help to program for this question
    By khamarutheen in forum C Programming
    Replies: 1
    Last Post: 01-19-2006, 01:34 AM
  5. Program question--need help!!!
    By Jedijacob in forum C Programming
    Replies: 5
    Last Post: 11-30-2004, 12:07 PM