Thread: Homework help

  1. #31
    Registered User
    Join Date
    Jul 2008
    Posts
    38
    Okay, how can I tweak the input sequence of subquestion E so that I can be able to finish the program?

  2. #32
    Registered User
    Join Date
    Jul 2008
    Posts
    38
    My first solution for subquestion J:

    Code:
    #include <stdio.h>
    
    int PrintToZero (int *num, int counter);
    
    main()
    {
        int *x, y;
        x = &y;
        printf("Input number: ");
        scanf("%d",&x);
        printf("\n");
        PrintToZero(x,y);
        getch();
        return 0;
    }
    
    int PrintToZero (int *num, int counter)
    {
        for (counter=0;counter<*num;counter++)
        {
            printf("%d ",*num);
            *num = *num - 1;
        }
        return;
    }
    Theres some mistake here, but it should print like "n n-1 n-2..... 0" (if n = 10, it would read "10 9 8 7 6 5 4 3 2 1 0")

  3. #33
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The problem is in the scanf statement. Why try to read an integer into a variable that is not of type int?

  4. #34
    Registered User
    Join Date
    Jul 2008
    Posts
    38
    what is an iterative statement?

  5. #35
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by mkdl750 View Post
    what is an iterative statement?
    www.dictionary.com

  6. #36
    Registered User
    Join Date
    Jul 2008
    Posts
    38
    You should have posted the meaning here. What I'm talking about is the iterative function in C.

  7. #37
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by mkdl750 View Post
    You should have posted the meaning here. What I'm talking about is the iterative function in C.
    You say that as though it exists.

    There are functions that are iterative in nature (the opposite of iterative is recursive). But iterative is not a keyword in C, nor is it language-specific--we are using the word in its ordinary English usage.

  8. #38
    Registered User
    Join Date
    Jul 2008
    Posts
    38
    Code:
    #include <stdio.h>
    #include <string.h>
    void CountEvenASCII(char str[5]);
    
    main()
    {
        char w[5];
        printf("Input character string: ");
        scanf("&#37;s",w);
        CountEvenASCII(w);
        getch();
        return 0;
    }
    
    void CountEvenASCII(char str[5])
    {
        int x,y,z,c,e;
        e=0;
        x=strlen(str);
        x=x-1;
        for (y=0;y<x;y++);
        {
            z=x-y;
            c=str[z];
            if (c % 2 == 0)
            {
                e=e+1;
            }
        }
        printf("\nNumber of characters with even ASCII numerical values: %d\n\n",e);
    }
    This code above should display the number of letters with even ASCII values, but it doesn't work (It just displays if the 1st character has an even ASCII value). Any further assistance or tweaking please?
    Last edited by mkdl750; 07-17-2008 at 06:49 PM.

  9. #39
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Please read the note inside the program.

    Code:
    //display number of letters with even ascii values - help
    #include <stdio.h>
    #include <string.h>
    void CountEvenASCII(char str[5]);
    
    main()
    {
        char w[5];
        printf("Input character string: ");
        scanf("&#37;s", w);
        CountEvenASCII(w);
        getch();
        return 0;
    }
    
    void CountEvenASCII(char str[5])
    {
       int c, even, i;
       even = i = 0;
       while(str[i] != '\0')   {
           if(str[i] % 2 == 0)
              even++;
           i++; 
       }
    
    
    /*  Please don't use variables of x,y,z,c,e. Aside from the commonly used
        loop counters (i, j, k, m, etc), variables with meaningless names just
        confuse everyone, including yourself.  
        int x,y,z,c,e;
        e=0;
        x=strlen(str);
        x=x-1;
        for (y=0;y<x;y++);
        {
            z=x-y;
            c=str[z];
            if (c % 2 == 0)
            {
                e=e+1;
            }
        }
    */
        printf("\nNumber of characters with even ASCII numerical values: %d\n\n", even);
    
    }
    Last edited by Adak; 07-17-2008 at 08:19 PM.

  10. #40
    Registered User guesst's Avatar
    Join Date
    Feb 2008
    Location
    Lehi, UT
    Posts
    179
    Quote Originally Posted by mkdl750 View Post
    Code:
    #include <stdio.h>
    #include <string.h>
    void CountEvenASCII(char str[5]);
    
    main()
    {
        char w[5];
        printf("Input character string: ");
        scanf("%s",w);
        CountEvenASCII(w);
        getch();
        return 0;
    }
    
    void CountEvenASCII(char str[5])
    {
        int x,y,c,e;
        e=0;
        x=strlen(str);
        x=x-1;
        for (y=0;y<x;y++);
        {
            c=str[y];
            if (c % 2 == 0)
            {
                e=e+1;
            }
        }
        printf("\nNumber of characters with even ASCII numerical values: %d\n\n",e);
    }
    This code above should display the number of letters with even ASCII values, but it doesn't work (It just displays if the 1st character has an even ASCII value). Any further assistance or tweaking please?
    Should be fixed now. I'm not sure what you were trying to accomplish with the z variable, but it wasn't helping.

    Also, you set a string length of 5, meaning if you type a sentence of more than 4 characters you're going to get a buffer overrun error. Better expand that. I'd say 255.

    Hmmm, spaces are #32 on the ascii table. I wonder about punctuation marks too. Will you need to take those into account? In all honesty, as beginner as you are I'm sure your teacher isn't going to dock you for it too badly.

    Lemme give you one more hint. e++; means the exact same thing as e=e+1; and x--; means the exact same thing as x=x-1;. They're called increment or decrement operators and are a sort of short hand for this exact sort of thing which turns up a ton.
    Type-ins are back! Visit Cymon's Games at http://www.cymonsgames.com for a new game every week!

  11. #41
    Registered User
    Join Date
    Jul 2008
    Posts
    38

    Smile thanks!

    thanks for your solution

  12. #42
    Registered User
    Join Date
    Jul 2008
    Posts
    38
    Okay how I change the recursive solution into iterative ones? just remove the while?

  13. #43
    Registered User
    Join Date
    Jul 2008
    Posts
    38
    how do I convert subquestion e into an iterative statement?

  14. #44
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    There are no recursive solutions shown on this page. They're all iterative one's.

    Removing the while will not make an iterative loop into a recursive solution. You need to google and read up on the terms "iterative" and "recursive".

  15. #45
    Registered User
    Join Date
    Jul 2008
    Posts
    38
    Code:
    #include <stdio.h>
    #include <string.h>
    void CountEvenASCII(char str[5]);
    
    main()
    {
        char w[5];
        printf("Input character string: ");
        scanf("&#37;s", w);
        CountEvenASCII(w);
        getch();
        return 0;
    }
    
    void CountEvenASCII(char str[5])
    {
       int c, even, i;
       even = i = 0;
       while(str[i] != '\0')   {
           if(str[i] % 2 == 0)
              even++;
           i++; 
       }
    If Mr. Adak's solution above is an iterative statement, how do I turn it into a recursive one?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 11-03-2001, 04:39 PM
  2. Homework
    By kermi3 in forum C Programming
    Replies: 10
    Last Post: 09-27-2001, 04:49 PM
  3. Homework
    By kermi3 in forum C++ Programming
    Replies: 15
    Last Post: 09-26-2001, 03:16 PM
  4. Homework
    By kermi3 in forum Windows Programming
    Replies: 5
    Last Post: 09-15-2001, 11:48 AM
  5. Homework
    By kermi3 in forum C Programming
    Replies: 0
    Last Post: 09-10-2001, 01:26 PM

Tags for this Thread