In case 2 for the switch
(f) print a blank line, a line with the message:"Part II:" and a blank line.
(g) Print a message:
"Series evaluation: x^n/n! from n=0 to n=n_end.\n" and a blank line.
In the above n! (n factorial)=1*2*...*n for n>0 and 0!=1.
(h) Enter x, a real number and n_end, a large positive integer
from the keyboard and print those values using %g and %d.
(i) In a for-loop evaluate the sum. In addition, print the partial sums
using %16.8e for n=10, 20, 30,.... Print the values of n_end, sum,
and x using %4d, %16.8e, and %g. Finally, print the value of
exp(x) using %g for x and %16.8e for exp(x).
I tried to do part one and i think i got most of it but it doesnt work when i do the a.out. any suggestions?
#include <stdio.h>
#include <math.h>
int main(void)
{
int menu, n, n_end, length;
char message[111];
printf("\nHomework 4:\n\n");
printf("There are two parts: part 1 and part 2.\n");
printf("Enter the part number to execute (1 or 2):");
scanf("%d", &menu);
switch(menu){
case 1:
printf("Part I:\n\n");
printf("\nEnter a text message:");
What is the variable "text"? Shouldn't it be "message"? I wonder, why the compiler doesn't comlpain...
And i think the scanf() leaves a newline character in the buffer, wich is read by gets(). I think, if you change it to scanf("%d\n",&menu);
it should work. If this doeasn't work, please give us the error message or tell what the program is doing now. I have got no other idea.
ok, this is what it says after i compile.
#include <stdio.h>
#include <math.h>
int main(void)
{
int menu, n, n_end, length;
char message[111];
printf("\nHomework 4:\n\n");
printf("There are two parts: part 1 and part 2.\n");
printf("Enter the part number to execute (1 or 2):");
scanf("%d", &menu);
switch(menu){
case 1:
printf("Part I:\n\n");
printf("\nEnter a text message:");
"hw4.c" 26 lines, 691 characters
iacs5.ucsd.edu% gcc hw4.c
hw4.c: In function `main':
hw4.c:17: error: `text' undeclared (first use in this function)
hw4.c:17: error: (Each undeclared identifier is reported only once
hw4.c:17: error: for each function it appears in.)
hw4.c:21: error: `k' undeclared (first use in this function)
can someone please help me? and can someone help me out with part 2 also? dont really understand what im supposed to do. thanks.
You haven't declared the variable "text". You have got a char array, but you called it "message", so you should rename it to "text"
You haven't declared the integer "k". You have got to type "int k;" before the for-loop.
For part 2 you'll need a for-loop and sum up the elements of the series. I suggest, you write a function to calculate the factorial. The math.h library contains a function exp().
hey i got the same questions here. i figured out the first part but i need help w/ the second myself. heres what i have:
#include <stdio.h>
#include <math.h>
int main(void)
{
int part, k, x, n, n_end, length, oneByn_fac, fact;
char text[111];
printf("\nHomework 4:\n\n");
printf("There are two parts: part 1 and part 2.\n\n");
printf("Enter the part number to execute (1 or 2):");
scanf("%d", &part);
switch(part){
case 1:
printf("\nPart I:\n\n");
printf("Enter a text message:");
while(strlen(gets(text))==0);
printf("%s\n", text);
length = (int)strlen(text);
printf("There are %d characters.\n", length);
for(k=length-1; k>=0; k--) printf("%c", text[k]);
printf("\n");
break;
case 2:
printf("\nPart II:\n\n");
printf("Series evaluation: x^n/n! from n=0 t n=n_en
d.\n\n");
printf("Enter a value for x (a real number) and a v
alue for n_end (a large positive interger):\n");
printf("x=");
scanf("%g", &x);
printf("n_end=");
scanf("%d", &n_end);
for(k=1, oneByn_fac=1.; k<=n_end; k++) oneByn_fac /
=(double)k;
fact = pow(x, n_end);
printf(" %g/%d!\n", fact, n_end);
}
}
as you can probably see i haven't got a clue what im doing in part 2, but part one works fine. hope this helps.
I really can't see, what you are doing in part two... did you get the idea of a series? Else, read wikipedia about the basic properties of a series. http://en.wikipedia.org/wiki/Series_(mathematics)
I would suggest, that you write a function to calculate the factorial of a given number. Then you can calculate the partial sum with an easy for-loop. And in the end, you can compare it with the function exp() from math.h