![]() |
| | #31 |
| Registered User Join Date: Jul 2008
Posts: 38
| |
| mkdl750 is offline | |
| | #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;
}
|
| mkdl750 is offline | |
| | #33 |
| and the Hat of Guessing 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 | |
| | #34 |
| Registered User Join Date: Jul 2008
Posts: 38
| what is an iterative statement? |
| mkdl750 is offline | |
| | #35 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| |
| tabstop is offline | |
| | #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 | |
| | #37 | |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Quote:
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 | |
| | #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);
}
Last edited by mkdl750; 07-17-2008 at 06:49 PM. |
| mkdl750 is offline | |
| | #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 | |
| | #40 | |
| Registered User Join Date: Feb 2008 Location: Lehi, UT
Posts: 179
| Quote:
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 | |
| | #41 |
| Registered User Join Date: Jul 2008
Posts: 38
| thanks for your solution |
| mkdl750 is offline | |
| | #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 | |
| | #43 |
| Registered User Join Date: Jul 2008
Posts: 38
| how do I convert subquestion e into an iterative statement? |
| mkdl750 is offline | |
| | #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 | |
| | #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++;
}
|
| mkdl750 is offline | |
![]() |
| Tags |
| assignment, c programming, homework |
| Thread Tools | |
| Display Modes | |
|
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 |