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
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...
C++ Recursive Functions: Please call functions in a main function as well. 1. A recursive function...
C++ Recursive Functions: Please call functions in a main function as well. 1. A recursive function that print the reverse of a string. (e.g., void printReverse(string exp)). For example, if exp =”coding”, then the function should print out “gnidoc”. 2. Implement a non-recursion-based binary search function. Convert this function into a recursion-based function. 3. Implement a recursive and non-recursive Fibonacci function.
please write in c++ 2. Write a function sumOfArray that recursively finds the sum of a...
please write in c++ 2. Write a function sumOfArray that recursively finds the sum of a one-dimensional array. A sample run is below. The elements of the array are: 0 8 -4 6 7 The sum of the array is: 17 Press any key to continue . . .
In C++, write a function to fill an array of size 13 with values 13 11...
In C++, write a function to fill an array of size 13 with values 13 11 9 7 5 3 1 2 4 6 8 10 12 recursively, starting with the value 1 in the middle.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT