Question

In: Computer Science

C-coding Question1: Fill in the function body for the provided sum() function such that the call...

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;

}

Solutions

Expert Solution

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


Related Solutions

How would you call a username in a different function when coding in C? I have...
How would you call a username in a different function when coding in C? I have a function that logs a user in, and I would like to say if (username == 'abc'){do function} but everytime I use the 'username' variable in a different function, it says it is an undeclared identifier.
Fill in the following function to sum the series of 1 + x/1 + x^2/2 +...
Fill in the following function to sum the series of 1 + x/1 + x^2/2 + x^3/3 + .. + x^n/n using Java Program
In C Write a main function with a function call to a function called GetLetter which...
In C Write a main function with a function call to a function called GetLetter which has two double arguments/parameters. The function returns a character. Declare and initialize the necessary data variables and assign values needed to make it executable and to prevent a loss of information
Using C++: Write a function that recursively calculates the sum of an array. The function should...
Using C++: Write a function that recursively calculates the sum of an array. The function should have 2 parameters, the array and an integer showing the number of elements (assume they are all integers) in the array. The function will use a recursive call to sum the value of all elements. You must prompt for a series of integers to be entered in the array. As your program reads the numbers in, increment a count so it knows how many...
execute coding examples that demonstrate the 4 scope rules file,function,class,block coding language c++
execute coding examples that demonstrate the 4 scope rules file,function,class,block coding language c++
Write a C or C++ program using the fork() system call function. You will need to...
Write a C or C++ program using the fork() system call function. You will need to create 3 processes – each process will perform a simple task. Firstly, create an integer "counter" initialized to a random value between 1 and 100. Print this number to the console. This can be done by: Including the stdio.h and stdlib.h libraries Using the rand() function to generate your randomly generated number The main thread consists of the parent process. Your job is to...
Coding language: C++. • Each functionality component must be implemented as a separate function, though the...
Coding language: C++. • Each functionality component must be implemented as a separate function, though the function does not need to be declared and defined separately • No global variables are allowed • No separate.hor.hpp are allowed • You may not use any of the hash tables, or hashing functions provided by the STL or Boost library to implement your hash table • Appropriate, informative messages must be provided for prompts and outputs You must implement a hash table using...
Coding language: C++. • Each functionality component must be implemented as a separate function, though the...
Coding language: C++. • Each functionality component must be implemented as a separate function, though the function does not need to be declared and defined separately • No global variables are allowed • No separate.hor.hpp are allowed • You may not use any of the hash tables, or hashing functions provided by the STL or Boost library to implement your hash table • Appropriate, informative messages must be provided for prompts and outputs You must implement a hash table using...
Coding language: C++. • Each functionality component must be implemented as a separate function, though the...
Coding language: C++. • Each functionality component must be implemented as a separate function, though the function does not need to be declared and defined separately • No global variables are allowed • No separate.hor.hpp are allowed • You may not use any of the hash tables, or hashing functions provided by the STL or Boost library to implement your hash table • Appropriate, informative messages must be provided for prompts and outputs You must implement a hash table using...
************CODING IN C++ ONLY ******************** Instructions Write a function, remove, that takes three parameters: an array...
************CODING IN C++ ONLY ******************** Instructions Write a function, remove, that takes three parameters: an array of integers, the number of elements in the array, and an integer (say, removeItem). The function should find and delete the first occurrence of removeItem in the array. (Note that after deleting the element, the number of elements in the array is reduced by 1.) Assume that the array is unsorted. Also, write a program to test the function. Your program should prompt the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT