In: Computer Science
C-coding
Question1:
Fill in the function body for the provided
sum() function such that the call sum(g,
i, j) returns the value of the
calculation:
Note:
For the case where j < i, the function
sum() should return 0.
#include <stdio.h>
int g(int val)
{
return val * val;
}
int sum(int (*f)(int val), int start, int end)
{
/* Your code goes here */
}
int main()
{
printf("Result: %d\n", sum(g, 10, 20));
return 0;
}
******************************************************
Question 2:
Fill in the function body for do_file_menu() such that, when given a string as an argument, the function searches the given array of structures for a matching command name and then calls the function associated with that name.
You will know your solution is working properly when you achieve the following output:
Created New File.
Opened File.
Closed File.
File Saved.
Printing File...
Goodbye.
#include <stdio.h>
#include <string.h>
struct menu {
char *cmd_name;
void (*cmd_ptr)(void);
};
void file_new()
{
printf("Created New File.\n");
}
void file_open()
{
printf("Opened File.\n");
}
void file_close()
{
printf("Closed File.\n");
}
void file_save()
{
printf("File Saved.\n");
}
void file_print()
{
printf("Printing File...\n");
}
void file_exit()
{
printf("Goodbye.\n");
}
/* global variable 'file': array of structs */
struct menu file[] = {
{"new", file_new}, /* file[0] */
{"open", file_open}, /* file[1] */
{"close", file_close}, /* file[2] */
{"save", file_save},
{"print", file_print},
{"exit", file_exit} /* file[5] */
};
void do_file_menu(char *name)
{
/* Your code goes here. */
}
int main()
{
int i;
/* array of char pointers to string literals */
char *test[] = {
"new",
"open",
"close",
"save",
"print",
"exit"
};
/* test all of the commands one by one */
for (i=0; i<sizeof(test)/sizeof(*test); i++)
do_file_menu(test[i]);
return 0;
}
Here is the solution to above problem in C. Please read the code comments for more information
PLEASE GIVE A THUMBS UP!!!
QUESTION 1.
C Code
#include <stdio.h>
int g(int val)
{
return val * val;
}
int sum(int (*f)(int val), int start, int end)
{
//end is less than i return 0
if(end<start)
return 0;
int S=0; //to store sum
int i;
//you need to call the function g here with
//iterate from start to end
for( i=start;i<=end;++i)
{
S+=(*f)(i);
}
return S;
}
int main()
{
printf("Result: %d\n", sum(g, 10, 20));
return 0;
}
Screenshot of output
QUESTION 2
C Code
#include <stdio.h>
#include <string.h>
struct menu {
char *cmd_name;
void (*cmd_ptr)(void);
};
void file_new()
{
printf("Created New File.\n");
}
void file_open()
{
printf("Opened File.\n");
}
void file_close()
{
printf("Closed File.\n");
}
void file_save()
{
printf("File Saved.\n");
}
void file_print()
{
printf("Printing File...\n");
}
void file_exit()
{
printf("Goodbye.\n");
}
/* global variable 'file': array of structs */
struct menu file[] = {
{"new", file_new}, /* file[0] */
{"open", file_open}, /* file[1] */
{"close", file_close}, /* file[2] */
{"save", file_save},
{"print", file_print},
{"exit", file_exit} /* file[5] */
};
void do_file_menu(char *name)
{
//match the corresponding string and print from the array
//and add the corresponding function
//strcmp function returns 0 if two string are same
//use if else to check for all the cases
if(strcmp(name,"new")==0)
file_new();
else if(strcmp(name,"open")==0)
file_open();
else if(strcmp(name,"close")==0)
file_close();
else if(strcmp(name,"save")==0)
file_save();
else if(strcmp(name,"print")==0)
file_print();
else if(strcmp(name,"exit")==0)
file_exit();
}
int main()
{
int i;
/* array of char pointers to string literals */
char *test[] = {
"new",
"open",
"close",
"save",
"print",
"exit"
};
/* test all of the commands one by one */
for (i=0; i<sizeof(test)/sizeof(*test); i++)
do_file_menu(test[i]);
return 0;
}
Screenshot of output