Okay, how can I tweak the input sequence of subquestion E so that I can be able to finish the program?
Printable View
Okay, how can I tweak the input sequence of subquestion E so that I can be able to finish the program?
My first solution for subquestion J:
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")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;
}
The problem is in the scanf statement. Why try to read an integer into a variable that is not of type int?
what is an iterative statement?
You should have posted the meaning here. What I'm talking about is the iterative function in C.
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?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);
}
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);
}
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.
thanks for your solution :D
Okay how I change the recursive solution into iterative ones? just remove the while?
how do I convert subquestion e into an iterative statement?
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".
If Mr. Adak's solution above is an iterative statement, how do I turn it into a recursive one?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++;
}