Keep practicing functions. One way to approach this is to have a "menu" function that is called from main, perhaps something like this:
Code:
int menu(void)
{
// print menu items
// prompt for input
// read input
// validate input
// return result
}
Then you would have separate functions associated with each menu item. Have a variable in main read the value returned from the menu function, and use a switch statement to run the appropriate function. For added pleasure, put this menu function and switch statement in a loop.
Two of the menu items will be simple programs which I wrote.
The problem I am have is that I don't quite understand how to write a function that will run one of the two programs.
I would advise against using functions to run other programs you wrote, especially if they are simple. Instead, take those simple programs you wrote and turn them into functions of their own. Here is a simple example to illustrate the point:
Code:
// *** Program A - find sum ***
#include <stdio.h>
int main(void)
{
int x = 9;
int y = 5;
int z = 0;
z = x + y;
printf("Sum is %d\n",z);
return 0;
}
// *** Program B - find difference***
#include <stdio.h>
int main(void)
{
int x = 9;
int y = 5;
int z = 0;
z = x - y;
printf("Difference is %d\n",z);
return 0;
}
// *** Final program - each separate "program" has been refactored into functions ***
#include <stdio.h>
int findSum(int a, int b);
int findDifference(int a, int b);
int main(void)
{
int x = 9;
int y = 5;
int z = 0;
z = findSum(x,y);
printf("Sum is %d\n",z);
z = findDifference(x,y);
printf("Difference is %d\n",z);
return 0;
}
int findSum(int a, int b)
{
int z = 0;
z = a + b;
return z;
}
int findDifference(int a, int b)
{
int z = 0;
z = a - b;
return z;
}
Again, please note that this is a contrived example meant only to illustrate my point.