C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-13-2008, 11:52 PM   #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?
mkdl750 is offline   Reply With Quote
Old 07-15-2008, 05:57 PM   #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")
mkdl750 is offline   Reply With Quote
Old 07-15-2008, 06:00 PM   #33
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
The problem is in the scanf statement. Why try to read an integer into a variable that is not of type int?
tabstop is offline   Reply With Quote
Old 07-15-2008, 09:34 PM   #34
Registered User
 
Join Date: Jul 2008
Posts: 38
what is an iterative statement?
mkdl750 is offline   Reply With Quote
Old 07-15-2008, 09:37 PM   #35
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Quote:
Originally Posted by mkdl750 View Post
what is an iterative statement?
www.dictionary.com
tabstop is offline   Reply With Quote
Old 07-15-2008, 09:47 PM   #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.
mkdl750 is offline   Reply With Quote
Old 07-15-2008, 09:57 PM   #37
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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.
tabstop is offline   Reply With Quote
Old 07-17-2008, 06:11 PM   #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("%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.
mkdl750 is offline   Reply With Quote
Old 07-17-2008, 08:14 PM   #39
Registered User
 
Join Date: Sep 2006
Posts: 2,516
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("%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.
Adak is online now   Reply With Quote
Old 07-17-2008, 08:24 PM   #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!
guesst is offline   Reply With Quote
Old 07-17-2008, 08:31 PM   #41
Registered User
 
Join Date: Jul 2008
Posts: 38
Smile thanks!

thanks for your solution
mkdl750 is offline   Reply With Quote
Old 07-17-2008, 08:35 PM   #42
Registered User
 
Join Date: Jul 2008
Posts: 38
Okay how I change the recursive solution into iterative ones? just remove the while?
mkdl750 is offline   Reply With Quote
Old 07-17-2008, 09:04 PM   #43
Registered User
 
Join Date: Jul 2008
Posts: 38
how do I convert subquestion e into an iterative statement?
mkdl750 is offline   Reply With Quote
Old 07-17-2008, 09:21 PM   #44
Registered User
 
Join Date: Sep 2006
Posts: 2,516
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".
Adak is online now   Reply With Quote
Old 07-17-2008, 09:27 PM   #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("%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?
mkdl750 is offline   Reply With Quote
Reply

Tags
assignment, c programming, homework

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Homework kermi3 A Brief History of Cprogramming.com 11 11-03-2001 04:39 PM
Homework kermi3 C Programming 10 09-27-2001 04:49 PM
Homework kermi3 C++ Programming 15 09-26-2001 03:16 PM
Homework kermi3 Windows Programming 5 09-15-2001 11:48 AM
Homework kermi3 C Programming 0 09-10-2001 01:26 PM


All times are GMT -6. The time now is 01:04 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22