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

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
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
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...
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 . . .
Write a C function to swap the first and last elements of an integer array. Call...
Write a C function to swap the first and last elements of an integer array. Call the function from main() with an int array of size 4. Print the results before and after swap (print all elements of the array in one line). The signature of the arrItemSwap() function is: void arrItemSwap(int *, int, int); /* array ptr, indices i, j to be swapped */ Submit the .c file. No marks will be given if your pgm does not compile...
C++ The program will call the start_game function with argument O or X to indicate first...
C++ The program will call the start_game function with argument O or X to indicate first player and will keep track of the next player while players take turns marking the board until the board is full. This version of the game only plays one game. Class Specifications TicTacToe class does not have a constructor.Member Functions and Member(variable) Specifications + = public - = private public/private New /Update/NoUpdate Function/Data Member Functionality of function or data member + New bool game_over...
Simple code for a game on C coding.
Simple code for a game on C coding.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT