#include #include #include #include #include #define pi 3.14 void program1(); vo
ID: 3902415 • Letter: #
Question
#include
#include
#include
#include
#include
#define pi 3.14
void program1();
void program2();
void program3();
void program4();
int main(void)
{
int choice, num;
printf("Press 1 for week 1 program ");
printf("Press 2 for week 2 program ");
printf("Press 3 for week 3 program ");
printf("Press 4 for week 4 program ");
printf("Enter your choice: ");
scanf_s("%d", &choice);
//choice = input();
switch (choice) {
case 1: {
program1();
break;
}
case 2: {
program2();
break;
}
case 3: {
program3();
break;
}
case 4: {
program4();
break;
}
default:
printf("wrong Input ");
}
return 0;
}
void program1()
{
char *nouns[] = { "man", "woman", "puppy", "city", "rocketship" };
char *articles[] = { "the", "a", "one", "any", "some" };
char *verbs[] = { "skipped", "flew", "walked","jumped", "ran" };
char *prepositions[] = { "on", "to","over","from","under" };
char sentence[100] = "";
int x;
for (x = 1; x <= 20; x++)
{
strcat_s(sentence, sentence, articles[rand() % 5]);
strcat_s(sentence, sentence, " ");
strcat_s(sentence, sentence, nouns[rand() % 5]);
strcat_s(sentence, sentence, "");
strcat_s(sentence, sentence, verbs[rand() % 5]);
strcat_s(sentence, sentence, "");
strcat_s(sentence, sentence, prepositions[rand() % 5]);
strcat_s(sentence, sentence, "");
strcat_s(sentence, sentence, articles[rand() % 5]);
strcat_s(sentence, sentence, "");
strcat_s(sentence, sentence, nouns[rand() % 5]);
strcat_s(sentence, sentence, "");
putchar(toupper(sentence[1]));
printf("%s. ", &sentence[2]);
sentence[1] = '';
}
}
void program2()
{
int diceTotal[13] = { 0 };
int probable[13] = { 0, 0, 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1 };
int rollcount;
int rolldisp;
int dice1;
int dice2;
int rolls = 0;
srand(time(NULL));
for (rollcount = 1; rollcount <= 36000; rollcount++)
{
dice1 = 1 + rand() % 6;
dice2 = 1 + rand() % 6;
++diceTotal[dice1 + dice2];
}
printf("%10s%10s%10s%10s ", "sum:", "total:", "expected:", "actual:");
for (rolldisp = 2; rolldisp <= 12; rolldisp++)
{
printf("%10d%10d%9.3f%%%9.3f%% ", rolldisp, diceTotal[rolldisp], 100.0 * probable[rolldisp] / 36, 100.0 * diceTotal[rolldisp] / 36000);
}
for (rolldisp = 2; rolldisp <= 12; rolldisp++)
{
rolls += diceTotal[rolldisp];
}
printf(" ----- ----- ");
printf("%9s:%8d ", "total rolls", rolls);
}
void program3()
{
int temp_f;
double temp_c;
printf(" %10s %12s ", "fahrenheit", "celsius");
for (temp_f = 0; temp_f <= 103; temp_f++);
{
temp_c = 2.0 / 9.0 * (temp_f - 52);
printf("%10d %+3.3f ", temp_f, temp_c);
}
}
void program4()
{
void circumfrence(double i);
int i
; {
printf(" calculating the circumference of the circle ");
printf(" the radius is %.3f ");
printf(" the circumference is: %.3f ", 2 * pi * i);
}
void area(double i);
int i;
{
printf(" calculating the area of the circle!");
printf(" the radius is %.3f ", i);
printf(" the area is: %.3f", pi * i * i);
}
void volsphere(double i);
int i
{
printf("calculating the volume of the sphere! ");
printf(" the radius is %.3f ", i);
printf(" the volume is: %.3f", (4 * pi* i * i *i) / 3);
}
void(*f[3])(double) = { circumfrence, area, volsphere };
int choice;
double r;
printf(" enter number between 0 and 2, 3 to end: ");
scanf_s("%d", &choice);
printf(" enter radius: ");
scanf_s("%lf", &r);
while (choice >= 0 && choice < 3)
{
(*f[choice])(r);
printf(" enter a number between 0 and 2, 3 to end: ");
scanf_s("%d", &choice);
}
}
there are errors I cant figure out how to fix.
expected a ';' at line 139
identifier "f" is undefined at line 154
'int i': redefinition at line 129
'int i': redefinition at 138
what am I supposed to do? I've been tinkering with the code for hours...
Explanation / Answer
Modified Source Code:-
-----------------------------
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include<time.h>
#define pi 3.14
void program1();
void program2();
void program3();
void program4();
int main(void)
{
int choice, num;
printf("Press 1 for week 1 program ");
printf("Press 2 for week 2 program ");
printf("Press 3 for week 3 program ");
printf("Press 4 for week 4 program ");
printf("Enter your choice: ");
scanf("%d", &choice);
//choice = input();
switch (choice)
{
case 1:
{
program1();
break;
}
case 2:
{
program2();
break;
}
case 3:
{
program3();
break;
}
case 4:
{
program4();
break;
}
default:
printf("wrong Input ");
}
return 0;
}
void program1()
{
char nouns[5][15]={"man", "woman", "puppy", "city", "rocketship"};
char articles[5][15]={ "the", "a", "one", "any", "some" };
char verbs[5][15] = { "skipped", "flew", "walked","jumped", "ran" };
char prepositions[5][15] = { "on", "to","over","from","under" };
char sentence[100] = "";
int x;
for(x=1;x<=20;x++)
{
strcat_s(sentence, sentence, articles[rand() % 5]);
strcat_s(sentence, sentence, " ");
strcat_s(sentence, sentence, nouns[rand() % 5]);
strcat_s(sentence, sentence, "");
strcat_s(sentence, sentence, verbs[rand() % 5]);
strcat_s(sentence, sentence, "");
strcat_s(sentence, sentence, prepositions[rand() % 5]);
strcat_s(sentence, sentence, "");
strcat_s(sentence, sentence, articles[rand() % 5]);
strcat_s(sentence, sentence, "");
strcat_s(sentence, sentence, nouns[rand() % 5]);
strcat_s(sentence, sentence, "");*/
putchar(toupper(sentence[1]));
printf("%s. ", &sentence[2]);
sentence[1] = '';
}
}
void program2()
{
int diceTotal[13] = { 0 };
int probable[13] = { 0, 0, 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1 };
int rollcount;
int rolldisp;
int dice1;
int dice2;
int rolls = 0;
srand(time(NULL));
for(rollcount=1;rollcount<=36000;rollcount++)
{
dice1=1+rand()%6;
dice2=1+rand()%6;
++diceTotal[dice1 + dice2];
}
printf("%10s%10s%10s%10s ", "sum:", "total:", "expected:", "actual:");
for(rolldisp=2;rolldisp<=12;rolldisp++)
{
printf("%10d%10d%9.3f%%%9.3f%% ", rolldisp, diceTotal[rolldisp], 100.0 * probable[rolldisp] / 36, 100.0 * diceTotal[rolldisp] / 36000);
}
for(rolldisp=2;rolldisp<=12;rolldisp++)
{
rolls += diceTotal[rolldisp];
}
printf(" ----- ----- ");
printf("%9s:%8d ", "total rolls", rolls);
}
void program3()
{
int temp_f;
double temp_c;
printf(" %10s %12s ", "fahrenheit", "celsius");
for(temp_f=0;temp_f<=103;temp_f++)
{
temp_c = 2.0 / 9.0 * (temp_f - 52);
printf("%10d %+3.3f ", temp_f, temp_c);
}
}
void program4()
{
void circumfrence(double i);
int i1;
{
printf(" calculating the circumference of the circle ");
printf(" the radius is %.3f ");
printf(" the circumference is: %.3f ", 2 * pi * i1);
}
void area(double i);
int i2;
{
printf(" calculating the area of the circle!");
printf(" the radius is %.3f ", i2);
printf(" the area is: %.3f", pi * i2 * i2);
}
void volsphere(double i);
int i3;
{
printf(" calculating the volume of the sphere! ");
printf(" the radius is %.3f ", i3);
printf(" the volume is: %.3f", (4 * pi* i3 * i3 *i3) / 3);
}
void(*f[3])(double) = { circumfrence, area, volsphere };
int choice;
double r;
printf(" enter number between 0 and 2, 3 to end: ");
scanf("%d", &choice);
printf(" enter radius: ");
scanf("%lf", &r);
while (choice >= 0 && choice < 3)
{
(*f[choice])(r);
printf(" enter a number between 0 and 2, 3 to end: ");
scanf("%d", &choice);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.