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:");
OMG. Unless this is a C assignment and not C++ I'd be having a word to your tutor.
1. You shouldn't used a fix length character array when you can use a string instead.
2. printf() should be replaced by cout <<
3. scanf should be replaced by getline()
4. gets() should never be used, it has no bounds checking
All of these could be done using built in C++ types and IOStreams. This assignment is teaching nothing but bad practices.
Apart from that, your code matches the requirements. It's the requirements that are not of good practice. The only thing I'd do is have char message[1024] = {0};
this could be C. i dont know. my class title is intro to c/c++ so could be either. he doesnt tell us which ones it is... he just gives us the assignment and i guess assumes that we know, but he's a pretty bad teacher so i dont jack about c/c++ which is why im here.. anyone else help out?
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.
yep, your code is all right, but you just missed to declare 'text' and 'k', that's why, when the copiler run, kick out an error, cause it dont know what 'text' and 'k' are.
#include <stdio.h>
#include <math.h>
int main(void)
{
int menu, n, n_end, length;
char message[111]; /* char text[111]; or some number declare 'text' here. */
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:");
gets(text); /* here you are using 'text', but you havent declared it yet. */
printf("echo: %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]); /* k is not declared yet, so its goes like: for( int k=length-1; k>=0; k--) */
printf("\n");
printf("\nPart II:\n\n");
}
}
ok thanks. i fixed it and got the first part down. can someone just help me do this:
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.
Enter x, a real number and n_end, a large positive integer
from the keyboard and print those values using %g and %d.
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).
after the printf(""Series evaluation: x^n/n! from n=0 to n=n_end.\n"); how do u the other parts?